A simple config loader for Python using Bevy for dependency injection.
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 SectionModel interface and declare the file name it should look for. Then you can inject that model wherever you
need it using Bevy.
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.get_active_controller().add_path method. It is also possible to add more file handlers using the
nubby.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.models import new_file_model
file_definition = new_file_model("person_info")
@file_definition.section("person")
@dataclass
class Person:
name: str
age: int
@inject
def print_person_details(person: Person = dependency()):
print(f"{person.name} is {person.age} years old")
Running print_person_details() prints the name and age of the person loaded from the person section of 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. Nubby uses Bevy to automatically inject 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 nubby import get_active_controller
# After modifying the person model
person.age = 32
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.
Name Normalization
By default, section names in config files match the class name. You can customize this behavior:
# Use snake_case for section names
file_definition = new_file_model("config", generate_normalized_names=True)
@file_definition.section() # Will use "user_profile" as the section name key in the config file
@dataclass
class UserProfile:
username: str
email: str
Supported File Formats
Nubby supports the following file formats out of the box:
- JSON (
.json) - TOML (
.toml) - requirestomlkitfor writing - YAML (
.yaml,.yml) - requirespyyaml
License
Nubby is made available under the MIT License.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nubby-0.1.2.tar.gz.
File metadata
- Download URL: nubby-0.1.2.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2fd610430b48d9a3f16a4f80401c9fbad3071f820b0de1fe6ec49d3e3aa555bb
|
|
| MD5 |
6d75addc0f70770e3f77a95059e3010a
|
|
| BLAKE2b-256 |
ecbdc4824eeffa72afb0f100328e5de421d6e7c1ae4e49ca0aed301b5c6c9979
|
File details
Details for the file nubby-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nubby-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b7d20e387771559cd6a5202fd476d9336328dbbb5d4872a316fbf85a9031aa3
|
|
| MD5 |
257f21206b17b78a65aa20515a111f34
|
|
| BLAKE2b-256 |
a8ebdb697ae7b96094e3ce46b6361465bd0578805a27f68b9cbd2d91b83915bd
|