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.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

hiraconf-0.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hiraconf-0.1.0.tar.gz
  • Upload date:
  • Size: 13.8 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.0.tar.gz
Algorithm Hash digest
SHA256 fe4923348ecbef841a847a5e559c515d5fb041e156f39cb15565061e11f5ec8f
MD5 9ba2143aeed40f3d3f680fc1fd1c07d0
BLAKE2b-256 1fc80d83978f389e0e1e58ef7a1b2acc6f901b333622bcfe20537856937517f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hiraconf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c1128e57aa674393233cd41647e3d2d372d2daaf53500d245a1868c85098b68
MD5 14cad2e23661009910fd3731f594ae5d
BLAKE2b-256 4b5bdeb69bdbdba5e50c35f0accdd6a0224ad0bfd8d213681b11c2b6dd294845

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