def resolve_anomaly(setting_name, help_string, associated_bears, values): """ Displays multiple possible values for the setting to the users and prompts them for actual value to be used. :param setting_name: Name of setting for which multiple possible values exist to fill. :param help_string: string describing the setting. :param associated_bears: list of bears for which the setting is required. :param values: list of possible values for the setting. :return: value provided by the user for the setting. """ values = list(set(values)) STR_ASK_FOR_CORRECT_VALUE = ('coala-quickstart has detected multiple ' 'potential values for the setting "{}" ({}) ' 'needed by {}. The detected values are: {}.\n' 'Please provide the correct value to use:') REPORT_ANOMALY_COLOR = 'green' print(colored(STR_ASK_FOR_CORRECT_VALUE.format(setting_name, help_string, join_names(associated_bears), join_names(values)), REPORT_ANOMALY_COLOR)) return input()
def resolve_anomaly(setting_name, help_string, associated_bears, values): """ Displays multiple possible values for the setting to the users and prompts them for actual value to be used. :param setting_name: Name of setting for which multiple possible values exist to fill. :param help_string: string describing the setting. :param associated_bears: list of bears for which the setting is required. :param values: list of possible values for the setting. :return: value provided by the user for the setting. """ values = list(set(values)) STR_ASK_FOR_CORRECT_VALUE = ('coala-quickstart has detected multiple ' 'potential values for the setting "{}" ({}) ' 'needed by {}. The detected values are: {}.\n' 'Please provide the correct value to use:') REPORT_ANOMALY_COLOR = 'green' print( colored( STR_ASK_FOR_CORRECT_VALUE.format(setting_name, help_string, join_names(associated_bears), join_names(values)), REPORT_ANOMALY_COLOR)) return input()
def acquire_settings(log_printer, settings_names_dict, section): """ This method prompts the user for the given settings. :param log_printer: Printer responsible for logging the messages. This is needed to comply with the interface. :param settings_names_dict: A dictionary with the settings name as key and a list containing a description in [0] and the name of the bears who need this setting in [1] and following. Example: :: {"UseTabs": ["describes whether tabs should be used instead of spaces", "SpaceConsistencyBear", "SomeOtherBear"]} :param section: The section the action corresponds to. :return: A dictionary with the settings name as key and the given value as value. """ if not isinstance(settings_names_dict, dict): raise TypeError('The settings_names_dict parameter has to be a ' 'dictionary.') result = {} for setting_name, arr in sorted(settings_names_dict.items(), key=lambda x: (join_names(x[1][1:]), x[0])): value = require_setting(setting_name, arr, section) result.update({setting_name: value} if value is not None else {}) return result
def acquire_settings(log_printer, settings_dict, section): """ This method prompts the user for the given settings. :param log_printer: Printer responsible for logging the messages. This is needed to comply with the interface. :param settings_names_dict: A dictionary with the settings name as key of the following form :: { "some_setting": { "help_text": "help string for the setting", "bears": [SomeBear", "SomeOtherBear"], "type": bool } :param section: The section the action corresponds to. :return: A dictionary with the settings name as key and the given value as value. """ if not isinstance(settings_dict, dict): raise TypeError('The settings_names_dict parameter has to be a ' 'dictionary.') result = {} for setting_name, setting_info in sorted( settings_dict.items(), key=lambda x: (join_names(x[1]['bears']), x[0])): # As quickstart generates language-based sections, the value for # `language` setting can be filled automatically. if setting_name == 'language': value = section.name else: value = require_setting(setting_name, setting_info, section) result.update({setting_name: value} if value is not None else {}) return result
def require_setting(setting_name, arr, section): """ This method is responsible for prompting a user about a missing setting and taking its value as input from the user. :param setting_name: Name of the setting missing :param arr: A list containing a description in [0] and the name of the bears who need this setting in [1] and following. :param section: The section the action corresponds to. """ needed = join_names(arr[1:]) # Don't use input, it can't deal with escapes! print(colored(STR_GET_VAL_FOR_SETTING.format(setting_name, arr[0], needed, section.name), REQUIRED_SETTINGS_COLOR)) return input()
def require_setting(setting_name, setting_info, section): """ This method is responsible for prompting a user about a missing setting and taking its value as input from the user. :param setting_name: Name of the setting missing :param setting_info: A dictionary of the form :: { "help_text": "help string for the setting", "bears": [SomeBear", "SomeOtherBear"], "type": bool } :param section: The section the action corresponds to. :return: The preferred value provided by the user for the setting. """ needed = join_names(setting_info['bears']) STR_GET_VAL_FOR_SETTING = ('\nPlease enter a value for the setting {} ' '({}) needed by {} for section {}: ') STR_REPORT_INVALID_VALUE_TYPE = ('coala-quickstart was unable to convert ' 'your input to the type {} required for ' 'the setting.') REQUIRED_SETTINGS_COLOR = 'green' REPORT_INVALID_TYPE_COLOR = 'cyan' user_input = '' while True: print( colored( STR_GET_VAL_FOR_SETTING.format(repr(setting_name), setting_info['help_text'], needed, repr(section.name)), REQUIRED_SETTINGS_COLOR)) user_input = input() if setting_info['type']: try: if setting_info['type'] is bool: processed_input = user_input.strip().strip('!').lower() if processed_input in TRUE_STRINGS: user_input = 'True' elif processed_input in FALSE_STRINGS: user_input = 'False' else: raise ValueError else: setting_info['type'](user_input) break except ValueError: print( colored( STR_REPORT_INVALID_VALUE_TYPE.format( setting_info['type']), REPORT_INVALID_TYPE_COLOR)) return user_input
def require_setting(setting_name, setting_info, section): """ This method is responsible for prompting a user about a missing setting and taking its value as input from the user. :param setting_name: Name of the setting missing :param setting_info: A dictionary of the form :: { "help_text": "help string for the setting", "bears": [SomeBear", "SomeOtherBear"], "type": bool } :param section: The section the action corresponds to. :return: The preferred value provided by the user for the setting. """ needed = join_names(setting_info['bears']) STR_GET_VAL_FOR_SETTING = ('\nPlease enter a value for the setting {} ' '({}) needed by {} for section {}: ') STR_REPORT_INVALID_VALUE_TYPE = ('coala-quickstart was unable to convert ' 'your input to the type {} required for ' 'the setting.') REQUIRED_SETTINGS_COLOR = 'green' REPORT_INVALID_TYPE_COLOR = 'cyan' user_input = '' while True: print(colored(STR_GET_VAL_FOR_SETTING.format(repr(setting_name), setting_info['help_text'], needed, repr(section.name)), REQUIRED_SETTINGS_COLOR)) user_input = input() if setting_info['type']: try: if setting_info['type'] is bool: processed_input = user_input.strip().strip('!').lower() if processed_input in TRUE_STRINGS: user_input = 'True' elif processed_input in FALSE_STRINGS: user_input = 'False' else: raise ValueError else: setting_info['type'](user_input) break except ValueError: print(colored( STR_REPORT_INVALID_VALUE_TYPE.format(setting_info['type']), REPORT_INVALID_TYPE_COLOR)) return user_input