Exemplo n.º 1
0
def create_object_graph(name,
                        debug=False,
                        testing=False,
                        import_name=None,
                        root_path=None,
                        loader=load_from_environ,
                        registry=_registry):
    """
    Create a new object graph.

    :param name: the name of the microservice
    :param debug: is development debugging enabled?
    :param testing: is unit testing enabled?
    :param loader: the configuration loader to use
    :param registry: the registry to use (defaults to the global)
    """
    metadata = Metadata(
        name=name,
        debug=debug,
        testing=testing,
        import_name=import_name,
        root_path=root_path,
    )

    config = Configuration({
        key: get_defaults(value)
        for key, value in registry.all.items()
    })
    config.merge(loader(metadata))

    return ObjectGraph(
        metadata=metadata,
        config=config,
        registry=registry,
    )
Exemplo n.º 2
0
def create_object_graph(name,
                        debug=False,
                        testing=False,
                        import_name=None,
                        root_path=None,
                        loader=load_from_environ,
                        registry=_registry):
    """
    Create a new object graph.

    :param name: the name of the microservice
    :param debug: is development debugging enabled?
    :param testing: is unit testing enabled?
    :param loader: the configuration loader to use
    :param registry: the registry to use (defaults to the global)
    """
    metadata = Metadata(
        name=name,
        debug=debug,
        testing=testing,
        import_name=import_name,
        root_path=root_path,
    )

    config = Configuration(
        {key: get_defaults(value)
         for key, value in registry.all.items()})
    config.merge(loader(metadata))

    return ObjectGraph(
        metadata=metadata,
        config=config,
        registry=registry,
    )
Exemplo n.º 3
0
def _load_from_file(metadata, load_func):
    """
    Load configuration from a file.

    The file path is derived from an environment variable
    named after the service of the form FOO_SETTINGS.

    """
    config_filename = get_config_filename(metadata)
    if config_filename is None:
        return Configuration()

    with open(config_filename, "r") as file_:
        data = load_func(file_.read())
        return Configuration(data)
Exemplo 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])))
Exemplo n.º 5
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())))
Exemplo n.º 6
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")))
Exemplo n.º 7
0
def test_attribute_access():
    """
    Configuration can be accessed by attribute and key.

    """
    config = Configuration(
        key="value",
        nested=dict(
            nested_key="nested_value",
            other_key=range(10),
        )
    )
    assert_that(config, has_entry("key", "value"))
    assert_that(config, has_property("key", "value"))
    assert_that(config["nested"], is_(instance_of(Configuration)))
    assert_that(config["nested"], has_entry("nested_key", "nested_value"))
    assert_that(config.nested, has_property("nested_key", "nested_value"))
Exemplo n.º 8
0
 def _load_from_dict(metadata):
     return Configuration(dct)