Package: envresolver
EnvresolverEnvresolver is a Python library for parsingenvironment variablesto 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.EnvResolverclass provides a nifty way of parsing the current
environment according to given specifications.Install with pip:pip install envresolverSupported types (at the moment)strboolintfloatlistholding any of the supported typesJsonXMLdatetimeWhy 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.UsageAll variables to-be-resolved must be specified with a supported type when usingEnvResolver.EnvResolverthen 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:fromenvresolverimportEnvResolverr=EnvResolver()r.add_variable("var")r.resolve()If an environment variablevarwas present in the current environment, it can be accessed after resolving by a special
namespace membernsor by an explicit methodgetr.r.ns.var# Orr.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:fromenvresolverimportEnvResolverr=EnvResolver()r.get("var")User can also supply default values to all requests:r.add_variable("var2",default="default_value")# Orr.get("var2",default="default_value")Environment Variables with Type ConversionsAs stated before,EnvResolveralso supports automated type conversions for environment variables. Variable types can
be specified as shown:r.add_variable("var",t=int,default=-1)# Orr.get("var",t=int,default=-1)Let's imagine the current environment would hold the variablevarwith a value of"5". By runningEnvResolver.resolve,
it would be automatically parsed. However, if the environment variablevarwould hold an incompatible value,"_"as an example,
the parsing would fail andr.varwould hold the default value, if one was given:fromenvresolverimportEnvResolver# export var=5r=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# -1Advanced TypesEnvResolvercurrently 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:fromtypingimportListfromenvresolverimportEnvResolver# 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 inenvresolver.Types. Json and XML will be parsed using Pythons built-injsonandxmlmodules. Json parsing will output native python lists/dicts and XML results will be of typexml.etree.ElementTree.Element.
Here is an example on Json parsing:fromenvresolverimportEnvResolver,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-indatetimemodule. 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:importdatetimefromenvresolverimportEnvResolver# 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 timeParsing certain advaced types, such asdatetimeobjects 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 atEnvResolverinitialization or afterwards using
the methodsset_list_separatorandset_datetime_format.Custom TypesUsers can supplyEnvResolverwith 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:fromenvresolverimportEnvResolverclassMyData:def__init__(self,a,b):self.a=aself.b=bdefmy_data_converter(e:str):s=e.split(".")# Raise ValueError if the given# environment variable is in# wrong formatiflen(s)!=2:raiseValueError# Return parsed datareturnMyData(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")