示例#1
0
    def test_popitem(self) -> None:
        """Configuration cannot use inherited popitem method."""

        one = Namespace({'a': {'x': 1, 'y': 2}, 'b': {'x': 3, 'z': 4}})
        two = Namespace({'b': {'x': 4, 'z': 2}, 'c': {'j': True, 'k': 3.14}})
        cfg = Configuration(one=one, two=two)

        with pytest.raises(NotImplementedError):
            cfg.popitem()
示例#2
0
    def test_duplicates(self) -> None:
        """Configuration can find duplicate leaves in the trees."""

        one = Namespace({'a': {'x': 1, 'y': 2}, 'b': {'x': 3, 'z': 4}})
        two = Namespace({'b': {'x': 4, 'z': 2}, 'c': {'j': True, 'k': 3.14}})
        cfg = Configuration(one=one, two=two)

        assert cfg.duplicates() == {'x': {'one': [('a',), ('b',)], 'two': [('b',)]},
                                    'z': {'one': [('b',)], 'two': [('b',)]}}
示例#3
0
    def test_whereis(self) -> None:
        """Configuration can find paths to leaves in the tree."""

        one = Namespace({'a': {'x': 1, 'y': 2}, 'b': {'x': 3, 'z': 4}})
        two = Namespace({'b': {'x': 4}, 'c': {'j': True, 'k': 3.14}})
        cfg = Configuration(one=one, two=two)

        assert cfg.whereis('x') == {'one': [('a',), ('b',)], 'two': [('b',)]}
        assert cfg.whereis('x', 1) == {'one': [('a',)], 'two': []}
        assert cfg.whereis('x', lambda v: v % 3 == 0) == {'one': [('b',)], 'two': []}
示例#4
0
def get_config() -> Configuration:
    """Load configuration."""
    return Configuration.from_local(env=True,
                                    prefix='STREAMKIT',
                                    default=DEFAULT,
                                    system=CONF_PATH['system'],
                                    user=CONF_PATH['user'],
                                    local=CONF_PATH['local'])
示例#5
0
def get_config() -> Configuration:
    """Load configuration."""
    return Configuration.from_local(env=True,
                                    prefix='REFITT',
                                    default=DEFAULT,
                                    system=PATH.system.config,
                                    user=PATH.user.config,
                                    local=PATH.local.config)
示例#6
0
    def test_from_local(self) -> None:
        """Test Configuration.from_local factory method."""

        # initial local files
        for label, data in CONFIG_SOURCES.items():
            with open(f'{TMPDIR}/{label}.toml', mode='w') as output:
                output.write(data)

        # clean environment of any existing variables with the item
        prefix = 'CMDKIT'
        for var in dict(os.environ):
            if var.startswith(prefix):
                os.environ.pop(var)

        # populate environment with test variables
        for line in CONFIG_ENVIRON.strip().split('\n'):
            field, value = line.strip().split('=')
            os.environ[field] = value

        # build configuration
        default = Namespace.from_toml(StringIO(CONFIG_DEFAULT))
        cfg = Configuration.from_local(default=default, env=True, prefix=prefix,
                                       system=f'{TMPDIR}/system.toml',
                                       user=f'{TMPDIR}/user.toml',
                                       local=f'{TMPDIR}/local.toml')

        # verify namespace isolation
        assert cfg.namespaces['default'] == Namespace.from_toml(StringIO(CONFIG_DEFAULT))
        assert cfg.namespaces['system'] == Namespace.from_toml(StringIO(CONFIG_SYSTEM))
        assert cfg.namespaces['user'] == Namespace.from_toml(StringIO(CONFIG_USER))
        assert cfg.namespaces['local'] == Namespace.from_toml(StringIO(CONFIG_LOCAL))
        assert cfg.namespaces['env'] == Environ(prefix).reduce()

        # verify parameter lineage
        assert cfg['a']['var0'] == 'default_var0' and cfg.which('a', 'var0') == 'default'
        assert cfg['a']['var1'] == 'system_var1' and cfg.which('a', 'var1') == 'system'
        assert cfg['a']['var2'] == 'user_var2' and cfg.which('a', 'var2') == 'user'
        assert cfg['b']['var3'] == 'local_var3' and cfg.which('b', 'var3') == 'local'
        assert cfg['c']['var4'] == 'env_var4' and cfg.which('c', 'var4') == 'env'
        assert cfg['c']['var5'] == 'env_var5' and cfg.which('c', 'var5') == 'env'
示例#7
0
    def test_blending(self) -> None:
        """Test that configuration applied depth-first blending of namespaces."""

        ns_a = Namespace({'a': {'x': 1, 'y': 2}})
        ns_b = Namespace({'b': {'z': 3}})  # new section
        ns_c = Namespace({'a': {'z': 4}})  # new value in existing section
        ns_d = Namespace({'a': {'x': 5}})  # altered value in existing section

        # configuration blends nested namespaces
        cfg = Configuration(A=ns_a, B=ns_b, C=ns_c, D=ns_d)

        # confirm separate namespaces
        assert cfg.namespaces['A'] == ns_a
        assert cfg.namespaces['B'] == ns_b
        assert cfg.namespaces['C'] == ns_c
        assert cfg.namespaces['D'] == ns_d

        # confirm d << c << b << a look up
        assert cfg['a']['x'] == 5
        assert cfg['a']['y'] == 2
        assert cfg['a']['z'] == 4
        assert cfg['b']['z'] == 3
示例#8
0
    def test_init(self) -> None:
        """Test initialization."""

        # test namespaces
        ns_a = Namespace(TEST_DICT)
        ns_b = Namespace.from_toml(StringIO(TEST_TOML))
        ns_c = Namespace.from_yaml(StringIO(TEST_YAML))
        ns_d = Namespace.from_json(StringIO(TEST_JSON))

        # initialize construction results in member namespaces
        cfg = Configuration(A=ns_a, B=ns_b)
        assert dict(cfg) == cfg.namespaces['A'] == ns_a == cfg.namespaces['B'] == ns_b

        # extend the configuration
        cfg.extend(C=ns_c, D=ns_d)
        assert (cfg.namespaces['A'] == ns_a ==
                cfg.namespaces['B'] == ns_b ==
                cfg.namespaces['C'] == ns_c ==
                cfg.namespaces['D'] == ns_d ==
                dict(cfg))

        # keys() and values() are available
        assert list(cfg.keys()) == list(ns_a.keys())
        assert list(cfg.values()) == list(ns_a.values())
示例#9
0
    def test_live_update(self) -> None:
        """Test direct modification of configuration data."""

        cfg = Configuration(a=Namespace(x=1))
        assert repr(cfg) == 'Configuration(a=Namespace({\'x\': 1}))'
        assert dict(cfg) == {'x': 1}
        assert cfg.which('x') == 'a'

        cfg.x = 2
        assert repr(cfg) == 'Configuration(a=Namespace({\'x\': 1}), _=Namespace({\'x\': 2}))'
        assert dict(cfg) == {'x': 2}
        assert cfg.which('x') == '_'

        cfg.update(y=2)
        assert dict(cfg) == {'x': 2, 'y': 2}
        assert repr(cfg) == 'Configuration(a=Namespace({\'x\': 1}), _=Namespace({\'x\': 2, \'y\': 2}))'
        assert cfg.which('y') == '_'

        cfg.update(y=3)
        assert dict(cfg) == {'x': 2, 'y': 3}
        assert repr(cfg) == 'Configuration(a=Namespace({\'x\': 1}), _=Namespace({\'x\': 2, \'y\': 3}))'
        assert cfg.which('y') == '_'