pyya 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.
pyya-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 shadowy-pycoder
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
pyya-0.1.0/PKG-INFO ADDED
@@ -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
pyya-0.1.0/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # paya - Simple tool that converts YAML configuration files to Python objects
2
+
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
4
+
5
+
6
+ ## Installation
7
+
8
+ ```shell
9
+ pip install pyya
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ Create YAML configuration files for your project:
15
+
16
+
17
+ ```yaml
18
+ # default.config.yaml - this file usually goes to version control system
19
+ database:
20
+ host: localhost
21
+ port: 5432
22
+ username: postgres
23
+ password: postgres
24
+ ```
25
+
26
+ ```yaml
27
+ # config.yaml - this file for production usage
28
+ database:
29
+ username: username
30
+ password: password
31
+ ```
32
+
33
+ Import configuration files in your Python code with pyya:
34
+
35
+ ```python
36
+ import json
37
+
38
+ from pyya import init_config
39
+
40
+ config = init_config(
41
+ 'config.yaml', 'default.config.yaml',
42
+ convert_keys_to_snake_case = False,
43
+ raise_error_non_identifiers = False)
44
+ print(json.dumps(config.database))
45
+
46
+ # Output:
47
+ # {"host": "localhost", "port": 5432, "username": "username", "password": "password"}
48
+
49
+ ```
50
+
51
+ As you can see, pyya automatically merges default config file with production config file.
52
+
53
+ Under the hood `pyya` uses [munch](https://pypi.org/project/munch/) library to create attribute-stylish dictionaries.
54
+
55
+ `paya` automatically adds underscore prefix to Python keywords and can be configured to convert `camelCase` or `PascalCase` keys to snake_case.
56
+
57
+ If `raise_error_non_identifiers` is True, `pyya` will raise error if section name is not valid Python identifier.
58
+
59
+ ## Contributing
60
+
61
+ Are you a developer?
62
+
63
+ - Fork the repository
64
+ - Create your feature branch: `git switch -c my-new-feature`
65
+ - Commit your changes: `git commit -am 'Add some feature'`
66
+ - Push to the branch: `git push origin my-new-feature`
67
+ - Submit a pull request
@@ -0,0 +1,118 @@
1
+ [project]
2
+ name = "pyya"
3
+ version = "0.1.0"
4
+ authors = [
5
+ { name="shadowy-pycoder", email="shadowy-pycoder@example.com" },
6
+ ]
7
+ description = "Convert YAML configuration files to Python objects"
8
+ readme = "README.md"
9
+ requires-python = ">=3.8"
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "Programming Language :: Python :: 3.8",
13
+ "Programming Language :: Python :: 3.9",
14
+ "Programming Language :: Python :: 3.10",
15
+ "Programming Language :: Python :: 3.11",
16
+ "Programming Language :: Python :: 3.12",
17
+ "Programming Language :: Python :: 3.13",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Operating System :: MacOS :: MacOS X",
20
+ "Operating System :: Microsoft :: Windows",
21
+ "Operating System :: POSIX :: Linux",
22
+ ]
23
+ dependencies = [
24
+ "camel-converter>=3.1.2",
25
+ "munch>=4.0.0",
26
+ "pyyaml>=6.0.2",
27
+ "types-pyyaml>=6.0.12.20240917",
28
+ ]
29
+ [project.urls]
30
+ Homepage = "https://github.com/shadowy-pycoder/pyya"
31
+ Repository = "https://github.com/shadowy-pycoder/pyya"
32
+ Issues = "https://github.com/shadowy-pycoder/pyya/issues"
33
+
34
+ [tool.setuptools]
35
+ license-files = []
36
+
37
+ [tool.mypy]
38
+ python_version = "3.8"
39
+ cache_dir = ".mypy_cache/strict"
40
+ allow_redefinition = true
41
+ strict_optional = false
42
+ show_error_codes = true
43
+ show_column_numbers = true
44
+ warn_no_return = true
45
+ disallow_any_unimported = false
46
+ warn_unused_configs = true
47
+ disallow_any_generics = true
48
+ disallow_subclassing_any = true
49
+ disallow_untyped_calls = false
50
+ disallow_untyped_defs = true
51
+ disallow_incomplete_defs = true
52
+ check_untyped_defs = true
53
+ disallow_untyped_decorators = true
54
+ no_implicit_optional = true
55
+ implicit_reexport = true
56
+ strict_equality = true
57
+ ignore_missing_imports = true
58
+
59
+ [tool.pytest.ini_options]
60
+ pythonpath = "."
61
+ filterwarnings = [
62
+ "ignore::DeprecationWarning",
63
+ "ignore::PendingDeprecationWarning",
64
+ "ignore::ImportWarning",
65
+ "ignore::ResourceWarning"
66
+ ]
67
+ asyncio_mode = "auto"
68
+ env_files = ".test.env"
69
+ env_override_existing_values = true
70
+
71
+ [tool.ruff]
72
+ exclude = [
73
+ ".bzr",
74
+ ".direnv",
75
+ ".eggs",
76
+ ".git",
77
+ ".git-rewrite",
78
+ ".hg",
79
+ ".ipynb_checkpoints",
80
+ ".mypy_cache",
81
+ ".nox",
82
+ ".pants.d",
83
+ ".pyenv",
84
+ ".pytest_cache",
85
+ ".pytype",
86
+ ".ruff_cache",
87
+ ".svn",
88
+ ".tox",
89
+ ".venv",
90
+ ".vscode",
91
+ "__pypackages__",
92
+ "_build",
93
+ "buck-out",
94
+ "build",
95
+ "dist",
96
+ "node_modules",
97
+ "site-packages",
98
+ "venv",
99
+ ]
100
+ line-length = 120
101
+ indent-width = 4
102
+ target-version = "py38"
103
+
104
+ [tool.ruff.format]
105
+ quote-style = "single"
106
+ indent-style = "space"
107
+ skip-magic-trailing-comma = false
108
+ line-ending = "auto"
109
+
110
+ [tool.ruff.lint]
111
+ select = ["E1", "E2", "E4", "E7", "E9", "F", "D3"]
112
+ ignore = ["ANN201", "Q000", "D102", "D103"]
113
+ fixable = ["ALL"]
114
+ unfixable = []
115
+ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
116
+
117
+ [tool.ruff.lint.isort]
118
+ lines-after-imports = 2
@@ -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,9 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ pyya/__init__.py
5
+ pyya.egg-info/PKG-INFO
6
+ pyya.egg-info/SOURCES.txt
7
+ pyya.egg-info/dependency_links.txt
8
+ pyya.egg-info/requires.txt
9
+ pyya.egg-info/top_level.txt
@@ -0,0 +1,4 @@
1
+ camel-converter>=3.1.2
2
+ munch>=4.0.0
3
+ pyyaml>=6.0.2
4
+ types-pyyaml>=6.0.12.20240917
@@ -0,0 +1 @@
1
+ pyya
pyya-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+