Beispiel #1
0
 def config_path(self):
     import os
     cls = self.__class__
     configfile_names = list(
         generate_configfile_names(cls.config_files, cls.config_searchpath))
     config_param = Param(type=click.File("r"))
     config_file = config_param.parse(configfile_names[0])
     return os.path.realpath(config_file.name)
Beispiel #2
0
    def test_generate_configfile_names__missing_variants_are_removed(
            self, isolated_filesystem):
        # -- PRECONDITIONS:
        assert not os.path.exists("hello.ini")
        assert not os.path.exists("hello.cfg")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        actual_config_files = list(
            generate_configfile_names(given_config_files))
        assert actual_config_files == []
Beispiel #3
0
    def config_path(self):
        import os
        cls = self.__class__
        config_param = Param(type=click.File('r'))
        configfile_names = list(
            generate_configfile_names(cls.config_files, cls.config_searchpath))

        if configfile_names:
            for name in configfile_names:
                config_file = config_param.parse(name)
                path = os.path.realpath(config_file.name)
        else:
            path = os.getcwd()
        return path
Beispiel #4
0
    def test_generate_configfile_names__without_searchpath_uses_curdir(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("hello.cfg", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("hello.cfg")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        actual_config_files = list(
            generate_configfile_names(given_config_files))
        expected_config_files = [
            os.path.join(".", "hello.cfg"),
            os.path.join(".", "hello.ini"),
        ]
        assert actual_config_files == expected_config_files
Beispiel #5
0
    def test_generate_configfile_names__when_searchpath_part_isfile(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("BAD_PART", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("BAD_PART")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        config_searchpath = [".", "BAD_PART"]
        actual_config_files = list(
            generate_configfile_names(given_config_files, config_searchpath))
        expected_config_files = [
            os.path.join(".", "hello.ini"),
        ]
        assert actual_config_files == expected_config_files
Beispiel #6
0
    def read_config(cls):
        configfile_names = list(
            generate_configfile_names(cls.config_files, cls.config_searchpath))
        parser = configparser.ConfigParser()
        parser.optionxform = str
        parser.read(configfile_names)

        if not cls.config_sections:
            # -- AUTO-DISCOVER (once): From cls.config_section_schemas
            cls.config_sections = cls.collect_config_sections_from_schemas()

        storage = {}
        for section_name in select_config_sections(parser.sections(),
                                                   cls.config_sections):
            # print("PROCESS-SECTION: %s" % section_name)
            config_section = parser[section_name]
            cls.process_config_section(parser, config_section, storage)
        return storage
Beispiel #7
0
    def test_generate_configfile_names__provides_existing_variants_with_searchpath(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("more/hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("more/hello.cfg", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("more/hello.ini")
        assert os.path.exists("more/hello.cfg")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        config_searchpath = [".", "more"]
        actual_config_files = list(
            generate_configfile_names(given_config_files, config_searchpath))
        expected_config_files = [
            os.path.join("more", "hello.cfg"),
            os.path.join("more", "hello.ini"),
            os.path.join(".", "hello.ini"),
        ]
        assert actual_config_files == expected_config_files
Beispiel #8
0
    def test_generate_configfile_names__provides_reversed_order(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("hello.cfg", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("hello.cfg")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        actual_config_files = list(
            generate_configfile_names(given_config_files))
        expected_config_files = [
            os.path.join(".", "hello.cfg"),
            os.path.join(".", "hello.ini"),
        ]
        expected_config_files2 = [
            os.path.normpath(p) for p in expected_config_files
        ]
        assert actual_config_files == expected_config_files
        assert list(reversed(given_config_files)) == expected_config_files2
Beispiel #9
0
 def test_generate_configfile_names__without_config_files(
         self, isolated_filesystem):
     config_files = list(generate_configfile_names([]))
     assert config_files == []