Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crxwns/hiraconf
Hierarchical config parser
https://github.com/crxwns/hiraconf
config hierarchical toml yaml
Last synced: 3 months ago
JSON representation
Hierarchical config parser
- Host: GitHub
- URL: https://github.com/crxwns/hiraconf
- Owner: crxwns
- License: mit
- Created: 2024-08-15T18:12:06.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-08-23T18:10:43.000Z (4 months ago)
- Last Synced: 2024-09-30T23:01:24.270Z (3 months ago)
- Topics: config, hierarchical, toml, yaml
- Language: Python
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.```python
from pydantic import BaseModelfrom 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 = Noneclass 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.