# Easily Launch Config (el_config)
Python-box based custom config package for python projects.
## Features
* Load environment variables - [https://pypi.org/project/python-dotenv](https://pypi.org/project/python-dotenv)
* Python-box based config - [https://pypi.org/project/python-box](https://pypi.org/project/python-box)
* Cerberus schema validation - [https://pypi.org/project/Cerberus](https://pypi.org/project/Cerberus)
* Custom base config module
* YAML or JSON based configs
* Update with extra configs
* Pre-load config
* Validate config
* Freeze config
* Config as dictioary
---
## Installation
### 1. Prerequisites
* **Python (>= v3.7)**
* **PyPi (>= v21)**
### 2. Install el-config
#### A. [RECOMMENDED] PyPi install
```sh
# Install or upgrade el-config package:
pip install --upgrade el-config
# To uninstall package:
pip uninstall -y el-config
```
#### B. Manually add to PYTHONPATH (Recommended for development)
```sh
# Clone repository by git:
git clone git@bitbucket.org:ellexiinc/el_config.git
cd el_config
# Install python dependencies:
pip install --upgrade pip
cat requirements.txt | xargs -n 1 -L 1 pip install --no-cache-dir
# Add current path to PYTHONPATH:
export PYTHONPATH="${PWD}:${PYTHONPATH}"
```
#### C. Manually compile and setup (Not recommended)
```sh
# Clone repository by git:
git clone git@bitbucket.org:ellexiinc/el_config.git
cd el_config
# Building python package:
pip install --upgrade pip setuptools wheel
python setup.py build
# Install python dependencies with built package to current python environment:
python setup.py install --record installed_files.txt
# To remove only installed el-config package:
head -n 1 installed_files.txt | xargs rm -vrf
# Or to remove all installed files and packages:
cat installed_files.txt | xargs rm -vrf
```
## Usage/Examples
* Sample python file - [https://bitbucket.org/ellexiinc/el_config/src/master/sample.py](https://bitbucket.org/ellexiinc/el_config/src/master/sample.py)
**configs/app.yml**:
```yml
env: development
app:
name: "My App"
host: 0.0.0.0
port: 80
secret: "my-secret"
debug: false
```
**.env**:
```sh
ENV=production
APP_PORT=8080
APP_SECRET="My_s3crEt_k3y"
```
**utils/validator_schemas.py**:
```python
config_schema = {
'env': { 'type': 'string', 'allowed': ['development', 'production'], 'default': 'development' },
'app':
{
'type': 'dict',
'schema':
{
'name': { 'type': 'string', 'minlength': 2, 'maxlength': 255 },
'host': { 'type': 'string', 'minlength': 2, 'maxlength': 255 },
'port': { 'type': 'integer', 'coerce': int, 'min': 1024, 'max': 65535 },
'secret': { 'type': 'string', 'minlength': 12, 'maxlength': 255 },
'debug': { 'type': 'boolean' }
}
}
}
```
**config.py**:
```python
import os
from utils.validator_schemas import config_schema
from el_config import ConfigBase
def _pre_load(config):
try:
config.env = os.getenv('ENV', config.env).strip().lower()
if config.env == 'production':
config.app.debug = False
if os.getenv('APP_SECRET') is None:
raise KeyError("Missing required `APP_SECRET` environment variable on 'production'!")
config.app.port = os.getenv('APP_PORT', config.app.port)
config.app.debug = os.getenv('APP_DEBUG', config.app.debug)
config.app.secret = os.getenv('APP_SECRET', config.app.secret)
except Exception as err:
print(f"ERROR: Error occured while pre-loading config:\n {err}")
exit(2)
return config
_cb = ConfigBase(pre_load=_pre_load, valid_schema=config_schema)
_cb.load()
config = _cb.config
print(config)
```
**main.py**:
```python
from flask import Flask
from config import config
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
if __name__ == '__main__':
app.run(host=config.app.host, port=config.app.port)
```
---
## Running Tests
To run tests, run the following command:
```sh
pytest
```
---
## References
* [https://saurabh-kumar.com/python-dotenv](https://saurabh-kumar.com/python-dotenv)
* [https://github.com/theskumar/python-dotenv](https://github.com/theskumar/python-dotenv)
* [https://github.com/cdgriffith/Box/wiki](https://github.com/cdgriffith/Box/wiki)
* [https://github.com/cdgriffith/Box](https://github.com/cdgriffith/Box)
* [https://docs.python-cerberus.org/en/stable](https://docs.python-cerberus.org/en/stable)
* [https://github.com/pyeve/cerberus](https://github.com/pyeve/cerberus)
Raw data
{
"_id": null,
"home_page": "https://bitbucket.org/ellexiinc/el_config/",
"name": "el-config",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "el_config,config,configs,python-box,custom-config",
"author": "Batkhuu Byambajav",
"author_email": "batkhuu@ellexi.com",
"download_url": "https://files.pythonhosted.org/packages/6b/6a/6ade9021ed737edc648ee3362a5d8132ba702e724fb5f774d204dba3bf9d/el_config-0.1.18.tar.gz",
"platform": null,
"description": "# Easily Launch Config (el_config)\n\nPython-box based custom config package for python projects.\n\n## Features\n\n* Load environment variables - [https://pypi.org/project/python-dotenv](https://pypi.org/project/python-dotenv)\n* Python-box based config - [https://pypi.org/project/python-box](https://pypi.org/project/python-box)\n* Cerberus schema validation - [https://pypi.org/project/Cerberus](https://pypi.org/project/Cerberus)\n* Custom base config module\n* YAML or JSON based configs\n* Update with extra configs\n* Pre-load config\n* Validate config\n* Freeze config\n* Config as dictioary\n\n---\n\n## Installation\n\n### 1. Prerequisites\n\n* **Python (>= v3.7)**\n* **PyPi (>= v21)**\n\n### 2. Install el-config\n\n#### A. [RECOMMENDED] PyPi install\n\n```sh\n# Install or upgrade el-config package:\npip install --upgrade el-config\n\n# To uninstall package:\npip uninstall -y el-config\n```\n\n#### B. Manually add to PYTHONPATH (Recommended for development)\n\n```sh\n# Clone repository by git:\ngit clone git@bitbucket.org:ellexiinc/el_config.git\ncd el_config\n\n# Install python dependencies:\npip install --upgrade pip\ncat requirements.txt | xargs -n 1 -L 1 pip install --no-cache-dir\n\n# Add current path to PYTHONPATH:\nexport PYTHONPATH=\"${PWD}:${PYTHONPATH}\"\n```\n\n#### C. Manually compile and setup (Not recommended)\n\n```sh\n# Clone repository by git:\ngit clone git@bitbucket.org:ellexiinc/el_config.git\ncd el_config\n\n# Building python package:\npip install --upgrade pip setuptools wheel\npython setup.py build\n# Install python dependencies with built package to current python environment:\npython setup.py install --record installed_files.txt\n\n# To remove only installed el-config package:\nhead -n 1 installed_files.txt | xargs rm -vrf\n# Or to remove all installed files and packages:\ncat installed_files.txt | xargs rm -vrf\n```\n\n## Usage/Examples\n\n* Sample python file - [https://bitbucket.org/ellexiinc/el_config/src/master/sample.py](https://bitbucket.org/ellexiinc/el_config/src/master/sample.py)\n\n**configs/app.yml**:\n\n```yml\nenv: development\n\napp:\n name: \"My App\"\n host: 0.0.0.0\n port: 80\n secret: \"my-secret\"\n debug: false\n```\n\n**.env**:\n\n```sh\nENV=production\n\nAPP_PORT=8080\nAPP_SECRET=\"My_s3crEt_k3y\"\n```\n\n**utils/validator_schemas.py**:\n\n```python\nconfig_schema = {\n 'env': { 'type': 'string', 'allowed': ['development', 'production'], 'default': 'development' },\n 'app':\n {\n 'type': 'dict',\n 'schema':\n {\n 'name': { 'type': 'string', 'minlength': 2, 'maxlength': 255 },\n 'host': { 'type': 'string', 'minlength': 2, 'maxlength': 255 },\n 'port': { 'type': 'integer', 'coerce': int, 'min': 1024, 'max': 65535 },\n 'secret': { 'type': 'string', 'minlength': 12, 'maxlength': 255 },\n 'debug': { 'type': 'boolean' }\n }\n }\n}\n```\n\n**config.py**:\n\n```python\nimport os\n\nfrom utils.validator_schemas import config_schema\nfrom el_config import ConfigBase\n\n\ndef _pre_load(config):\n try:\n config.env = os.getenv('ENV', config.env).strip().lower()\n\n if config.env == 'production':\n config.app.debug = False\n\n if os.getenv('APP_SECRET') is None:\n raise KeyError(\"Missing required `APP_SECRET` environment variable on 'production'!\")\n\n config.app.port = os.getenv('APP_PORT', config.app.port)\n config.app.debug = os.getenv('APP_DEBUG', config.app.debug)\n config.app.secret = os.getenv('APP_SECRET', config.app.secret)\n except Exception as err:\n print(f\"ERROR: Error occured while pre-loading config:\\n {err}\")\n exit(2)\n\n return config\n\n\n_cb = ConfigBase(pre_load=_pre_load, valid_schema=config_schema)\n_cb.load()\nconfig = _cb.config\n\nprint(config)\n```\n\n**main.py**:\n\n```python\nfrom flask import Flask\nfrom config import config\n\n\napp = Flask(__name__)\n\n@app.route(\"/\")\ndef hello_world():\n return \"<p>Hello, World!</p>\"\n\nif __name__ == '__main__':\n app.run(host=config.app.host, port=config.app.port)\n```\n\n---\n\n## Running Tests\n\nTo run tests, run the following command:\n\n```sh\npytest\n```\n\n---\n\n## References\n\n* [https://saurabh-kumar.com/python-dotenv](https://saurabh-kumar.com/python-dotenv)\n* [https://github.com/theskumar/python-dotenv](https://github.com/theskumar/python-dotenv)\n* [https://github.com/cdgriffith/Box/wiki](https://github.com/cdgriffith/Box/wiki)\n* [https://github.com/cdgriffith/Box](https://github.com/cdgriffith/Box)\n* [https://docs.python-cerberus.org/en/stable](https://docs.python-cerberus.org/en/stable)\n* [https://github.com/pyeve/cerberus](https://github.com/pyeve/cerberus)",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python-box based custom config package for python projects.",
"version": "0.1.18",
"split_keywords": [
"el_config",
"config",
"configs",
"python-box",
"custom-config"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6b6a6ade9021ed737edc648ee3362a5d8132ba702e724fb5f774d204dba3bf9d",
"md5": "3760b395b79deced719bb604f818c969",
"sha256": "0dc193b69f15d7824a49434341771620850bd121467faeee5d8af14e5d52f735"
},
"downloads": -1,
"filename": "el_config-0.1.18.tar.gz",
"has_sig": false,
"md5_digest": "3760b395b79deced719bb604f818c969",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5863,
"upload_time": "2023-01-12T05:43:48",
"upload_time_iso_8601": "2023-01-12T05:43:48.515850Z",
"url": "https://files.pythonhosted.org/packages/6b/6a/6ade9021ed737edc648ee3362a5d8132ba702e724fb5f774d204dba3bf9d/el_config-0.1.18.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-12 05:43:48",
"github": false,
"gitlab": false,
"bitbucket": true,
"bitbucket_user": "ellexiinc",
"bitbucket_project": "el_config",
"lcname": "el-config"
}