Skip to main content

Config loader that injects configs using Bevy.

Project description

Nubby

A simple config loader for Python using Bevy for dependency injection. You just need to create a model, declare what file name it should look for, and mark that model as a dependency wherever you need to have it made available.

Installation

pip install nubby

You can optionally install the toml or yaml extras to support those file formats.

pip install nubby[toml]
pip install nubby[yaml]

Out of the box Nubby will use tomllib to read TOML files but the toml extra installs the tomlkit package which allows writing to TOML files.

Usage

Nubby is designed to have the smallest possible API surface area. You just need to create a model type that implements the ConfigModel interface and declare the file name it should look for. Then you can inject that model wherever you need it using Bevy.

The ConfigModel type provides some helper functionality for setting the file name as a class keyword argument. Alternatively you can set the __config_filename__ attribute on the class directly.

The file name shouldn't include the file extension. Nubby's file handlers look for supported file extensions in the search paths. By default, the current working directory is the only search path. You can add more paths using the nubby.controllers.get_active_controller().add_path method. It is also possible to add more file handlers using the nubby.controllers.get_active_controller().add_handler method.

Here's a basic example of loading a model from a file.

from dataclasses import dataclass
from bevy import inject, dependency
from nubby import ConfigModel

@dataclass
class Person(ConfigModel, filename="person_info"):
    name: str
    age: int

    def to_dict(self):
        return {"name": self.name, "age": self.age}

@inject
def print_person_details(person: Person = dependency()):
    print(f"{person.name} is {person.age} years old")

Running print_person_details() will print the name and age of the person loaded from the person_info file. Depending on the available file handlers it could be a json, toml, or yaml file.

Note that it is not necessary to pass anything to the print_person_details function. Bevy automatically injects the Person model and loads the appropriate config file.

To save any modifications to a model just call the active controller's save method, passing it the updated model.

from dataclasses import dataclass
from nubby import ConfigModel
from nubby.controllers import get_active_controller

@dataclass
class Person(ConfigModel, filename="person_info"):
    name: str
    age: int

    def to_dict(self):
        return {"name": self.name, "age": self.age}

person = Person("Bob", 31)
get_active_controller().save(person)

The save method will update the in-memory cache as well as the file on disk. It will not update any models that have already been created.

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

nubby-0.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

nubby-0.0.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file nubby-0.0.2.tar.gz.

File metadata

  • Download URL: nubby-0.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.8.0-1014-azure

File hashes

Hashes for nubby-0.0.2.tar.gz
Algorithm Hash digest
SHA256 26ea1537294a88894d003caf994026c1e89be487d11530c6fb1d252692dd0416
MD5 243ab9d802bd11e986916a1bcdfd9826
BLAKE2b-256 087c8814e3996cdc6b786099a82f3ec42dcc74ca745f0cfec557163375bbe4fa

See more details on using hashes here.

File details

Details for the file nubby-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: nubby-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Linux/6.8.0-1014-azure

File hashes

Hashes for nubby-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c5ab0ae8d888fcedd8c6009224ca8e510854d65197426c90e66e6c4635a22950
MD5 6ce3d639a8afb2727b3888b7c1cee5e6
BLAKE2b-256 97305bda3a6598ae73fcb436e163351f7fdb88e377f77b44d328fa04417842d4

See more details on using hashes here.

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