Skip to main content

Asyncio plugins, components, dependency injection and configs

Project description

This is heavily inspired by Pyramid and my daily needs to fastly create and maintain microservice like applications.

a plugin mechanic

  • plugin may depend on other plugins

  • plugins yield tasks to run

  • a context registry serves as a store for application components created by plugins

  • a dependency injection creates intermediate components

  • a config source is mapped to plugin specific configuration and may be fully overridden by environment vars

  • structlog boilerplate for json/tty logging

  • fork the process and share bound sockets

  • pytest fixtures to reduce testing boilerplate

You bootstrap like following:

from buvar import plugin

plugin.stage("some.module.with.prepare")
# some.module.with.prepare
from buvar import context, plugin

class Foo:
    ...


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


async def server():
    my_component = context.get(Foo)
    await asyncio.Future()


# there is also plugin.Teardown and plugin.Cancel
async def prepare(load: plugin.Loader):
    await load('.another.plugin')

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

    # you may run simple tasks
    yield task()

    # you may run server tasks
    yield server()

a components and dependency injection solution

Dependency 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.

from buvar import di

class Bar:
    pass

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

    @classmethod
    async def adapt(cls, baz: str) -> Foo:
        return Foo()

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

async def prepare():
    di.register(Foo.adapt)
    di.register(adapt)

    yield task()

a config source

buvar.config.ConfigSource is just a dict, 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 custom attrs class instance. ConfigSource will override values by environment variables if present.

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"

There is a shortcut to the above approach provided by buvar.config.Config, which requires to be subclassed from it with a distinct section attribute. If one adds a buvar.config.ConfigSource component, he will receive the mapped config in one call.

from buvar import config, plugin


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


@attr.s(auto_attribs=True)
class FoobarConfig(config.Config, section="foobar"):
    some: str


async def prepare(load: plugin.Loader):
    # this would by typically placed in the main CLI entry point
    source = 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 point
    await load(config)
    foobar_config = await di.nject(FoobarConfig)

a structlog

Just structlog boilerplate.

import sys

from buvar import log

log_config = log.LogConfig(tty=sys.stdout.isatty(), level="DEBUG")
log_config.setup()

forked process and shared sockets

You 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.

import aiohttp.web
from buvar import fork, plugin, di, context
from buvar_aiohttp import AioHttpConfig


async def hello(request):
    return aiohttp.web.Response(body=b"Hello, world")


async def prepare_aiohttp(load: plugin.Loader):
    await load("buvar_aiohttp")

    app = await di.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"])

pytest

There are a couple of pytest fixtures provided to get your context into a reasonable state:

buvar_config_source

A dict with arbitrary application settings.

buvar_context

The basic context staging operates on.

buvar_stage

The actual stage processing all plugins.

buvar_load

The loader to add plugins to the stage.

buvar_plugin_context

The context all plugins share, when they are prepared.

Following markers may be applied to a test

buvar_plugins(plugin, ...)

Load all plugins into plugin context.

import pytest


async def prepare():
    from buvar import context

    context.add("foobar")


@pytest.mark.asyncio
@pytest.mark.buvar_plugins("tests.test_testing")
async def test_wrapped_stage_context():
    from buvar import context, plugin

    assert context.get(str) == "foobar"
    assert context.get(plugin.Cancel)


@pytest.mark.asyncio
@pytest.mark.buvar_plugins()
async def test_wrapped_stage_context_load(buvar_load):
    from buvar import context, plugin

    await buvar_load(prepare)
    assert context.get(str) == "foobar"
    assert context.get(plugin.Cancel)

Project details


Release history Release notifications | RSS feed

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.44.16.tar.gz (33.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

buvar-0.44.16-cp314-cp314t-musllinux_1_2_x86_64.whl (618.1 kB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

buvar-0.44.16-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (623.0 kB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

buvar-0.44.16-cp314-cp314t-macosx_11_0_arm64.whl (120.6 kB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

buvar-0.44.16-cp314-cp314-musllinux_1_2_x86_64.whl (545.0 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

buvar-0.44.16-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (544.3 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

buvar-0.44.16-cp314-cp314-macosx_11_0_arm64.whl (112.9 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

buvar-0.44.16-cp313-cp313-musllinux_1_2_x86_64.whl (549.0 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

buvar-0.44.16-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (549.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

buvar-0.44.16-cp313-cp313-macosx_11_0_arm64.whl (111.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

buvar-0.44.16-cp312-cp312-musllinux_1_2_x86_64.whl (570.3 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

buvar-0.44.16-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (571.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

buvar-0.44.16-cp312-cp312-macosx_11_0_arm64.whl (112.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

buvar-0.44.16-cp311-cp311-musllinux_1_2_x86_64.whl (533.0 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

buvar-0.44.16-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (530.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

buvar-0.44.16-cp311-cp311-macosx_11_0_arm64.whl (112.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: buvar-0.44.16.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for buvar-0.44.16.tar.gz
Algorithm Hash digest
SHA256 5720ba33155e8ed2bafe27a614f9d48ea08a448d451bad56e3a08bbbd1d66e32
MD5 ee9786b20a02f552a60e11fe356bd213
BLAKE2b-256 92d831e6a92c8d5efc285877bf1cbf452da19a0f6b3008e473605ef16579c18a

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16.tar.gz:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b47d3a706ad2a9e13cefddbad5759ef74d51930c12de7e5adb596b175298b38e
MD5 e28952f2c2a06925f37d1fedaf40bd93
BLAKE2b-256 76ff95749757f5a4804e7533ad844258fff07897c8209ad1719022467a5c7aeb

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp314-cp314t-musllinux_1_2_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 2edfa3eddcdd8ff28c09db7a24bb9dda6468128df2e5886c3e5c12916d0377f8
MD5 7189bddc31c907e6c0f77b9cb4c5c9d2
BLAKE2b-256 249d411e598cace72e4887fd5403433cc3dcf38e88733d80a1e553c02cdd406b

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 606e295312488d1a5cea1bd2e2906d84f121a26a83b1285131ae8f380bdcb10b
MD5 42e1d1275ed402694b1635784975dd26
BLAKE2b-256 31ed22cb12a12bb6142034470b57996747b4ea43d8f6d54284516c6cf9521a5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp314-cp314t-macosx_11_0_arm64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6c1b875915b0d6f4aa99d3c0c1c18007d9cfbbca4de53c45f95bc3496a5382b4
MD5 8e3ab31026db7337eb6d91548325bc5f
BLAKE2b-256 dd0d918877a128450563e6f143b4740d34ab660b0e21b69f92b5a512c9e0f8d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp314-cp314-musllinux_1_2_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 3b297032c42039d35428ed1fab728f321b06bd7a2c1bb3efba40a58027cdb110
MD5 477c9211c4760efd00edb4bb26e01b14
BLAKE2b-256 78d6ea01fac2eaa042774294bf9da507c190312726091da3a33d2ec39e01d679

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 73ef2f100e18fe18c7b3761ec44333ee7767692619dcc96eddb018a2f8ad29c9
MD5 19b6489d166a2b3047afe38522932e91
BLAKE2b-256 3d81cb982c835cd464d166cc9c2c336b8cac5efbc70cc42c04e943a4347f5b65

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 657cf69194fe3a731a156e73aa682b76e00e467bb40cf820ab88e7ebc886896b
MD5 de2e042da0da7229a6ae40d1d6f2aca5
BLAKE2b-256 b82fd82e96a03db043bcd5a31a5cb2f6a8e2432365676c04fbfdb0e3458fb2ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp313-cp313-musllinux_1_2_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 d7a28e0333a007a901a24046321af655c82bb5e64ef3eb7b5e3d76905312f68e
MD5 254ffd520ec91ad37ff3f5a4162c41c0
BLAKE2b-256 d12ae588bee56fb92245365b8e056709fc71e38d6656e9f7d9b4b24d3f142dcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dedbd0985bf1a25acff2756e88bdfe842adcbf321ac77b768098ba08fa88a2d8
MD5 1871ef5c12ec0104a918b0aa73a18965
BLAKE2b-256 cc3c0c0057496f77f17b5ef554cdd1933aeeed85108cabfd776e7d38aba67ceb

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3f43828d24e88fd96be2f2b8a884f1cecdf289ea872061b81f06d0c8f66a87be
MD5 0cae7adda04b438b29b39a6acc6c80cf
BLAKE2b-256 fc245ac496e132bde5cc7ef965b576b716f830633713fde2ec6a8f9e53b7016d

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 ae93d5e665e5a7f6679d14fbc12494ed9646b488fff18b659301c075dfe4df9d
MD5 9699b3f2492ce542a172ee557a2079fc
BLAKE2b-256 a504c886389d17faed71dfc691caf49cf4891c80f66092c41cc6f79bdde4c15d

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5100b48d91ddecec5bbad719013c16a2932a1a617ba1e19794d809f9773eed09
MD5 6d9a547fbeabd13b26bd7f5e2de98e46
BLAKE2b-256 0ed9f00a204d116bce429e50e147462aa5de01258335ab3794aa282df4d9e85b

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dab4347e6f27bd9c47d850ad6626cfd7408637ba595de58c8c725fe9302194c0
MD5 6756ce67347c97d1063486e4f2939e39
BLAKE2b-256 0d311aa5d8ff715f7f83dc45461d3feef84ea4b690147657d80bdd67862096b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 ad871c48ef255c9c778e4eef76b75153fea3a963e8f470b23c1e2ce7e33782db
MD5 3c733fdd6932b3415cc3ee6710017ded
BLAKE2b-256 66e14f20e7f2d270f2d2b51cdbf6b3336a2f945a0bb24524775e79375d5a3bfd

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file buvar-0.44.16-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.16-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b3fb67d5dad15044e89cb7f63a8027d8a26c6b51480b80b3b898ef4fb060c41d
MD5 fab5fea061565bcf57e4be15882b4a36
BLAKE2b-256 243b16816546d502c83934edc42ac1af7c64778b6407c1cf45fab8811eea1113

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.16-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: package.yaml on diefans/buvar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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