From 54aba72efdd49e4d6a5a4fd9359719759dc28164 Mon Sep 17 00:00:00 2001 From: Jiashuo Li Date: Mon, 16 Mar 2020 20:54:10 +0800 Subject: [PATCH] Allow disabling color (#12601) --- src/azure-cli-core/azure/cli/core/__init__.py | 9 ++++----- src/azure-cli-core/azure/cli/core/_help.py | 19 ++++++++++++------- .../azure/cli/core/commands/__init__.py | 9 --------- .../azure/cli/core/commands/constants.py | 5 +++-- .../azure/cli/core/extension/operations.py | 6 ------ 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/__init__.py b/src/azure-cli-core/azure/cli/core/__init__.py index b4ab074a924..8e2e1b42fe1 100644 --- a/src/azure-cli-core/azure/cli/core/__init__.py +++ b/src/azure-cli-core/azure/cli/core/__init__.py @@ -85,8 +85,8 @@ def get_cli_version(self): def show_version(self): from azure.cli.core.util import get_az_version_string - from azure.cli.core.commands.constants import SURVEY_PROMPT - import colorama + from azure.cli.core.commands.constants import SURVEY_PROMPT, SURVEY_PROMPT_COLOR + ver_string, updates_available = get_az_version_string() print(ver_string) if updates_available == -1: @@ -98,9 +98,7 @@ def show_version(self): else: print('Your CLI is up-to-date.') - colorama.init() # This could be removed when knack fix is released - print('\n' + SURVEY_PROMPT) - colorama.deinit() # This could be removed when knack fix is released + print('\n' + (SURVEY_PROMPT_COLOR if self.enable_color else SURVEY_PROMPT)) def exception_handler(self, ex): # pylint: disable=no-self-use from azure.cli.core.util import handle_exception @@ -448,6 +446,7 @@ def command_group(self, group_name, command_type=None, **kwargs): kwargs['deprecate_info'].target = group_name if kwargs.get('is_preview', False): kwargs['preview_info'] = PreviewItem( + cli_ctx=self.cli_ctx, target=group_name, object_type='command group' ) diff --git a/src/azure-cli-core/azure/cli/core/_help.py b/src/azure-cli-core/azure/cli/core/_help.py index 500dbeebafb..3b960021b0a 100644 --- a/src/azure-cli-core/azure/cli/core/_help.py +++ b/src/azure-cli-core/azure/cli/core/_help.py @@ -7,7 +7,7 @@ import argparse from azure.cli.core.commands import ExtensionCommandSource -from azure.cli.core.commands.constants import SURVEY_PROMPT +from azure.cli.core.commands.constants import SURVEY_PROMPT, SURVEY_PROMPT_COLOR from knack.help import (HelpFile as KnackHelpFile, CommandHelpFile as KnackCommandHelpFile, GroupHelpFile as KnackGroupHelpFile, ArgumentGroupRegistry as KnackArgumentGroupRegistry, @@ -64,6 +64,7 @@ def _print_header(self, cli_name, help_file): def _print_detailed_help(self, cli_name, help_file): CLIPrintMixin._print_extensions_msg(help_file) super(CLIPrintMixin, self)._print_detailed_help(cli_name, help_file) + self._print_az_find_message(help_file.command, self.cli_ctx.enable_color) @staticmethod def _get_choices_defaults_sources_str(p): @@ -75,7 +76,6 @@ def _get_choices_defaults_sources_str(p): @staticmethod def _print_examples(help_file): - from colorama import Style indent = 0 _print_indent('Examples', indent) for e in help_file.examples: @@ -86,9 +86,15 @@ def _print_examples(help_file): _print_indent(u'{0}'.format(e.long_summary), indent) _print_indent(u'{0}'.format(e.command), indent) print('') + + @staticmethod + def _print_az_find_message(command, enable_color): + from colorama import Style indent = 0 - message = 'For more specific examples, use: az find "az {}"'.format(help_file.command) - _print_indent(Style.BRIGHT + message + Style.RESET_ALL + '\n', indent) + message = 'For more specific examples, use: az find "az {}"'.format(command) + if enable_color: + message = Style.BRIGHT + message + Style.RESET_ALL + _print_indent(message + '\n', indent) @staticmethod def _process_value_sources(p): @@ -150,8 +156,6 @@ def new_normalize_text(s): def show_help(self, cli_name, nouns, parser, is_group): self.update_loaders_with_help_file_contents(nouns) - import colorama - colorama.init(autoreset=True) delimiters = ' '.join(nouns) help_file = self.command_help_cls(self, delimiters, parser) if not is_group \ else self.group_help_cls(self, delimiters, parser) @@ -162,7 +166,8 @@ def show_help(self, cli_name, nouns, parser, is_group): AzCliHelp.update_examples(help_file) self._print_detailed_help(cli_name, help_file) - print(SURVEY_PROMPT) + from colorama import Fore, Style + print(SURVEY_PROMPT_COLOR if self.cli_ctx.enable_color else SURVEY_PROMPT) def _register_help_loaders(self): import azure.cli.core._help_loaders as help_loaders diff --git a/src/azure-cli-core/azure/cli/core/commands/__init__.py b/src/azure-cli-core/azure/cli/core/commands/__init__.py index 7ffb6b20577..4d14b97ccec 100644 --- a/src/azure-cli-core/azure/cli/core/commands/__init__.py +++ b/src/azure-cli-core/azure/cli/core/commands/__init__.py @@ -686,8 +686,6 @@ def resolve_warnings(self, cmd, parsed_args): self._resolve_extension_override_warning(cmd) def _resolve_preview_and_deprecation_warnings(self, cmd, parsed_args): - import colorama - deprecations = [] + getattr(parsed_args, '_argument_deprecations', []) if cmd.deprecate_info: deprecations.append(cmd.deprecate_info) @@ -724,12 +722,10 @@ def _resolve_preview_and_deprecation_warnings(self, cmd, parsed_args): del preview_kwargs['_get_message'] previews.append(ImplicitPreviewItem(**preview_kwargs)) - colorama.init() for d in deprecations: print(d.message, file=sys.stderr) for p in previews: print(p.message, file=sys.stderr) - colorama.deinit() def _resolve_extension_override_warning(self, cmd): # pylint: disable=no-self-use if isinstance(cmd.command_source, ExtensionCommandSource) and cmd.command_source.overrides_command: @@ -864,12 +860,8 @@ def _generate_template_progress(self, correlation_id): # pylint: disable=no-sel logger.info(result) def __call__(self, poller): - import colorama from msrest.exceptions import ClientException - # https://github.com/azure/azure-cli/issues/3555 - colorama.init() - correlation_message = '' self.cli_ctx.get_progress_controller().begin() correlation_id = None @@ -910,7 +902,6 @@ def __call__(self, poller): handle_long_running_operation_exception(client_exception) self.cli_ctx.get_progress_controller().end() - colorama.deinit() return result diff --git a/src/azure-cli-core/azure/cli/core/commands/constants.py b/src/azure-cli-core/azure/cli/core/commands/constants.py index ed9dfe710fb..19c6cedb4be 100644 --- a/src/azure-cli-core/azure/cli/core/commands/constants.py +++ b/src/azure-cli-core/azure/cli/core/commands/constants.py @@ -31,5 +31,6 @@ BLACKLISTED_MODS = ['context', 'shell', 'documentdb', 'component'] -SURVEY_PROMPT = Fore.YELLOW + Style.BRIGHT + 'Please let us know how we are doing: ' + Fore.BLUE \ - + 'https://aka.ms/clihats' + Style.RESET_ALL +SURVEY_PROMPT = 'Please let us know how we are doing: https://aka.ms/clihats' +SURVEY_PROMPT_COLOR = Fore.YELLOW + Style.BRIGHT + 'Please let us know how we are doing: ' + Fore.BLUE \ + + 'https://aka.ms/clihats' + Style.RESET_ALL diff --git a/src/azure-cli-core/azure/cli/core/extension/operations.py b/src/azure-cli-core/azure/cli/core/extension/operations.py index ca26b00322f..c1a5ca30fd1 100644 --- a/src/azure-cli-core/azure/cli/core/extension/operations.py +++ b/src/azure-cli-core/azure/cli/core/extension/operations.py @@ -77,8 +77,6 @@ def _validate_whl_extension(ext_file): def _add_whl_ext(cmd, source, ext_sha256=None, pip_extra_index_urls=None, pip_proxy=None): # pylint: disable=too-many-statements - import colorama - colorama.init() # Required for displaying the spinner correctly on windows issue #9140 cmd.cli_ctx.get_progress_controller().add(message='Analyzing') if not source.endswith('.whl'): raise ValueError('Unknown extension type. Only Python wheels are supported.') @@ -155,7 +153,6 @@ def _add_whl_ext(cmd, source, ext_sha256=None, pip_extra_index_urls=None, pip_pr dst = os.path.join(extension_path, whl_filename) shutil.copyfile(ext_file, dst) logger.debug('Saved the whl to %s', dst) - colorama.deinit() def is_valid_sha256sum(a_file, expected_sum): @@ -201,10 +198,7 @@ def add_extension(cmd, source=None, extension_name=None, index_url=None, yes=Non pip_extra_index_urls=None, pip_proxy=None): ext_sha256 = None if extension_name: - import colorama - colorama.init() # Required for displaying the spinner correctly on windows issue #9140 cmd.cli_ctx.get_progress_controller().add(message='Searching') - colorama.deinit() ext = None try: ext = get_extension(extension_name)