-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli_tools.py
More file actions
849 lines (685 loc) · 27.8 KB
/
cli_tools.py
File metadata and controls
849 lines (685 loc) · 27.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# Copyright (C) 2013, 2014, 2017 by Kevin L. Mitchell <klmitch@mit.edu>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import inspect
import sys
import pkg_resources
import six
__all__ = ['console', 'prog', 'usage', 'description', 'epilog',
'formatter_class', 'argument', 'argument_group',
'mutually_exclusive_group', 'subparsers', 'load_subcommands']
def _clean_text(text):
"""
Clean up a multiple-line, potentially multiple-paragraph text
string. This is used to extract the first paragraph of a string
and eliminate line breaks and indentation. Lines will be joined
together by a single space.
:param text: The text string to clean up. It is safe to pass
``None``.
:returns: The first paragraph, cleaned up as described above.
"""
desc = []
for line in (text or '').strip().split('\n'):
# Clean up the line...
line = line.strip()
# We only want the first paragraph
if not line:
break
desc.append(line)
return ' '.join(desc)
def expose(func):
"""
A decorator for ``ScriptAdaptor`` methods. Methods so decorated
will be exposed on the function decorated by ``cli_tools``. This
has no effect on classes decorated by ``cli_tools``.
:param func: The function to expose.
:returns: The function.
"""
# Just set the expose attribute on the function.
func._cli_expose = True
return func
class ScriptAdaptorMeta(type):
"""
A metaclass for ``ScriptAdaptor``. This builds a list of the
names of methods that have been decorated with ``@expose``. This
is used to copy the exposed methods onto a decorated function.
"""
def __new__(mcs, name, bases, namespace):
"""
Create the ``ScriptAdaptor`` class. This ensures that an
``exposed`` class attribute is set to the set of method names
that should be exposed on a decorated function.
:param name: The class name.
:param bases: A tuple of the base classes.
:param namespace: A dictionary containing the class namespace.
:returns: The constructed class.
"""
# Build up the set of exposed method names
exposed = set()
for name, value in namespace.items():
if callable(value) and getattr(value, '_cli_expose', False):
exposed.add(name)
namespace['exposed'] = exposed
# Construct and return the class
return super(ScriptAdaptorMeta, mcs).__new__(mcs, name, bases,
namespace)
@six.add_metaclass(ScriptAdaptorMeta)
class ScriptAdaptor(object):
"""
An adaptor for the function. Keeps track of the declared command
line arguments and includes methods for declaring processors and
calling the function from the console.
"""
@classmethod
def _get_adaptor(cls, func):
"""
Gets the ``ScriptAdaptor`` for a function.
:param func: The function to obtain the ``ScriptAdaptor`` of.
:returns: The ``ScriptAdaptor``.
"""
# Get the adaptor, creating one if necessary
adaptor = getattr(func, 'cli_tools', None)
if adaptor is None:
is_class = inspect.isclass(func)
adaptor = cls(func, is_class)
func.cli_tools = adaptor
# Set up the added functions
if not is_class:
for meth in cls.exposed:
setattr(func, meth, getattr(adaptor, meth))
return adaptor
def __init__(self, func, is_class=None):
"""
Initialize a ``ScriptAdaptor``.
:param func: The underlying function.
:param is_class: A boolean specifying whether the ``func`` is
actually a class.
"""
self._func = func
self._is_class = (is_class if is_class is not None
else inspect.isclass(func))
self._run = 'run' if self._is_class else None
self._args_hook = lambda x: None
self._processor = lambda x: None
self._arguments = []
self._groups = {}
self._subcommands = {}
self._entrypoints = set()
self.do_subs = False
self.subkwargs = {}
self.prog = None
self.usage = None
self.description = _clean_text(func.__doc__)
self.epilog = None
self.formatter_class = argparse.HelpFormatter
# This will be an attribute name for the adaptor implementing
# the subcommand; this allows for the potential of arbitrary
# depth on subcommands
self._subcmd_attr = '_script_adaptor_%x' % id(self)
def _add_argument(self, args, kwargs, group):
"""
Add an argument specification to the list of argument
specifications. The argument specification is inserted at the
beginning of the list of argument specifications, so that the
decorators may be added in natural order.
:param args: The positional arguments of the argument
specification.
:param kwargs: The keyword arguments of the argument
specification.
:param group: An argument group name. If provided, the
argument specification will be added to the
named group, rather than to the general list of
arguments.
"""
if group:
self._groups.setdefault(group, dict(arguments=[]))
self._groups[group]['arguments'].insert(0, (args, kwargs))
else:
self._arguments.insert(0, ('argument', args, kwargs))
def _add_group(self, group, type, kwargs):
"""
Add an argument group specification to the list of argument
specifications. The group specification is inserted at the
beginning of the list of argument specifications, so that the
decorators may be added in natural order.
:param group: The name of the argument group. If the group is
already defined, an ``argparse.ArgumentError``
will be raised.
:param type: Either "group" or "exclusive", depending on the
desired group type.
:param kwargs: The keyword arguments of the group
specification.
"""
# Make sure the group exists
self._groups.setdefault(group, dict(arguments=[]))
# Look out for the pre-existence of the group
if 'type' in self._groups[group]:
raise argparse.ArgumentError(None, "group %s: conflicting groups" %
group)
# Save the data
self._groups[group]['type'] = type
# Add the group to the argument specification list
self._arguments.insert(0, ('group', group, kwargs))
def _add_subcommand(self, name, adaptor):
"""
Add a subcommand to the parser.
:param name: The name of the command to be added.
:param adaptor: The corresponding ScriptAdaptor instance.
"""
self._subcommands[name] = adaptor
self.do_subs = True
def _add_extensions(self, group):
"""
Adds extensions to the parser. This will cause a walk of a
``pkg_resources`` entrypoint group, adding each discovered
function that has an attached ScriptAdaptor instance as a
subcommand. This walk is performed immediately prior to
building the subcommand processor. Note that no attempt is
made to avoid duplication of subcommands.
:param group: The entrypoint group name.
"""
self._entrypoints.add(group)
# We are now in subparsers mode
self.do_subs = True
def _process_entrypoints(self):
"""
Perform a walk of all entrypoint groups declared using
``_add_extensions()``. This is called immediately prior to
building the subcommand processor.
"""
# Walk the set of all declared entrypoints
for group in self._entrypoints:
for ep in pkg_resources.iter_entry_points(group):
try:
func = ep.load()
self._add_subcommand(ep.name, func.cli_tools)
except (ImportError, AttributeError,
pkg_resources.UnknownExtra):
# Ignore any expected errors
pass
# We've processed these entrypoints; avoid double-processing
self._entrypoints = set()
@expose
def args_hook(self, func):
"""
Sets a hook for constructing the arguments. This hook could
be used to allow, for instance, a set of authentication
plugins to add their configuration options to the argument
parser. This method may be used as a decorator, e.g.:
@console
def func():
pass
@func.args_hook
def _hook(parser):
pass
If the hook is a regular function, it will be called after
processing all of the regular argument specifications.
If the hook is a generator, the segment before the first
``yield`` statement will be executed before adding any regular
argument specifications, and the remainder will be executed
afterward.
:param func: The function to be installed as an argument hook.
:returns: The function, allowing this method to be used as a
decorator.
"""
self._args_hook = func
return func
@expose
def processor(self, func):
"""
Sets a processor for the underlying function. A processor
function runs before and potentially after the underlying
function, but only when it is being called as a console
script. This method may be used as a decorator, e.g.:
@console
def func():
pass
@func.processor
def _proc(args):
pass
If the processor is a regular function, it will be called just
before the underlying function is called, and it will be
passed the parsed arguments.
If the processor is a generator, the segment before the first
``yield`` statement will be executed just before the
underlying function is called. The return result of the
``yield`` statement will be the return result of the
underlying function, and if another value is ``yield``ed, that
value will replace the return result for the purposes of the
console script.
:param func: The function to be installed as a processor.
:returns: The function, allowing this method to be used as a
decorator.
"""
self._processor = func
return func
@expose
def subcommand(self, name=None):
"""
Decorator used to mark another function as a subcommand of
this function. If ``function()`` is the parent function, this
decorator can be used in any of the following ways:
@function.subcommand('spam')
def foo():
pass
@function.subcommand()
def bar():
pass
@function.subcommand
def baz():
pass
In the first case, the command name is set explicitly. In the
latter two cases, the command name is the function name.
:param name: If a string, gives the name of the subcommand.
If a callable, specifies the function being added
as a subcommand. If not specified, a decorator
will be returned which will derive the name from
the function.
:returns: If ``name`` was a callable, it will be returned.
Otherwise, returns a callable which takes a callable
as an argument and returns that callable, to conform
with the decorator syntax.
"""
def decorator(func):
cmdname = name or func.__name__
adaptor = self._get_adaptor(func)
self._add_subcommand(cmdname, adaptor)
return func
# If we were passed a callable, we were used without
# parentheses, and will derive the command name from the
# function...
if callable(name):
func = name
name = None
return decorator(func)
return decorator
@expose
def setup_args(self, parser):
"""
Set up an ``argparse.ArgumentParser`` object by adding all the
arguments taken by the function. This is available to allow
other users access to the argument specifications.
:param parser: An ``argparse.ArgumentParser`` object, or any
related object having an ``add_argument()``
method.
"""
# Run the args hook, if it's a generator
post = self._args_hook
if inspect.isgeneratorfunction(self._args_hook):
post = self._args_hook(parser)
try:
six.next(post)
except StopIteration:
# Won't be doing any post-processing anyway
post = None
for arg_type, args, kwargs in self._arguments:
if arg_type == 'argument':
parser.add_argument(*args, **kwargs)
elif arg_type == 'group':
# Get the group information
arguments = self._groups[args]['arguments']
type = self._groups[args]['type']
# Create the group in the parser
if type == 'group':
group = parser.add_argument_group(**kwargs)
elif type == 'exclusive':
group = parser.add_mutually_exclusive_group(**kwargs)
else:
# Huh, don't know that group...
continue # pragma: no cover
# Set up all the arguments
for a_args, a_kwargs in arguments:
group.add_argument(*a_args, **a_kwargs)
# If we have subcommands, set up the parser appropriately
if self.do_subs:
self._process_entrypoints()
subparsers = parser.add_subparsers(**self.subkwargs)
for cmd, adaptor in self._subcommands.items():
cmd_parser = subparsers.add_parser(
cmd,
prog=adaptor.prog,
usage=adaptor.usage,
description=adaptor.description,
epilog=adaptor.epilog,
formatter_class=adaptor.formatter_class,
)
adaptor.setup_args(cmd_parser)
# Remember which adaptor implements the subcommand
defaults = {self._subcmd_attr: adaptor}
cmd_parser.set_defaults(**defaults)
# If the hook has a post phase, run it
if post:
if inspect.isgenerator(post):
try:
six.next(post)
except StopIteration:
pass
post.close()
else:
post(parser)
@expose
def get_kwargs(self, func, args=None):
"""
Given an ``argparse.Namespace``, as produced by
``argparse.ArgumentParser.parse_args()``, determines the
keyword arguments to pass to the specified function. Note
that an ``AttributeError`` exception will be raised if any
argument required by the function is not set in ``args``.
:param func: A callable to introspect.
:param args: A ``argparse.Namespace`` object containing the
argument values.
:returns: A dictionary containing the keyword arguments to be
passed to the underlying function.
"""
# For backwards compatibility, handle the case when we were
# called with only one argument
if args is None:
args = func
func = self._func
# Get the argument spec for the correct underlying function
if inspect.isclass(func):
try:
# Try __new__() first; this will raise a TypeError if
# __new__() hasn't been overridden
argspec = inspect.getargspec(func.__new__)
ismethod = True
except TypeError:
try:
# OK, no __new__(); try __init__()
argspec = inspect.getargspec(func.__init__)
ismethod = True
except TypeError:
# OK, no __init__(); that means that the class
# initializer takes no arguments
argspec = inspect.ArgSpec([], None, None, None)
ismethod = False
else:
argspec = inspect.getargspec(func)
ismethod = inspect.ismethod(func)
# We need to figure out which arguments the final function
# actually needs
kwargs = {}
req_args = (argspec.args[:-len(argspec.defaults)]
if argspec.defaults else argspec.args)
required = set(req_args[1:] if ismethod else req_args)
for arg_name in argspec.args:
try:
kwargs[arg_name] = getattr(args, arg_name)
except AttributeError:
if arg_name in required:
# If this happens, that's a programming error
raise
# If the function accepts any keyword argument, add whatever
# remains
if argspec.keywords:
for key, value in args.__dict__.items():
if key in kwargs:
# Already handled
continue
kwargs[key] = value
return kwargs
@expose
def safe_call(self, args):
"""
Call the processor and the underlying function. If the
``debug`` attribute of ``args`` exists and is ``True``, any
exceptions raised by the underlying function will be
re-raised.
:param args: This should be an ``argparse.Namespace`` object;
the keyword arguments for the function will be
derived from it.
:returns: A tuple of the function return value and exception
information. Only one of these values will be
non-``None``.
"""
# Run the processor
post = None
if inspect.isgeneratorfunction(self._processor):
post = self._processor(args)
try:
six.next(post)
except StopIteration:
# Won't be doing any post-processing anyway
post = None
else:
self._processor(args)
# Initialize the results
result = None
exc_info = None
try:
# Call the function
result = self._func(**self.get_kwargs(self._func, args))
except Exception:
if args and getattr(args, 'debug', False):
# Re-raise if desired
raise
exc_info = sys.exc_info()
if self._is_class:
# All we've done so far is initialize the class; now we
# need to actually run it
try:
meth = getattr(result, self._run)
result = meth(**self.get_kwargs(meth, args))
except Exception:
if args and getattr(args, 'debug', False):
# Re-raise if desired
raise
result = None # must clear result
exc_info = sys.exc_info()
# If the processor has a post phase, run it
if post:
try:
if exc_info:
# Overwrite the result and exception information
result = post.throw(*exc_info)
exc_info = None
else:
result = post.send(result)
except StopIteration:
# No result replacement...
pass
except Exception:
# Overwrite the result and exception information
exc_info = sys.exc_info()
result = None
post.close()
return result, exc_info
@expose
def console(self, args=None, argv=None):
"""
Call the function as a console script. Command line arguments
are parsed (unless ``args`` is passed), the processor (if any)
is called, then the underlying function is called. If a
``debug`` attribute is set by the command line arguments, and
if it is ``True``, any exception raised by the underlying
function will be re-raised; otherwise, the return value will
be either the return value of the function or the string value
of the exception (unless overwritten by the processor).
:param args: If provided, should be an ``argparse.Namespace``
containing the required argument values for the
function. This can be used to parse the
parameters separately.
:param argv: If provided, should be a list of argument strings
to be parsed by the argument parser, in
preference to ``sys.argv[1:]``.
:returns: The function return value, the string value of any
exception raised by the function, or a value yielded
by the processor to replace the function value.
"""
# First, let's parse the arguments
if not args:
parser = argparse.ArgumentParser(
prog=self.prog,
usage=self.usage,
description=self.description,
epilog=self.epilog,
formatter_class=self.formatter_class,
)
self.setup_args(parser)
args = parser.parse_args(args=argv)
# Get the adaptor
if self.do_subs:
# If the subcommand attribute isn't set, we'll call our
# underlying function
adaptor = getattr(args, self._subcmd_attr, self)
else:
adaptor = self
# Call the function
result, exc_info = adaptor.safe_call(args)
if exc_info:
return str(exc_info[1])
return result
@expose
def get_subcommands(self):
"""
Retrieve a dictionary of the recognized subcommands.
:returns: A dictionary mapping subcommand names to the
implementing functions.
"""
# We only have a return value if we're in subparsers mode
if not self.do_subs:
return {}
# Process any declared entrypoints
self._process_entrypoints()
# Return the subcommands dictionary
return dict((k, v._func) for k, v in self._subcommands.items())
def console(func):
"""
Decorator to mark a script as a console script. This decorator is
optional, but can be used if no arguments other than the default
``argparse`` arguments (such as "--help") are specified.
"""
# This will ensure that the ScriptAdaptor is attached to the
# function
ScriptAdaptor._get_adaptor(func)
return func
def prog(text):
"""
Decorator used to specify the program name for the console script
help message.
:param text: The text to use for the program name.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor.prog = text
return func
return decorator
def usage(text):
"""
Decorator used to specify a usage string for the console script
help message.
:param text: The text to use for the usage.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor.usage = text
return func
return decorator
def description(text):
"""
Decorator used to specify a short description of the console
script. This can be used to override the default, which is
derived from the docstring of the function.
:param text: The text to use for the description.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor.description = text
return func
return decorator
def epilog(text):
"""
Decorator used to specify an epilog for the console script help
message.
:param text: The text to use for the epilog.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor.epilog = text
return func
return decorator
def formatter_class(klass):
"""
Decorator used to specify the formatter class for the console
script.
:param klass: The formatter class to use.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor.formatter_class = klass
return func
return decorator
def argument(*args, **kwargs):
"""
Decorator used to specify an argument taken by the console script.
Positional and keyword arguments have the same meaning as those
given to ``argparse.ArgumentParser.add_argument()``.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
group = kwargs.pop('group', None)
adaptor._add_argument(args, kwargs, group=group)
return func
return decorator
def argument_group(group, **kwargs):
"""
Decorator used to specify an argument group. Keyword arguments
have the same meaning as those given to
``argparse.ArgumentParser.add_argument_group()``.
Arguments may be placed in a given argument group by passing the
``group`` keyword argument to @argument().
:param group: The name of the argument group.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor._add_group(group, 'group', kwargs)
return func
return decorator
def mutually_exclusive_group(group, **kwargs):
"""
Decorator used to specify a mutually exclusive argument group.
Keyword arguments have the same meaning as those given to
``argparse.ArgumentParser.add_mutually_exclusive_group()``.
Arguments may be placed in a given argument group by passing the
``group`` keyword argument to @argument().
:param group: The name of the argument group.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor._add_group(group, 'exclusive', kwargs)
return func
return decorator
def subparsers(**kwargs):
"""
Decorator used to specify alternate keyword arguments to pass to
the ``argparse.ArgumentParser.add_subparsers()`` call.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor.subkwargs = kwargs
adaptor.do_subs = True
return func
return decorator
def load_subcommands(group):
"""
Decorator used to load subcommands from a given ``pkg_resources``
entrypoint group. Each function must be appropriately decorated
with the ``cli_tools`` decorators to be considered an extension.
:param group: The name of the ``pkg_resources`` entrypoint group.
"""
def decorator(func):
adaptor = ScriptAdaptor._get_adaptor(func)
adaptor._add_extensions(group)
return func
return decorator