Esempio n. 1
0
class ConfigLoader(object):
    def __init__(self,
                 stdout_builder,
                 stderr_builder,
                 paths=None,
                 error_handler=lambda error: None):
        self.locator = ConfigLocator(paths)
        self.parser = ConfigParser(stdout_builder, stderr_builder, paths, error_handler)
        self.error_handler = error_handler

    def load_config_by_name(self, config):
        config_file = self.locator.locate_config_file(config)
        if config_file:
            self.parser.parse_file(config_file)
        else:
            self.error_handler('Could not resolve config "%s"' % config)

    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)

    @staticmethod
    def find_config_name_from_command_line(command_line_args):
        for arg in command_line_args:
            if arg[0] != '-':
                basename = os.path.basename(arg)
                if basename not in PRECOMMANDS:
                    return basename
        return
Esempio n. 2
0
 def __init__(self,
              stdout_builder,
              stderr_builder,
              paths=None,
              error_handler=lambda error: None):
     self.locator = ConfigLocator(paths)
     self.stdout_builder = stdout_builder
     self.stderr_builder = stderr_builder
     self.error_handler = error_handler
Esempio n. 3
0
 def __init__(self,
              stdout_builder,
              stderr_builder,
              paths=None,
              error_handler=lambda error: None):
     self.locator = ConfigLocator(paths)
     self.parser = ConfigParser(stdout_builder, stderr_builder, paths, error_handler)
     self.error_handler = error_handler
Esempio n. 4
0
class ConfigLoader(object):
    def __init__(self,
                 stdout_builder,
                 stderr_builder,
                 paths=None,
                 error_handler=lambda error: None):
        self.locator = ConfigLocator(paths)
        self.parser = ConfigParser(stdout_builder, stderr_builder, paths,
                                   error_handler)
        self.error_handler = error_handler

    def load_config_by_name(self, config):
        config_file = self.locator.locate_config_file(config)
        if config_file:
            self.parser.parse_file(config_file)
        else:
            self.error_handler('Could not resolve config "%s"' % config)

    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)

    @staticmethod
    def find_config_name_from_command_line(command_line_args):
        for arg in command_line_args:
            if arg[0] != '-':
                basename = os.path.basename(arg)
                if basename not in PRECOMMANDS:
                    return basename
        return
Esempio n. 5
0
class ConfigParser(object):
    def __init__(self,
                 stdout_builder,
                 stderr_builder,
                 paths=None,
                 error_handler=lambda error: None):
        self.locator = ConfigLocator(paths)
        self.stdout_builder = stdout_builder
        self.stderr_builder = stderr_builder
        self.error_handler = error_handler

    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)
Esempio n. 6
0
def test_locate_config_file_in_directory_filename_without_extension():
    assert ConfigLocator().locate_config_file_in_directory(
        'tests/data/cfg', "config001") == "tests/data/cfg/config001.cfg"
Esempio n. 7
0
def test_locate_config_file_in_directory_filename():
    assert ConfigLocator().locate_config_file_in_directory(
        'tests/data/cfg', "config001.cfg") == "tests/data/cfg/config001.cfg"
Esempio n. 8
0
def test_locate_config_file_filename_without_extension():
    assert ConfigLocator([
        'tests/data/cfg'
    ]).locate_config_file("config001") == "tests/data/cfg/config001.cfg"
Esempio n. 9
0
def test_locate_config_file_filename():
    assert ConfigLocator([
        'tests/data/cfg'
    ]).locate_config_file("config001.cfg") == "tests/data/cfg/config001.cfg"
Esempio n. 10
0
def test_locate_config_file_path():
    assert ConfigLocator().locate_config_file(
        "tests/data/cfg/config001.cfg") == "tests/data/cfg/config001.cfg"
Esempio n. 11
0
def test_locate_config_file_path_without_extension():
    assert ConfigLocator().locate_config_file(
        "tests/data/cfg/config001") == "tests/data/cfg/config001.cfg"
Esempio n. 12
0
def test_locate_inexistent_config_file_none_in_path():
    assert not ConfigLocator([None]).locate_config_file("does_not_exist")
Esempio n. 13
0
def test_locate_config_file_none_in_path():
    assert ConfigLocator([None]).locate_config_file(
        "tests/data/cfg/config001") == "tests/data/cfg/config001.cfg"
Esempio n. 14
0
def test_locate_inexistent_config_file_empty_path():
    assert not ConfigLocator([]).locate_config_file("does_not_exist")
Esempio n. 15
0
class ConfigParser(object):
    def __init__(self,
                 stdout_builder,
                 stderr_builder,
                 paths=None,
                 error_handler=lambda error: None):
        self.locator = ConfigLocator(paths)
        self.stdout_builder = stdout_builder
        self.stderr_builder = stderr_builder
        self.error_handler = error_handler

    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)