def enable_aliases_autocomplete(_, **kwargs): """ Enable aliases autocomplete by injecting aliases into Azure CLI tab completion list. """ external_completions = kwargs.get('external_completions', []) prefix = kwargs.get('cword_prefix', []) cur_commands = kwargs.get('comp_words', []) alias_table = get_alias_table() # Transform aliases if they are in current commands, # so parser can get the correct subparser when chaining aliases _transform_cur_commands(cur_commands, alias_table=alias_table) for alias, alias_command in filter_aliases(alias_table): if alias.startswith( prefix) and alias.strip() != prefix and _is_autocomplete_valid( cur_commands, alias_command): # Only autocomplete the first word because alias is space-delimited external_completions.append(alias) # Append spaces if necessary (https://github.com/kislyuk/argcomplete/blob/master/argcomplete/__init__.py#L552-L559) prequote = kwargs.get('cword_prequote', '') continuation_chars = "=/:" if len(external_completions) == 1 and external_completions[0][ -1] not in continuation_chars and not prequote: external_completions[0] += ' '
def enable_aliases_autocomplete_interactive(_, **kwargs): """ Enable aliases autocomplete on interactive mode by injecting aliases in the command tree. """ subtree = kwargs.get('subtree', None) if not subtree or not hasattr(subtree, 'children'): return for alias, alias_command in filter_aliases(get_alias_table()): # Only autocomplete the first word because alias is space-delimited if subtree.in_tree(alias_command.split()): subtree.add_child(CommandBranch(alias))
def remove_alias(alias_name): """ Remove an alias. Args: alias_name: The name of the alias to be removed. """ alias_table = get_alias_table() if alias_name not in alias_table.sections(): raise CLIError(ALIAS_NOT_FOUND_ERROR.format(alias_name)) alias_table.remove_section(alias_name) _commit_change(alias_table)
def enable_aliases_autocomplete_interactive(_, **kwargs): """ Enable aliases autocomplete on interactive mode by injecting aliases in the command tree. """ subtree = kwargs.get('subtree', None) if not subtree or not hasattr(subtree, 'children'): return for alias, alias_command in filter_aliases(get_alias_table()): # Only autocomplete the first word because alias is space-delimited if subtree.in_tree(alias_command.split()): subtree.add_child(CommandBranch(alias))
def remove_alias(alias_names): """ Remove an alias. Args: alias_name: The name of the alias to be removed. """ alias_table = get_alias_table() for alias_name in alias_names: if alias_name not in alias_table.sections(): raise CLIError(ALIAS_NOT_FOUND_ERROR.format(alias_name)) alias_table.remove_section(alias_name) _commit_change(alias_table)
def import_aliases(alias_source): """ Import aliases from a file or an URL. Args: alias_source: The source of the alias. It can be a filepath or an URL. """ alias_table = get_alias_table() if is_url(alias_source): alias_source = retrieve_file_from_url(alias_source) alias_table.read(alias_source) os.remove(alias_source) else: alias_table.read(alias_source) _commit_change(alias_table)
def create_alias(alias_name, alias_command): """ Create an alias. Args: alias_name: The name of the alias. alias_command: The command that the alias points to. """ alias_name, alias_command = alias_name.strip(), alias_command.strip() alias_table = get_alias_table() if alias_name not in alias_table.sections(): alias_table.add_section(alias_name) alias_table.set(alias_name, 'command', alias_command) _commit_change(alias_table)
def import_aliases(alias_source): """ Import aliases from a file or an URL. Args: alias_source: The source of the alias. It can be a filepath or an URL. """ alias_table = get_alias_table() if is_url(alias_source): alias_source = retrieve_file_from_url(alias_source) alias_table.read(alias_source) os.remove(alias_source) else: alias_table.read(alias_source) _commit_change(alias_table)
def create_alias(alias_name, alias_command): """ Create an alias. Args: alias_name: The name of the alias. alias_command: The command that the alias points to. """ alias_name, alias_command = alias_name.strip(), alias_command.strip() alias_table = get_alias_table() if alias_name not in alias_table.sections(): alias_table.add_section(alias_name) alias_table.set(alias_name, 'command', alias_command) _commit_change(alias_table)
def _transform_cur_commands(cur_commands, alias_table=None): """ Transform any aliases in cur_commands into their respective commands. Args: alias_table: The alias table. cur_commands: current commands typed in the console. """ transformed = [] alias_table = alias_table if alias_table else get_alias_table() for cmd in cur_commands: if cmd in alias_table.sections() and alias_table.has_option(cmd, 'command'): transformed += alias_table.get(cmd, 'command').split() else: transformed.append(cmd) cur_commands[:] = transformed
def _transform_cur_commands(cur_commands, alias_table=None): """ Transform any aliases in cur_commands into their respective commands. Args: alias_table: The alias table. cur_commands: current commands typed in the console. """ transformed = [] alias_table = alias_table if alias_table else get_alias_table() for cmd in cur_commands: if cmd in alias_table.sections() and alias_table.has_option( cmd, 'command'): transformed += alias_table.get(cmd, 'command').split() else: transformed.append(cmd) cur_commands[:] = transformed
def list_alias(): """ List all registered aliases. Returns: An array of dictionary containing the alias and the command that it points to. """ alias_table = get_alias_table() output = [] for alias in alias_table.sections(): if alias_table.has_option(alias, 'command'): output.append({ 'alias': alias, # Remove unnecessary whitespaces 'command': ' '.join(alias_table.get(alias, 'command').split()) }) return output
def export_aliases(export_path=None, exclusions=None): """ Export all registered aliases to a given path, as an INI configuration file. Args: export_path: The path of the alias configuration file to export to. exclusions: Space-separated aliases excluded from export. """ if not export_path: export_path = os.path.abspath(ALIAS_FILE_NAME) alias_table = get_alias_table() for exclusion in exclusions or []: if exclusion not in alias_table.sections(): raise CLIError(ALIAS_NOT_FOUND_ERROR.format(exclusion)) alias_table.remove_section(exclusion) _commit_change(alias_table, export_path=export_path, post_commit=False) logger.warning(POST_EXPORT_ALIAS_MSG, export_path) # pylint: disable=superfluous-parens
def export_aliases(export_path=None, exclusions=None): """ Export all registered aliases to a given path, as an INI configuration file. Args: export_path: The path of the alias configuration file to export to. exclusions: Space-separated aliases excluded from export. """ if not export_path: export_path = os.path.abspath(ALIAS_FILE_NAME) alias_table = get_alias_table() for exclusion in exclusions or []: if exclusion not in alias_table.sections(): raise CLIError(ALIAS_NOT_FOUND_ERROR.format(exclusion)) alias_table.remove_section(exclusion) _commit_change(alias_table, export_path=export_path, post_commit=False) logger.warning(POST_EXPORT_ALIAS_MSG, export_path) # pylint: disable=superfluous-parens
def list_alias(): """ List all registered aliases. Returns: An array of dictionary containing the alias and the command that it points to. """ alias_table = get_alias_table() output = [] for alias in alias_table.sections(): if alias_table.has_option(alias, 'command'): output.append({ 'alias': alias, # Remove unnecessary whitespaces 'command': ' '.join(alias_table.get(alias, 'command').split()) }) return output
def enable_aliases_autocomplete(_, **kwargs): """ Enable aliases autocomplete by injecting aliases into Azure CLI tab completion list. """ external_completions = kwargs.get('external_completions', []) prefix = kwargs.get('cword_prefix', []) cur_commands = kwargs.get('comp_words', []) alias_table = get_alias_table() # Transform aliases if they are in current commands, # so parser can get the correct subparser when chaining aliases _transform_cur_commands(cur_commands, alias_table=alias_table) for alias, alias_command in filter_aliases(alias_table): if alias.startswith(prefix) and alias.strip() != prefix and _is_autocomplete_valid(cur_commands, alias_command): # Only autocomplete the first word because alias is space-delimited external_completions.append(alias) # Append spaces if necessary (https://github.com/kislyuk/argcomplete/blob/master/argcomplete/__init__.py#L552-L559) prequote = kwargs.get('cword_prequote', '') continuation_chars = "=/:" if len(external_completions) == 1 and external_completions[0][-1] not in continuation_chars and not prequote: external_completions[0] += ' '
def get_alias_completer(cmd, prefix, namespace, **kwargs): # pylint: disable=unused-argument """ An argument completer for alias name. """ return get_alias_table().sections()
def get_alias_completer(cmd, prefix, namespace, **kwargs): # pylint: disable=unused-argument """ An argument completer for alias name. """ return get_alias_table().sections()