Skip to content

Commit

Permalink
Added flag for merging configs, bumped to v0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowy-pycoder committed Dec 11, 2024
1 parent b487da0 commit 4e7f8d8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ from pyya import init_config

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

```python
# merge default and production configuration files
# setting to `False` disables other flags and makes default config optional
# `False` means "open config file and apply `ymal.safe_load` and `munchify` with no formatting"
merge_configs=True
```
```python
# convert `camelCase` or `PascalCase` keys to `snake_case`
convert_keys_to_snake_case=True
# `pyya` converts `camelCase` or `PascalCase` keys to `snake_case`
```

```python
# add underscore prefix to keys that are Python keywords
add_underscore_prefix_to_keywords=True
# `pyya` adds underscore prefix to keys that are Python keywords
```

```python
# raise error if key name is not valid Python identifier
raise_error_non_identifiers=True
# `pyya` raises error if key name is not valid Python identifier
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyya"
version = "0.1.3"
version = "0.1.4"
authors = [
{ name="shadowy-pycoder", email="shadowy-pycoder@example.com" },
]
Expand Down
17 changes: 10 additions & 7 deletions pyya.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: pyya
Version: 0.1.3
Version: 0.1.4
Summary: Convert YAML configuration files to Python objects
Author-email: shadowy-pycoder <shadowy-pycoder@example.com>
Project-URL: Homepage, https://github.com/shadowy-pycoder/pyya
Expand Down Expand Up @@ -80,6 +80,7 @@ from pyya import init_config

config = init_config(
'config.yaml', 'default.config.yaml',
merge_configs = True,
convert_keys_to_snake_case = False,
add_underscore_prefix_to_keywords = False
raise_error_non_identifiers = False)
Expand All @@ -94,23 +95,25 @@ As you can see, `pyya` automatically merges default config file with production

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.

and can be configured to .

### Flags

```python
# merge default and production configuration files
# setting to `False` disables other flags
merge_configs=True
```
```python
# convert `camelCase` or `PascalCase` keys to `snake_case`
convert_keys_to_snake_case=True
# `pyya` converts `camelCase` or `PascalCase` keys to `snake_case`
```

```python
# add underscore prefix to keys that are Python keywords
add_underscore_prefix_to_keywords=True
# `pyya` adds underscore prefix to keys that are Python keywords
```

```python
# raise error if key name is not valid Python identifier
raise_error_non_identifiers=True
# `pyya` raises error if key name is not valid Python identifier
```

## Contributing
Expand Down
43 changes: 29 additions & 14 deletions pyya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@ def init_config(
config: Union[str, Path] = 'config.yaml',
default_config: Union[str, Path] = 'default.config.yaml',
*,
merge_configs: bool = True,
convert_keys_to_snake_case: bool = False,
add_underscore_prefix_to_keywords: bool = False,
raise_error_non_identifiers: bool = False,
) -> Munch:
"""
Initialize attribute-stylish configuration from YAML file.
Args:
config: path to config file
default_config: path to default config file
merge_configs: merge default config with config (setting to `False` disables other flags)
convert_keys_to_snake_case: convert config keys to snake case
add_underscore_prefix_to_keywords: add underscore prefix to Python keywords
raise_error_non_identifiers: raise error if config key is not a valid identifier
"""

def _merge_configs(_raw_data: ConfigType, _default_raw_data: ConfigType) -> None:
for section, entry in _default_raw_data.items():
if section not in _raw_data or _raw_data[section] is None:
Expand Down Expand Up @@ -58,19 +71,6 @@ def _sanitize_section(section: str) -> str:
section = f'_{section}'
return section

try:
try:
with open(Path(default_config)) as fstream:
_default_raw_data: Optional[ConfigType] = yaml.safe_load(fstream)
except yaml.YAMLError as e:
err_msg = f'{default_config} file is corrupted: {e}'
logger.error(err_msg)
raise PyyaError(err_msg) from None
if _default_raw_data is None:
raise FileNotFoundError()
except FileNotFoundError as e:
logger.error(e)
raise PyyaError(f'{default_config} file is missing or empty') from None
try:
with open(Path(config)) as fstream:
_raw_data: ConfigType = yaml.safe_load(fstream) or {}
Expand All @@ -81,7 +81,22 @@ def _sanitize_section(section: str) -> str:
except FileNotFoundError:
logger.warning(f'{config} file not found, using {default_config}')
_raw_data = {}
_merge_configs(_raw_data, _default_raw_data)

if merge_configs:
try:
try:
with open(Path(default_config)) as fstream:
_default_raw_data: Optional[ConfigType] = yaml.safe_load(fstream)
except yaml.YAMLError as e:
err_msg = f'{default_config} file is corrupted: {e}'
logger.error(err_msg)
raise PyyaError(err_msg) from None
if _default_raw_data is None:
raise FileNotFoundError()
except FileNotFoundError as e:
logger.error(e)
raise PyyaError(f'{default_config} file is missing or empty') from None
_merge_configs(_raw_data, _default_raw_data)
try:
return munchify(_raw_data)
except Exception as e:
Expand Down

0 comments on commit 4e7f8d8

Please sign in to comment.