Skip to main content

Easily deserialize Data Classes from ini files

Project description

dataclasses-configobj

Hydrate Data Classes from ini/dat/toml files.

Aims to be dataclasses-json, but for configobj.

This is very alpha right now, but the feaures which work, should work:

Usage

Define the shape of your config:

  • Each subsection will be mapped to a nested class
  • Each subsection may define a single _many
  • A _many must have type List[...]
  • The type of List of a _many shall have a _name

Example:

@dataclass
class Single:
    other: str

@dataclass
class OneOfMany:
    _name: str
    val: str

@dataclass
class Config:
    single: Single
    _many: List[OneOfMany]
    optional: Optional[str] = None

To load this .ini file:

[single]
other = hello
[one]
val = apple
[two]
val = banana

We can read, validate, and lift to an instance of Config with:

spec = core.to_spec(Config)
co = configobj.ConfigObj(infile=infile, configspec=spec)

validator = validate.Validator()
co.validate(validator)

config: Config = dataclasses.lift(Config, co)

To yield config:

Config(
    single=Single(other='hello'),
    optional=None,
    withdefault='test123'
    _many=[
        OneOfMany(_name='one', val='apple'),
        OneOfMany(_name='two', val='banana')
    ]
)

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

dataclasses-configobj-0.0.4.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

dataclasses_configobj-0.0.4-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page