config

Utilities for setting up and managing configurations in a project.

PathRef

Inherits from: BaseModel


Methods

process_path

process_path(cls, data: Any) -> Any

PkgConfig

Inherits from: BaseModel


Methods

_post_process

_post_process(cls, obj: 'PkgConfig') -> Any

from_toml

from_toml(cls, toml_path: str) -> 'PkgConfig'

impart

impart(self, target_obj, model_dump: bool)

Imparts the configuration values to the target object.

The target object should have attributes matching the config keys.


class FooConfig(PkgConfig):
    my_path1: PathRef
    my_path2: PathRef
    my_path3: PathRef = '/an/absolute/path/'
    my_path4: PathRef = '~/my/path4'
    my_path5: PathRef = Path('~/my/path5')
    my_path6: PathRef|None = None
    my_path7: PathRef|None
    
foo = FooConfig(
    my_path1 = "~/my/path1",
    my_path2 = PathRef(parent='my_path1', path='subdir/my_path2'),
    my_path7 = "~/my/path7",
)

assert isinstance(foo.my_path1, Path) and foo.my_path1 == Path('~/my/path1').expanduser().resolve()
assert isinstance(foo.my_path2, Path) and foo.my_path2 == foo.my_path1 / 'subdir/my_path2'
assert isinstance(foo.my_path3, Path) and foo.my_path3 == Path('/an/absolute/path/')
assert isinstance(foo.my_path4, Path) and foo.my_path4 == Path('~/my/path4').expanduser().resolve()
assert isinstance(foo.my_path5, Path) and foo.my_path5 == Path('~/my/path5').expanduser().resolve()
assert foo.my_path6 is None
assert isinstance(foo.my_path7, Path) and foo.my_path7 == Path('~/my/path7').expanduser().resolve()
class BarConfig(BaseModel):
    my_var: str

class FooConfig(PkgConfig):
    my_var: str
    bar: BarConfig
    
foo = FooConfig(**{
    'my_var' : 'Hello',
    'bar' : {
        'my_var' : 'world!'
    }
})

from types import SimpleNamespace
obj = SimpleNamespace()
foo.impart(obj)

assert obj.my_var == 'Hello'
assert obj.bar.my_var == 'world!'

foo.impart(obj, model_dump=True)

assert obj.my_var == 'Hello'
assert obj.bar['my_var'] == 'world!'