def test_check_conflicts(self): sections = parse_cli(arg_list=["--save", "--no-config"]) with self.assertRaises(SystemExit) as cm: check_conflicts(sections) self.assertEqual(cm.exception.code, 2) sections = parse_cli(arg_list=["--no-config", "-S", "val=42"]) self.assertTrue(check_conflicts(sections))
def test_check_conflicts(self): sections = parse_cli(arg_list=['--save', '--no-config']) with self.assertRaisesRegex(SystemExit, '2') as cm: check_conflicts(sections) self.assertEqual(cm.exception.code, 2) sections = parse_cli(arg_list=['--no-config', '-S', 'val=42']) self.assertTrue(check_conflicts(sections)) sections = parse_cli(arg_list=['--relpath']) with self.assertRaisesRegex(SystemExit, '2') as cm: check_conflicts(sections) self.assertEqual(cm.exception.code, 2) sections = parse_cli(arg_list=['--output', 'iraiseValueError']) with self.assertRaisesRegex(SystemExit, '2') as cm: check_conflicts(sections) self.assertEqual(cm.exception.code, 2) sections = parse_cli(arg_list=['--no-config', '--config', '.coafile']) with self.assertRaisesRegex(SystemExit, '2') as cm: check_conflicts(sections) self.assertEqual(cm.exception.code, 2)
def load_configuration(arg_list, log_printer=None, arg_parser=None, args=None, silent=False): """ Parses the CLI args and loads the config file accordingly, taking default_coafile and the users .coarc into account. :param arg_list: The list of CLI arguments. :param log_printer: The LogPrinter object for logging. :param arg_parser: An ``argparse.ArgumentParser`` instance used for parsing the CLI arguments. :param args: Alternative pre-parsed CLI arguments. :param silent: Whether or not to display warnings, ignored if ``save`` is enabled. :return: A tuple holding (log_printer: LogPrinter, sections: dict(str, Section), targets: list(str)). (Types indicated after colon.) """ cli_sections = parse_cli(arg_list=arg_list, arg_parser=arg_parser, args=args) check_conflicts(cli_sections) if ( bool(cli_sections['cli'].get('find_config', 'False')) and str(cli_sections['cli'].get('config')) == ''): cli_sections['cli'].add_or_create_setting( Setting('config', PathArg(find_user_config(os.getcwd())))) # We don't want to store targets argument back to file, thus remove it targets = [item.lower() for item in list( cli_sections['cli'].contents.pop('targets', ''))] if bool(cli_sections['cli'].get('no_config', 'False')): sections = cli_sections else: base_sections = load_config_file(Constants.system_coafile, silent=silent) user_sections = load_config_file( Constants.user_coafile, silent=True) default_config = str(base_sections['default'].get('config', '.coafile')) user_config = str(user_sections['default'].get( 'config', default_config)) config = os.path.abspath( str(cli_sections['cli'].get('config', user_config))) try: save = bool(cli_sections['cli'].get('save', 'False')) except ValueError: # A file is deposited for the save parameter, means we want to save # but to a specific file. save = True coafile_sections = load_config_file(config, silent=save or silent) sections = merge_section_dicts(base_sections, user_sections) sections = merge_section_dicts(sections, coafile_sections) if 'cli' in sections: logging.warning('\'cli\' is an internally reserved section name. ' 'It may have been generated into your coafile ' 'while running coala with `--save`. The settings ' 'in that section will inherit implicitly to all ' 'sections as defaults just like CLI args do. ' 'Please change the name of that section in your ' 'coafile to avoid any unexpected behavior.') sections = merge_section_dicts(sections, cli_sections) for name, section in list(sections.items()): section.set_default_section(sections) if name == 'default': if section.contents: logging.warning('Implicit \'Default\' section inheritance is ' 'deprecated. It will be removed soon. To ' 'silence this warning remove settings in the ' '\'Default\' section from your coafile. You ' 'can use dots to specify inheritance: the ' 'section \'all.python\' will inherit all ' 'settings from \'all\'.') sections['default'].update(sections['cli']) sections['default'].name = 'cli' sections['cli'] = sections['default'] del sections['default'] str_log_level = str(sections['cli'].get('log_level', '')).upper() logging.getLogger().setLevel(LOG_LEVEL.str_dict.get(str_log_level, LOG_LEVEL.INFO)) return sections, targets
def load_configuration(arg_list, log_printer, arg_parser=None): """ Parses the CLI args and loads the config file accordingly, taking default_coafile and the users .coarc into account. :param arg_list: The list of command line arguments. :param log_printer: The LogPrinter object for logging. :return: A tuple holding (log_printer: LogPrinter, sections: dict(str, Section), targets: list(str)). (Types indicated after colon.) """ cli_sections = parse_cli(arg_list=arg_list, arg_parser=arg_parser) check_conflicts(cli_sections) if (bool(cli_sections["default"].get("find_config", "False")) and str(cli_sections["default"].get("config")) == ""): cli_sections["default"].add_or_create_setting( Setting("config", re.escape(find_user_config(os.getcwd())))) targets = [] # We don't want to store targets argument back to file, thus remove it for item in list(cli_sections["default"].contents.pop("targets", "")): targets.append(item.lower()) if bool(cli_sections["default"].get("no_config", "False")): sections = cli_sections else: default_sections = load_config_file(Constants.system_coafile, log_printer) user_sections = load_config_file(Constants.user_coafile, log_printer, silent=True) default_config = str(default_sections["default"].get( "config", ".coafile")) user_config = str(user_sections["default"].get("config", default_config)) config = os.path.abspath( str(cli_sections["default"].get("config", user_config))) try: save = bool(cli_sections["default"].get("save", "False")) except ValueError: # A file is deposited for the save parameter, means we want to save # but to a specific file. save = True coafile_sections = load_config_file(config, log_printer, silent=save) sections = merge_section_dicts(default_sections, user_sections) sections = merge_section_dicts(sections, coafile_sections) sections = merge_section_dicts(sections, cli_sections) for section in sections: if section != "default": sections[section].defaults = sections["default"] str_log_level = str(sections["default"].get("log_level", "")).upper() log_printer.log_level = LOG_LEVEL.str_dict.get(str_log_level, LOG_LEVEL.INFO) warn_config_absent(sections, 'files', log_printer) warn_config_absent(sections, 'bears', log_printer) return sections, targets
def load_configuration(arg_list, log_printer, arg_parser=None): """ Parses the CLI args and loads the config file accordingly, taking default_coafile and the users .coarc into account. :param arg_list: The list of command line arguments. :param log_printer: The LogPrinter object for logging. :return: A tuple holding (log_printer: LogPrinter, sections: dict(str, Section), targets: list(str)). (Types indicated after colon.) """ cli_sections = parse_cli(arg_list=arg_list, arg_parser=arg_parser) check_conflicts(cli_sections) if ( bool(cli_sections['default'].get('find_config', 'False')) and str(cli_sections['default'].get('config')) == ''): cli_sections['default'].add_or_create_setting( Setting('config', re.escape(find_user_config(os.getcwd())))) targets = [] # We don't want to store targets argument back to file, thus remove it for item in list(cli_sections['default'].contents.pop('targets', '')): targets.append(item.lower()) if bool(cli_sections['default'].get('no_config', 'False')): sections = cli_sections else: default_sections = load_config_file(Constants.system_coafile, log_printer) user_sections = load_config_file( Constants.user_coafile, log_printer, silent=True) default_config = str( default_sections['default'].get('config', '.coafile')) user_config = str(user_sections['default'].get( 'config', default_config)) config = os.path.abspath( str(cli_sections['default'].get('config', user_config))) try: save = bool(cli_sections['default'].get('save', 'False')) except ValueError: # A file is deposited for the save parameter, means we want to save # but to a specific file. save = True coafile_sections = load_config_file(config, log_printer, silent=save) sections = merge_section_dicts(default_sections, user_sections) sections = merge_section_dicts(sections, coafile_sections) sections = merge_section_dicts(sections, cli_sections) for section in sections: if section != 'default': sections[section].defaults = sections['default'] str_log_level = str(sections['default'].get('log_level', '')).upper() log_printer.log_level = LOG_LEVEL.str_dict.get(str_log_level, LOG_LEVEL.INFO) return sections, targets
def load_configuration(arg_list, log_printer=None, arg_parser=None, args=None, silent=False): """ Parses the CLI args and loads the config file accordingly, taking default_coafile and the users .coarc into account. :param arg_list: The list of CLI arguments. :param log_printer: The LogPrinter object for logging. :param arg_parser: An ``argparse.ArgumentParser`` instance used for parsing the CLI arguments. :param args: Alternative pre-parsed CLI arguments. :param silent: Whether or not to display warnings, ignored if ``save`` is enabled. :return: A tuple holding (log_printer: LogPrinter, sections: dict(str, Section), targets: list(str)). (Types indicated after colon.) """ cli_sections = parse_cli(arg_list=arg_list, arg_parser=arg_parser, args=args) check_conflicts(cli_sections) if ( bool(cli_sections['cli'].get('find_config', 'False')) and str(cli_sections['cli'].get('config')) == ''): cli_sections['cli'].add_or_create_setting( Setting('config', PathArg(find_user_config(os.getcwd())))) targets = [] # We don't want to store targets argument back to file, thus remove it for item in list(cli_sections['cli'].contents.pop('targets', '')): targets.append(item.lower()) if bool(cli_sections['cli'].get('no_config', 'False')): sections = cli_sections else: base_sections = load_config_file(Constants.system_coafile, silent=silent) user_sections = load_config_file( Constants.user_coafile, silent=True) default_config = str(base_sections['default'].get('config', '.coafile')) user_config = str(user_sections['default'].get( 'config', default_config)) config = os.path.abspath( str(cli_sections['cli'].get('config', user_config))) try: save = bool(cli_sections['cli'].get('save', 'False')) except ValueError: # A file is deposited for the save parameter, means we want to save # but to a specific file. save = True coafile_sections = load_config_file(config, silent=save or silent) sections = merge_section_dicts(base_sections, user_sections) sections = merge_section_dicts(sections, coafile_sections) if 'cli' in sections: logging.warning('\'cli\' is an internally reserved section name. ' 'It may have been generated into your coafile ' 'while running coala with `--save`. The settings ' 'in that section will inherit implicitly to all ' 'sections as defaults just like CLI args do. ' 'Please change the name of that section in your ' 'coafile to avoid any unexpected behavior.') sections = merge_section_dicts(sections, cli_sections) for name, section in list(sections.items()): section.set_default_section(sections) if name == 'default': if section.contents: logging.warning('Implicit \'Default\' section inheritance is ' 'deprecated. It will be removed soon. To ' 'silence this warning remove settings in the ' '\'Default\' section from your coafile. You ' 'can use dots to specify inheritance: the ' 'section \'all.python\' will inherit all ' 'settings from \'all\'.') sections['default'].update(sections['cli']) sections['default'].name = 'cli' sections['cli'] = sections['default'] del sections['default'] str_log_level = str(sections['cli'].get('log_level', '')).upper() logging.getLogger().setLevel(LOG_LEVEL.str_dict.get(str_log_level, LOG_LEVEL.INFO)) return sections, targets