Separate the handling of envvars by prefix

This commit is contained in:
Bálint Bokros
2018-02-09 14:48:37 +01:00
parent 2499c4759e
commit 4f181b8f09
6 changed files with 19 additions and 12 deletions

9
lib/envvars.py Normal file
View File

@ -0,0 +1,9 @@
from collections import namedtuple
from os import environ
def generate_namedtuple_from_prefixed_envvars(prefix: str, tuple_name: str):
envvars = {envvar.replace(prefix, '', 1): environ.get(envvar)
for envvar in environ.keys()
if envvar.startswith(prefix)}
return namedtuple(tuple_name, envvars)(**envvars)

0
lib/tao/__init__.py Normal file
View File

View File

@ -0,0 +1 @@
from .envvars import *

View File

@ -0,0 +1,3 @@
from envvars import generate_namedtuple_from_prefixed_envvars
taoenv = generate_namedtuple_from_prefixed_envvars('AVATAO_', 'taoenvtuple')

View File

@ -1,10 +1,3 @@
from os import environ, getenv
from collections import namedtuple
from envvars import generate_namedtuple_from_prefixed_envvars
TFW_PREFIX = 'TFW_'
tfwenvvars = {envvar.replace(TFW_PREFIX, '', 1): environ.get(envvar)
for envvar in environ.keys()
if envvar.startswith(TFW_PREFIX)}
tfwenv = namedtuple('tfwenvtuple', tfwenvvars)(**tfwenvvars)
AVATAO_SECRET = getenv('AVATAO_SECRET', 'secret')
tfwenv = generate_namedtuple_from_prefixed_envvars('TFW_', 'tfwenvtuple')