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 details)

Uploaded Source

File details

Details for the file buvar-0.10.1.tar.gz.

File metadata

  • Download URL: buvar-0.10.1.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for buvar-0.10.1.tar.gz
Algorithm Hash digest
SHA256 0c600e845e2a5e603db76daec57dfa93ef6facb659364d6a5c5cc6fc676d54dc
MD5 5d31f7dbff8de1c5072698022b163696
BLAKE2b-256 dfb7030d4cb4d005f609689b9d29574a0a3e3368e72313f014b21a5608e085f6

See more details on using hashes here.

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