├── .gitignore ├── .travis.yml ├── AUTHORS.rst ├── CONTRIBUTING.rst ├── HISTORY.rst ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.md ├── configoat ├── __init__.py ├── cli.py ├── configoat.py └── templates │ ├── config.yaml │ ├── main.yaml │ ├── main_nested.yaml │ ├── main_nested_script.yaml │ ├── nested.yaml │ └── script.py ├── docs ├── Makefile ├── authors.rst ├── conf.py ├── contributing.rst ├── history.rst ├── index.rst ├── installation.rst ├── make.bat ├── readme.rst └── usage.rst ├── requirements_dev.txt ├── setup.cfg ├── setup.py ├── tests ├── __init__.py └── test_configoat.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | ### Linux template 2 | *~ 3 | 4 | # temporary files which can be created if a process still has a handle open of a deleted file 5 | .fuse_hidden* 6 | 7 | # KDE directory preferences 8 | .directory 9 | 10 | # Linux trash folder which might appear on any partition or disk 11 | .Trash-* 12 | 13 | # .nfs files are created when an open file is removed but is still being accessed 14 | .nfs* 15 | 16 | ### macOS template 17 | # General 18 | .DS_Store 19 | .AppleDouble 20 | .LSOverride 21 | 22 | # Icon must end with two \r 23 | Icon 24 | 25 | # Thumbnails 26 | ._* 27 | 28 | # Files that might appear in the root of a volume 29 | .DocumentRevisions-V100 30 | .fseventsd 31 | .Spotlight-V100 32 | .TemporaryItems 33 | .Trashes 34 | .VolumeIcon.icns 35 | .com.apple.timemachine.donotpresent 36 | 37 | # Directories potentially created on remote AFP share 38 | .AppleDB 39 | .AppleDesktop 40 | Network Trash Folder 41 | Temporary Items 42 | .apdisk 43 | 44 | ### Python template 45 | # Byte-compiled / optimized / DLL files 46 | __pycache__/ 47 | *.py[cod] 48 | *$py.class 49 | 50 | # C extensions 51 | *.so 52 | 53 | # Distribution / packaging 54 | .Python 55 | build/ 56 | develop-eggs/ 57 | dist/ 58 | downloads/ 59 | eggs/ 60 | .eggs/ 61 | lib/ 62 | lib64/ 63 | parts/ 64 | sdist/ 65 | var/ 66 | wheels/ 67 | share/python-wheels/ 68 | *.egg-info/ 69 | .installed.cfg 70 | *.egg 71 | MANIFEST 72 | 73 | # PyInstaller 74 | # Usually these files are written by a python script from a template 75 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 76 | *.manifest 77 | *.spec 78 | 79 | # Installer logs 80 | pip-log.txt 81 | pip-delete-this-directory.txt 82 | 83 | # Unit test / coverage reports 84 | htmlcov/ 85 | .tox/ 86 | .nox/ 87 | .coverage 88 | .coverage.* 89 | .cache 90 | nosetests.xml 91 | coverage.xml 92 | *.cover 93 | *.py,cover 94 | .hypothesis/ 95 | .pytest_cache/ 96 | cover/ 97 | 98 | # Translations 99 | *.mo 100 | *.pot 101 | 102 | # Django stuff: 103 | *.log 104 | local_settings.py 105 | db.sqlite3 106 | db.sqlite3-journal 107 | 108 | # Flask stuff: 109 | instance/ 110 | .webassets-cache 111 | 112 | # Scrapy stuff: 113 | .scrapy 114 | 115 | # Sphinx documentation 116 | docs/_build/ 117 | 118 | # PyBuilder 119 | .pybuilder/ 120 | target/ 121 | 122 | # Jupyter Notebook 123 | .ipynb_checkpoints 124 | 125 | # IPython 126 | profile_default/ 127 | ipython_config.py 128 | 129 | # pyenv 130 | # For a library or package, you might want to ignore these files since the code is 131 | # intended to run in multiple environments; otherwise, check them in: 132 | # .python-version 133 | 134 | # pipenv 135 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 136 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 137 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 138 | # install all needed dependencies. 139 | #Pipfile.lock 140 | 141 | # poetry 142 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 143 | # This is especially recommended for binary packages to ensure reproducibility, and is more 144 | # commonly ignored for libraries. 145 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 146 | #poetry.lock 147 | 148 | # pdm 149 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 150 | #pdm.lock 151 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 152 | # in version control. 153 | # https://pdm.fming.dev/#use-with-ide 154 | .pdm.toml 155 | 156 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 157 | __pypackages__/ 158 | 159 | # Celery stuff 160 | celerybeat-schedule 161 | celerybeat.pid 162 | 163 | # SageMath parsed files 164 | *.sage.py 165 | 166 | # Environments 167 | .env 168 | .venv 169 | env/ 170 | venv/ 171 | ENV/ 172 | env.bak/ 173 | venv.bak/ 174 | 175 | # Spyder project settings 176 | .spyderproject 177 | .spyproject 178 | 179 | # Rope project settings 180 | .ropeproject 181 | 182 | # mkdocs documentation 183 | /site 184 | 185 | # mypy 186 | .mypy_cache/ 187 | .dmypy.json 188 | dmypy.json 189 | 190 | # Pyre type checker 191 | .pyre/ 192 | 193 | # pytype static type analyzer 194 | .pytype/ 195 | 196 | # Cython debug symbols 197 | cython_debug/ 198 | 199 | # PyCharm 200 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 201 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 202 | # and can be added to the global gitignore or merged into this file. For a more nuclear 203 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 204 | .idea/ 205 | 206 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Config file for automatic testing at travis-ci.com 2 | 3 | language: python 4 | python: 5 | - 3.8 6 | - 3.7 7 | - 3.6 8 | 9 | # Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors 10 | install: pip install -U tox-travis 11 | 12 | # Command to run tests, e.g. python setup.py test 13 | script: tox 14 | 15 | # Assuming you have installed the travis-ci CLI tool, after you 16 | # create the Github repo and add it to Travis, run the 17 | # following command to finish PyPI deployment setup: 18 | # $ travis encrypt --add deploy.password 19 | deploy: 20 | provider: pypi 21 | distributions: sdist bdist_wheel 22 | user: hasanulbanna 23 | password: 24 | secure: PLEASE_REPLACE_ME 25 | on: 26 | tags: true 27 | repo: aag13/configoat 28 | python: 3.8 29 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | Credits 3 | ======= 4 | 5 | Development Lead 6 | ---------------- 7 | 8 | * Hasan-UL-Banna, Asadullah Al Galib 9 | 10 | Contributors 11 | ------------ 12 | 13 | None yet. Why not be the first? 14 | -------------------------------------------------------------------------------- /CONTRIBUTING.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Contributing 5 | ============ 6 | 7 | Contributions are welcome, and they are greatly appreciated! Every little bit 8 | helps, and credit will always be given. 9 | 10 | You can contribute in many ways: 11 | 12 | Types of Contributions 13 | ---------------------- 14 | 15 | Report Bugs 16 | ~~~~~~~~~~~ 17 | 18 | Report bugs at https://github.com/aag13/configoat/issues. 19 | 20 | If you are reporting a bug, please include: 21 | 22 | * Your operating system name and version. 23 | * Any details about your local setup that might be helpful in troubleshooting. 24 | * Detailed steps to reproduce the bug. 25 | 26 | Fix Bugs 27 | ~~~~~~~~ 28 | 29 | Look through the GitHub issues for bugs. Anything tagged with "bug" and "help 30 | wanted" is open to whoever wants to implement it. 31 | 32 | Implement Features 33 | ~~~~~~~~~~~~~~~~~~ 34 | 35 | Look through the GitHub issues for features. Anything tagged with "enhancement" 36 | and "help wanted" is open to whoever wants to implement it. 37 | 38 | Write Documentation 39 | ~~~~~~~~~~~~~~~~~~~ 40 | 41 | confiGOAT could always use more documentation, whether as part of the 42 | official confiGOAT docs, in docstrings, or even on the web in blog posts, 43 | articles, and such. 44 | 45 | Submit Feedback 46 | ~~~~~~~~~~~~~~~ 47 | 48 | The best way to send feedback is to file an issue at https://github.com/aag13/configoat/issues. 49 | 50 | If you are proposing a feature: 51 | 52 | * Explain in detail how it would work. 53 | * Keep the scope as narrow as possible, to make it easier to implement. 54 | * Remember that this is a volunteer-driven project, and that contributions 55 | are welcome :) 56 | 57 | Get Started! 58 | ------------ 59 | 60 | Ready to contribute? Here's how to set up `configoat` for local development. 61 | 62 | 1. Fork the `configoat` repo on GitHub. 63 | 2. Clone your fork locally:: 64 | 65 | $ git clone git@github.com:your_name_here/configoat.git 66 | 67 | 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: 68 | 69 | $ mkvirtualenv configoat 70 | $ cd configoat/ 71 | $ python setup.py develop 72 | 73 | 4. Create a branch for local development:: 74 | 75 | $ git checkout -b name-of-your-bugfix-or-feature 76 | 77 | Now you can make your changes locally. 78 | 79 | 5. When you're done making changes, check that your changes pass flake8 and the 80 | tests, including testing other Python versions with tox:: 81 | 82 | $ flake8 configoat tests 83 | $ python setup.py test or pytest 84 | $ tox 85 | 86 | To get flake8 and tox, just pip install them into your virtualenv. 87 | 88 | 6. Commit your changes and push your branch to GitHub:: 89 | 90 | $ git add . 91 | $ git commit -m "Your detailed description of your changes." 92 | $ git push origin name-of-your-bugfix-or-feature 93 | 94 | 7. Submit a pull request through the GitHub website. 95 | 96 | Pull Request Guidelines 97 | ----------------------- 98 | 99 | Before you submit a pull request, check that it meets these guidelines: 100 | 101 | 1. The pull request should include tests. 102 | 2. If the pull request adds functionality, the docs should be updated. Put 103 | your new functionality into a function with a docstring, and add the 104 | feature to the list in README.rst. 105 | 3. The pull request should work for Python 3.5, 3.6, 3.7 and 3.8, and for PyPy. Check 106 | https://travis-ci.com/aag13/configoat/pull_requests 107 | and make sure that the tests pass for all supported Python versions. 108 | 109 | Tips 110 | ---- 111 | 112 | To run a subset of tests:: 113 | 114 | $ pytest tests.test_configoat 115 | 116 | 117 | Deploying 118 | --------- 119 | 120 | A reminder for the maintainers on how to deploy. 121 | Make sure all your changes are committed (including an entry in HISTORY.rst). 122 | Then run:: 123 | 124 | $ bump2version patch # possible: major / minor / patch 125 | $ git push 126 | $ git push --tags 127 | 128 | Travis will then deploy to PyPI if tests pass. 129 | -------------------------------------------------------------------------------- /HISTORY.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | History 3 | ======= 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023, Hasan-UL-Banna, Asadullah Al Galib 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS.rst 2 | include CONTRIBUTING.rst 3 | include HISTORY.rst 4 | include LICENSE 5 | include README.rst 6 | include configoat/templates/* 7 | 8 | recursive-include tests * 9 | recursive-exclude * __pycache__ 10 | recursive-exclude * *.py[co] 11 | 12 | recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif 13 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean clean-build clean-pyc clean-test coverage dist docs help install lint lint/flake8 2 | .DEFAULT_GOAL := help 3 | 4 | define BROWSER_PYSCRIPT 5 | import os, webbrowser, sys 6 | 7 | from urllib.request import pathname2url 8 | 9 | webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) 10 | endef 11 | export BROWSER_PYSCRIPT 12 | 13 | define PRINT_HELP_PYSCRIPT 14 | import re, sys 15 | 16 | for line in sys.stdin: 17 | match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) 18 | if match: 19 | target, help = match.groups() 20 | print("%-20s %s" % (target, help)) 21 | endef 22 | export PRINT_HELP_PYSCRIPT 23 | 24 | BROWSER := python -c "$$BROWSER_PYSCRIPT" 25 | 26 | help: 27 | @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) 28 | 29 | clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts 30 | 31 | clean-build: ## remove build artifacts 32 | rm -fr build/ 33 | rm -fr dist/ 34 | rm -fr .eggs/ 35 | find . -name '*.egg-info' -exec rm -fr {} + 36 | find . -name '*.egg' -exec rm -f {} + 37 | 38 | clean-pyc: ## remove Python file artifacts 39 | find . -name '*.pyc' -exec rm -f {} + 40 | find . -name '*.pyo' -exec rm -f {} + 41 | find . -name '*~' -exec rm -f {} + 42 | find . -name '__pycache__' -exec rm -fr {} + 43 | 44 | clean-test: ## remove test and coverage artifacts 45 | rm -fr .tox/ 46 | rm -f .coverage 47 | rm -fr htmlcov/ 48 | rm -fr .pytest_cache 49 | 50 | lint/flake8: ## check style with flake8 51 | flake8 configoat tests 52 | 53 | lint: lint/flake8 ## check style 54 | 55 | test: ## run tests quickly with the default Python 56 | pytest 57 | 58 | test-all: ## run tests on every Python version with tox 59 | tox 60 | 61 | coverage: ## check code coverage quickly with the default Python 62 | coverage run --source configoat -m pytest 63 | coverage report -m 64 | coverage html 65 | $(BROWSER) htmlcov/index.html 66 | 67 | docs: ## generate Sphinx HTML documentation, including API docs 68 | rm -f docs/configoat.rst 69 | rm -f docs/modules.rst 70 | sphinx-apidoc -o docs/ configoat 71 | $(MAKE) -C docs clean 72 | $(MAKE) -C docs html 73 | $(BROWSER) docs/_build/html/index.html 74 | 75 | servedocs: docs ## compile the docs watching for changes 76 | watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D . 77 | 78 | release: dist ## package and upload a release 79 | twine upload dist/* 80 | 81 | dist: clean ## builds source and wheel package 82 | python setup.py sdist 83 | python setup.py bdist_wheel 84 | ls -l dist 85 | 86 | install: clean ## install the package to the active Python's site-packages 87 | python setup.py install 88 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

confiGOAT

2 | 3 | confiGOAT is a powerful, flexible, and developer-friendly management tool for all your 4 | environment variables and configurations. 🔥🔥🔥 5 | 6 | ## Features 7 | Here are some of the features that confiGOAT provides: 8 | 9 | - Manage all environment variables or configuration parameters from a single setup. 10 | - Support all development, testing, and production environments. 11 | - Define configurations once, use it everywhere. 12 | - Define configuration parameters using both YAML and Python scripts. 13 | - Cast values before using them. 14 | - Powerful reference mechanism to reuse variables *from any levels at any levels in any direction*. 15 | - Multiple resource types in the YAML to support the vast majority of use cases. 16 | - Support both simple use cases and complex, multi-layered nested configurations. 17 | - Use dynamic modules to access the parameters through import interface in Python. 18 | - Use a single exposed API to interact with the layered configurations. 19 | - Support nested structures to model the configurations as per business needs. 20 | 21 | 🎉🚀🌟 22 | 23 | ## Installation 24 | 25 | confiGOAT can be installed with [pip](https://pip.pypa.io): 26 | 27 | ```bash 28 | pip install configoat 29 | ``` 30 | 31 | Alternatively, you can grab the latest source code from [GitHub](https://github.com/aag13/configoat): 32 | 33 | ```bash 34 | git clone https://github.com/aag13/configoat 35 | cd configoat 36 | pip install . 37 | ``` 38 | 39 | ## How to Use 40 | 41 | ### Initial Setup 42 | confiGOAT provides a user-friendly CLI command to initialize the configuration setup 43 | for your project. 44 | 45 | 1. Go to the root directory of your project and run the following command 46 | in the terminal. 47 | 48 | ```bash 49 | configoat init 50 | ``` 51 | 52 | 2. For the directory name, enter the name of the folder that will contain all 53 | the configuration files and scripts. E.g. *"environments/"* will create a directory 54 | inside the root directory of your project. Default is *"configs/"*. 55 | 3. For the main configuration file, provide a name that confiGOAT will look 56 | for when initializing the setup for your project. (we will see how to do that 57 | later). Default is *"main.yaml"*. 58 | 4. While selecting the type of configurations, you have 3 options. Choose the one that 59 | best suits your specific project needs. 👇 60 | - **Single (Only one YAML file)** : Use this for small projects where all your configurations will fit 61 | in one single YAML file. 62 | - **Nested (Parent-child YAML files)** : Use this for projects where it makes sense to structure the 63 | configuration files in a nested hierarchy. 64 | - **Nested with Scripts (Includes .py scripts)** : Use this for large-scale projects where some 65 | configurations need to be resolved using python scripts on top of a multi-layered hierarchy. 66 | 67 | For example, if you can fit all your configuration variables in a single file, choose option 1. Choose option 68 | 2 if you want to structure all the configuration parameters in separate files based on their types, such as, 69 | security configurations in *security.yaml* file and database configurations in *database.yaml* file. Option 3 70 | is best suited for cases where you need both the nested hierarchy structure and need to resolve values 71 | for some parameters that require executing some code. 72 | 73 | ❄️❄️❄️ 74 | 75 | ### Preparing Configuration Files 76 | Now that you have set up the configuration folder and starting file(s), you need to populate all your 77 | configuration and environment variables in the generated YAML and python script files. 78 | 1. First, open the *main.yaml* (or whatever name you gave during the initial setup) file. 79 | 2. Each YAML file has 2 root properties, namely *doc* and *resources*. 👇 80 | - You can use *doc* to provide basic description on the type of environment or configuration variables 81 | which are defined in this YAML. 82 | - *resources* contains all the configuration variables that are defined in this file. If you want to 83 | create a new variable, you need to define it inside *resources*. 84 | ```yaml 85 | doc: 'This is the main config file where the processing starts from' 86 | resources: 87 | ... 88 | ``` 89 | 3. You can define 4 different types of resources in confiGOAT. 👇 90 | - *normal* : Use this type if the value of the variable will be different for different environments. You can 91 | use different data types for different environments, such as string, integer, float, boolean, list, and dictionary. 92 | ```yaml 93 | var1: 94 | type: 'normal' 95 | value: 96 | dev: 'value of var1 for dev' 97 | stage: 'value of var1 for stage' 98 | uat: 'value of var1 for uat' 99 | production: 'value of var1 for production' 100 | qa: 'value of var1 for qa' 101 | ``` 102 | - *common* : Use this type if the value of the variable will be same across all environments. We support 103 | the following data types - string, integer, float, boolean, list, and dictionary. 104 | ```yaml 105 | var2: 106 | type: 'common' 107 | value: "value of var2" 108 | 109 | var3: 110 | type: 'common' 111 | value: False 112 | 113 | var4: 114 | type: 'common' 115 | value: 100 116 | 117 | var5: 118 | type: 'common' 119 | value: [ "Banana", "Mango", "Apple" ] 120 | 121 | var6: 122 | type: 'common' 123 | value: { 124 | "name": "Raihan The Boss", 125 | "age": 66, 126 | "address": { 127 | "city": "Dhaka", 128 | "country": "Bangladesh", 129 | } 130 | } 131 | ``` 132 | - *nested* : Use this type for a nested YAML file (Available if you chose option 2 or 3 during setup). *path* 133 | is the relative path from the project root folder to that nested YAML file, e.g. *configs/yamls/nested.yaml*. 134 | All the variables which are defined in the specified nested YAML file will be available under the 135 | namespace of the *var7* variable. ✨ 136 | ```yaml 137 | var7: 138 | type: 'nested' 139 | path: 'path/from/project/root/to/nested.yaml' 140 | ``` 141 | - *script* : Use this type for a python script file (Available if you chose option 3 during setup). *path* 142 | is the relative path from the project root folder to that nested script file, e.g. *configs/scripts/script.py*. 143 | Only the variables which are defined in the *variable_list* property will be available from specified 144 | nested script file under the namespace of the *var9* variable. ✨ 145 | ```yaml 146 | var9: 147 | type: 'script' 148 | variable_list: ['a', 'b', 'c', 'd', 'e'] 149 | path: 'path/from/project/root/to/script.py' 150 | ``` 151 | 4. For the *nested* type, confiGOAT will process the specified YAML file **recursively**, so that if 152 | the nested YAML file contains other *nested* variables, it will resolve all those nested YAML files 153 | recursively as well. 👀 154 | 5. If you want to reuse the value from another variable using reference, either in the same file or any file 155 | in the configuration hierarchy, you need to use **$ref(variable_name)** format by replacing the 156 | variable_name with the actual variable in your configuration that you want to refer to. 👇 157 | 6. Let's consider a couple of scenarios to demonstrate how variable referencing works. 🌅 158 | - First, you need to understand the difference between the *source* and *target* variables. Here, 159 | *source* variable is the one whose reference is being used and *target* variable is the one that is 160 | using the reference. 🎁🎁 161 | - ***Target* variable in the main config YAML file** : If the *target* variable is in the main config file, 162 | such as *main.yaml*, then you can accomplish that in two ways depending on where the *source* 163 | variable is. 164 | - **Source variable in the main config file** : If the *source* variable is in the main config 165 | file, then use *$ref(SIBLING)* in the *target variable*. This will get the value from a variable 166 | called *SIBLING* to the *target* variable like this. ✨ 167 | ```yaml 168 | target_var: 169 | type: 'common' 170 | value: "$ref(SIBLING)" 171 | ``` 172 | - **Source variable in a nested file** : If there is a *nested* variable called *nested1* and inside 173 | the YAML of this file, there exists a variable called *varAA*, then the full *dot notation path* to this variable 174 | from the main config file is *nested1.varAA*. So use *$ref(nested1.varAA)* in the *target variable*. ✨ 175 | ```yaml 176 | target_var: 177 | type: 'common' 178 | value: "$ref(nested1.varAA)" 179 | ``` 180 | - ***Target* variable in any other YAML/script file** : If the *target* variable is in any file other than 181 | the main config file, then you can accomplish that in two ways depending on where the *source* variable is. 182 | - **Source variable in the same file** : If the *source* variable is in the same file as the 183 | *target variable*, then use *$ref(SIBLING)* in the *target variable*. This will get the value from a variable 184 | called *SIBLING* to the *target* variable like this. ✨ 185 | ```yaml 186 | target_var: 187 | type: 'common' 188 | value: "$ref(SIBLING)" 189 | ``` 190 | - **Source variable in any other file** : If there is a *nested* variable called *nested1* in the 191 | main config file and inside the YAML of this *nested* variable, there exists a variable called 192 | *varAA*, then the full *dot notation path* to this variable is *@.nested1.varAA*. 193 | So use *$ref(@.nested1.varAA)* in the *target variable*. ✨ 194 | ```yaml 195 | target_var: 196 | type: 'common' 197 | value: "$ref(@.nested1.varAA)" 198 | ``` 199 | - **So, when to use *@* in the reference**: If the *target* variable is in the root config file, then you don't need 200 | to add *@* in the full *dot notation path*. Because you can use the dotted path to follow the nested 201 | variable hierarchy since you are already in the root config file. However, if the *target* variable 202 | is in anywhere other than the root config file, then you need to prepend the *dot notation path* 203 | with *@* to indicate whether to start looking for the *nested* variable from the root file or the 204 | current file. Starting the dotted path with *@* simply means to *start looking for this variable 205 | from the root config file*. 🎂🎂 206 | 207 | NOTE: Referencing a variable is bidirectional and depth agnostic, meaning that any variable can be referenced 208 | **at any depth from any depth in any direction**, as long as no circular dependency is created during the 209 | referencing of another variable. *Circular Dependency* means that definitions of two variables are dependent 210 | on each other and neither variable can be resolved due to this dependency. In case of any circular dependency, 211 | confiGOAT will raise an exception indicating the circular dependency. 212 | 213 | 🌟🌟🌟 214 | 215 | ### Using Configuration Parameters in the Project 216 | 217 | Now that you have prepared the configurations for your project, you need to use them in your project. 218 | 219 | 1. First, you need to initialize and load all the configuration and environment variables. 220 | ```python3 221 | from configoat import conf 222 | conf.initialize(config="configs/main.yaml", env="dev", module="all_config") 223 | ``` 224 | - *config* denotes the path to the main configuration YAML file to start loading the variables from. 225 | - *env* denotes which environment the variables should be loaded for. 226 | - *module* denotes the name of the namespace under which all variables will be made available for the 227 | dynamic module access. 228 | - In practice, you don't want to hardcode the environment value like this, *env="dev"*. This way, you 229 | won't be able to change it dynamically on the different environments your app is running on. We 230 | recommend getting this value from another source that can be resolved during runtime. E.g. if you are 231 | using confiGOAT in a Django app, then using CI/CD or starting script, inject the environment value 232 | as the command line argument during the project run. Then, before initialization, 233 | fetch this value from *os* like below, 234 | ```python3 235 | import os 236 | from configoat import conf 237 | current_env = os.getenv("YOUR_ENVIRONMENT_VARIABLE") 238 | conf.initialize(config="configs/main.yaml", env=current_env, module="all_config") 239 | ``` 240 | 2. To access the configuration variables, you have 2 options. 241 | - *Dot notation* : You can use the *conf* object to access any variable by providing its full 242 | *dot notation path* from the root configuration file. *@* denotes the root of the configuration, 243 | i.e. the main configuration file. You can also pass the *conf* variable around like any other variable in python 244 | and access values like shown below. 245 | ```python3 246 | print(conf("@.VARIABLE_NAME")) 247 | print(conf("@.NESTED.VARIABLE_NAME")) 248 | 249 | print(conf.get("@.VARIABLE_NAME")) 250 | print(conf.get("@.NESTED.VARIABLE_NAME")) 251 | ``` 252 | - *Dynamic module* : Use python's module and import mechanism to access any configuration 253 | variable. In this approach, you import the module that you defined during the initialization step, 254 | e.g. *all_config*. When confiGOAT initialized your configuration variables, it also created dynamic 255 | modules and attributes in those modules following your configuration nested hierarchy. All these 256 | dynamic modules are inserted under the provided namespace, e.g. the *all_config* module name. After 257 | initialization, you can import this module anywhere in your project and access the variables like any other modules 258 | and their attributes. Some examples are given below on how variables can be accessed using dynamic module. 259 | ```python3 260 | # accessing variables from the root module name, i.e. all_config 261 | import all_config 262 | print(all_config.var3) 263 | print(all_config.var2.var4) 264 | 265 | # importing all variables using * from the root module name, i.e. all_config 266 | from all_config import * 267 | print(var3) 268 | print(var2.var4) 269 | 270 | from all_config import var2 as current 271 | print(current.var4) 272 | ``` 273 | 274 | **You can also provide a default value in case the variable is not found and a casting function to transform 275 | the final value before returning. Casting and default value features are available only when 276 | accessing values using *conf()* or *conf.get()***. 277 | ```python3 278 | print(conf("@.VARIABLE_NAME", default=10, cast=int)) 279 | 280 | print(conf.get("@.VARIABLE_NAME", default=10, cast=int)) 281 | ``` 282 | 283 | 284 | ## Issues 285 | 286 | Please let us know if you find any issue by [filing an issue.](https://github.com/aag13/configoat/issues) 287 | 288 | ## Maintainers 289 | 290 | - [@banna](https://github.com/Hasan-Ul-Banna) (Hasan-UL-Banna) 291 | - [@galib](https://github.com/aag13) (Asadullah Al Galib) 292 | 293 | 👋 294 | 295 | 296 | 297 | -------------------------------------------------------------------------------- /configoat/__init__.py: -------------------------------------------------------------------------------- 1 | """Top-level package for confiGOAT.""" 2 | 3 | __author__ = """Hasan-UL-Banna, Asadullah Al Galib""" 4 | __email__ = 'hasanulbanna04056@gmail.com, asadullahgalib13@gmail.com' 5 | __version__ = '0.1.6' 6 | 7 | from .configoat import ConfiGOAT, conf 8 | 9 | __all__ = [ 10 | "ConfiGOAT", "conf" 11 | ] 12 | -------------------------------------------------------------------------------- /configoat/cli.py: -------------------------------------------------------------------------------- 1 | """Console script for configoat.""" 2 | import click 3 | from pathlib import Path 4 | import sys 5 | import shutil 6 | from pkg_resources import resource_filename 7 | 8 | @click.group() 9 | def cli(): 10 | pass 11 | 12 | @cli.command() 13 | def init(): 14 | dir_name = click.prompt('Enter directory name', default='configs', type=str) 15 | file_name = click.prompt('Enter name of the main configuration file', default='main.yaml', type=str) 16 | click.echo("Configuration setup:") 17 | options = [ 18 | "1. Single (Only one YAML file)", 19 | "2. Nested (Parent-child YAML files)", 20 | "3. Nested with Scripts (Includes .py scripts)" 21 | ] 22 | for option in options: 23 | click.echo("\t{}".format(option)) 24 | 25 | choice = click.prompt("Select your setup choice", default=1, type=int) 26 | 27 | if dir_name.endswith("/"): 28 | dir_name = dir_name[:-1] 29 | 30 | if not (file_name.endswith(".yaml") or file_name.endswith(".yml")): 31 | file_name += ".yaml" 32 | 33 | if Path(dir_name).is_dir(): 34 | click.echo("Folder '{}' already exists".format(dir_name)) 35 | raise click.Abort() 36 | 37 | if choice not in [1, 2, 3]: 38 | click.echo("Invalid choice") 39 | raise click.Abort() 40 | 41 | templates_dir = resource_filename(__name__, "templates") 42 | 43 | try: 44 | if choice == 1: 45 | # create directory and copy only the main yaml file 46 | Path(dir_name).mkdir(parents=True) 47 | shutil.copy("{}/main.yaml".format(templates_dir), "{}/{}".format(dir_name, file_name)) 48 | elif choice == 2: 49 | # create directory and copy both parent-child yaml files 50 | nested_yamls_dir = "{}/{}".format(dir_name, "yamls") 51 | 52 | Path(nested_yamls_dir).mkdir(parents=True) 53 | shutil.copy("{}/main_nested.yaml".format(templates_dir), "{}/{}".format(dir_name, file_name)) 54 | shutil.copy("{}/nested.yaml".format(templates_dir), "{}/{}".format(nested_yamls_dir, "nested.yaml")) 55 | elif choice == 3: 56 | # create directory and copy parent-child yaml files and scripts folder 57 | nested_yamls_dir = "{}/{}".format(dir_name, "yamls") 58 | nested_scripts_dir = "{}/{}".format(dir_name, "scripts") 59 | 60 | Path(nested_yamls_dir).mkdir(parents=True) 61 | Path(nested_scripts_dir).mkdir(parents=True, exist_ok=True) 62 | shutil.copy("{}/main_nested_script.yaml".format(templates_dir), "{}/{}".format(dir_name, file_name)) 63 | shutil.copy("{}/nested.yaml".format(templates_dir), "{}/{}".format(nested_yamls_dir, "nested.yaml")) 64 | shutil.copy("{}/script.py".format(templates_dir), "{}/{}".format(nested_scripts_dir, "script.py")) 65 | except: 66 | click.echo("Error during setup operation. Failed to cleanup") 67 | click.echo("Setup completed") 68 | 69 | 70 | if __name__ == "__main__": 71 | sys.exit(cli()) # pragma: no cover 72 | -------------------------------------------------------------------------------- /configoat/configoat.py: -------------------------------------------------------------------------------- 1 | import enum 2 | import os 3 | import types 4 | import sys 5 | import yaml 6 | import copy 7 | import importlib.util 8 | import re 9 | 10 | DEFAULT_ROOT_CONFIG = "main.yaml" 11 | DEFAULT_ROOT_MODULE = "root_config" 12 | META_TYPE_KEY = "__type" 13 | ENV_NAME = "CONFIGOAT_ENVIRONMENT" 14 | # REFERENCE_PREFIX = "$ref" 15 | # REFERENCE_HOME = "@" 16 | 17 | 18 | class ConfiGOAT: 19 | def __init__(self): 20 | self._data_dict = None 21 | self._environment = None 22 | self._root_config = DEFAULT_ROOT_CONFIG 23 | self._root_module = DEFAULT_ROOT_MODULE 24 | 25 | def get(self, key, default=None, cast=None): 26 | copy_data = copy.deepcopy(self._data_dict) 27 | modules = key.split('.') 28 | if modules[0] == '@': 29 | modules = modules[1:] 30 | else: 31 | raise InvalidAccessKeyExeption( 32 | "Invalid Access Key. Access Key must start with '@' to denote root config. Key ({})".format(key)) 33 | 34 | try: 35 | for m in modules: 36 | copy_data = copy_data[m] 37 | 38 | return copy_data.get('value', default) if cast is None else cast(copy_data.get('value', default)) 39 | except KeyError: 40 | return default if cast is None else cast(default) 41 | 42 | def initialize(self, config=None, env=None, module=None): 43 | if not env: 44 | raise ValueError("Must provide an environment") 45 | 46 | self._root_config = config if config is not None else self._root_config 47 | self._root_module = module if module is not None else self._root_module 48 | self._environment = env 49 | os.environ.setdefault(ENV_NAME, self._environment) 50 | self._data_dict = self._build_data_dict(self._root_config, '@') 51 | self._update_reference_value(self._data_dict) 52 | self._initiate_dynamic_module() 53 | 54 | def _update_reference_value(self, current_dict): 55 | for key in {key: value for key, value in current_dict.items() if isinstance(value, dict)}: 56 | if current_dict[key]['__type'] in [ResourceTypeEnum.COMMON.value, ResourceTypeEnum.NORMAL.value, ResourceTypeEnum.DEFAULT.value]: 57 | if current_dict[key]['__ref']: 58 | current_dict[key]['value'] = self._reference_resolver(current_dict[key]['__ref']) 59 | elif current_dict[key]['__type'] in [ResourceTypeEnum.NESTED.value, ResourceTypeEnum.SCRIPT.value]: 60 | self._update_reference_value(current_dict[key]) 61 | 62 | def _build_data_dict(self, config_path, parent_path): 63 | current_dict = {} 64 | with open(config_path, 'r') as file: 65 | try: 66 | data = yaml.safe_load(file) 67 | data = data['resources'] 68 | for key in data: 69 | resource_type = data[key]['type'] 70 | value = data[key].get('value', None) 71 | if resource_type == ResourceTypeEnum.COMMON.value: 72 | current_dict[key] = self._get_unresolved_value(value, resource_type, parent_path) 73 | elif resource_type == ResourceTypeEnum.NORMAL.value: 74 | value = value[self._environment] 75 | current_dict[key] = self._get_unresolved_value(value, resource_type, parent_path) 76 | elif resource_type == ResourceTypeEnum.NESTED.value: 77 | current_dict[key] = self._build_data_dict(data[key]['path'], '{}.{}'.format(parent_path, key)) 78 | current_dict[key][META_TYPE_KEY] = resource_type 79 | elif resource_type == ResourceTypeEnum.SCRIPT.value: 80 | should_flat = data[key].get('flat', False) 81 | parsed_dict = self._script_parser(data[key]['path'], data[key]['variable_list']) 82 | if should_flat: 83 | current_dict.update(parsed_dict) 84 | else: 85 | current_dict[key] = parsed_dict 86 | current_dict[key][META_TYPE_KEY] = resource_type 87 | else: 88 | raise YAMLFormatException("Invalid YAML KeyType. KeyType ({})".format(resource_type)) 89 | except: 90 | raise YAMLFormatException("Invalid YAML format. Key ({})".format(key)) 91 | return current_dict 92 | 93 | def _get_unresolved_value(self, val_or_ref, resource_type, parent_path=None): 94 | val_dict = { 95 | '__type': resource_type, 96 | '__ref': None, 97 | 'value': val_or_ref, 98 | } 99 | if isinstance(val_or_ref, str) and '$ref' in val_or_ref: 100 | val_dict.update({ 101 | '__ref': self._get_absolute_ref(val_or_ref, parent_path), 102 | 'value': None 103 | }) 104 | 105 | return val_dict 106 | 107 | def _get_absolute_ref(self, value, parent_path): 108 | for reference in set(self._extract_references(value)): 109 | if not reference.startswith('@.'): 110 | old = '$ref({})'.format(reference) 111 | new = '$ref({}.{})'.format(parent_path, reference) 112 | value = value.replace(old, new) 113 | return value 114 | 115 | def _extract_references(self, reference_string): 116 | pattern = r'\$ref\((.*?)\)' 117 | matches = re.findall(pattern, reference_string) 118 | return matches 119 | 120 | def _script_parser(self, script_path, variable_list): 121 | current_dict = {} 122 | spec = importlib.util.spec_from_file_location('module', script_path) 123 | module = importlib.util.module_from_spec(spec) 124 | spec.loader.exec_module(module) 125 | for var in variable_list: 126 | value = getattr(module, var) 127 | current_dict[var] = self._get_unresolved_value(value, ResourceTypeEnum.DEFAULT.value) 128 | return current_dict 129 | 130 | def _get_reference_value(self, reference, references_seen): 131 | copy_data = copy.deepcopy(self._data_dict) 132 | modules = reference.split('.') 133 | for m in modules: 134 | copy_data = copy_data[m] 135 | if copy_data['__ref'] is not None: 136 | copy_data['value'] = self._reference_resolver(copy_data['__ref'], references_seen) 137 | 138 | return copy_data['value'] 139 | 140 | def _reference_resolver(self, reference_string, references_seen=None): 141 | references_seen = {} if references_seen is None else references_seen 142 | references = set(self._extract_references(reference_string)) 143 | for reference in references: 144 | if reference.startswith('@.'): 145 | reference = reference[2::] 146 | else: 147 | raise YAMLFormatException("Invalid YAML format. Key ({})".format(reference)) 148 | if reference not in list(references_seen.keys()): 149 | references_seen[reference] = 'ref' 150 | references_seen[reference] = self._get_reference_value(reference, references_seen) 151 | elif references_seen.get(reference) == 'ref': 152 | raise CircularImportExeption("Circular reference detected. Reference ({})".format(reference)) 153 | return self._build_reference_value(reference_string, references_seen) 154 | 155 | def _build_reference_value(self, reference_string, references_seen): 156 | pattern = r'\$ref\(@\.(.*?)\)' 157 | matches = re.finditer(pattern, reference_string) 158 | 159 | for match in matches: 160 | reference_key = match.group(1) 161 | if reference_key in references_seen: 162 | replacement_value = references_seen[reference_key] 163 | reference_string = reference_string.replace(match.group(0), str(replacement_value)) 164 | 165 | return reference_string 166 | 167 | def _create_module(self, mod_dict, mod_name, parent=""): 168 | current_module = types.ModuleType(mod_name) 169 | mod_with_parent = mod_name if parent == "" else "{}.{}".format(parent, mod_name) 170 | 171 | for key, val in mod_dict.items(): 172 | if not isinstance(val, dict): 173 | continue 174 | 175 | if isinstance(val, dict) and val.get(META_TYPE_KEY) in [ResourceTypeEnum.NESTED.value, ResourceTypeEnum.SCRIPT.value]: 176 | # this is a dict for nested/script module, handle recursively 177 | val_or_mod = self._create_module(val, key, mod_with_parent) 178 | else: 179 | val_or_mod = val.get('value') 180 | 181 | setattr(current_module, key, val_or_mod) 182 | 183 | sys.modules[mod_with_parent] = current_module 184 | return current_module 185 | 186 | def _initiate_dynamic_module(self): 187 | root_module = self._create_module(self._data_dict, self._root_module) 188 | 189 | def _validate_reference(self): 190 | print(self._data_dict) 191 | 192 | def __call__(self, key, default=None, cast=None): 193 | return self.get(key=key, default=default, cast=cast) 194 | 195 | 196 | conf = ConfiGOAT() 197 | 198 | 199 | class YAMLFormatException(Exception): 200 | pass 201 | 202 | class CircularImportExeption(Exception): 203 | pass 204 | 205 | class InvalidAccessKeyExeption(Exception): 206 | pass 207 | 208 | class ModuleAccessException(Exception): 209 | pass 210 | 211 | class ResourceTypeEnum(enum.Enum): 212 | COMMON = "common" 213 | NORMAL = "normal" 214 | NESTED = "nested" 215 | SCRIPT = "script" 216 | DEFAULT = "default" 217 | -------------------------------------------------------------------------------- /configoat/templates/config.yaml: -------------------------------------------------------------------------------- 1 | doc: 'This is the main config file where the processing starts from' 2 | 3 | resources: 4 | var1: 5 | type: 'normal' 6 | value: 7 | dev: 'value of var1 in dev' 8 | stage: 'value of var1 in stage' 9 | uat: 'value of var1 in uat' 10 | production: 'value of var1 in production' 11 | qa: 'value of var1 in qa' 12 | 13 | var2: 14 | type: 'common' 15 | value: var2 16 | 17 | nested1: 18 | type: 'nested' 19 | path: 'templates/nested1.yaml' 20 | 21 | script1: 22 | type: 'script' 23 | variable_list: ['a', 'b', 'c'] # only these variables from the script will be available under the namespace of 'script1' 24 | path: 'templates/script.py' 25 | 26 | var3: 27 | type: 'normal' 28 | value: 29 | dev: 'dev' 30 | stage: 'stage' 31 | uat: 'uat' 32 | production: 'production' 33 | qa: 'qa' 34 | -------------------------------------------------------------------------------- /configoat/templates/main.yaml: -------------------------------------------------------------------------------- 1 | doc: 'This is the main config file where the processing starts from' 2 | 3 | # Create all your variables inside resources as shown below. Use proper variable name 4 | # Resource Types => 'normal', 'common', 'nested', 'script' 5 | # 'normal' => Use this type if the value of the variable will be different for different environments 6 | # 'common' => Use this type if the value of the variable will be same across all environments 7 | # 'nested' => Use this type for a nested YAML file. 8 | # 'script' => Use this type for a python script file. 9 | 10 | resources: 11 | var1: 12 | type: 'normal' 13 | value: 14 | dev: 'value of var1 for dev' 15 | stage: 'value of var1 for stage' 16 | uat: 'value of var1 for uat' 17 | production: 'value of var1 for production' 18 | qa: 'value of var1 for qa' 19 | 20 | var2: 21 | type: 'common' 22 | value: "value of var2" 23 | 24 | var3: 25 | type: 'common' 26 | value: False 27 | 28 | var4: 29 | type: 'common' 30 | value: 100 31 | 32 | var5: 33 | type: 'common' 34 | value: [ "Banana", "Mango", "Apple" ] 35 | 36 | var6: 37 | type: 'common' 38 | value: { 39 | "name": "Raihan Boss", 40 | "age": 66, 41 | "address": { 42 | "city": "Dhaka", 43 | "country": "Bangladesh", 44 | } 45 | } 46 | 47 | # DON'T DO THIS! You can ONLY use '$ref' inside str variables 48 | # var6: 49 | # type: 'common' 50 | # value: { 51 | # "name": "Raihan Boss", 52 | # "age": 66, 53 | # "address": { 54 | # "city": "$ref(var7.varAA)", 55 | # "country": "Bangladesh", 56 | # } 57 | # } 58 | -------------------------------------------------------------------------------- /configoat/templates/main_nested.yaml: -------------------------------------------------------------------------------- 1 | doc: 'This is the main config file where the processing starts from' 2 | 3 | # Create all your variables inside resources as shown below. Use proper variable name 4 | # Resource Types => 'normal', 'common', 'nested', 'script' 5 | # 'normal' => Use this type if the value of the variable will be different for different environments 6 | # 'common' => Use this type if the value of the variable will be same across all environments 7 | # 'nested' => Use this type for a nested YAML file. 8 | # 'script' => Use this type for a python script file. 9 | 10 | resources: 11 | var1: 12 | type: 'normal' 13 | value: 14 | dev: 'value of var1 for dev' 15 | stage: 'value of var1 for stage' 16 | uat: 'value of var1 for uat' 17 | production: 'value of var1 for production' 18 | qa: 'value of var1 for qa' 19 | 20 | var2: 21 | type: 'common' 22 | value: "value of var2" 23 | 24 | var3: 25 | type: 'common' 26 | value: False 27 | 28 | var4: 29 | type: 'common' 30 | value: 100 31 | 32 | var5: 33 | type: 'common' 34 | value: [ "Banana", "Mango", "Apple" ] 35 | 36 | var6: 37 | type: 'common' 38 | value: { 39 | "name": "Raihan Boss", 40 | "age": 66, 41 | "address": { 42 | "city": "Dhaka", 43 | "country": "Bangladesh", 44 | } 45 | } 46 | 47 | # DON'T DO THIS! You can ONLY use '$ref' inside str variables 48 | # var6: 49 | # type: 'common' 50 | # value: { 51 | # "name": "Raihan Boss", 52 | # "age": 66, 53 | # "address": { 54 | # "city": "$ref(var7.varAA)", 55 | # "country": "Bangladesh", 56 | # } 57 | # } 58 | 59 | var7: 60 | type: 'nested' 61 | path: 'path/from/project/root/to/nested.yaml' # This is the path of child nested.yaml file, e.g. 'configs/yamls/nested.yaml' 62 | 63 | var8: 64 | type: 'common' 65 | value: "$ref(var7.varAA)" # Here we are referencing the value of the variable 'varAA' which is inside child 'var7' 66 | -------------------------------------------------------------------------------- /configoat/templates/main_nested_script.yaml: -------------------------------------------------------------------------------- 1 | doc: 'This is the main config file where the processing starts from' 2 | 3 | # Create all your variables inside resources as shown below. Use proper variable name 4 | # Resource Types => 'normal', 'common', 'nested', 'script' 5 | # 'normal' => Use this type if the value of the variable will be different for different environments 6 | # 'common' => Use this type if the value of the variable will be same across all environments 7 | # 'nested' => Use this type for a nested YAML file. 8 | # 'script' => Use this type for a python script file. 9 | 10 | resources: 11 | var1: 12 | type: 'normal' 13 | value: 14 | dev: 'value of var1 for dev' 15 | stage: 'value of var1 for stage' 16 | uat: 'value of var1 for uat' 17 | production: 'value of var1 for production' 18 | qa: 'value of var1 for qa' 19 | 20 | var2: 21 | type: 'common' 22 | value: "value of var2" 23 | 24 | var3: 25 | type: 'common' 26 | value: False 27 | 28 | var4: 29 | type: 'common' 30 | value: 100 31 | 32 | var5: 33 | type: 'common' 34 | value: [ "Banana", "Mango", "Apple" ] 35 | 36 | var6: 37 | type: 'common' 38 | value: { 39 | "name": "Raihan Boss", 40 | "age": 66, 41 | "address": { 42 | "city": "Dhaka", 43 | "country": "Bangladesh", 44 | } 45 | } 46 | 47 | # DON'T DO THIS! You can ONLY use '$ref' inside str variables 48 | # var6: 49 | # type: 'common' 50 | # value: { 51 | # "name": "Raihan Boss", 52 | # "age": 66, 53 | # "address": { 54 | # "city": "$ref(var7.varAA)", 55 | # "country": "Bangladesh", 56 | # } 57 | # } 58 | 59 | var7: 60 | type: 'nested' 61 | path: 'path/from/project/root/to/nested.yaml' # This is the path of child nested.yaml file, e.g. 'configs/yamls/nested.yaml' 62 | 63 | var8: 64 | type: 'common' 65 | value: "$ref(var7.varAA)" # Here we are referencing the value of the variable 'varAA' which is inside child 'var7' 66 | 67 | var9: 68 | type: 'script' 69 | variable_list: ['a', 'b', 'c', 'd', 'e'] # only these variables from the script will be available under the namespace of 'var9' 70 | path: 'path/from/project/root/to/script.py' # This is the path of child script.py file, e.g. 'configs/scripts/script.py' 71 | -------------------------------------------------------------------------------- /configoat/templates/nested.yaml: -------------------------------------------------------------------------------- 1 | doc: 'Help text for this config file' 2 | 3 | resources: 4 | varAA: 5 | type: 'normal' 6 | value: 7 | dev: 'value of varAA in dev' 8 | stage: 'value of varAA in stage' 9 | uat: 'value of varAA in uat' 10 | production: 'value of varAA in production' 11 | qa: 'value of varAA in qa' 12 | 13 | varBB: 14 | type: 'normal' 15 | value: 16 | # $ref(@.var2) -> references the variable 'var2' which is inside the main YAML file 17 | # $ref(@.var3) -> references the variable 'var3' which is inside the main YAML file 18 | 19 | dev: '$ref(@.var2)/cricket/$ref(@.var3) bowler' 20 | stage: '$ref(@.var2)_football_$ref(@.var3) striker' 21 | uat: '$ref(@.var2)-tennis-$ref(@.var3) player' 22 | production: '$ref(@.var2) badminton $ref(@.var3) player' 23 | qa: '$ref(@.var2) baseball $ref(@.var3) runner' 24 | 25 | varCC: 26 | type: "common" 27 | value: "$ref(varAA)" # $ref(varAA) -> references the variable 'varAA' inside this file 28 | -------------------------------------------------------------------------------- /configoat/templates/script.py: -------------------------------------------------------------------------------- 1 | import math 2 | import os 3 | 4 | a = 100 5 | 6 | if os.environ.get("CONFIGOAT_ENVIRONMENT", "dev") == "dev": 7 | b = "hello from dev" 8 | else: 9 | b = 'hello from non-dev' 10 | 11 | c = math.sqrt(5) 12 | 13 | # $ref(@.var7.varCC) -> references the variable 'varCC' which is inside the variable 'var7' which is inside the main YAML file 14 | d = "$ref(@.var7.varCC)_test" 15 | 16 | # $ref(@.var2) -> references the variable 'var2' which is inside the main YAML file 17 | e = "$ref(@.var2)_test" 18 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = python -msphinx 7 | SPHINXPROJ = configoat 8 | SOURCEDIR = . 9 | BUILDDIR = _build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | -------------------------------------------------------------------------------- /docs/authors.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../AUTHORS.rst 2 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # configoat documentation build configuration file, created by 4 | # sphinx-quickstart on Fri Jun 9 13:47:02 2017. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | # If extensions (or modules to document with autodoc) are in another 16 | # directory, add these directories to sys.path here. If the directory is 17 | # relative to the documentation root, use os.path.abspath to make it 18 | # absolute, like shown here. 19 | # 20 | import os 21 | import sys 22 | sys.path.insert(0, os.path.abspath('..')) 23 | 24 | import configoat 25 | 26 | # -- General configuration --------------------------------------------- 27 | 28 | # If your documentation needs a minimal Sphinx version, state it here. 29 | # 30 | # needs_sphinx = '1.0' 31 | 32 | # Add any Sphinx extension module names here, as strings. They can be 33 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 34 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] 35 | 36 | # Add any paths that contain templates here, relative to this directory. 37 | templates_path = ['_templates'] 38 | 39 | # The suffix(es) of source filenames. 40 | # You can specify multiple suffix as a list of string: 41 | # 42 | # source_suffix = ['.rst', '.md'] 43 | source_suffix = '.rst' 44 | 45 | # The master toctree document. 46 | master_doc = 'index' 47 | 48 | # General information about the project. 49 | project = 'confiGOAT' 50 | copyright = "2023, Hasan-UL-Banna, Asadullah Al Galib" 51 | author = "Hasan-UL-Banna, Asadullah Al Galib" 52 | 53 | # The version info for the project you're documenting, acts as replacement 54 | # for |version| and |release|, also used in various other places throughout 55 | # the built documents. 56 | # 57 | # The short X.Y version. 58 | version = configoat.__version__ 59 | # The full version, including alpha/beta/rc tags. 60 | release = configoat.__version__ 61 | 62 | # The language for content autogenerated by Sphinx. Refer to documentation 63 | # for a list of supported languages. 64 | # 65 | # This is also used if you do content translation via gettext catalogs. 66 | # Usually you set "language" from the command line for these cases. 67 | language = None 68 | 69 | # List of patterns, relative to source directory, that match files and 70 | # directories to ignore when looking for source files. 71 | # This patterns also effect to html_static_path and html_extra_path 72 | exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] 73 | 74 | # The name of the Pygments (syntax highlighting) style to use. 75 | pygments_style = 'sphinx' 76 | 77 | # If true, `todo` and `todoList` produce output, else they produce nothing. 78 | todo_include_todos = False 79 | 80 | 81 | # -- Options for HTML output ------------------------------------------- 82 | 83 | # The theme to use for HTML and HTML Help pages. See the documentation for 84 | # a list of builtin themes. 85 | # 86 | html_theme = 'alabaster' 87 | 88 | # Theme options are theme-specific and customize the look and feel of a 89 | # theme further. For a list of options available for each theme, see the 90 | # documentation. 91 | # 92 | # html_theme_options = {} 93 | 94 | # Add any paths that contain custom static files (such as style sheets) here, 95 | # relative to this directory. They are copied after the builtin static files, 96 | # so a file named "default.css" will overwrite the builtin "default.css". 97 | html_static_path = ['_static'] 98 | 99 | 100 | # -- Options for HTMLHelp output --------------------------------------- 101 | 102 | # Output file base name for HTML help builder. 103 | htmlhelp_basename = 'configoatdoc' 104 | 105 | 106 | # -- Options for LaTeX output ------------------------------------------ 107 | 108 | latex_elements = { 109 | # The paper size ('letterpaper' or 'a4paper'). 110 | # 111 | # 'papersize': 'letterpaper', 112 | 113 | # The font size ('10pt', '11pt' or '12pt'). 114 | # 115 | # 'pointsize': '10pt', 116 | 117 | # Additional stuff for the LaTeX preamble. 118 | # 119 | # 'preamble': '', 120 | 121 | # Latex figure (float) alignment 122 | # 123 | # 'figure_align': 'htbp', 124 | } 125 | 126 | # Grouping the document tree into LaTeX files. List of tuples 127 | # (source start file, target name, title, author, documentclass 128 | # [howto, manual, or own class]). 129 | latex_documents = [ 130 | (master_doc, 'configoat.tex', 131 | 'confiGOAT Documentation', 132 | 'Hasan-UL-Banna, Asadullah Al Galib', 'manual'), 133 | ] 134 | 135 | 136 | # -- Options for manual page output ------------------------------------ 137 | 138 | # One entry per manual page. List of tuples 139 | # (source start file, name, description, authors, manual section). 140 | man_pages = [ 141 | (master_doc, 'configoat', 142 | 'confiGOAT Documentation', 143 | [author], 1) 144 | ] 145 | 146 | 147 | # -- Options for Texinfo output ---------------------------------------- 148 | 149 | # Grouping the document tree into Texinfo files. List of tuples 150 | # (source start file, target name, title, author, 151 | # dir menu entry, description, category) 152 | texinfo_documents = [ 153 | (master_doc, 'configoat', 154 | 'confiGOAT Documentation', 155 | author, 156 | 'configoat', 157 | 'One line description of project.', 158 | 'Miscellaneous'), 159 | ] 160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /docs/contributing.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../CONTRIBUTING.rst 2 | -------------------------------------------------------------------------------- /docs/history.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../HISTORY.rst 2 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to confiGOAT's documentation! 2 | ====================================== 3 | 4 | .. toctree:: 5 | :maxdepth: 2 6 | :caption: Contents: 7 | 8 | readme 9 | installation 10 | usage 11 | modules 12 | contributing 13 | authors 14 | history 15 | 16 | Indices and tables 17 | ================== 18 | * :ref:`genindex` 19 | * :ref:`modindex` 20 | * :ref:`search` 21 | -------------------------------------------------------------------------------- /docs/installation.rst: -------------------------------------------------------------------------------- 1 | .. highlight:: shell 2 | 3 | ============ 4 | Installation 5 | ============ 6 | 7 | 8 | Stable release 9 | -------------- 10 | 11 | To install confiGOAT, run this command in your terminal: 12 | 13 | .. code-block:: console 14 | 15 | $ pip install configoat 16 | 17 | This is the preferred method to install confiGOAT, as it will always install the most recent stable release. 18 | 19 | If you don't have `pip`_ installed, this `Python installation guide`_ can guide 20 | you through the process. 21 | 22 | .. _pip: https://pip.pypa.io 23 | .. _Python installation guide: http://docs.python-guide.org/en/latest/starting/installation/ 24 | 25 | 26 | From sources 27 | ------------ 28 | 29 | The sources for confiGOAT can be downloaded from the `Github repo`_. 30 | 31 | You can either clone the public repository: 32 | 33 | .. code-block:: console 34 | 35 | $ git clone git://github.com/aag13/configoat 36 | 37 | Or download the `tarball`_: 38 | 39 | .. code-block:: console 40 | 41 | $ curl -OJL https://github.com/aag13/configoat/tarball/master 42 | 43 | Once you have a copy of the source, you can install it with: 44 | 45 | .. code-block:: console 46 | 47 | $ python setup.py install 48 | 49 | 50 | .. _Github repo: https://github.com/aag13/configoat 51 | .. _tarball: https://github.com/aag13/configoat/tarball/master 52 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=python -msphinx 9 | ) 10 | set SOURCEDIR=. 11 | set BUILDDIR=_build 12 | set SPHINXPROJ=configoat 13 | 14 | if "%1" == "" goto help 15 | 16 | %SPHINXBUILD% >NUL 2>NUL 17 | if errorlevel 9009 ( 18 | echo. 19 | echo.The Sphinx module was not found. Make sure you have Sphinx installed, 20 | echo.then set the SPHINXBUILD environment variable to point to the full 21 | echo.path of the 'sphinx-build' executable. Alternatively you may add the 22 | echo.Sphinx directory to PATH. 23 | echo. 24 | echo.If you don't have Sphinx installed, grab it from 25 | echo.http://sphinx-doc.org/ 26 | exit /b 1 27 | ) 28 | 29 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 30 | goto end 31 | 32 | :help 33 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 34 | 35 | :end 36 | popd 37 | -------------------------------------------------------------------------------- /docs/readme.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst 2 | -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | ===== 2 | Usage 3 | ===== 4 | 5 | To use confiGOAT in a project:: 6 | 7 | import configoat 8 | -------------------------------------------------------------------------------- /requirements_dev.txt: -------------------------------------------------------------------------------- 1 | pip 2 | bump2version 3 | wheel 4 | watchdog 5 | flake8 6 | tox 7 | coverage 8 | Sphinx 9 | twine 10 | Click 11 | pyyaml 12 | pytest 13 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 0.1.0 3 | commit = True 4 | tag = True 5 | 6 | [bumpversion:file:setup.py] 7 | search = version='{current_version}' 8 | replace = version='{new_version}' 9 | 10 | [bumpversion:file:configoat/__init__.py] 11 | search = __version__ = '{current_version}' 12 | replace = __version__ = '{new_version}' 13 | 14 | [bdist_wheel] 15 | universal = 1 16 | 17 | [flake8] 18 | exclude = docs 19 | [tool:pytest] 20 | addopts = --ignore=setup.py 21 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """The setup script.""" 4 | 5 | from setuptools import setup, find_packages 6 | 7 | with open('README.md') as readme_file: 8 | readme = readme_file.read() 9 | 10 | with open('HISTORY.rst') as history_file: 11 | history = history_file.read() 12 | 13 | requirements = ['Click>=7.0', 'PyYAML>=5.0'] 14 | # install_requires=['pandas>=1.0', 'scipy==1.1', 'matplotlib>=2.2.1,<3'] 15 | 16 | test_requirements = ['pytest>=3', ] 17 | 18 | setup( 19 | author="Hasan-UL-Banna, Asadullah Al Galib", 20 | author_email='hasanulbanna04056@gmail.com, asadullahgalib13@gmail.com', 21 | python_requires='>=3.5', 22 | classifiers=[ 23 | 'Development Status :: 4 - Beta', 24 | 'Framework :: Django', 25 | 'Framework :: Flask', 26 | 'Intended Audience :: Developers', 27 | 'License :: OSI Approved :: MIT License', 28 | 'Natural Language :: English', 29 | 'Operating System :: OS Independent', 30 | 'Programming Language :: Python :: 3', 31 | 'Programming Language :: Python :: 3.5', 32 | 'Programming Language :: Python :: 3.6', 33 | 'Programming Language :: Python :: 3.7', 34 | 'Programming Language :: Python :: 3.8', 35 | 'Topic :: Software Development :: Libraries', 36 | ], 37 | description="confiGOAT is a powerful, flexible, and developer-friendly configuration management tool.", 38 | entry_points={ 39 | 'console_scripts': [ 40 | 'configoat=configoat.cli:cli', 41 | ], 42 | }, 43 | install_requires=requirements, 44 | license="MIT license", 45 | long_description=readme + '\n\n' + history, 46 | long_description_content_type="text/markdown", 47 | include_package_data=True, 48 | keywords='configoat', 49 | name='configoat', 50 | packages=find_packages(include=['configoat', 'configoat.*']), 51 | test_suite='tests', 52 | tests_require=test_requirements, 53 | url='https://github.com/aag13/configoat', 54 | version='0.1.6', 55 | zip_safe=False, 56 | ) 57 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | """Unit test package for configoat.""" 2 | -------------------------------------------------------------------------------- /tests/test_configoat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """Tests for `configoat` package.""" 4 | 5 | import pytest 6 | 7 | from click.testing import CliRunner 8 | 9 | from configoat import configoat 10 | from configoat import cli 11 | 12 | 13 | @pytest.fixture 14 | def response(): 15 | """Sample pytest fixture. 16 | 17 | See more at: http://doc.pytest.org/en/latest/fixture.html 18 | """ 19 | # import requests 20 | # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') 21 | 22 | 23 | def test_content(response): 24 | """Sample pytest test function with the pytest fixture as an argument.""" 25 | # from bs4 import BeautifulSoup 26 | # assert 'GitHub' in BeautifulSoup(response.content).title.string 27 | 28 | 29 | def test_command_line_interface(): 30 | """Test the CLI.""" 31 | runner = CliRunner() 32 | result = runner.invoke(cli.main) 33 | assert result.exit_code == 0 34 | assert 'configoat.cli.main' in result.output 35 | help_result = runner.invoke(cli.main, ['--help']) 36 | assert help_result.exit_code == 0 37 | assert '--help Show this message and exit.' in help_result.output 38 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py35, py36, py37, py38, py3.10 3 | 4 | [travis] 5 | python = 6 | 3.10: py3.10 7 | 3.8: py38 8 | 3.7: py37 9 | 3.6: py36 10 | 3.5: py35 11 | 12 | [testenv:flake8] 13 | basepython = python 14 | deps = flake8 15 | commands = flake8 configoat tests 16 | 17 | [testenv] 18 | setenv = 19 | PYTHONPATH = {toxinidir} 20 | deps = 21 | -r{toxinidir}/requirements_dev.txt 22 | ; If you want to make tox run the tests with the same versions, create a 23 | ; requirements.txt with the pinned versions and uncomment the following line: 24 | ; -r{toxinidir}/requirements.txt 25 | commands = 26 | pip install -U pip 27 | pytest --basetemp={envtmpdir} 28 | 29 | --------------------------------------------------------------------------------