def __init__(self, **kwargs): super(AzCli, self).__init__(**kwargs) from azure.cli.core.commands import register_cache_arguments from azure.cli.core.commands.arm import ( register_ids_argument, register_global_subscription_argument) from azure.cli.core.cloud import get_active_cloud from azure.cli.core.commands.transform import register_global_transforms from azure.cli.core._session import ACCOUNT, CONFIG, SESSION, INDEX from knack.util import ensure_dir self.data['headers'] = {} self.data['command'] = 'unknown' self.data['command_extension_name'] = None self.data['completer_active'] = ARGCOMPLETE_ENV_NAME in os.environ self.data['query_active'] = False azure_folder = self.config.config_dir ensure_dir(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) INDEX.load(os.path.join(azure_folder, 'commandIndex.json')) self.cloud = get_active_cloud(self) logger.debug('Current cloud config:\n%s', str(self.cloud.name)) self.local_context = AzCLILocalContext(self) register_global_transforms(self) register_global_subscription_argument(self) register_ids_argument( self) # global subscription must be registered first! register_cache_arguments(self) self.progress_controller = None
def create_invoker_and_load_cmds_and_args(cli_ctx): from knack import events from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument invoker = cli_ctx.invocation_cls( cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.skip_applicability = True invoker.commands_loader.load_command_table(None) # turn off applicability check for all loaders for loaders in invoker.commands_loader.cmd_to_loader_map.values(): for loader in loaders: loader.skip_applicability = True for command in invoker.commands_loader.command_table: invoker.commands_loader.load_arguments(command) register_global_subscription_argument(cli_ctx) register_ids_argument( cli_ctx) # global subscription must be registered first! cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader) invoker.parser.load_command_table(invoker.commands_loader)
def create_invoker_and_load_cmds_and_args(cli_ctx): from knack import events from azure.cli.core.commands import register_cache_arguments from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.skip_applicability = True invoker.commands_loader.load_command_table(None) # Deal with failed import MainCommandsLoader.load_command_table._update_command_table_from_modules # during tox test. if not invoker.commands_loader.cmd_to_loader_map: module_command_table, module_group_table = mock_load_command_loader(invoker.commands_loader, None, MOCKED_COMMAND_LOADER_MOD, None) for cmd in module_command_table.values(): cmd.command_source = MOCKED_COMMAND_LOADER_MOD invoker.commands_loader.command_table.update(module_command_table) invoker.commands_loader.command_group_table.update(module_group_table) # turn off applicability check for all loaders for loaders in invoker.commands_loader.cmd_to_loader_map.values(): for loader in loaders: loader.skip_applicability = True for command in invoker.commands_loader.command_table: invoker.commands_loader.load_arguments(command) register_global_subscription_argument(cli_ctx) register_ids_argument(cli_ctx) # global subscription must be registered first! register_cache_arguments(cli_ctx) cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader) invoker.parser.load_command_table(invoker.commands_loader)
def create_invoker_and_load_cmds_and_args(cli_ctx): from knack import events from azure.cli.core.commands import register_cache_arguments from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.skip_applicability = True invoker.commands_loader.load_command_table(None) # Deal with failed import MainCommandsLoader.load_command_table._update_command_table_from_modules # during tox test. if not invoker.commands_loader.cmd_to_loader_map: module_command_table, module_group_table = mock_load_command_loader(invoker.commands_loader, None, MOCKED_COMMAND_LOADER_MOD, None) for cmd in module_command_table.values(): cmd.command_source = MOCKED_COMMAND_LOADER_MOD invoker.commands_loader.command_table.update(module_command_table) invoker.commands_loader.command_group_table.update(module_group_table) # turn off applicability check for all loaders for loaders in invoker.commands_loader.cmd_to_loader_map.values(): for loader in loaders: loader.skip_applicability = True for command in invoker.commands_loader.command_table: invoker.commands_loader.load_arguments(command) register_global_subscription_argument(cli_ctx) register_ids_argument(cli_ctx) # global subscription must be registered first! register_cache_arguments(cli_ctx) cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader) invoker.parser.load_command_table(invoker.commands_loader)
def __init__(self, **kwargs): super(AzCli, self).__init__(**kwargs) from azure.cli.core.commands.arm import ( register_ids_argument, register_global_subscription_argument) from azure.cli.core.cloud import get_active_cloud from azure.cli.core.commands.transform import register_global_transforms from azure.cli.core._session import ACCOUNT, CONFIG, SESSION from knack.util import ensure_dir self.data['headers'] = {} self.data['command'] = 'unknown' self.data['command_extension_name'] = None self.data['completer_active'] = ARGCOMPLETE_ENV_NAME in os.environ self.data['query_active'] = False azure_folder = self.config.config_dir ensure_dir(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) self.cloud = get_active_cloud(self) logger.debug('Current cloud config:\n%s', str(self.cloud.name)) register_global_transforms(self) register_global_subscription_argument(self) register_ids_argument(self) # global subscription must be registered first! self.progress_controller = None
def dump_command_table(self, shell_ctx=None): """ dumps the command table """ from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument from knack import events import timeit start_time = timeit.default_timer() shell_ctx = shell_ctx or self.shell_ctx main_loader = AzInteractiveCommandsLoader(shell_ctx.cli_ctx) main_loader.load_command_table(None) main_loader.load_arguments(None) register_global_subscription_argument(shell_ctx.cli_ctx) register_ids_argument(shell_ctx.cli_ctx) shell_ctx.cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=main_loader) cmd_table = main_loader.command_table cmd_table_data = {} for command_name, cmd in cmd_table.items(): try: command_description = cmd.description if callable(command_description): command_description = command_description() # checking all the parameters for a single command parameter_metadata = {} for arg in cmd.arguments.values(): options = { 'name': [name for name in arg.options_list], 'required': REQUIRED_TAG if arg.type.settings.get('required') else '', 'help': arg.type.settings.get('help') or '' } # the key is the first alias option if arg.options_list: parameter_metadata[arg.options_list[0]] = options cmd_table_data[command_name] = { 'parameters': parameter_metadata, 'help': command_description, 'examples': '' } except (ImportError, ValueError): pass load_help_files(cmd_table_data) elapsed = timeit.default_timer() - start_time logger.debug('Command table dumped: %s sec', elapsed) FreshTable.loader = main_loader # dump into the cache file command_file = shell_ctx.config.get_help_files() with open(os.path.join(get_cache_dir(shell_ctx), command_file), 'w') as help_file: json.dump(cmd_table_data, help_file, default=lambda x: x.target or '', skipkeys=True)
def dump_command_table(self, shell_ctx=None): """ dumps the command table """ from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument from knack import events import timeit start_time = timeit.default_timer() shell_ctx = shell_ctx or self.shell_ctx main_loader = AzInteractiveCommandsLoader(shell_ctx.cli_ctx) main_loader.load_command_table(None) main_loader.load_arguments(None) register_global_subscription_argument(shell_ctx.cli_ctx) register_ids_argument(shell_ctx.cli_ctx) shell_ctx.cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=main_loader) cmd_table = main_loader.command_table cmd_table_data = {} for command_name, cmd in cmd_table.items(): try: command_description = cmd.description if callable(command_description): command_description = command_description() # checking all the parameters for a single command parameter_metadata = {} for arg in cmd.arguments.values(): options = { 'name': [name for name in arg.options_list], 'required': REQUIRED_TAG if arg.type.settings.get('required') else '', 'help': arg.type.settings.get('help') or '' } # the key is the first alias option if arg.options_list: parameter_metadata[arg.options_list[0]] = options cmd_table_data[command_name] = { 'parameters': parameter_metadata, 'help': command_description, 'examples': '' } except (ImportError, ValueError): pass load_help_files(cmd_table_data) elapsed = timeit.default_timer() - start_time logger.debug('Command table dumped: %s sec', elapsed) FreshTable.loader = main_loader # dump into the cache file command_file = shell_ctx.config.get_help_files() with open(os.path.join(get_cache_dir(shell_ctx), command_file), 'w') as help_file: json.dump(cmd_table_data, help_file, default=lambda x: x.target or '', skipkeys=True)
def create_invoker_and_load_cmds_and_args(cli_ctx): global loaded_helps from knack import events from azure.cli.core.commands import register_cache_arguments from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.skip_applicability = True temp_help = helps.copy() invoker.commands_loader.load_command_table(None) # this ends up loading all the helpfiles, which could be problematic with duplicate key commands like acs create helps.clear() helps.update(temp_help) # turn off applicability check for applicable loaders new_cmd_to_loader_map = {} new_command_group_table = {} new_command_table = {} for cmd in loaded_helps.keys(): if cmd in invoker.commands_loader.cmd_to_loader_map: # if a command from help, then update commands_loader new_cmd_to_loader_map[cmd] = invoker.commands_loader.cmd_to_loader_map[cmd] new_command_table[cmd] = invoker.commands_loader.command_table[cmd] else: # else a group then update new_command_group_table[cmd] = invoker.commands_loader.command_group_table[cmd] # include commands of groups in help.py, even though the commands themselves might not be in help.py, so their subparsers can be added. for cmd in new_command_group_table.keys(): all_cmds = invoker.commands_loader.cmd_to_loader_map.keys() for old_cmd in all_cmds: if old_cmd.startswith(cmd) and old_cmd != cmd: new_cmd_to_loader_map[old_cmd] = invoker.commands_loader.cmd_to_loader_map[old_cmd] new_command_table[old_cmd] = invoker.commands_loader.command_table[old_cmd] invoker.commands_loader.cmd_to_loader_map = new_cmd_to_loader_map invoker.commands_loader.command_table = new_command_table invoker.commands_loader.command_group_table = new_command_group_table for loaders in invoker.commands_loader.cmd_to_loader_map.values(): for loader in loaders: loader.skip_applicability = True for command in invoker.commands_loader.command_table: invoker.commands_loader.load_arguments(command) assert len(new_command_table) == len(new_cmd_to_loader_map) assert set(list(new_command_table.keys()) + list(new_command_group_table.keys())) >= set(loaded_helps.keys()) register_global_subscription_argument(cli_ctx) register_ids_argument(cli_ctx) # global subscription must be registered first! register_cache_arguments(cli_ctx) cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader) invoker.parser.load_command_table(invoker.commands_loader)
def __init__(self, **kwargs): super(AzCli, self).__init__(**kwargs) from azure.cli.core.commands import register_cache_arguments from azure.cli.core.commands.arm import ( register_ids_argument, register_global_subscription_argument) from azure.cli.core.cloud import get_active_cloud from azure.cli.core.commands.transform import register_global_transforms from azure.cli.core._session import ACCOUNT, CONFIG, SESSION, INDEX, VERSIONS from azure.cli.core.style import format_styled_text from azure.cli.core.util import handle_version_update from azure.cli.core.commands.query_examples import register_global_query_examples_argument from knack.util import ensure_dir self.data['headers'] = {} self.data['command'] = 'unknown' self.data['command_extension_name'] = None self.data['completer_active'] = ARGCOMPLETE_ENV_NAME in os.environ self.data['query_active'] = False azure_folder = self.config.config_dir ensure_dir(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) INDEX.load(os.path.join(azure_folder, 'commandIndex.json')) VERSIONS.load(os.path.join(azure_folder, 'versionCheck.json')) handle_version_update() self.cloud = get_active_cloud(self) logger.debug('Current cloud config:\n%s', str(self.cloud.name)) self.local_context = AzCLILocalContext(self) register_global_transforms(self) register_global_subscription_argument(self) register_global_query_examples_argument(self) register_ids_argument( self) # global subscription must be registered first! register_cache_arguments(self) self.progress_controller = None if self.enable_color: theme = self.config.get('core', 'theme', fallback='dark') else: theme = 'none' format_styled_text.theme = theme
def setUpClass(cls): super(TestNormalizeAndSortParameters, cls).setUpClass() from knack.events import EVENT_INVOKER_POST_CMD_TBL_CREATE from azure.cli.core.commands.events import EVENT_INVOKER_PRE_LOAD_ARGUMENTS, EVENT_INVOKER_POST_LOAD_ARGUMENTS from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument # setup a dummy CLI with a valid invocation object. cls.cli = DummyCli() cli_ctx = cls.cli.commands_loader.cli_ctx cls.cli.invocation = cli_ctx.invocation_cls( cli_ctx=cli_ctx, parser_cls=cli_ctx.parser_cls, commands_loader_cls=cli_ctx.commands_loader_cls, help_cls=cli_ctx.help_cls) # load command table for every module cmd_loader = cls.cli.invocation.commands_loader cmd_loader.load_command_table(None) # Note: Both of the below events rely on EVENT_INVOKER_POST_CMD_TBL_CREATE. # register handler for adding subscription argument register_global_subscription_argument(cli_ctx) # register handler for adding ids argument. register_ids_argument(cli_ctx) cli_ctx.raise_event(EVENT_INVOKER_PRE_LOAD_ARGUMENTS, commands_loader=cmd_loader) # load arguments for each command for cmd in get_commands(): # simulate command invocation by filling in required metadata. cmd_loader.command_name = cmd cli_ctx.invocation.data['command_string'] = cmd # load argument info for the given command. cmd_loader.load_arguments(cmd) cli_ctx.raise_event(EVENT_INVOKER_POST_LOAD_ARGUMENTS, commands_loader=cmd_loader) cli_ctx.raise_event(EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=cmd_loader) cls.cmd_tbl = cmd_loader.command_table
def create_invoker_and_load_cmds_and_args(cli_ctx): from knack import events from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument invoker = cli_ctx.invocation_cls( cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.load_command_table(None) for command in invoker.commands_loader.command_table: invoker.commands_loader.load_arguments(command) invoker.parser.load_command_table(invoker.commands_loader) register_global_subscription_argument(cli_ctx) register_ids_argument( cli_ctx) # global subscription must be registered first! cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader)
def create_invoker_and_load_cmds_and_args(cli_ctx): from knack.events import (EVENT_INVOKER_PRE_CMD_TBL_CREATE, EVENT_INVOKER_POST_CMD_TBL_CREATE) from azure.cli.core.commands import register_cache_arguments from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument from azure.cli.core.commands.events import ( EVENT_INVOKER_PRE_LOAD_ARGUMENTS, EVENT_INVOKER_POST_LOAD_ARGUMENTS) import time start_time = time.time() register_global_subscription_argument(cli_ctx) register_ids_argument(cli_ctx) register_cache_arguments(cli_ctx) invoker = cli_ctx.invocation_cls( cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.skip_applicability = True cli_ctx.raise_event(EVENT_INVOKER_PRE_CMD_TBL_CREATE, args=[]) invoker.commands_loader.load_command_table(None) invoker.commands_loader.command_name = '' cli_ctx.raise_event(EVENT_INVOKER_PRE_LOAD_ARGUMENTS, commands_loader=invoker.commands_loader) invoker.commands_loader.load_arguments() cli_ctx.raise_event(EVENT_INVOKER_POST_LOAD_ARGUMENTS, commands_loader=invoker.commands_loader) cli_ctx.raise_event(EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader) invoker.parser.cli_ctx = cli_ctx invoker.parser.load_command_table(invoker.commands_loader) end_time = time.time() logger.info('Time to load entire command table: %.3f sec', end_time - start_time)
def create_invoker_and_load_cmds_and_args(cli_ctx): from knack import events from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument invoker = cli_ctx.invocation_cls(cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.skip_applicability = True invoker.commands_loader.load_command_table(None) # turn off applicability check for all loaders for loaders in invoker.commands_loader.cmd_to_loader_map.values(): for loader in loaders: loader.skip_applicability = True for command in invoker.commands_loader.command_table: invoker.commands_loader.load_arguments(command) register_global_subscription_argument(cli_ctx) register_ids_argument(cli_ctx) # global subscription must be registered first! cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader) invoker.parser.load_command_table(invoker.commands_loader)
def create_invoker_and_load_cmds_and_args(cli_ctx): global loaded_helps from knack import events from azure.cli.core.commands import register_cache_arguments from azure.cli.core.commands.arm import register_global_subscription_argument, register_ids_argument invoker = cli_ctx.invocation_cls( cli_ctx=cli_ctx, commands_loader_cls=cli_ctx.commands_loader_cls, parser_cls=cli_ctx.parser_cls, help_cls=cli_ctx.help_cls) cli_ctx.invocation = invoker invoker.commands_loader.skip_applicability = True temp_help = helps.copy() invoker.commands_loader.load_command_table( None ) # this ends up loading all the helpfiles, which could be problematic with duplicate key commands like acs create helps.clear() helps.update(temp_help) # turn off applicability check for applicable loaders new_cmd_to_loader_map = {} new_command_group_table = {} new_command_table = {} for cmd in loaded_helps.keys(): if cmd in invoker.commands_loader.cmd_to_loader_map: # if a command from help, then update commands_loader new_cmd_to_loader_map[ cmd] = invoker.commands_loader.cmd_to_loader_map[cmd] new_command_table[cmd] = invoker.commands_loader.command_table[cmd] else: # else a group then update new_command_group_table[ cmd] = invoker.commands_loader.command_group_table[cmd] # include commands of groups in help.py, even though the commands themselves might not be in help.py, so their subparsers can be added. for cmd in new_command_group_table.keys(): all_cmds = invoker.commands_loader.cmd_to_loader_map.keys() for old_cmd in all_cmds: if old_cmd.startswith(cmd) and old_cmd != cmd: new_cmd_to_loader_map[ old_cmd] = invoker.commands_loader.cmd_to_loader_map[ old_cmd] new_command_table[ old_cmd] = invoker.commands_loader.command_table[old_cmd] invoker.commands_loader.cmd_to_loader_map = new_cmd_to_loader_map invoker.commands_loader.command_table = new_command_table invoker.commands_loader.command_group_table = new_command_group_table for loaders in invoker.commands_loader.cmd_to_loader_map.values(): for loader in loaders: loader.skip_applicability = True for command in invoker.commands_loader.command_table: invoker.commands_loader.load_arguments(command) assert len(new_command_table) == len(new_cmd_to_loader_map) assert set( list(new_command_table.keys()) + list(new_command_group_table.keys())) >= set(loaded_helps.keys()) register_global_subscription_argument(cli_ctx) register_ids_argument( cli_ctx) # global subscription must be registered first! register_cache_arguments(cli_ctx) cli_ctx.raise_event(events.EVENT_INVOKER_POST_CMD_TBL_CREATE, commands_loader=invoker.commands_loader) invoker.parser.load_command_table(invoker.commands_loader)