pyya 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.
pyya/__init__.py ADDED
@@ -0,0 +1,71 @@
1
+ import keyword
2
+ import logging
3
+ from pathlib import Path
4
+ from typing import Any, Dict, Optional, Union
5
+
6
+ import yaml
7
+ from camel_converter import to_snake
8
+ from munch import Munch, munchify
9
+
10
+
11
+ logging.basicConfig(format='%(asctime)-15s \t%(levelname)-8s \t%(name)-8s \t%(message)s')
12
+ logger = logging.getLogger('paya')
13
+
14
+
15
+ ConfigType = Dict[str, Any]
16
+
17
+
18
+ class PayaError(RuntimeError): ...
19
+
20
+
21
+ def init_config(
22
+ config: Union[str, Path] = 'config.yaml',
23
+ default_config: Union[str, Path] = 'default.config.yaml',
24
+ *,
25
+ convert_keys_to_snake_case: bool = False,
26
+ raise_error_non_identifiers: bool = False,
27
+ ) -> Munch:
28
+ def _merge_configs(_raw_data: ConfigType, _default_raw_data: ConfigType) -> None:
29
+ for section, entry in _default_raw_data.items():
30
+ if section not in _raw_data or _raw_data[section] is None:
31
+ section = _sanitize_section(section)
32
+ _raw_data[section] = entry
33
+ logger.warning(f'section `{section}` with value `{entry}` taken from {default_config}')
34
+ elif isinstance(entry, dict):
35
+ section = _sanitize_section(section)
36
+ _merge_configs(_raw_data[section], entry)
37
+
38
+ def _sanitize_section(section: str) -> str:
39
+ if convert_keys_to_snake_case:
40
+ logger.warning(f'converting section `{section}` to snake case')
41
+ section = to_snake(section)
42
+ if raise_error_non_identifiers and not section.isidentifier():
43
+ err_msg = f'section `{section}` is not a valid identifier, aborting'
44
+ logger.error(err_msg)
45
+ raise PayaError(err_msg)
46
+ if keyword.iskeyword(section):
47
+ logger.warning(f'section `{section}` is a keyword, renaming to `_{section}`')
48
+ section = f'_{section}'
49
+ return section
50
+
51
+ try:
52
+ with open(Path(default_config)) as fstream:
53
+ _default_raw_data: Optional[ConfigType] = yaml.safe_load(fstream)
54
+ if _default_raw_data is None:
55
+ raise FileNotFoundError()
56
+ except FileNotFoundError as e:
57
+ logger.error(e)
58
+ raise PayaError(f'{default_config} file is missing or empty') from None
59
+ try:
60
+ with open(Path(config)) as fstream:
61
+ _raw_data: ConfigType = yaml.safe_load(fstream) or {}
62
+ except FileNotFoundError:
63
+ logger.warning(f'{config} file not found, using {default_config}')
64
+ _raw_data = {}
65
+ _merge_configs(_raw_data, _default_raw_data)
66
+ try:
67
+ return munchify(_raw_data)
68
+ except Exception as e:
69
+ message = f'{default_config} file is corrupted: {repr(e)}'
70
+ logger.error(message)
71
+ raise PayaError(message) from None
@@ -0,0 +1,93 @@
1
+ Metadata-Version: 2.1
2
+ Name: pyya
3
+ Version: 0.1.0
4
+ Summary: Convert YAML configuration files to Python objects
5
+ Author-email: shadowy-pycoder <shadowy-pycoder@example.com>
6
+ Project-URL: Homepage, https://github.com/shadowy-pycoder/pyya
7
+ Project-URL: Repository, https://github.com/shadowy-pycoder/pyya
8
+ Project-URL: Issues, https://github.com/shadowy-pycoder/pyya/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.8
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: MacOS :: MacOS X
18
+ Classifier: Operating System :: Microsoft :: Windows
19
+ Classifier: Operating System :: POSIX :: Linux
20
+ Requires-Python: >=3.8
21
+ Description-Content-Type: text/markdown
22
+ Requires-Dist: camel-converter >=3.1.2
23
+ Requires-Dist: munch >=4.0.0
24
+ Requires-Dist: pyyaml >=6.0.2
25
+ Requires-Dist: types-pyyaml >=6.0.12.20240917
26
+
27
+ # paya - Simple tool that converts YAML configuration files to Python objects
28
+
29
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
30
+
31
+
32
+ ## Installation
33
+
34
+ ```shell
35
+ pip install pyya
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ Create YAML configuration files for your project:
41
+
42
+
43
+ ```yaml
44
+ # default.config.yaml - this file usually goes to version control system
45
+ database:
46
+ host: localhost
47
+ port: 5432
48
+ username: postgres
49
+ password: postgres
50
+ ```
51
+
52
+ ```yaml
53
+ # config.yaml - this file for production usage
54
+ database:
55
+ username: username
56
+ password: password
57
+ ```
58
+
59
+ Import configuration files in your Python code with pyya:
60
+
61
+ ```python
62
+ import json
63
+
64
+ from pyya import init_config
65
+
66
+ config = init_config(
67
+ 'config.yaml', 'default.config.yaml',
68
+ convert_keys_to_snake_case = False,
69
+ raise_error_non_identifiers = False)
70
+ print(json.dumps(config.database))
71
+
72
+ # Output:
73
+ # {"host": "localhost", "port": 5432, "username": "username", "password": "password"}
74
+
75
+ ```
76
+
77
+ As you can see, pyya automatically merges default config file with production config file.
78
+
79
+ Under the hood `pyya` uses [munch](https://pypi.org/project/munch/) library to create attribute-stylish dictionaries.
80
+
81
+ `paya` automatically adds underscore prefix to Python keywords and can be configured to convert `camelCase` or `PascalCase` keys to snake_case.
82
+
83
+ If `raise_error_non_identifiers` is True, `pyya` will raise error if section name is not valid Python identifier.
84
+
85
+ ## Contributing
86
+
87
+ Are you a developer?
88
+
89
+ - Fork the repository
90
+ - Create your feature branch: `git switch -c my-new-feature`
91
+ - Commit your changes: `git commit -am 'Add some feature'`
92
+ - Push to the branch: `git push origin my-new-feature`
93
+ - Submit a pull request
@@ -0,0 +1,5 @@
1
+ pyya/__init__.py,sha256=khXPCVFAGOy4RjKqz45xYaePBZ5WIqKJ7rC5wp6609c,2662
2
+ pyya-0.1.0.dist-info/METADATA,sha256=aNgp1BNEZFDMbIhvt6x0w7djyaNfxGjhzLur8mvzF0g,2848
3
+ pyya-0.1.0.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
4
+ pyya-0.1.0.dist-info/top_level.txt,sha256=81clY-eZ87xIwCdhb-J4id4Cm3jzUOBVt5_AY3JBDvo,5
5
+ pyya-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.3.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ pyya