Pythonic parsing of environment variables
Project description
Envresolver
Envresolver is a Python library for parsing environment variables
to common Python datatypes. Environment variables are only accessible as pure text (string) typed variables and thus need
some manipulation to transform into any other types. EnvResolver
class provides a nifty way of parsing the current
environment according to given specifications.
Install with pip:
pip install envresolver
Supported types (at the moment)
str
bool
int
float
list
holding any of the supported typesJson
XML
datetime
Why Envresolver?
Envresolver is a lightweight alternative to other environment parsers and customizable by-design. Adding support for new types is ultra-simplified and does not need any source modifications of the library itself.
Usage
All variables to-be-resolved must be specified with a supported type when using EnvResolver
. EnvResolver
then inspects
all the specified variables and tries to parse an environment variable with the same name to the given type format. For example,
to parse simple string values:
from envresolver import EnvResolver
r = EnvResolver()
r.add_variable("var")
r.resolve()
If an environment variable var
was present in the current environment, it can be accessed after resolving by a special
namespace member ns
or by an explicit method getr
.
r.ns.var
# Or
r.getr("var")
Additionally, variables can also be fetched from the current environment without pre-calculated resolving. This is suitable for simple variables and values that can change constantly:
from envresolver import EnvResolver
r = EnvResolver()
r.get("var")
User can also supply default values to all requests:
r.add_variable("var2", default="default_value")
# Or
r.get("var2", default="default_value")
Environment Variables with Type Conversions
As stated before, EnvResolver
also supports automated type conversions for environment variables. Variable types can
be specified as shown:
r.add_variable("var", t=int, default=-1)
# Or
r.get("var", t=int, default=-1)
Let's imagine the current environment would hold the variable var
with a value of "5"
. By running EnvResolver.resolve
,
it would be automatically parsed. However, if the environment variable var
would hold an incompatible value, "_"
as an example,
the parsing would fail and r.var
would hold the default value, if one was given:
from envresolver import EnvResolver
# export var=5
r = EnvResolver()
r.add_variable("var", t=int, default=-1)
r.resolve()
r.ns.var # 5
# export var=_
r = EnvResolver()
r.add_variable("var", t=int, default=-1)
r.resolve()
r.ns.var # -1
Advanced Types
EnvResolver
currently supports also some more advanced types of variables, such as lists, Json and XML. Lists have full support
of type hinting and will try to convert all elements accordingly:
from typing import List
from envresolver import EnvResolver
# export my_list="1,2,3,4"
r = EnvResolver()
r.add_variable("my_list", t=List[int])
r.resolve()
r.ns.my_list # [1, 2, 3, 4]
Json and XML are supported via custom type notations stored in envresolver.Types
. Json and XML will be parsed using Pythons built-in
json
and xml
modules. Json parsing will output native python lists/dicts and XML results will be of type xml.etree.ElementTree.Element
.
Here is an example on Json parsing:
from envresolver import EnvResolver, Types
# export json='{"key": "val"}'
r = EnvResolver()
r.add_variable("json", t=Types.Json)
r.resolve()
r.ns.json # {"key": "val"}
Date objects are supported via Pythons built-in datetime
module. User can specify in which format the date strings are expected, with the default being
%Y-%m-%d %H:%M:%S
. Here is an example:
import datetime
from envresolver import EnvResolver
# export mydate="2021-01-01 12:34:56"
r = EnvResolver()
r.add_variable("mydate", datetime.datetime)
r.resolve()
r.ns.mydate # datetime.datetime -object with the correct time
Parsing certain advaced types, such as datetime
objects or lists, relies on additional information
regarding the data format. List parsing needs to know the list separator character and datetime conversions
rely on certain date formats. These can be configured either at EnvResolver
initialization or afterwards using
the methods set_list_separator
and set_datetime_format
.
Custom Types
Users can supply EnvResolver
with custom parsers as well as override existing ones. Below is an example of using a custom parser
for reading data into a user-defined class:
from envresolver import EnvResolver
class MyData:
def __init__(self, a, b):
self.a = a
self.b = b
def my_data_converter(e: str):
s = e.split(".")
# Raise ValueError if the given
# environment variable is in
# wrong format
if len(s) != 2:
raise ValueError
# Return parsed data
return MyData(a=s[0], b=s[1])
# export data="john.smith"
r = EnvResolver()
r.add_converter(MyData, my_data_converter)
r.add_variable("data", t=MyData)
r.resolve()
r.ns.data # MyData(a = "john", b = "smith")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file envresolver-1.0.0.tar.gz
.
File metadata
- Download URL: envresolver-1.0.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a33187492c085005e0261d95113c3c7025b54d1a22a994788481c75141529edd |
|
MD5 | e2a353bf0472d093d6d47c7aea39fb53 |
|
BLAKE2b-256 | e4cc3a179991c44147a72d3d4212cc56eeae5f8e936417664eaa61a43d718731 |
File details
Details for the file envresolver-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: envresolver-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a14011d2516bb6e8c7502ac3f1def44e6d8a0dd8ea1fcb710ea6778a0f9a5f74 |
|
MD5 | 25458b88e89f59c4339e89931dd5b1d7 |
|
BLAKE2b-256 | 6ba291348da3ad28d72d6708316184ad565c47044493839dff1fe2177c207cf1 |