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.15.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.15-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.15-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.15-cp314-cp314t-macosx_11_0_arm64.whl (120.6 kB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

buvar-0.44.15-cp314-cp314-musllinux_1_2_x86_64.whl (545.1 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

buvar-0.44.15-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.15-cp314-cp314-macosx_11_0_arm64.whl (113.0 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

buvar-0.44.15-cp313-cp313-musllinux_1_2_x86_64.whl (549.1 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

buvar-0.44.15-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.15-cp313-cp313-macosx_11_0_arm64.whl (111.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

buvar-0.44.15-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.15-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (571.6 kB view details)

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

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

Uploaded CPython 3.12macOS 11.0+ ARM64

buvar-0.44.15-cp311-cp311-musllinux_1_2_x86_64.whl (533.1 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

buvar-0.44.15-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (530.9 kB view details)

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

buvar-0.44.15-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.15.tar.gz.

File metadata

  • Download URL: buvar-0.44.15.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.15.tar.gz
Algorithm Hash digest
SHA256 74d3fbd95d2f6f37c9839866c0cab3e8ba5538e62411f538b6d282a26bfe5fe3
MD5 69ca3043526ae0fc2c41f8ab0d4a9910
BLAKE2b-256 91decc51ad7381cbd7568f85d33f64a1ebfae70f41af850a5dc58bc7b484dbf0

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15.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.15-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f5c71a13e4a13296bce1d8000e7ae56754fc681c061856330e1d543c6d8bfb1f
MD5 363f94fd22f59de91fde5507905d3913
BLAKE2b-256 09db0b260d0d6c9bfa95478f00d5d1643b11f2e016d5fa42b5fcb87f3f3d26cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-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.15-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 447152865c97d5293fc02d4306188f46ef3653a39c81e3a16754dc82dc2399b8
MD5 19f7ec0066133b42d8e1642ae97ba0b0
BLAKE2b-256 02f3047f431b4d83a06a7932ac9beb836ed25d46a69d0e34ba0d9ccd351d19ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 74bdaad6a73c4db2488147eeb0d9857eae8d1708591a1474395b6be20b9fffe3
MD5 0df5a09843d66a9ea5cc2f648a449590
BLAKE2b-256 6339ab25b15770d7f15e7883fe016fabbd8d08e1786e33760468d82a5fb6f113

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1be8c90607e98f0e2fe84c50ae0b5053554f412904c293b6c574ad4a95567b84
MD5 386175d47e538fa1652f36fb60855530
BLAKE2b-256 a445144a400b9840c77de0e6dd7e49d1d53fb2d516fd93957ee693a4bafbe16f

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-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.15-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 4e6a691892975a17a519004bc5b3a452f608e1cbb89edb1df0f150ef2ff2fb24
MD5 3771ba5db1b86fee9c0b47980cee09bf
BLAKE2b-256 7df37c60d4b897eebd88e20ad93e5710f07139f61d4e9ca21db2ed806c82bd64

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 951e86cac8e09c44fdae141bebc196fd32c2c26c0f6fc4efa34604e6f9a45682
MD5 165d1f71eb720745bbe48a5811b7d1b4
BLAKE2b-256 3c59dc57c54b445c4f4d0c9147fc56c85c196da19957ba39749b8d88103d9bc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 eab140d2fb6ec11a4ece0fa5707e8d4e891ec656644b847a09b77d7567791e55
MD5 08c8800004c5b7a07565acecf40a2827
BLAKE2b-256 9e6a5a616615cc66f655657705031143b96d7869707756dd6bcf1c83eb70d04f

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-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.15-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 88091884a7efca7600598127511aa4dd62b127765a13f78e86dde25ac20de571
MD5 013b6292606e0ab5a77b1c8461f472cf
BLAKE2b-256 45d4fe05e75ced71a863dd122f625316f57622bc1b8a631c6bb87c6e63b07e68

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a98b2cd774a26440ac3442dd54a9483fb8b300ac758e1aa617b21cadc6ff8088
MD5 6502be79f9b685d1c7036178e76741c2
BLAKE2b-256 33a37af80d9225c30418245e2aa46f793bc8ff8e4c2b725f8bb6bb7fae44f413

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 02cee74f01e6c74c534434a875ffaf4acec758dfa04f2ee45945b8fe1820896c
MD5 a2eb1495d743f1b3394492c9ea28c5b6
BLAKE2b-256 89cac295ad0fcfcb9c0360d927af199ee03be6e2a115a17de18978dbaa26ddaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-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.15-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 7cbd1dfcd828c17c3fad3ddfc26642dada23dfb1ba1efd61283d6db4ed2a27e7
MD5 cec1bac48c951f70539070fa946fcd2c
BLAKE2b-256 0c6f9e835f1e1331aea139b3e0120f4da87459c52f4ae132966af61240beef2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5f54254dfa749628f3c9317e09a2a23212e0034bc7da3d9675e074f0e5ec932f
MD5 38d1388e370a4cc16d930af9c4ecff22
BLAKE2b-256 c8946ad3436bc217b56b74cf6857ba881a305bf5755e25fbc1f4740d57d34163

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6b75690bfb880dcc4f994fae2df2383cd5c831ead151f2bc86b0973d45b79f97
MD5 fd673bfd90fec658970e21377566f09a
BLAKE2b-256 7dccb467d644abdccfba1e0cf9167e2e07cb93ce14e625fa45ff2f5f381697af

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-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.15-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 e5f02d14335720861579c074387bff269b26b3be5aeebe41c3fcf3ab9e44ad2f
MD5 0cc932de70b2d413d7f078975ee85ab1
BLAKE2b-256 f912c9dcb42d4a80f4b9f0961c0f7d9324fdd1a4ee3ce582e923fc07539830ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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.15-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for buvar-0.44.15-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8a8696041229b7e21522f1231384b314477aaf5a416b944f06e686efc6f2d568
MD5 9e88e20b133b8fde3f94ced2b63d95f2
BLAKE2b-256 f00d3839ff47d9e7694dae6d06f48201ec1bcfaa4b3631e913c928a5ea6abbf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for buvar-0.44.15-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