Package: buvar
This is heavily inspired byPyramidand my daily needs to fastly create and
maintain microservice like applications.a plugin mechanicplugin may depend on other pluginsplugins yield tasks to runa context registry serves as a store for application components created by pluginsa dependency injection creates intermediate componentsa config source is mapped to plugin specific configuration and may be fully
overridden by environment varsstructlog boilerplate for json/tty loggingfork the process and share bound socketspytest fixtures to reduce testing boilerplateYou bootstrap like following:frombuvarimportpluginplugin.stage("some.module.with.prepare")# some.module.with.preparefrombuvarimportcontext,pluginclassFoo:...asyncdeftask():asyncio.sleep(1)asyncdefserver():my_component=context.get(Foo)awaitasyncio.Future()# there is also plugin.Teardown and plugin.Cancelasyncdefprepare(load:plugin.Loader):awaitload('.another.plugin')# create some long lasting componentsmy_component=context.add(Foo())# you may run simple tasksyieldtask()# you may run server tasksyieldserver()a components and dependency injection solutionDependency injection relies on registered adapters, which may be a function, a
method, a class, a classmethod or a generic classmthod.Dependencies are looked up in components or may be provided via kwargs.frombuvarimportdiclassBar:passclassFoo:def__init__(self,bar:Bar=None):self.bar=bar@classmethodasyncdefadapt(cls,baz:str)->Foo:returnFoo()asyncdefadapt(bar:Bar)->Foofoo=Foo(bar)returnfooasyncdeftask():foo=awaitdi.nject(Foo,baz="baz")assertfoo.barisNonebar=Bar()foo=awaitdi.nject(Foo,bar=bar)assertfoo.barisbarasyncdefprepare():di.register(Foo.adapt)di.register(adapt)yieldtask()a config sourcebuvar.config.ConfigSourceis just adict, which merges
arbitrary dicts into one. It serves as the single source of truth for
application variability.You can load a section of config values into your customattrsclass instance. ConfigSource will override values by environment variables if present.config.tomllog_level="DEBUG"show_warnings="yes"[foobar]some="value"exportAPP_FOOBAR_SOME=thingimportattrimporttomlfrombuvarimportconfig@attr.s(auto_attribs=True)classGeneralConfig:log_level:str="INFO"show_warnings:bool=config.bool_var(False)@attr.s(auto_attribs=True)classFoobarConfig:some:strsource=config.ConfigSource(toml.load('config.toml'),env_prefix="APP")general_config=source.load(GeneralConfig)assertgeneral_config==GeneralConfig(log_level="DEBUG",show_warnings=True)foobar_config=source.load(FoobarConfig,'foobar')assertfoobar_config.some=="thing"There is a shortcut to the above approach provided bybuvar.config.Config, which requires to be subclassed from it with a
distinctsectionattribute. If one adds abuvar.config.ConfigSourcecomponent, he will receive the mapped config in one call.frombuvarimportconfig,plugin@attr.s(auto_attribs=True)classGeneralConfig(config.Config):log_level:str="INFO"show_warnings:bool=config.bool_var(False)@attr.s(auto_attribs=True)classFoobarConfig(config.Config,section="foobar"):some:strasyncdefprepare(load:plugin.Loader):# this would by typically placed in the main CLI entry pointsource=context.add(config.ConfigSource(toml.load('config.toml'),env_prefix="APP"))# to provide the adapter to di, which could also be done in the main entry pointawaitload(config)foobar_config=awaitdi.nject(FoobarConfig)a structlogJuststructlogboilerplate.importsysfrombuvarimportloglog_config=log.LogConfig(tty=sys.stdout.isatty(),level="DEBUG")log_config.setup()forked process and shared socketsYou may fork your process and bind and share sockets, to leverage available
CPUs e.g. for serving an aiohttp microservice.Signals like INT, TERM, HUP are forwarded to the child processes.importaiohttp.webfrombuvarimportfork,plugin,di,contextfrombuvar_aiohttpimportAioHttpConfigasyncdefhello(request):returnaiohttp.web.Response(body=b"Hello, world")asyncdefprepare_aiohttp(load:plugin.Loader):awaitload("buvar_aiohttp")app=awaitdi.nject(aiohttp.web.Application)app.router.add_route("GET","/",hello)context.add(AioHttpConfig(host="0.0.0.0",port=5678))fork.stage(prepare_aiohttp,forks=0,sockets=["tcp://:5678"])pytestThere are a couple of pytest fixtures provided to get your context into a
reasonable state:buvar_config_sourceAdictwith arbitrary application settings.buvar_contextThe basic context staging operates on.buvar_stageThe actual stage processing all plugins.buvar_loadThe loader to add plugins to the stage.buvar_plugin_contextThe context all plugins share, when they are prepared.Following markers may be applied to a testbuvar_plugins(plugin, ...)Load all plugins into plugin context.importpytestasyncdefprepare():frombuvarimportcontextcontext.add("foobar")@pytest.mark.asyncio@pytest.mark.buvar_plugins("tests.test_testing")asyncdeftest_wrapped_stage_context():frombuvarimportcontext,pluginassertcontext.get(str)=="foobar"assertcontext.get(plugin.Cancel)@pytest.mark.asyncio@pytest.mark.buvar_plugins()asyncdeftest_wrapped_stage_context_load(buvar_load):frombuvarimportcontext,pluginawaitbuvar_load(prepare)assertcontext.get(str)=="foobar"assertcontext.get(plugin.Cancel)