Skip to content

Commit 2783cd6

Browse files
Updated README.md
1 parent 6316f83 commit 2783cd6

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.

pyya/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def init_config(
2323
default_config: Union[str, Path] = 'default.config.yaml',
2424
*,
2525
convert_keys_to_snake_case: bool = False,
26-
check_for_identifiers: bool = False,
26+
raise_error_non_identifiers: bool = False,
2727
) -> Munch:
2828
def _merge_configs(_raw_data: ConfigType, _default_raw_data: ConfigType) -> None:
2929
for section, entry in _default_raw_data.items():
@@ -39,11 +39,10 @@ def _sanitize_section(section: str) -> str:
3939
if convert_keys_to_snake_case:
4040
logger.warning(f'converting section `{section}` to snake case')
4141
section = to_snake(section)
42-
if check_for_identifiers:
43-
if not section.isidentifier():
44-
err_msg = f'section `{section}` is not a valid identifier, aborting'
45-
logger.error(err_msg)
46-
raise PayaError(err_msg)
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)
4746
if keyword.iskeyword(section):
4847
logger.warning(f'section `{section}` is a keyword, renaming to `_{section}`')
4948
section = f'_{section}'

0 commit comments

Comments
 (0)