Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/commands.py
1566 views
1
import logging
2
import os
3
4
from botocore import model
5
from botocore.compat import OrderedDict
6
from botocore.validate import validate_parameters
7
8
import awscli
9
from awscli.argparser import ArgTableArgParser
10
from awscli.argprocess import unpack_argument, unpack_cli_arg
11
from awscli.arguments import CustomArgument, create_argument_model_from_schema
12
from awscli.clidocs import OperationDocumentEventHandler
13
from awscli.clidriver import CLICommand
14
from awscli.bcdoc import docevents
15
from awscli.help import HelpCommand
16
from awscli.schema import SchemaTransformer
17
18
LOG = logging.getLogger(__name__)
19
_open = open
20
21
22
class _FromFile(object):
23
24
def __init__(self, *paths, **kwargs):
25
"""
26
``**kwargs`` can contain a ``root_module`` argument
27
that contains the root module where the file contents
28
should be searched. This is an optional argument, and if
29
no value is provided, will default to ``awscli``. This means
30
that by default we look for examples in the ``awscli`` module.
31
32
"""
33
self.filename = None
34
if paths:
35
self.filename = os.path.join(*paths)
36
if 'root_module' in kwargs:
37
self.root_module = kwargs['root_module']
38
else:
39
self.root_module = awscli
40
41
42
class BasicCommand(CLICommand):
43
44
"""Basic top level command with no subcommands.
45
46
If you want to create a new command, subclass this and
47
provide the values documented below.
48
49
"""
50
51
# This is the name of your command, so if you want to
52
# create an 'aws mycommand ...' command, the NAME would be
53
# 'mycommand'
54
NAME = 'commandname'
55
# This is the description that will be used for the 'help'
56
# command.
57
DESCRIPTION = 'describe the command'
58
# This is optional, if you are fine with the default synopsis
59
# (the way all the built in operations are documented) then you
60
# can leave this empty.
61
SYNOPSIS = ''
62
# If you want to provide some hand written examples, you can do
63
# so here. This is written in RST format. This is optional,
64
# you don't have to provide any examples, though highly encouraged!
65
EXAMPLES = ''
66
# If your command has arguments, you can specify them here. This is
67
# somewhat of an implementation detail, but this is a list of dicts
68
# where the dicts match the kwargs of the CustomArgument's __init__.
69
# For example, if I want to add a '--argument-one' and an
70
# '--argument-two' command, I'd say:
71
#
72
# ARG_TABLE = [
73
# {'name': 'argument-one', 'help_text': 'This argument does foo bar.',
74
# 'action': 'store', 'required': False, 'cli_type_name': 'string',},
75
# {'name': 'argument-two', 'help_text': 'This argument does some other thing.',
76
# 'action': 'store', 'choices': ['a', 'b', 'c']},
77
# ]
78
#
79
# A `schema` parameter option is available to accept a custom JSON
80
# structure as input. See the file `awscli/schema.py` for more info.
81
ARG_TABLE = []
82
# If you want the command to have subcommands, you can provide a list of
83
# dicts. We use a list here because we want to allow a user to provide
84
# the order they want to use for subcommands.
85
# SUBCOMMANDS = [
86
# {'name': 'subcommand1', 'command_class': SubcommandClass},
87
# {'name': 'subcommand2', 'command_class': SubcommandClass2},
88
# ]
89
# The command_class must subclass from ``BasicCommand``.
90
SUBCOMMANDS = []
91
92
FROM_FILE = _FromFile
93
# You can set the DESCRIPTION, SYNOPSIS, and EXAMPLES to FROM_FILE
94
# and we'll automatically read in that data from the file.
95
# This is useful if you have a lot of content and would prefer to keep
96
# the docs out of the class definition. For example:
97
#
98
# DESCRIPTION = FROM_FILE
99
#
100
# will set the DESCRIPTION value to the contents of
101
# awscli/examples/<command name>/_description.rst
102
# The naming conventions for these attributes are:
103
#
104
# DESCRIPTION = awscli/examples/<command name>/_description.rst
105
# SYNOPSIS = awscli/examples/<command name>/_synopsis.rst
106
# EXAMPLES = awscli/examples/<command name>/_examples.rst
107
#
108
# You can also provide a relative path and we'll load the file
109
# from the specified location:
110
#
111
# DESCRIPTION = awscli/examples/<filename>
112
#
113
# For example:
114
#
115
# DESCRIPTION = FROM_FILE('command, 'subcommand, '_description.rst')
116
# DESCRIPTION = 'awscli/examples/command/subcommand/_description.rst'
117
#
118
119
# At this point, the only other thing you have to implement is a _run_main
120
# method (see the method for more information).
121
122
def __init__(self, session):
123
self._session = session
124
self._arg_table = None
125
self._subcommand_table = None
126
self._lineage = [self]
127
128
def __call__(self, args, parsed_globals):
129
# args is the remaining unparsed args.
130
# We might be able to parse these args so we need to create
131
# an arg parser and parse them.
132
self._subcommand_table = self._build_subcommand_table()
133
self._arg_table = self._build_arg_table()
134
event = 'before-building-argument-table-parser.%s' % \
135
".".join(self.lineage_names)
136
self._session.emit(event, argument_table=self._arg_table, args=args,
137
session=self._session, parsed_globals=parsed_globals)
138
parser = ArgTableArgParser(self.arg_table, self.subcommand_table)
139
parsed_args, remaining = parser.parse_known_args(args)
140
141
# Unpack arguments
142
for key, value in vars(parsed_args).items():
143
cli_argument = None
144
145
# Convert the name to use dashes instead of underscore
146
# as these are how the parameters are stored in the
147
# `arg_table`.
148
xformed = key.replace('_', '-')
149
if xformed in self.arg_table:
150
cli_argument = self.arg_table[xformed]
151
152
value = unpack_argument(
153
self._session,
154
'custom',
155
self.name,
156
cli_argument,
157
value
158
)
159
160
# If this parameter has a schema defined, then allow plugins
161
# a chance to process and override its value.
162
if self._should_allow_plugins_override(cli_argument, value):
163
override = self._session\
164
.emit_first_non_none_response(
165
'process-cli-arg.%s.%s' % ('custom', self.name),
166
cli_argument=cli_argument, value=value, operation=None)
167
168
if override is not None:
169
# A plugin supplied a conversion
170
value = override
171
else:
172
# Unpack the argument, which is a string, into the
173
# correct Python type (dict, list, etc)
174
value = unpack_cli_arg(cli_argument, value)
175
self._validate_value_against_schema(
176
cli_argument.argument_model, value)
177
178
setattr(parsed_args, key, value)
179
180
if hasattr(parsed_args, 'help'):
181
self._display_help(parsed_args, parsed_globals)
182
elif getattr(parsed_args, 'subcommand', None) is None:
183
# No subcommand was specified so call the main
184
# function for this top level command.
185
if remaining:
186
raise ValueError("Unknown options: %s" % ','.join(remaining))
187
return self._run_main(parsed_args, parsed_globals)
188
else:
189
return self.subcommand_table[parsed_args.subcommand](remaining,
190
parsed_globals)
191
192
def _validate_value_against_schema(self, model, value):
193
validate_parameters(value, model)
194
195
def _should_allow_plugins_override(self, param, value):
196
if (param and param.argument_model is not None and
197
value is not None):
198
return True
199
return False
200
201
def _run_main(self, parsed_args, parsed_globals):
202
# Subclasses should implement this method.
203
# parsed_globals are the parsed global args (things like region,
204
# profile, output, etc.)
205
# parsed_args are any arguments you've defined in your ARG_TABLE
206
# that are parsed. These will come through as whatever you've
207
# provided as the 'dest' key. Otherwise they default to the
208
# 'name' key. For example: ARG_TABLE[0] = {"name": "foo-arg", ...}
209
# can be accessed by ``parsed_args.foo_arg``.
210
raise NotImplementedError("_run_main")
211
212
def _build_subcommand_table(self):
213
subcommand_table = OrderedDict()
214
for subcommand in self.SUBCOMMANDS:
215
subcommand_name = subcommand['name']
216
subcommand_class = subcommand['command_class']
217
subcommand_table[subcommand_name] = subcommand_class(self._session)
218
self._session.emit('building-command-table.%s' % self.NAME,
219
command_table=subcommand_table,
220
session=self._session,
221
command_object=self)
222
self._add_lineage(subcommand_table)
223
return subcommand_table
224
225
def _display_help(self, parsed_args, parsed_globals):
226
help_command = self.create_help_command()
227
help_command(parsed_args, parsed_globals)
228
229
def create_help_command(self):
230
command_help_table = {}
231
if self.SUBCOMMANDS:
232
command_help_table = self.create_help_command_table()
233
return BasicHelp(self._session, self, command_table=command_help_table,
234
arg_table=self.arg_table)
235
236
def create_help_command_table(self):
237
"""
238
Create the command table into a form that can be handled by the
239
BasicDocHandler.
240
"""
241
commands = {}
242
for command in self.SUBCOMMANDS:
243
commands[command['name']] = command['command_class'](self._session)
244
self._add_lineage(commands)
245
return commands
246
247
def _build_arg_table(self):
248
arg_table = OrderedDict()
249
self._session.emit('building-arg-table.%s' % self.NAME,
250
arg_table=self.ARG_TABLE)
251
for arg_data in self.ARG_TABLE:
252
253
# If a custom schema was passed in, create the argument_model
254
# so that it can be validated and docs can be generated.
255
if 'schema' in arg_data:
256
argument_model = create_argument_model_from_schema(
257
arg_data.pop('schema'))
258
arg_data['argument_model'] = argument_model
259
custom_argument = CustomArgument(**arg_data)
260
261
arg_table[arg_data['name']] = custom_argument
262
return arg_table
263
264
def _add_lineage(self, command_table):
265
for command in command_table:
266
command_obj = command_table[command]
267
command_obj.lineage = self.lineage + [command_obj]
268
269
@property
270
def arg_table(self):
271
if self._arg_table is None:
272
self._arg_table = self._build_arg_table()
273
return self._arg_table
274
275
@property
276
def subcommand_table(self):
277
if self._subcommand_table is None:
278
self._subcommand_table = self._build_subcommand_table()
279
return self._subcommand_table
280
281
@classmethod
282
def add_command(cls, command_table, session, **kwargs):
283
command_table[cls.NAME] = cls(session)
284
285
@property
286
def name(self):
287
return self.NAME
288
289
@property
290
def lineage(self):
291
return self._lineage
292
293
@lineage.setter
294
def lineage(self, value):
295
self._lineage = value
296
297
298
class BasicHelp(HelpCommand):
299
300
def __init__(self, session, command_object, command_table, arg_table,
301
event_handler_class=None):
302
super(BasicHelp, self).__init__(session, command_object,
303
command_table, arg_table)
304
# This is defined in HelpCommand so we're matching the
305
# casing here.
306
if event_handler_class is None:
307
event_handler_class = BasicDocHandler
308
self.EventHandlerClass = event_handler_class
309
310
# These are public attributes that are mapped from the command
311
# object. These are used by the BasicDocHandler below.
312
self._description = command_object.DESCRIPTION
313
self._synopsis = command_object.SYNOPSIS
314
self._examples = command_object.EXAMPLES
315
316
@property
317
def name(self):
318
return self.obj.NAME
319
320
@property
321
def description(self):
322
return self._get_doc_contents('_description')
323
324
@property
325
def synopsis(self):
326
return self._get_doc_contents('_synopsis')
327
328
@property
329
def examples(self):
330
return self._get_doc_contents('_examples')
331
332
@property
333
def event_class(self):
334
return '.'.join(self.obj.lineage_names)
335
336
def _get_doc_contents(self, attr_name):
337
value = getattr(self, attr_name)
338
if isinstance(value, BasicCommand.FROM_FILE):
339
if value.filename is not None:
340
trailing_path = value.filename
341
else:
342
trailing_path = os.path.join(self.name, attr_name + '.rst')
343
root_module = value.root_module
344
doc_path = os.path.join(
345
os.path.abspath(os.path.dirname(root_module.__file__)),
346
'examples', trailing_path)
347
with _open(doc_path) as f:
348
return f.read()
349
else:
350
return value
351
352
def __call__(self, args, parsed_globals):
353
# Create an event handler for a Provider Document
354
instance = self.EventHandlerClass(self)
355
# Now generate all of the events for a Provider document.
356
# We pass ourselves along so that we can, in turn, get passed
357
# to all event handlers.
358
docevents.generate_events(self.session, self)
359
self.renderer.render(self.doc.getvalue())
360
instance.unregister()
361
362
363
class BasicDocHandler(OperationDocumentEventHandler):
364
365
def __init__(self, help_command):
366
super(BasicDocHandler, self).__init__(help_command)
367
self.doc = help_command.doc
368
369
def doc_description(self, help_command, **kwargs):
370
self.doc.style.h2('Description')
371
self.doc.write(help_command.description)
372
self.doc.style.new_paragraph()
373
374
def doc_synopsis_start(self, help_command, **kwargs):
375
if not help_command.synopsis:
376
super(BasicDocHandler, self).doc_synopsis_start(
377
help_command=help_command, **kwargs)
378
else:
379
self.doc.style.h2('Synopsis')
380
self.doc.style.start_codeblock()
381
self.doc.writeln(help_command.synopsis)
382
383
def doc_synopsis_option(self, arg_name, help_command, **kwargs):
384
if not help_command.synopsis:
385
doc = help_command.doc
386
argument = help_command.arg_table[arg_name]
387
if argument.synopsis:
388
option_str = argument.synopsis
389
elif argument.group_name in self._arg_groups:
390
if argument.group_name in self._documented_arg_groups:
391
# This arg is already documented so we can move on.
392
return
393
option_str = ' | '.join(
394
[a.cli_name for a in
395
self._arg_groups[argument.group_name]])
396
self._documented_arg_groups.append(argument.group_name)
397
elif argument.cli_type_name == 'boolean':
398
option_str = '%s' % argument.cli_name
399
elif argument.nargs == '+':
400
option_str = "%s <value> [<value>...]" % argument.cli_name
401
else:
402
option_str = '%s <value>' % argument.cli_name
403
if not (argument.required or argument.positional_arg):
404
option_str = '[%s]' % option_str
405
doc.writeln('%s' % option_str)
406
407
else:
408
# A synopsis has been provided so we don't need to write
409
# anything here.
410
pass
411
412
def doc_synopsis_end(self, help_command, **kwargs):
413
if not help_command.synopsis and not help_command.command_table:
414
super(BasicDocHandler, self).doc_synopsis_end(
415
help_command=help_command, **kwargs)
416
else:
417
self.doc.style.end_codeblock()
418
419
def doc_global_option(self, help_command, **kwargs):
420
if not help_command.command_table:
421
super().doc_global_option(help_command, **kwargs)
422
423
def doc_examples(self, help_command, **kwargs):
424
if help_command.examples:
425
self.doc.style.h2('Examples')
426
self.doc.write(help_command.examples)
427
428
def doc_subitems_start(self, help_command, **kwargs):
429
if help_command.command_table:
430
doc = help_command.doc
431
doc.style.h2('Available Commands')
432
doc.style.toctree()
433
434
def doc_subitem(self, command_name, help_command, **kwargs):
435
if help_command.command_table:
436
doc = help_command.doc
437
doc.style.tocitem(command_name)
438
439
def doc_subitems_end(self, help_command, **kwargs):
440
pass
441
442
def doc_output(self, help_command, event_name, **kwargs):
443
pass
444
445