././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1605112396.0 pyyaml_env_tag-0.1/.gitignore0000644000000000000000000000376300000000000014520 0ustar0000000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover *.py,cover .hypothesis/ .pytest_cache/ cover/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 db.sqlite3-journal # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder .pybuilder/ target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv # For a library or package, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: # .python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies # having no cross-platform support, pipenv may install dependencies that don't work, or not # install all needed dependencies. #Pipfile.lock # PEP 582; used by e.g. github.com/David-OConnor/pyflow __pypackages__/ # Celery stuff celerybeat-schedule celerybeat.pid # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/ # pytype static type analyzer .pytype/ # Cython debug symbols cython_debug/ ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1605146428.0 pyyaml_env_tag-0.1/LICENSE0000644000000000000000000000205600000000000013527 0ustar0000000000000000MIT License Copyright (c) 2020 Waylan Limberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1605146450.0 pyyaml_env_tag-0.1/README.md0000644000000000000000000000622500000000000014003 0ustar0000000000000000# pyyaml_env_tag A custom YAML tag for referencing environment variables in YAML files. ## Installation Install `PyYAML` and the `pyyaml_env_tag` package with pip: ```bash pip install pyyaml pyyaml_env_tag ``` ### Enabling the tag To enable the tag, import and add the `construct_env_tag` constructor to your YAML loader of choice. ```python import yaml from yaml_env_tag import construct_env_tag yaml.Loader.add_constructor('!ENV', construct_env_tag) ``` Then you may use the loader as per usual. For example: ```python yaml.load(data, Loader=yaml.Loader) ``` ## Using the tag Include the tag `!ENV` followed by the name of an environment variable in a YAML file and the value of the environment variable will be used in its place. ```yaml key: !ENV SOME_VARIABLE ``` If `SOME_VARIABLE` is set to `A string!`, then the above YAML would result in the following Python object: ```python {'key': 'A string!'} ``` The content of the variable is parsed using YAML's implicit scalar types, such as string, bool, integer, float, datestamp and null. More complex types are not recognized and simply passed through as a string. For example, if `SOME_VARIABLE` was set to the string `true`, then the above YAML would result in the following: ```python {'key': True} ``` If the variable specified is not set, then a `null` value is assigned as a default. You may define your own default as the last item in a sequence. ```yaml key: !ENV [SOME_VARIABLE, default] ``` In the above example, if `SOME_VARIABLE` is not defined, the string `default` would be used instead, as follows: ```python {'key': 'default'} ``` You may list multiple variables as fallbacks. The first variable which is set is used. In any sequance with more than one item, the last item must always be a default value and will not be resolved as an environment variable. ```yaml key: !ENV [SOME_VARIABLE, FALLBACK, default] ``` As with variable contents, the default is resolved to a Python object of the implied type (string, bool, integer, float, datestamp and null). When `SOME_VARIABLE` is not set, all four of the following items will resolve to the same value (`None`): ```yaml - !ENV SOME_VARIABLE - !ENV [SOME_VARIABLE] - !ENV [SOME_VARIABLE, ~] - !ENV [SOME_VARIABLE, null] ``` ## Related pyyaml_env_tag was inspired by the Ruby package [yaml-env-tag]. An alternate method of referencing environment variables in YAML files is implemented by [pyyaml-tags] and [python_yaml_environment_variables]. Each of those libraries use a template string and replace the template tag with the content of the variable. While this allows a single value to reference multiple variables and to contain additional content, it restricts all values to strings only and does not provide a way to define defaults. [yaml-env-tag]: https://github.com/jirutka/yaml-env-tag [pyyaml-tags]: https://github.com/meiblorn/pyyaml-tags [python_yaml_environment_variables]: https://gist.github.com/mkaranasou/ba83e25c835a8f7629e34dd7ede01931 ## License pyyaml_env_tag is licensed under the [MIT License] as defined in `LICENSE`. [MIT License]: https://opensource.org/licenses/MIT ## Changelog ### Version 0.1 (released 2020-11-11) The initial release. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1605147460.0 pyyaml_env_tag-0.1/pyproject.toml0000644000000000000000000000155600000000000015442 0ustar0000000000000000[build-system] requires = ["flit_core >=2,<4"] build-backend = "flit_core.buildapi" [tool.flit.metadata] dist-name = "pyyaml_env_tag" module = "yaml_env_tag" author = "Waylan Limberg" author-email = "waylan.limberg@icloud.com" home-page = "https://github.com/waylan/pyyaml-env-tag" description-file = "README.md" requires-python = ">=3.6" requires = ["pyyaml"] classifiers = [ 'License :: OSI Approved :: MIT License', 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'Operating System :: OS Independent', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Topic :: Software Development :: Libraries :: Python Modules', 'Topic :: Text Processing :: Markup', ] ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1605148260.0 pyyaml_env_tag-0.1/test_yaml_env_tag.py0000644000000000000000000001704700000000000016606 0ustar0000000000000000import os import yaml import datetime import unittest from unittest import mock from yaml_env_tag import construct_env_tag def mockenv(**kwargs): ''' Decorator to mock os.environ with provided variables. ''' return mock.patch.dict(os.environ, kwargs) class TestYamlEnvTag(unittest.TestCase): def assertYamlLoad(self, data, expected, loader=yaml.Loader): loader.add_constructor('!ENV', construct_env_tag) self.assertEqual(expected, yaml.load(data, Loader=loader)) @mockenv(VAR='foo') def test_scalar(self): self.assertYamlLoad( '!ENV VAR', 'foo' ) def test_scalar_undefined(self): self.assertYamlLoad( '!ENV VAR', None ) @mockenv(VAR='foo') def test_safe_loader(self): self.assertYamlLoad( '!ENV VAR', 'foo', yaml.SafeLoader ) @mockenv(VAR='foo') def test_scalar_in_squence(self): self.assertYamlLoad( '- !ENV VAR', ['foo'] ) @mockenv(VAR='foo') def test_scalar_in_mapping(self): self.assertYamlLoad( 'key: !ENV VAR', {'key': 'foo'} ) @mockenv(VAR='foo') def test_sequence_1(self): self.assertYamlLoad( '!ENV [VAR]', 'foo' ) def test_sequence_1_undefined(self): self.assertYamlLoad( '!ENV [VAR]', None ) @mockenv(VAR='foo') def test_sequence_2(self): self.assertYamlLoad( '!ENV [VAR, default]', 'foo' ) def test_sequence_2_undefined(self): self.assertYamlLoad( '!ENV [VAR, default]', 'default' ) @mockenv(VAR1='foo', VAR2='bar') def test_sequence_3(self): self.assertYamlLoad( '!ENV [VAR1, VAR2, default]', 'foo' ) @mockenv(VAR2='bar') def test_sequence_3_1_undefined(self): self.assertYamlLoad( '!ENV [VAR1, VAR2, default]', 'bar' ) def test_sequence_3_undefined(self): self.assertYamlLoad( '!ENV [VAR1, VAR2, default]', 'default' ) def test_default_type_null(self): self.assertYamlLoad( '!ENV [VAR, null]', None ) def test_default_type_tilde(self): self.assertYamlLoad( '!ENV [VAR, ~]', None ) def test_default_type_bool_false(self): self.assertYamlLoad( '!ENV [VAR, false]', False ) def test_default_type_bool_true(self): self.assertYamlLoad( '!ENV [VAR, true]', True ) def test_default_type_str(self): self.assertYamlLoad( '!ENV [VAR, "a string"]', 'a string' ) def test_default_type_int(self): self.assertYamlLoad( '!ENV [VAR, 42]', 42 ) def test_default_type_float(self): self.assertYamlLoad( '!ENV [VAR, 3.14]', 3.14 ) def test_default_type_date(self): self.assertYamlLoad( '!ENV [VAR, 2020-11-11]', datetime.date(2020, 11, 11) ) def test_default_type_sequence(self): self.assertYamlLoad( '!ENV [VAR, [foo, bar]]', ['foo', 'bar'] ) def test_default_type_mapping(self): self.assertYamlLoad( '!ENV [VAR, foo: bar]', {'foo': 'bar'} ) @mockenv(VAR='null') def test_env_value_type_null(self): self.assertYamlLoad( '!ENV [VAR, default]', None ) @mockenv(VAR='~') def test_env_value_type_tilde(self): self.assertYamlLoad( '!ENV [VAR, default]', None ) @mockenv(VAR='false') def test_env_value_type_bool_false(self): self.assertYamlLoad( '!ENV VAR', False ) @mockenv(VAR='true') def test_env_value_type_bool_true(self): self.assertYamlLoad( '!ENV VAR', True ) @mockenv(VAR='a string') def test_env_value_type_str(self): self.assertYamlLoad( '!ENV VAR', 'a string' ) @mockenv(VAR='42') def test_env_value_type_int(self): self.assertYamlLoad( '!ENV VAR', 42 ) @mockenv(VAR='3.14') def test_env_value_type_float(self): self.assertYamlLoad( '!ENV VAR', 3.14 ) @mockenv(VAR='2020-11-11') def test_env_value_type_date(self): self.assertYamlLoad( '!ENV VAR', datetime.date(2020, 11, 11) ) @mockenv(VAR='[foo, bar]') def test_env_value_type_sequence(self): self.assertYamlLoad( '!ENV VAR', '[foo, bar]' ) @mockenv(VAR='foo: bar') def test_env_value_type_mapping(self): self.assertYamlLoad( '!ENV VAR', 'foo: bar' ) @mockenv(UPPERCASE='foo') def test_env_name_uppercase(self): self.assertYamlLoad( '!ENV UPPERCASE', 'foo' ) @mockenv(lowercase='foo') def test_env_name_lowercase(self): self.assertYamlLoad( '!ENV lowercase', 'foo' ) @mockenv(CamelCase='foo') def test_env_name_CamelCase(self): self.assertYamlLoad( '!ENV CamelCase', 'foo' ) @mockenv(snake_case='foo') def test_env_name_snake_case(self): self.assertYamlLoad( '!ENV snake_case', 'foo' ) # WARNING! The Environment Variable names in the following tests are # probably a bad idea in use. In fact, it may not even be possable to # set them in most OSs. We are testing that they don't get converted # to native Python types, ensuring expected results in edge cases. @mockenv(null='foo') def test_env_name_null(self): self.assertYamlLoad( '!ENV null', 'foo' ) @mockenv(**{'~': 'foo'}) def test_env_name_tilde(self): self.assertYamlLoad( '!ENV ~', 'foo' ) @mockenv(**{'true': 'foo'}) def test_env_name_true(self): self.assertYamlLoad( '!ENV true', 'foo' ) @mockenv(**{'false': 'foo'}) def test_env_name_false(self): self.assertYamlLoad( '!ENV false', 'foo' ) @mockenv(**{'42': 'foo'}) def test_env_name_int(self): self.assertYamlLoad( '!ENV 42', 'foo' ) @mockenv(**{'3.14': 'foo'}) def test_env_name_float(self): self.assertYamlLoad( '!ENV 3.14', 'foo' ) @mockenv(**{'2020-11-11': 'foo'}) def test_env_name_date(self): self.assertYamlLoad( '!ENV 2020-11-11', 'foo' ) def test_env_name_sequance(self): yaml.Loader.add_constructor('!ENV', construct_env_tag) self.assertRaises( yaml.constructor.ConstructorError, yaml.load, '!ENV [[foo]]', Loader=yaml.Loader ) def test_env_name_mapping(self): yaml.Loader.add_constructor('!ENV', construct_env_tag) self.assertRaises( yaml.constructor.ConstructorError, yaml.load, '!ENV {key: value}', Loader=yaml.Loader ) if __name__ == '__main__': unittest.main() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1605143036.0 pyyaml_env_tag-0.1/yaml_env_tag.py0000644000000000000000000000247100000000000015542 0ustar0000000000000000""" A custom YAML tag for referencing environment variables in YAML files. """ __version__ = '0.1' import os import yaml from typing import Any def construct_env_tag(loader: yaml.Loader, node: yaml.Node) -> Any: """Assign value of ENV variable referenced at node.""" default = None if isinstance(node, yaml.nodes.ScalarNode): vars = [loader.construct_scalar(node)] elif isinstance(node, yaml.nodes.SequenceNode): child_nodes = node.value if len(child_nodes) > 1: # default is resolved using YAML's (implicit) types. default = loader.construct_object(child_nodes[-1]) child_nodes = child_nodes[:-1] # Env Vars are resolved as string values, ignoring (implicit) types. vars = [loader.construct_scalar(child) for child in child_nodes] else: raise yaml.constructor.ConstructorError(None, None, f'expected a scalar or sequence node, but found {node.id}', node.start_mark) for var in vars: if var in os.environ: value = os.environ[var] # Resolve value to Python type using YAML's implicit resolvers tag = loader.resolve(yaml.nodes.ScalarNode, value, (True, False)) return loader.construct_object(yaml.nodes.ScalarNode(tag, value)) return default pyyaml_env_tag-0.1/setup.py0000644000000000000000000000104200000000000014226 0ustar0000000000000000#!/usr/bin/env python # setup.py generated by flit for tools that don't yet use PEP 517 from distutils.core import setup install_requires = \ ['pyyaml'] setup(name='pyyaml_env_tag', version='0.1', description='A custom YAML tag for referencing environment variables in YAML files. ', author='Waylan Limberg', author_email='waylan.limberg@icloud.com', url='https://github.com/waylan/pyyaml-env-tag', py_modules=['yaml_env_tag'], install_requires=install_requires, python_requires='>=3.6', ) pyyaml_env_tag-0.1/PKG-INFO0000644000000000000000000000037400000000000013620 0ustar0000000000000000Metadata-Version: 1.1 Name: pyyaml_env_tag Version: 0.1 Summary: A custom YAML tag for referencing environment variables in YAML files. Home-page: https://github.com/waylan/pyyaml-env-tag Author: Waylan Limberg Author-email: waylan.limberg@icloud.com