Skip to main content

Hierarchical config parser

Project description

HiraConf

A hierarchical parser for configurations, allowing to merge or join configurations from multiple sources.

Example

Using Pydantic for extracting the parsed configuration in this example, which is optional. You can also access the data property to directly access the dictionary.

from pydantic import BaseModel

from hiraconf.parser import Parser
from hiraconf.provider import TomlStringProvider, YamlStringProvider


# Create the BaseModels for validation of configuration
class Settings(BaseModel):
    debug: bool
    version: str
    new: int | None = None


class Config(BaseModel):
    settings: Settings


# Define your config as file, env, string, toml, yaml or any provider that implements the Provider ABC

yaml_settings = """settings:
  debug: true
  version: 1.0.0
  """

parsed_yaml = Parser().merge(YamlStringProvider(yaml_string=yaml_settings)).extract(Config)

print(parsed_yaml)
# >>> settings=Settings(debug=False, version='1.0.0', new=None)

# If you have multiple configs and want to merge those, you can. Like overwriting the debug setting.

toml_settings = """[settings]
debug = false
"""

parsed_merged = (
    Parser()
    .merge(YamlStringProvider(yaml_string=yaml_settings))
    .merge(TomlStringProvider(toml_string=toml_settings))
    .extract(Config)
)

print(parsed_merged)
# >>> settings=Settings(debug=False, version='1.0.0', new=None)

# Or you join it and keep the original value if it exists as is

toml_join_settings = """[settings]
debug = false
new = 10
"""

parsed_joined = (
    Parser()
    .merge(YamlStringProvider(yaml_string=yaml_settings))
    .join(TomlStringProvider(toml_string=toml_join_settings))
    .extract(Config)
)

print(parsed_joined)
# >>> settings=Settings(debug=True, version='1.0.0', new=10)

Provider

You can define your own Provider by inheriting from Provider and implementing the abstract load(self) -> dict[str, Any] method.

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

hiraconf-0.1.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

hiraconf-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file hiraconf-0.1.1.tar.gz.

File metadata

  • Download URL: hiraconf-0.1.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for hiraconf-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5f248e3ddc9d6e3ce4a3f81a82dc20e14de42a2cfa71ede30aa65b2f3fa15efb
MD5 30fc14c71c38058c6ac9a28259b23642
BLAKE2b-256 42bb667f3bde6bfd50484e5a169b894e425e9186968946cf09fb782f3df87c85

See more details on using hashes here.

File details

Details for the file hiraconf-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hiraconf-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for hiraconf-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e8f68f8d188d319465b5d0b45ef21682c659c95f7b9bd41c73b8f14d1370e05f
MD5 2fb4016a54bd305a109e5e17befff233
BLAKE2b-256 17cf0c3706753e0fcd783dd613de7da4726d29c3fb69fad35b17e522644d35ce

See more details on using hashes here.

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