8000 Added flag for merging configs, bumped to v0.1.4 · shadowy-pycoder/pyya@4e7f8d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e7f8d8

Browse files
Added flag for merging configs, bumped to v0.1.4
1 parent b487da0 commit 4e7f8d8

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ from pyya import init_config
5454

5555
config = init_config(
5656
'config.yaml', 'default.config.yaml',
57+
merge_configs = True,
5758
convert_keys_to_snake_case = False,
5859
add_underscore_prefix_to_keywords = False
5960
raise_error_non_identifiers = False)
@@ -72,18 +73,22 @@ Under the hood `pyya` uses [PyYAML](https://pypi.org/project/PyYAML/) to parse Y
7273
### Flags
7374

7475
```python
76+
# merge default and production configuration files
77+
# setting to `False` disables other flags and makes default config optional
78+
# `False` means "open config file and apply `ymal.safe_load` and `munchify` with no formatting"
79+
merge_configs=True
80+
```
81+
```python
82+
# convert `camelCase` or `PascalCase` keys to `snake_case`
7583
convert_keys_to_snake_case=True
76-
# `pyya` converts `camelCase` or `PascalCase` keys to `snake_case`
7784
```
78-
7985
```python
86+
# add underscore prefix to keys that are Python keywords
8087
add_underscore_prefix_to_keywords=True
81-
# `pyya` adds underscore prefix to keys that are Python keywords
8288
```
83-
8489
```python
90+
# raise error if key name is not valid Python identifier
8591
raise_error_non_identifiers=True
86-
# `pyya` raises error if key name is not valid Python identifier
8792
```
8893

8994
## Contributing

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pyya"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
authors = [
55
{ name="shadowy-pycoder", email="shadowy-pycoder@example.com" },
66
]

pyya.egg-info/PKG-INFO

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 2.1
22
Name: pyya
3-
Version: 0.1.3
3+
Version: 0.1.4
44
Summary: Convert YAML configuration files to Python objects
55
Author-email: shadowy-pycoder <shadowy-pycoder@example.com>
66
Project-URL: Homepage, https://github.com/shadowy-pycoder/pyya
@@ -80,6 +80,7 @@ from pyya import init_config
8080

8181
config = init_config(
8282
'config.yaml', 'default.config.yaml',
83+
merge_configs = True,
8384
convert_keys_to_snake_case = False,
8485
add_underscore_prefix_to_keywords = False
8586
raise_error_non_identifiers = False)
@@ -94,23 +95,25 @@ As you can see, `pyya` automatically merges default config file with production
9495

9596
Under the hood `pyya` uses [PyYAML](https://pypi.org/project/PyYAML/) to parse YAML files and [munch](https://pypi.org/project/munch/) library to create attribute-stylish dictionaries.
9697

97-
and can be configured to .
9898

9999
### Flags
100100

101101
```python
102+
# merge default and production configuration files
103+
# setting to `False` disables other flags
104+
merge_configs=True
105+
```
106+
```python
107+
# convert `camelCase` or `PascalCase` keys to `snake_case`
102108
convert_keys_to_snake_case=True
103-
# `pyya` converts `camelCase` or `PascalCase` keys to `snake_case`
104109
```
105-
106110
```python
111+
# add underscore prefix to keys that are Python keywords
107112
add_underscore_prefix_to_keywords=True
108-
# `pyya` adds underscore prefix to keys that are Python keywords
109113
```
110-
111114
```python
115+
# raise error if key name is not valid Python identifier
112116
raise_error_non_identifiers=True
113-
# `pyya` raises error if key name is not valid Python identifier
114117
```
115118

116119
## Contributing

pyya/__init__.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@ def init_config(
2222
config: Union[str, Path] = 'config.yaml',
2323
default_config: Union[str, Path] = 'default.config.yaml',
2424
*,
25+
merge_configs: bool = True,
2526
convert_keys_to_snake_case: bool = False,
2627
add_underscore_prefix_to_keywords: bool = False,
2728
raise_error_non_identifiers: bool = False,
2829
) -> Munch:
30+
"""
31+
Initialize attribute-stylish configuration from YAML file.
32+
33+
Args:
34+
config: path to config file
35+
default_config: path to default config file
36+
merge_configs: merge default config with config (setting to `False` disables other flags)
37+
convert_keys_to_snake_case: convert config keys to snake case
38+
add_underscore_prefix_to_keywords: add underscore prefix to Python keywords
39+
raise_error_non_identifiers: raise error if config key is not a valid identifier
40+
"""
41+
2942
def _merge_configs(_raw_data: ConfigType, _default_raw_data: ConfigType) -> None:
3043
for section, entry in _default_raw_data.items():
3144
if section not in _raw_data or _raw_data[section] is None:
@@ -58,19 +71,6 @@ def _sanitize_section(section: str) -> str:
5871
section = f'_{section}'
5972
return section
6073

61-
try:
62-
try:
63-
with open(Path(default_config)) as fstream:
64-
_default_raw_data: Optional[ConfigType] = yaml.safe_load(fstream)
65-
except yaml.YAMLError as e:
66-
err_msg = f'{default_config} file is corrupted: {e}'
67-
logger.error(err_msg)
68-
raise PyyaError(err_msg) from None
69-
if _default_raw_data is None:
70-
raise FileNotFoundError()
71-
except FileNotFoundError as e:
72-
logger.error(e)
73-
raise PyyaError(f'{default_config} file is missing or empty') from None
7474
try:
7575
with open(Path(config)) as fstream:
7676
_raw_data: ConfigType = yaml.safe_load(fstream) or {}
@@ -81,7 +81,22 @@ def _sanitize_section(section: str) -> str:
8181
except FileNotFoundError:
8282
logger.warning(f'{config} file not found, using {default_config}')
8383
_raw_data = {}
84-
_merge_configs(_raw_data, _default_raw_data)
84+
85+
if merge_configs:
86+
try:
87+
try:
88+
with open(Path(default_config)) as fstream:
89+
_default_raw_data: Optional[ConfigType] = yaml.safe_load(fstream)
90+
except yaml.YAMLError as e:
91+
err_msg = f'{default_config} file is corrupted: {e}'
92+
logger.error(err_msg)
93+
raise PyyaError(err_msg) from None
94+
if _default_raw_data is None:
95+
raise FileNotFoundError()
96+
except FileNotFoundError as e:
97+
logger.error(e)
98+
raise PyyaError(f'{default_config} file is missing or empty') from None
99+
_merge_configs(_raw_data, _default_raw_data)
85100
try:
86101
return munchify(_raw_data)
87102
except Exception as e:

0 commit comments

Comments
 (0)
0