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, )
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())))
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, )
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])))
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")))