def load_config_from_command_line(self, command_line_args): LOGGER.debug('Trying to load config from command line "%s".', command_line_args) config_name = self.find_config_name_from_command_line(command_line_args) if config_name: config_file = self.locator.locate_config_file(config_name) if config_file: self.parser.parse_file(config_file)
def test_verbose_twice_long_option(): level = LOGGER.level try: (command, errors) = parse(['--verbose', '--verbose']) assert not errors assert isinstance(command, STDINCommand) assert LOGGER.level == level - 20 finally: LOGGER.setLevel(level)
def test_verbose_short_option(): level = LOGGER.level try: (command, errors) = parse(['-v']) assert not errors assert isinstance(command, STDINCommand) assert LOGGER.level == level - 10 finally: LOGGER.setLevel(level)
def load_config_from_command_line(self, command_line_args): LOGGER.debug('Trying to load config from command line "%s".', command_line_args) config_name = self.find_config_name_from_command_line( command_line_args) if config_name: config_file = self.locator.locate_config_file(config_name) if config_file: self.parser.parse_file(config_file)
def locate_config_file(self, config, working_directory=None): LOGGER.debug('Trying to find config "%s"', config) config_file = self.locate_config_file_in_directory(os.getcwd(), config) if config_file: return config_file if working_directory: config_file = self.locate_config_file_in_directory(working_directory, config) if config_file: return config_file for directory in self.paths: if directory: config_file = self.locate_config_file_in_directory(directory, config) if config_file: return config_file
def parse_file(self, config_file): LOGGER.debug('Loading the config file "%s"', config_file) config_parser = configparser.ConfigParser() try: if not config_parser.read(config_file): self.error_handler('Could not open config file "%s"' % config_file) return except configparser.DuplicateSectionError as e: # no cover (inconsistent between Python 2 and 3) self.error_handler('Duplicate section "%s" in "%s"' % (e.section, config_file)) return enable_stderr_filtering = True for section in config_parser.sections(): if section == 'general': for key, value in config_parser.items(section): if key == 'imports': if not value: self.error_handler( 'Empty imports section in config "%s"' % config_file) else: for config_import in [ v.strip() for v in value.split(',') ]: if not config_import: self.error_handler( 'Empty import in config "%s"' % config_file) else: config_import_file = self.locator.locate_config_file( config_import, dirname(config_file)) if config_import_file: self.parse_file(config_import_file) else: self.error_handler( 'Failed to resolve import of "%s" in config "%s"' % (config_import, config_file)) elif key == 'enable-stderr-filtering': try: enable_stderr_filtering = config_parser.getboolean( section, 'enable-stderr-filtering') except ValueError: self.error_handler( 'Invalid value "%s" for key "%s" in config "%s"' % (value, key, config_file)) else: self.error_handler( 'Invalid key "%s" in general section of config "%s"' % (key, config_file)) elif section == 'filters': for filter_name, pattern_lines in config_parser.items(section): resolved_filter = \ FILTERS_BY_NAME.get(filter_name) or \ FILTERS_BY_LONG_OPTION.get(filter_name) or \ FILTERS_BY_SHORT_OPTION.get(filter_name) if not resolved_filter: self.error_handler( 'Unknown filter "%s" in config "%s"' % (filter_name, config_file)) continue if not pattern_lines: self.error_handler( 'Empty pattern for "%s" in config "%s"' % (filter_name, config_file)) continue for pattern in pattern_lines.splitlines(): self.stdout_builder.add_mapping( pattern, resolved_filter) if enable_stderr_filtering: self.stderr_builder.add_mapping( pattern, resolved_filter) else: self.error_handler('Invalid section "%s" in config "%s"' % (section, config_file)) LOGGER.info('Loaded config "%s"', config_file)
def parse_file(self, config_file): LOGGER.debug('Loading the config file "%s"', config_file) config_parser = configparser.ConfigParser() try: if not config_parser.read(config_file): self.error_handler('Could not open config file "%s"' % config_file) return except configparser.DuplicateSectionError as e: # no cover (inconsistent between Python 2 and 3) self.error_handler('Duplicate section "%s" in "%s"' % (e.section, config_file)) return enable_stderr_filtering = True for section in config_parser.sections(): if section == 'general': for key, value in config_parser.items(section): if key == 'imports': if not value: self.error_handler('Empty imports section in config "%s"' % config_file) else: for config_import in [v.strip() for v in value.split(',')]: if not config_import: self.error_handler('Empty import in config "%s"' % config_file) else: config_import_file = self.locator.locate_config_file(config_import, dirname(config_file)) if config_import_file: self.parse_file(config_import_file) else: self.error_handler('Failed to resolve import of "%s" in config "%s"' % (config_import, config_file)) elif key == 'enable-stderr-filtering': try: enable_stderr_filtering = config_parser.getboolean(section, 'enable-stderr-filtering') except ValueError: self.error_handler( 'Invalid value "%s" for key "%s" in config "%s"' % (value, key, config_file)) else: self.error_handler('Invalid key "%s" in general section of config "%s"' % (key, config_file)) elif section == 'filters': for filter_name, pattern_lines in config_parser.items(section): resolved_filter = \ FILTERS_BY_NAME.get(filter_name) or \ FILTERS_BY_LONG_OPTION.get(filter_name) or \ FILTERS_BY_SHORT_OPTION.get(filter_name) if not resolved_filter: self.error_handler('Unknown filter "%s" in config "%s"' % (filter_name, config_file)) continue if not pattern_lines: self.error_handler('Empty pattern for "%s" in config "%s"' % (filter_name, config_file)) continue for pattern in pattern_lines.splitlines(): self.stdout_builder.add_mapping(pattern, resolved_filter) if enable_stderr_filtering: self.stderr_builder.add_mapping(pattern, resolved_filter) else: self.error_handler('Invalid section "%s" in config "%s"' % (section, config_file)) LOGGER.info('Loaded config "%s"', config_file)