Ejemplo n.º 1
0
def configure(defaults, metadata, loader):
    """
    Build a fresh configuration.

    :params defaults: a nested dictionary of keys and their default values
    :params metadata: the graph metadata
    :params loader: a configuration loader

    """
    config = Configuration(defaults)
    config.merge(loader(metadata))
    validate(defaults, metadata, config)
    return config
Ejemplo n.º 2
0
def test_merge():
    """
    Configuration support recursive merging

    """
    config = Configuration(nested=dict(
        nested_key="nested_value",
        other_key="initial_value",
    ))
    config.merge(
        key="value",
        nested=dict(other_key="new_value", ),
    )
    assert_that(config.key, is_(equal_to("value")))
    assert_that(config.nested.nested_key, is_(equal_to("nested_value")))
    assert_that(config.nested.other_key, is_(equal_to("new_value")))

    config.merge(dict(key=dict()), )
    assert_that(config.key, is_(equal_to(dict())))
Ejemplo n.º 3
0
def test_dont_merge():
    """
    Configuration support disabling recursive merging

    """
    config = Configuration(nested=dict(
        __merge__=False,
        nested_key="nested_value",
        other_key="initial_value",
    ))
    config.merge(
        key="value",
        nested=dict(other_key="new_value", ),
    )
    assert_that(config.key, is_(equal_to("value")))
    assert_that(
        calling(getattr).with_args(config.nested, "nested_key"),
        raises(AttributeError),
    )
    assert_that(config.nested.other_key, is_(equal_to("new_value")))
Ejemplo n.º 4
0
def test_merge_lists():
    """
    Configuration merges lists but not tuples.

    """
    config = Configuration(
        lst1=[1, 2],
        lst2=[1, 2],
        tpl1=(1, 2),
        tpl2=(1, 2),
    )
    config.merge(
        lst1=[3, 4],
        lst2=(3, 4),
        tpl1=(3, 4),
        tpl2=[3, 4],
    )
    assert_that(config["lst1"], is_(equal_to([1, 2, 3, 4])))
    assert_that(config["lst2"], is_(equal_to((3, 4))))
    assert_that(config["tpl1"], is_(equal_to((3, 4))))
    assert_that(config["tpl2"], is_(equal_to([3, 4])))
Ejemplo n.º 5
0
def merge(configs):
    result = Configuration()
    for config in configs:
        result.merge(config)
    return result