Skip to main content

The library for creating nested environ configuration through composition

Project description

environ composition

The library for parsing environ configuration and creating nested configuration classes through composition.

  • EnvConfigParser is designed to parse environment variables and a .env file to create a structured configuration.
  • EnvConfig takes the parsed environment variables (a dictionary) and creates an object with attributes matching the keys.

Installation

pip install environ-composition

Import

from environ_composition import EnvConfigParser

Usage

config = EnvConfigParser(dotenv_path='path_to/.env', separator='__').parse()

In EnvConfigParser each environment variable name goes into a lowercase attribute of EnvConfig instance.

Adding some separator (i.g. double underscores "__") to the variable name adds a level of nesting to the config.

VAR1=value1
VAR2__NESTED_VAR1=nested_value1
config.var1 = "value1" 
config.var2.nested_var1 = "nested_value1"

When specifying a template, the parser updates the values in the template.

Without a template, the parser returns the config in EnvConfig instance.

class NestedTemplate:
    def __init__(self):
        self.nested_var1 = None

class Template:
    def __init__(self):
        self.var1 = None
        self.var2 = NestedTemplate()

template = Template()

parser = EnvConfigParser('path_to/.env')

config = parser.parse(template)

The structure and nesting levels of the template must match the names in the environment variable.

Mismatched names of attributes and private attributes in the template are not updated.

Parsing values

The ast library and the literal_eval function are used for parsing string values.

Standard data types are available: str, int, float, bool, list, tuple, set, dict, etc.

VAR1=['string', 1, 1.01, True]
VAR2__NESTED_VAR1={"string": 1}

For variables with null string values ('', 'None', 'null'), None is assigned.

VAR1=
VAR2=None
VAR3=null

The use_literal_eval flag in the parse method determines to use or not parsing.

Type validation

If there is a type annotation for class attributes, the parser validates the assigned values.

If there is a type annotation for instance attributes, the parser does not validate.

Validation is not applied to nested config instances.

class NestedTemplate:
    nested_var1: str = None

class Template:
    var1: str = None
    var2: int = None
    var3: NestedTemplate = NestedTemplate() # it will not be validated

    def __init__(self):
        self.var4: int = None  # it will not be validated
        self.var5: NestedTemplate = NestedTemplate() # it will not be validated

The use_type_validation flag in the parse method determines to use or not type validation. Validation only works if use_literal_eval = True.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

environ_composition-1.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

environ_composition-1.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file environ_composition-1.1.0.tar.gz.

File metadata

  • Download URL: environ_composition-1.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.1

File hashes

Hashes for environ_composition-1.1.0.tar.gz
Algorithm Hash digest
SHA256 719a321330a8f249b9363baeccec829e9efee536b458f7cb7572472391532048
MD5 9df52a0d862def59ddf7b4f09af5af73
BLAKE2b-256 1ea4621696090f70d4bd4a59dc63ce50cf5cb50dd8a36ab1bfa8a63f9ca9dbdd

See more details on using hashes here.

File details

Details for the file environ_composition-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for environ_composition-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0cfff99f280270603dad434ae14bdc3019554f8d66756d1213e2f185ff4153bb
MD5 9130ebc7f8f0e714effcbf95b11a514d
BLAKE2b-256 cf493419560ac8aa826aa387cb51fe395650bb39c8e72efcba1917cf5f30dbfd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page