def load_default_config(metadata): """ Construct application default configuration. There should be very little here. """ config = Configuration( flask=dict( port=5000, ), logging=dict( levels=dict( override=dict( warn=[], ), ), ), swagger_convention=dict( version="v1", ), ) if metadata.testing: config.logging.levels.override.warn.append("alembic.runtime.migration") config.update(sns_topic_arns=dict(default="default")) return config
def load_default_config(metadata): """ Construct application default configuration. There should be very little here. """ config = Configuration( connexion=dict( port={{ cookiecutter.service_port }}, host="0.0.0.0", enable_swagger_ui=True, ), logging=dict( levels=dict( override=dict( warn=[], ), ), ), postgres=dict( password="******", ) ) if metadata.testing: config.logging.levels.override.warn.append("alembic.runtime.migration") return config
def load_default_config(metadata: Metadata) -> Configuration: """ Construct application default configuration. There should be very little here. """ config = Configuration( flask=dict( port=8080, ), logging=dict( levels=dict( override=dict( warn=[], ), ), ), swagger_convention=dict( version="v1", ), # We want our routes to come directly after the root / build_route_path=dict( prefix="", ), ) return config
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
def loader(metadata: Metadata) -> Configuration: primary_config = Configuration(primary_loader(metadata)) secondary_config = secondary_loader(metadata, primary_config) if prefer_secondary: return merge([ primary_config, secondary_config, ]) else: return merge([ secondary_config, primary_config, ])
def load_default_config(metadata: Metadata) -> Configuration: """ Construct application default configuration. There should be very little here. """ config = Configuration( active_bundle="example_bundle", active_evaluation="example_evaluation", ) return config
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"))
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 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")))
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 empty_loader(metadata: Metadata): return Configuration()
def _load_from_dict(metadata): return Configuration(dct)
def merge(configs): result = Configuration() for config in configs: result.merge(config) return result
def secondary_loader(metadata, config): return Configuration({ config.foo: "bazman", })