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.1.tar.gz (5.2 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.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nubby-0.0.1.tar.gz
  • Upload date:
  • Size: 5.2 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.1.tar.gz
Algorithm Hash digest
SHA256 b5f53967d822b2207bee96910e64dd261eae4240850a05035c94507dd6573c2d
MD5 f8c7dd0610a0b0677f47cb3c77d368a8
BLAKE2b-256 1523e20f852f121b26db874376b3a96c99b56474eb139884d27f70e95835ee7b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nubby-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62a3b0d2e0f91db7e967aed695d3d38d56280c4dbb8bc6fb8813fc0415d6b1b6
MD5 70ba7a2314343dcae9369780112ed6fd
BLAKE2b-256 4fc6ec5288ae2b304b614b8c52ee5fa2f18b315fc17df88ce760da20efd4eedb

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