confuk 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- confuk-0.1.0/PKG-INFO +44 -0
- confuk-0.1.0/README.md +26 -0
- confuk-0.1.0/confuk/__init__.py +1 -0
- confuk-0.1.0/confuk/parse.py +52 -0
- confuk-0.1.0/pyproject.toml +21 -0
confuk-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: confuk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Very opinionated configuration loading package for Python projects
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Krzysztof J. Czarnecki
|
|
7
|
+
Author-email: kjczarne@gmail.com
|
|
8
|
+
Requires-Python: >=3.10,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Requires-Dist: easydict (>=1.11,<2.0)
|
|
14
|
+
Requires-Dist: pydantic (>=2.5.3,<3.0.0)
|
|
15
|
+
Requires-Dist: toml (>=0.10.2,<0.11.0)
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# Confuk
|
|
19
|
+
|
|
20
|
+
This is yet another package for managing configuration files in Python projects.
|
|
21
|
+
|
|
22
|
+
At the moment all it does is it exposes a consistent API that lets you provide a path to a TOML configuration file. It parses the config file into a dictionary by default. If a config class is provided when parsing, the class instance will be created using a dictionary of keyword arguments coming from the original TOML file.
|
|
23
|
+
|
|
24
|
+
In human words: I made this package so that I don't have to explicilty load, parse and return a class instance every single time I have something to do with a TOML file:
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from confuk import parse_config
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
from somewhere import ConfigClass
|
|
30
|
+
|
|
31
|
+
cfg_dict = parse_config(Path("some.toml")) # returns a dictionary
|
|
32
|
+
cfg_obj = parse_config(Path("some.toml"), ConfigClass) # returns an instance of `ConfigClass`
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Currently you can build the package using Poetry:
|
|
38
|
+
|
|
39
|
+
1. Clone this repo.
|
|
40
|
+
2. Run `poetry build`.
|
|
41
|
+
3. Grab the installable wheel from the `dist` folder and install it with `pip` or add the package as a local dependency of another project.
|
|
42
|
+
|
|
43
|
+
Once I get some time to take care of it I will add the package to PyPI so that it's installable via a simple `pip install confuk` command.
|
|
44
|
+
|
confuk-0.1.0/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Confuk
|
|
2
|
+
|
|
3
|
+
This is yet another package for managing configuration files in Python projects.
|
|
4
|
+
|
|
5
|
+
At the moment all it does is it exposes a consistent API that lets you provide a path to a TOML configuration file. It parses the config file into a dictionary by default. If a config class is provided when parsing, the class instance will be created using a dictionary of keyword arguments coming from the original TOML file.
|
|
6
|
+
|
|
7
|
+
In human words: I made this package so that I don't have to explicilty load, parse and return a class instance every single time I have something to do with a TOML file:
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from confuk import parse_config
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from somewhere import ConfigClass
|
|
13
|
+
|
|
14
|
+
cfg_dict = parse_config(Path("some.toml")) # returns a dictionary
|
|
15
|
+
cfg_obj = parse_config(Path("some.toml"), ConfigClass) # returns an instance of `ConfigClass`
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Currently you can build the package using Poetry:
|
|
21
|
+
|
|
22
|
+
1. Clone this repo.
|
|
23
|
+
2. Run `poetry build`.
|
|
24
|
+
3. Grab the installable wheel from the `dist` folder and install it with `pip` or add the package as a local dependency of another project.
|
|
25
|
+
|
|
26
|
+
Once I get some time to take care of it I will add the package to PyPI so that it's installable via a simple `pip install confuk` command.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .parse import parse_config
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import toml
|
|
2
|
+
from typing import Type, Any, Literal
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from easydict import EasyDict as edict
|
|
6
|
+
|
|
7
|
+
CfgClass = Type[Any]
|
|
8
|
+
PydanticCfgClass = Type[BaseModel]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _parse_config_dict(toml_file: Path):
|
|
12
|
+
with open(toml_file, 'r') as file:
|
|
13
|
+
config_dict = toml.load(file)
|
|
14
|
+
return config_dict
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _parse_config_kwarg_constructor(toml_file: Path, cfg_class: CfgClass):
|
|
18
|
+
config_dict = _parse_config_dict(toml_file)
|
|
19
|
+
config = cfg_class(**config_dict)
|
|
20
|
+
return config
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _parse_config_pydantic(toml_file: Path, cfg_class: PydanticCfgClass):
|
|
24
|
+
return _parse_config_kwarg_constructor(toml_file, cfg_class)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _parse_config_easydict(toml_file: Path):
|
|
28
|
+
config_dict = _parse_config_dict(toml_file)
|
|
29
|
+
return edict(config_dict)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def parse_config(toml_file: Path, cfg_class: CfgClass | Literal["attr"] | None = None):
|
|
33
|
+
"""Takes a path object to a toml file and returns a config object.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
toml_file (Path): path to the toml file
|
|
37
|
+
cfg_class (CfgClass | Literal["attr"], optional): config loader class. Defaults to None.
|
|
38
|
+
If set to `"attr"`, the config will be loaded as an `easydict` object instead
|
|
39
|
+
of a conventional dictionary.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
An instance of the class used to load the config
|
|
43
|
+
"""
|
|
44
|
+
match cfg_class:
|
|
45
|
+
case None:
|
|
46
|
+
return _parse_config_dict(toml_file)
|
|
47
|
+
case "attr":
|
|
48
|
+
return _parse_config_easydict(toml_file)
|
|
49
|
+
case BaseModel():
|
|
50
|
+
return _parse_config_pydantic(toml_file, cfg_class)
|
|
51
|
+
case _:
|
|
52
|
+
return _parse_config_kwarg_constructor(toml_file, cfg_class)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "confuk"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Very opinionated configuration loading package for Python projects"
|
|
5
|
+
authors = ["Krzysztof J. Czarnecki <kjczarne@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "^3.10"
|
|
11
|
+
toml = "^0.10.2"
|
|
12
|
+
pydantic = "^2.5.3"
|
|
13
|
+
easydict = "^1.11"
|
|
14
|
+
|
|
15
|
+
[tool.poetry.group.dev.dependencies]
|
|
16
|
+
poethepoet = "^0.24.4"
|
|
17
|
+
pylint = "^3.0.3"
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["poetry-core"]
|
|
21
|
+
build-backend = "poetry.core.masonry.api"
|