confuk 0.1.0__py3-none-any.whl

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/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .parse import parse_config
confuk/parse.py ADDED
@@ -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,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
+
@@ -0,0 +1,5 @@
1
+ confuk/__init__.py,sha256=pGkdIezdVk6SQ3kcnDtHqeSPNvpP0-Eib3oH-RPuBOE,32
2
+ confuk/parse.py,sha256=lxqLwSFsGnVqGsdvX2MpBy2Wg1K3dYsrCY2e01AEEU8,1666
3
+ confuk-0.1.0.dist-info/METADATA,sha256=knZkR3Hp6k1ty-R6mrolAelezDnczby2BjZy2XFTmWc,1844
4
+ confuk-0.1.0.dist-info/WHEEL,sha256=d2fvjOD7sXsVzChCqf0Ty0JbHKBaLYwDbGQDwQTnJ50,88
5
+ confuk-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.7.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any