| 14 | |
| 15 | |
| 16 | class InMemoryIndex(model.ModelIndex): |
| 17 | # An in-memory version of a model index. |
| 18 | |
| 19 | def __init__(self, index): |
| 20 | """ |
| 21 | |
| 22 | The index param you provide is a dictionary that has top level |
| 23 | keys that correspond to the method names here: |
| 24 | |
| 25 | |
| 26 | :: |
| 27 | |
| 28 | {'command_names': {'dot.lineage': [<values>]}, |
| 29 | 'arg_names': {'dot.lineage': {'command_name': [<values>]}}, |
| 30 | 'get_argument_data': { |
| 31 | 'dot.lineage': { |
| 32 | 'command-name': { |
| 33 | 'arg-name': (argname, type, command, parent, |
| 34 | nargs, positional_arg, required, |
| 35 | help_text) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | """ |
| 41 | self.index = index |
| 42 | |
| 43 | def command_names(self, lineage): |
| 44 | return [row[0] for row in self.commands_with_full_name(lineage)] |
| 45 | |
| 46 | def commands_with_full_name(self, lineage): |
| 47 | parent = '.'.join(lineage) |
| 48 | return self.index['command_names'].get(parent, []) |
| 49 | |
| 50 | def arg_names(self, lineage, command_name, positional_arg=False): |
| 51 | parent = '.'.join(lineage) |
| 52 | arg_names = ( |
| 53 | self.index['arg_names'].get(parent, {}).get(command_name, []) |
| 54 | ) |
| 55 | filtered_arg_names = [] |
| 56 | for arg_name in arg_names: |
| 57 | arg_data = self.get_argument_data(lineage, command_name, arg_name) |
| 58 | if arg_data.positional_arg == positional_arg: |
| 59 | filtered_arg_names.append(arg_name) |
| 60 | return filtered_arg_names |
| 61 | |
| 62 | def get_argument_data(self, lineage, command_name, arg_name): |
| 63 | parent = '.'.join(lineage) |
| 64 | arg_data = ( |
| 65 | self.index['arg_data'] |
| 66 | .get(parent, {}) |
| 67 | .get(command_name, {}) |
| 68 | .get(arg_name) |
| 69 | ) |
| 70 | if arg_data is not None: |
| 71 | return model.CLIArgument(*arg_data) |
| 72 | |
| 73 | def get_global_arg_data(self, lineage=[], command_name='aws'): |