Skip to main content

General purpose asyncio service loader and dependency injector

Project description

a plugin mechanic

I want to have a plugin mechanic similar to Pyramid's aproach. It should provide means to spawn arbitrary tasks to run, where every lifecycle stage should be yielded to the developer control.

You bootstrap like following:

from buvar import plugin, components

plugin.run("some.module.with.plugin.function")
# some.module.with.plugin.function
from buvar import context


# you may omit include in arguments
async def plugin(include):
   await include('.another.plugin')

   # create some long lasting components
   my_component = context.add("some value")

   async def task():
      asyncio.sleep(1)

   async def server():
      await asyncio.Future()

   # you may run simple tasks
   yield task()

   # you may run server tasks
   yield server()

a components and dependency injection solution

I want to have some utility to store some long lasting means of aid to my business problem. I want a non-verbose lookup to those.

from buvar import di

class Bar:
   pass

class Foo:
   def __init__(self, bar: Bar = None):
      self.bar = bar

   @di.register
   async def adapt(cls, baz: str) -> Foo:
      return Foo()

@di.register
async def adapt(bar: Bar) -> Foo
   foo = Foo(bar)
   return foo


async def task():
   foo = await di.nject(Foo, baz="baz")
   assert foo.bar is None

   bar = Bar()
   foo = await di.nject(Foo, bar=bar)
   assert foo.bar is bar

a config source

I want to have a config source, which automatically applies environment variables to the defaults.

config.toml

log_level = "DEBUG"
show_warnings = "yes"

[foobar]
some = "value"
export APP_FOOBAR_SOME=thing
import attr
import toml

from buvar import config

@attr.s(auto_attribs=True)
class GeneralConfig:
    log_level: str = "INFO"
    show_warnings: bool = config.bool_var(False)

@attr.s(auto_attribs=True)
class FoobarConfig:
   some: str

source = config.ConfigSource(toml.load('config.toml'), env_prefix="APP")

general_config = source.load(GeneralConfig)
assert general_config == GeneralConfig(log_level="DEBUG", show_warnings=True)

foobar_config = source.load(FoobarConfig, 'foobar')
assert foobar_config.some == "thing"

a structlog

I want to have a nice and readable structlog in my terminal and a json log in production.

import sys

from buvar import log

log.setup_logging(sys.stdout.isatty(), general_config.log_level)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

buvar-0.10.1.tar.gz (23.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page