| 41 | |
| 42 | |
| 43 | class AliasLoader: |
| 44 | def __init__( |
| 45 | self, |
| 46 | alias_filename=os.path.expanduser( |
| 47 | os.path.join('~', '.aws', 'cli', 'alias') |
| 48 | ), |
| 49 | ): |
| 50 | """Interface for loading and interacting with alias file |
| 51 | |
| 52 | :param alias_filename: The name of the file to load aliases from. |
| 53 | This file must be an INI file. |
| 54 | """ |
| 55 | self._filename = alias_filename |
| 56 | self._aliases = None |
| 57 | |
| 58 | def _build_aliases(self): |
| 59 | self._aliases = self._load_aliases() |
| 60 | self._cleanup_alias_values(self._aliases) |
| 61 | |
| 62 | def _load_aliases(self): |
| 63 | parsed = {} |
| 64 | if os.path.exists(self._filename): |
| 65 | parsed = raw_config_parse(self._filename, parse_subsections=False) |
| 66 | self._normalize_key_names(parsed) |
| 67 | return parsed |
| 68 | |
| 69 | def _normalize_key_names(self, parsed): |
| 70 | for key in list(parsed): |
| 71 | if key.startswith('command '): |
| 72 | new_key = tuple(key.split()) |
| 73 | value = parsed.pop(key) |
| 74 | parsed[new_key] = value |
| 75 | |
| 76 | def _cleanup_alias_values(self, all_aliases): |
| 77 | for section_aliases in all_aliases.values(): |
| 78 | for alias in section_aliases: |
| 79 | # Beginning and end line separators should not be included |
| 80 | # in the internal representation of the alias value. |
| 81 | section_aliases[alias] = section_aliases[alias].strip() |
| 82 | |
| 83 | def get_aliases(self, command=None): |
| 84 | # To preserve existing behavior, get_alises() with no args |
| 85 | # will return the top level aliases, i.e `[topleve]`. |
| 86 | # However, you can now get aliases for a specific section by |
| 87 | # providing a command key which is a tuple of the command |
| 88 | # lineage, i.e. `('ec2', 'wait',)`. |
| 89 | if command is None: |
| 90 | key = 'toplevel' |
| 91 | else: |
| 92 | key = ('command',) + tuple(command) |
| 93 | if self._aliases is None: |
| 94 | self._build_aliases() |
| 95 | return self._aliases.get(key, {}) |
| 96 | |
| 97 | |
| 98 | class BaseAliasCommandInjector: |
no outgoing calls