Skip to main content

Syenv

Syenv is a lightweight tool whose role is to load environment variables for configuration purposes.
It has the advantage of offering some rather practical features to improve configuration management.

Table of content

Features

  • Environment variables importer;
  • Typing of environment variables;
  • Supports interpolation;
  • Prefix management to select a specific set of variables ;
  • Pattern selector for retrieve some specifics variables.

Requirements

  • python >= 3.8

Usage

Basics

Syenv is really easy to use, as the following example shows.
We consider the following environment file:

# .env
MY_APP_FTPS_PARAM_HOST=hostname
MY_APP_FTPS_PARAM_USER=username
MY_APP_FTPS_PARAM_PORT=int::22

MY_APP_STORAGE_DIR=pathlib.Path::storage
MY_APP_STUFF_STORAGE=pathlib.Path::{{MY_APP_STORAGE_DIR}}/stuffs

We can observe that the syntax of the values is a bit special. Syenv supports variable typing and interpolation (see Variables syntax).
Important: Syenv has no environment variable file loader. It is not its role to do this type of processing. So you are free to use the tool of your choice, like python-dotenv for example.

# __main__.py

import dotenv
import syenv

# In this example, we are using python-dotenv to load environment variables.
dotenv.load_dotenv()
env: syenv.Syenv = syenv.Syenv(prefix='MY_APP_')

# Now, we can access to our env vars!
print(env.FTPS_PARAM_HOST)
'''
>>> 'hostname' 
'''

print(env.STUFF_STORAGE)
''' 
>>> PosixPath('storage/stuffs') 
'''

We can notice that the prefix MY_APP_ has been substitued.

Advanced

Custom configuration class

We can use Syenv with a custom config class like the following example.

# config/config.py

import syenv

class Config(syenv.Syenv):
    def __init__(self, prefix: str = 'MY_APP_') -> None:
        super().__init__(prefix)

        self.another_var: str = 'Hey!'

    @property
    def ftp_uri(self) -> str:
        return f'ftp://{self.FTPS_PARAM_HOST}:{self.FTPS_PARAM_PORT}'

We can also instanciate the Config class in the __init__.py file for facilities...

# config/__init__.py

from config.config import Config

conf: Config = Config()

Then, considering the same env vars as above:

# __main__.py

from config import conf

print(conf.as_dict)
'''
>>> {'FTPS_PARAM_HOST': 'hostname',
     'FTPS_PARAM_USER': 'username',
     'FTPS_PARAM_PORT': 22,
     'STORAGE_DIR': PosixPath('storage'),
     'STUFF_STORAGE': PosixPath('storage/stuffs'),
     'another_var': 'Hey!'}
'''

print(conf.ftp_uri)
''' 
>>> 'ftp://hostname:22'
'''

Using pattern selector

Consider using this env file :

# .env
MY_APP_FTP_PARAM_HOST=hostname
MY_APP_FTP_PARAM_USER=username
MY_APP_FTP_PARAM_PASSWORD=secret

MY_APP_STORAGE_DIR=pathlib.Path::storage
MY_APP_STUFF_STORAGE=pathlib.Path::{{MY_APP_STORAGE_DIR}}/stuffs

Lets using the python FTP lib for example :

from ftplib import FTP
import dotenv
from syenv import Syenv

dotenv.load_dotenv()
env: Syenv = Syenv(prefix='MY_APP_')
ftp: FTP = FTP(**env.from_pattern('FTP_PARAM_', to_lower=True))

print(**env.from_pattern('FTP_PARAM_', to_lower=True))
'''
>>> {'host': 'hostname',
     'user': 'username',
     'password': 'secret'}
'''

Note: we can also keep the pattern string to the keys if we want.

Variables syntax

Foreword

Syenv reads system environment variables. The basic syntax is therefore the same.

<ENV_KEY>=<ENV_VALUE>

For example:

export APP_TEST_ENVVAR=some value

Will result of:

import syenv

env: syenv.Syenv = syenv.Syenv('APP_TEST_')

print(env.ENVVAR)
'''
>>> 'some value'
'''

Typing

We can specify the type of an environment variable in its value.

<ENV_KEY>=[<TYPE>:]<VALUE>

Note: notice that the typing is optionnal. If no type are specified, the str object is used.

For example:

export APP_TEST_TIMEOUT=int:30

Will result of:

import syenv

env: syenv.Syenv = syenv.Syenv('APP_TEST_')

print(type(env.TIMEOUT), env.TIMEOUT)
'''
>>> <class 'int'> 30
'''

Note: As you can guess, the Syenv typing support all standards python objects as well as any external librairies which are installed into the project.
The only important thing is to specify the correct package path during the writing of the value.

Interpolation

Syenv also support interpolation for better configuration managing.
The syntax is pretty generic:

{{<ENV_KEY>}}

For example:

export APP_TEST_ROOT_DIR=my_project
export APP_TEST_STATIC_DIR={{APP_TEST_ROOT_DIR}}/statics

Will result of:

import syenv

env: syenv.Syenv = syenv.Syenv('APP_TEST_')

print(env.ROOT_DIR)
'''
>>> 'my_project'
'''

print(env.STATIC_DIR)
'''
>>> 'my_project/statics'
'''

Note: We can also mixe the typing with interpolation. Just like the following example:

export APP_TEST_ROOT_DIR=pathlib.Path:my_project
export APP_TEST_STATIC_DIR=pathlib.Path:{{APP_TEST_ROOT_DIR}}/statics

Will result of:

...

print(env.ROOT_DIR)
'''
>>> PosixPath('my_project')
'''

print(env.STATIC_DIR)
'''
>>> PosixPath('my_project/statics')
'''

Metadata

Release files for syenv 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for syenv 1.2.1
File Size Uploaded
syenv-1.2.1.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for syenv 1.2.1
File Interpreter ABI Platform
syenv-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / syenv-1.2.1.tar.gz

Download URL syenv-1.2.1.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6f6113d1be92ab4436399a02fe00ddf1ecdd29314cf780461cbb024f82c4bfbe
BLAKE2b-256 checksum
How to use checksums
6f66336049411eaede7b29d99e9ced26e85f2c511ae7adc38c3a2ba1146976f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

Release files / syenv-1.2.1-py3-none-any.whl

Download URL syenv-1.2.1-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
425f66d22ffe7c90baaf5c941d26c0c2c4b693516edd502c54edb695e4b0900f
BLAKE2b-256 checksum
How to use checksums
b5f704e8802cbba5d0d6b24c9b7e6a66012be879647223d7c9c65844d8cd21a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page