-
Notifications
You must be signed in to change notification settings - Fork 16
Feat: Add pyaml-env constructor PyamlEnvConstructor #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dougppaz
wants to merge
12
commits into
mkaranasou:main
Choose a base branch
from
dougppaz:feat/add-pyaml-env-constructor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
19d21bf
chore: fix lint with flake8
4d0a53a
chore: lint base_config.py
4ca631b
feat: add PyamlEnvConstructor
03f4857
chore: add default value to default_sep at PyamlEnvConstructor init
6d9297d
chore: remove magic strings
e51815b
chore: update PyamlEnvConstructor doc
447498e
chore: add PyamlEnvConstructor README
bb93699
chore: update README
d275cdd
feat: add add_implicit_resolver param
c931389
feat: add implict resolver
5c96ec8
fix: requirements pyyaml
497cad8
feat: supports pyyaml 6
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [[source]] | ||
| url = "https://pypi.org/simple" | ||
| verify_ssl = true | ||
| name = "pypi" | ||
|
|
||
| [packages] | ||
| pyyaml = "<6.1,>=5" | ||
|
|
||
| [dev-packages] | ||
|
|
||
| [requires] | ||
| python_version = "3.10" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| PyYAML>=5.*, <=6.* | ||
| PyYAML>=5,<6.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from .constructor import PyamlEnvConstructor | ||
| from .parse_config import parse_config | ||
| from .base_config import BaseConfig | ||
|
|
||
| __all__ = ['parse_config', 'BaseConfig'] | ||
| __all__ = ['PyamlEnvConstructor', 'parse_config', 'BaseConfig'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import os | ||
| import re | ||
| import yaml | ||
|
|
||
|
|
||
| class PyamlEnvConstructor: | ||
| """The `env constructor` for PyYAML Loaders | ||
| Call :meth:`add_to_loader_class` or :meth:`yaml.Loader.add_constructor` to | ||
| add it into loader. | ||
| In YAML files, use ``!ENV`` to resolves the environment variables:: | ||
| !ENV ${DB_USER:paws} | ||
| or:: | ||
| !ENV 'http://${DB_BASE_URL:straight_to_production}:${DB_PORT:12345}' | ||
| """ | ||
|
|
||
| DEFAULT_TAG_NAME = '!ENV' | ||
| DEFAULT_ADD_IMPLICIT_RESOLVER = False | ||
| DEFAULT_SEP = ':' | ||
| DEFAULT_VALUE = 'N/A' | ||
| DEFAULT_RAISE_IF_NA = False | ||
|
|
||
| @classmethod | ||
| def add_to_loader_class(cls, | ||
| loader_class=None, | ||
| tag=DEFAULT_TAG_NAME, | ||
| add_implicit_resolver=DEFAULT_ADD_IMPLICIT_RESOLVER, | ||
| **kwargs): | ||
| instance = cls(**kwargs) | ||
| if add_implicit_resolver: | ||
| yaml.add_implicit_resolver(tag, instance.pattern, None, loader_class) | ||
| yaml.add_constructor(tag, instance, loader_class) | ||
| return instance | ||
|
|
||
| @property | ||
| def pattern(self): | ||
| sep_pattern = r'(' + self.sep + '[^}]+)?' if self.sep else '' | ||
| return re.compile(r'.*?\$\{([^}{' + self.sep + r']+)' + sep_pattern + r'\}.*?') | ||
|
|
||
| def __init__(self, sep=DEFAULT_SEP, default_value=DEFAULT_VALUE, raise_if_na=DEFAULT_RAISE_IF_NA): | ||
| self.sep = sep | ||
| self.default_value = default_value | ||
| self.raise_if_na = raise_if_na | ||
|
|
||
| def __call__(self, loader, node): | ||
| """ | ||
| Extracts the environment variable from the yaml node's value | ||
| :param yaml.Loader loader: the yaml loader (as defined above) | ||
| :param node: the current node (key-value) in the yaml | ||
| :return: the parsed string that contains the value of the environment | ||
| variable or the default value if defined for the variable. If no value | ||
| for the variable can be found, then the value is replaced by | ||
| default_value='N/A' | ||
| """ | ||
| value = loader.construct_scalar(node) | ||
| match = self.pattern.findall(value) # to find all env variables in line | ||
| if match: | ||
| full_value = value | ||
| for g in match: | ||
| curr_default_value = self.default_value | ||
| env_var_name = g | ||
| env_var_name_with_default = g | ||
| if self.sep and isinstance(g, tuple) and len(g) > 1: | ||
| env_var_name = g[0] | ||
| env_var_name_with_default = ''.join(g) | ||
| found = False | ||
| for each in g: | ||
| if self.sep in each: | ||
| _, curr_default_value = each.split(self.sep, 1) | ||
| found = True | ||
| break | ||
| if not found and self.raise_if_na: | ||
| raise ValueError( | ||
| f'Could not find default value for {env_var_name}' | ||
| ) | ||
| full_value = full_value.replace( | ||
| f'${{{env_var_name_with_default}}}', | ||
| os.environ.get(env_var_name, curr_default_value) | ||
| ) | ||
| return full_value | ||
| return value | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.