| Name | adi-env-parser JSON |
| Version |
1.1.1
JSON |
| download |
| home_page | None |
| Summary | Adinsure Environment parser |
| upload_time | 2024-09-05 11:47:18 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | <4,>=3.8 |
| license | MIT License Copyright (c) 2022 ADACTA d.o.o. 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. |
| keywords |
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# Environment parser
## Requirements
* Python - Minimum required version is 3.8
## Using the environment parser
EnvironmentParser class parses all environment variables with certain prefix and
creates a Python dictionary based on the structure of these variables. The values
are converted to booleans and integers when detected as such by default.
General variable structure rules:
* variable name after prefix should not be empty
* first character of variable name after prefix should not be "_"
* different levels of depth within environment variables are specified by using
"__" string.
* arrays can be specified by using numeric index as a key within particular level
* array numeric indices should be defined in order, variables with invalid index
will be discarded
Value conversion rules:
* value will be converted to boolean if it matches `true` or `false` when lower
cased
* value will be converted to integer if it contains digits only
### Using the EnvironmentParser class
Example of instantiating of EnvironmentParser object using `MYPREFIX` as a prefix
for environment variables. Upon instantiation, the object will automatically parse
the current environment variables and store them in its `configuration` property.
```python
import json
from adi_env_parser import EnvironmentParser
parser = EnvironmentParser(prefix="MYPREFIX")
print(json.dump(json.dumps(parser.configuration, indent=4)))
```
It is possible to provide existing JSON formatted file as a configuration base.
```python
import json
from adi_env_parser import EnvironmentParser
parser = EnvironmentParser(prefix="MYPREFIX", config_file="configuration.json")
print(json.dump(json.dumps(parser.configuration, indent=4)))
```
It is possible to disable value conversion by setting `convert_values` parameter
when instantiating `EnvironmentParser` object.
```python
from adi_env_parser import EnvironmentParser
parser = EnvironmentParser(prefix="MYPREFIX", convert_values=False)
```
### Examples
Examples use PYENV as environment variable prefix. This is default prefix used
when not specifying one explicitly when instatiating EnvironmentParser.
#### Creating dictionary
Environment variables:
```sh
PYENV_hotel_name="Blue Falcon"
PYENV_rooms__room_1="James Holden"
PYENV_rooms__room_2="Amos Burton"
PYENV_rooms__room_3="Naomi Nagata"
PYENV_rooms__room_4="Alex Kamal"
```
Resulting object:
```json
{
"hotel_name": "Blue Falcon",
"rooms": {
"room_1": "James Holden",
"room_2": "Amos Burton",
"room_3": "Naomi Nagata",
"room_4": "Alex Kamal"
}
}
```
#### Creating array
Environment variables:
```sh
PYENV_hotel_name="Blue Falcon"
PYENV_room_1__inventory__0="Wardrobe"
PYENV_room_1__inventory__1="Table"
PYENV_room_1__inventory__2="Lamp"
```
Resulting object:
```json
{
"hotel_name": "Blue Falcon",
"room_1": {
"inventory": [
"Wardrobe",
"Table",
"Lamp"
]
}
}
```
#### Dictionaries within list
Environment variables:
```sh
PYENV_hotel_name="Blue Falcon"
PYENV_rooms__0__name="Room 1"
PYENV_rooms__0__capacity="2"
PYENV_rooms__2__name="Room 2"
PYENV_rooms__2__capacity="2"
```
Resulting object:
```json
{
"hotel_name": "Blue Falcon",
"rooms": [
{
"name": "Room 1",
"capacity": "2"
},
{
"name": "Room 2",
"capacity": "2"
}
]
}
```
### Console utility
Module provides console utility which can be used for parsing of environment
variables. It also supports reading of existing JSON formatted file and setting
indentation for output of created configuration JSON object.
```sh
➜ adi-env-parser --help
usage: adi-env-parser -p <prefix> -j <base_json_file>
Parses environment variables with defined prefix and creates JSON output from the parsed structure.
optional arguments:
-h, --help show this help message and exit
--prefix [PREFIX], -p [PREFIX]
Environment variable prefix. Default: PYENV
--json [JSON], -j [JSON]
JSON formatted file to read as base configuration
--indent [INDENT], -i [INDENT]
Number of spaces to use for indentation of output JSON string
--ignore-prefix IGNORE_PREFIX, -n IGNORE_PREFIX
Environment variable prefix to ignore. Can be used multiple times.
```
## Development
### Install development packages
```sh
pip install -e ".[dev]"
pip install -e ".[test]"
# Install build-local package group if you want to build packages locally
pip install -e ".[build-local]"
```
### Install pre-commit
```sh
pre-commit install
```
### Building and publishing new version
New version is built and published on tag in GitHub repository. The package version is infered from commit name.
Raw data
{
"_id": null,
"home_page": null,
"name": "adi-env-parser",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.8",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Jernej Kladnik <jernej.kladnik@adacta-fintech.com>",
"download_url": null,
"platform": null,
"description": "# Environment parser\n\n## Requirements\n\n* Python - Minimum required version is 3.8\n\n## Using the environment parser\n\nEnvironmentParser class parses all environment variables with certain prefix and\ncreates a Python dictionary based on the structure of these variables. The values\nare converted to booleans and integers when detected as such by default.\n\nGeneral variable structure rules:\n\n* variable name after prefix should not be empty\n* first character of variable name after prefix should not be \"_\"\n* different levels of depth within environment variables are specified by using\n \"__\" string.\n* arrays can be specified by using numeric index as a key within particular level\n* array numeric indices should be defined in order, variables with invalid index\n will be discarded\n\nValue conversion rules:\n\n* value will be converted to boolean if it matches `true` or `false` when lower\n cased\n* value will be converted to integer if it contains digits only\n\n### Using the EnvironmentParser class\n\nExample of instantiating of EnvironmentParser object using `MYPREFIX` as a prefix\nfor environment variables. Upon instantiation, the object will automatically parse\nthe current environment variables and store them in its `configuration` property.\n\n```python\nimport json\nfrom adi_env_parser import EnvironmentParser\n\nparser = EnvironmentParser(prefix=\"MYPREFIX\")\nprint(json.dump(json.dumps(parser.configuration, indent=4)))\n```\n\nIt is possible to provide existing JSON formatted file as a configuration base.\n\n```python\nimport json\nfrom adi_env_parser import EnvironmentParser\n\nparser = EnvironmentParser(prefix=\"MYPREFIX\", config_file=\"configuration.json\")\nprint(json.dump(json.dumps(parser.configuration, indent=4)))\n```\n\nIt is possible to disable value conversion by setting `convert_values` parameter\nwhen instantiating `EnvironmentParser` object.\n\n```python\nfrom adi_env_parser import EnvironmentParser\n\nparser = EnvironmentParser(prefix=\"MYPREFIX\", convert_values=False)\n```\n\n### Examples\n\nExamples use PYENV as environment variable prefix. This is default prefix used\nwhen not specifying one explicitly when instatiating EnvironmentParser.\n\n#### Creating dictionary\n\nEnvironment variables:\n\n```sh\nPYENV_hotel_name=\"Blue Falcon\"\nPYENV_rooms__room_1=\"James Holden\"\nPYENV_rooms__room_2=\"Amos Burton\"\nPYENV_rooms__room_3=\"Naomi Nagata\"\nPYENV_rooms__room_4=\"Alex Kamal\"\n```\n\nResulting object:\n\n```json\n{\n \"hotel_name\": \"Blue Falcon\",\n \"rooms\": {\n \"room_1\": \"James Holden\",\n \"room_2\": \"Amos Burton\",\n \"room_3\": \"Naomi Nagata\",\n \"room_4\": \"Alex Kamal\"\n }\n}\n```\n\n#### Creating array\n\nEnvironment variables:\n\n```sh\nPYENV_hotel_name=\"Blue Falcon\"\nPYENV_room_1__inventory__0=\"Wardrobe\"\nPYENV_room_1__inventory__1=\"Table\"\nPYENV_room_1__inventory__2=\"Lamp\"\n```\n\nResulting object:\n\n```json\n{\n \"hotel_name\": \"Blue Falcon\",\n \"room_1\": {\n \"inventory\": [\n \"Wardrobe\",\n \"Table\",\n \"Lamp\"\n ]\n }\n}\n```\n\n#### Dictionaries within list\n\nEnvironment variables:\n\n```sh\nPYENV_hotel_name=\"Blue Falcon\"\nPYENV_rooms__0__name=\"Room 1\"\nPYENV_rooms__0__capacity=\"2\"\nPYENV_rooms__2__name=\"Room 2\"\nPYENV_rooms__2__capacity=\"2\"\n```\n\nResulting object:\n\n```json\n{\n \"hotel_name\": \"Blue Falcon\",\n \"rooms\": [\n {\n \"name\": \"Room 1\",\n \"capacity\": \"2\"\n },\n {\n \"name\": \"Room 2\",\n \"capacity\": \"2\"\n }\n ]\n}\n```\n\n### Console utility\n\nModule provides console utility which can be used for parsing of environment\nvariables. It also supports reading of existing JSON formatted file and setting\nindentation for output of created configuration JSON object.\n\n```sh\n\u279c adi-env-parser --help\nusage: adi-env-parser -p <prefix> -j <base_json_file>\n\nParses environment variables with defined prefix and creates JSON output from the parsed structure.\n\noptional arguments:\n -h, --help show this help message and exit\n --prefix [PREFIX], -p [PREFIX]\n Environment variable prefix. Default: PYENV\n --json [JSON], -j [JSON]\n JSON formatted file to read as base configuration\n --indent [INDENT], -i [INDENT]\n Number of spaces to use for indentation of output JSON string\n --ignore-prefix IGNORE_PREFIX, -n IGNORE_PREFIX\n Environment variable prefix to ignore. Can be used multiple times.\n```\n\n## Development\n\n### Install development packages\n\n```sh\npip install -e \".[dev]\"\npip install -e \".[test]\"\n# Install build-local package group if you want to build packages locally\npip install -e \".[build-local]\"\n```\n\n### Install pre-commit\n\n```sh\npre-commit install\n```\n\n### Building and publishing new version\n\nNew version is built and published on tag in GitHub repository. The package version is infered from commit name.\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2022 ADACTA d.o.o. 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. ",
"summary": "Adinsure Environment parser",
"version": "1.1.1",
"project_urls": {
"documentation": "https://github.com/adactafintech/environment-parser",
"homepage": "https://github.com/adactafintech/environment-parser",
"repository": "https://github.com/adactafintech/environment-parser"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1bf6496e6798f15801ec5b0369fc8126105e326ee0e7247be35f04ebed6cc4d1",
"md5": "2e84769282eadc54ad937cf7297c8c5c",
"sha256": "b04ffe88fa560c256589b864000cc4e560735f6f876c8f4c883b3bce673c5d94"
},
"downloads": -1,
"filename": "adi_env_parser-1.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e84769282eadc54ad937cf7297c8c5c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.8",
"size": 9944,
"upload_time": "2024-09-05T11:47:18",
"upload_time_iso_8601": "2024-09-05T11:47:18.870480Z",
"url": "https://files.pythonhosted.org/packages/1b/f6/496e6798f15801ec5b0369fc8126105e326ee0e7247be35f04ebed6cc4d1/adi_env_parser-1.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-05 11:47:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "adactafintech",
"github_project": "environment-parser",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "adi-env-parser"
}