|
1 | 1 | import argparse
|
| 2 | +from collections import OrderedDict |
2 | 3 |
|
3 | 4 | from mig.shared.compat import PY2
|
4 | 5 |
|
5 | 6 | _EMPTY_DICT = {}
|
| 7 | +_EMPTY_LIST = {} |
6 | 8 | _NO_DEFAULT = object()
|
7 | 9 | _POSITIONAL_MARKER = '__args__'
|
8 | 10 |
|
9 | 11 |
|
| 12 | +class ArgumentBundleDefinition: |
| 13 | + def __init__(self, name, positional=_EMPTY_LIST): |
| 14 | + self._definition_name = name |
| 15 | + self._expected_positions = 0 |
| 16 | + self._item_checks = [] |
| 17 | + self._item_names = [] |
| 18 | + |
| 19 | + if positional is not _EMPTY_LIST: |
| 20 | + self._define_positional(positional) |
| 21 | + |
| 22 | + @property |
| 23 | + def _fields(self): |
| 24 | + return self._item_names |
| 25 | + |
| 26 | + @property |
| 27 | + def _validators(self): |
| 28 | + return self._item_checks |
| 29 | + |
| 30 | + def __call__(self, *args): |
| 31 | + return self._extract_and_bundle(args, extract_by='position') |
| 32 | + |
| 33 | + def _define_positional(self, positional): |
| 34 | + for flag, name, validator_fn in positional: |
| 35 | + assert flag is None |
| 36 | + self._item_names.append(name) |
| 37 | + self._item_checks.append(validator_fn) |
| 38 | + self._expected_positions = len(positional) |
| 39 | + |
| 40 | + def _extract_and_bundle(self, args, extract_by=None): |
| 41 | + if extract_by == 'position': |
| 42 | + actual_positions = len(args) |
| 43 | + if actual_positions < self._expected_positions: |
| 44 | + raise ValueError('Error: too few arguments given (expected %d got %d)' % ( |
| 45 | + self._expected_positions, actual_positions)) |
| 46 | + keys_to_bundle = list(range(actual_positions)) |
| 47 | + elif extract_by == 'name': |
| 48 | + keys_to_bundle = self._item_names |
| 49 | + elif extract_by == 'short': |
| 50 | + keys_to_bundle = self._item_short |
| 51 | + else: |
| 52 | + raise RuntimeError() |
| 53 | + |
| 54 | + return ArgumentBundle.from_args(self, args, keys_to_bundle) |
| 55 | + |
| 56 | + def ensure_bundle(self, bundle_or_args): |
| 57 | + assert isinstance(self, ArgumentBundleDefinition) |
| 58 | + |
| 59 | + bundle_definition = self |
| 60 | + |
| 61 | + if isinstance(bundle_or_args, ArgumentBundle): |
| 62 | + assert bundle_or_args.name == bundle_definition._definition_name |
| 63 | + return bundle_or_args |
| 64 | + else: |
| 65 | + return bundle_definition(*bundle_or_args) |
| 66 | + |
| 67 | + |
| 68 | +class ArgumentBundle(OrderedDict): |
| 69 | + def __init__(self, definition, dictionary): |
| 70 | + super(ArgumentBundle, self).__init__(dictionary) |
| 71 | + self._definition = definition |
| 72 | + |
| 73 | + @property |
| 74 | + def name(self): |
| 75 | + return self._definition._definition_name |
| 76 | + |
| 77 | + def __iter__(self): |
| 78 | + return iter(self.values()) |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def from_args(cls, definition, args, keys): |
| 82 | + dictionary = {key: args[key] for key in keys} |
| 83 | + return cls(definition, dictionary) |
| 84 | + |
| 85 | + |
10 | 86 | class GetoptCompatNamespace(argparse.Namespace):
|
11 | 87 | """Small glue abstraction to provide an object that when iterated yields
|
12 | 88 | tuples of cli-like options and their corresponding values thus emulating
|
|
0 commit comments