Use power of LazyInitialise to murder TFW/TAOENV global state

This commit is contained in:
Kristóf Tóth 2018-06-01 14:00:44 +02:00
parent 06c2fc97ad
commit 8817f991a5
3 changed files with 20 additions and 9 deletions

View File

@ -4,9 +4,20 @@
from collections import namedtuple from collections import namedtuple
from os import environ from os import environ
from tfw.decorators import LazyInitialise
def prefixed_envvars_to_namedtuple(prefix: str, tuple_name: str):
envvars = {envvar.replace(prefix, '', 1): environ.get(envvar) class LazyEnvironment:
def __init__(self, prefix, tuple_name):
self._prefix = prefix
self._tuple_name = tuple_name
@LazyInitialise
def environment(self):
return self.prefixed_envvars_to_namedtuple()
def prefixed_envvars_to_namedtuple(self):
envvars = {envvar.replace(self._prefix, '', 1): environ.get(envvar)
for envvar in environ.keys() for envvar in environ.keys()
if envvar.startswith(prefix)} if envvar.startswith(self._prefix)}
return namedtuple(tuple_name, envvars)(**envvars) return namedtuple(self._tuple_name, envvars)(**envvars)

View File

@ -1,6 +1,6 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from envvars import prefixed_envvars_to_namedtuple from envvars import LazyEnvironment
TAOENV = prefixed_envvars_to_namedtuple('AVATAO_', 'taoenvtuple') TAOENV = LazyEnvironment('AVATAO_', 'taoenvtuple').environment

View File

@ -1,6 +1,6 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from envvars import prefixed_envvars_to_namedtuple from envvars import LazyEnvironment
TFWENV = prefixed_envvars_to_namedtuple('TFW_', 'tfwenvtuple') TFWENV = LazyEnvironment('TFW_', 'tfwenvtuple').environment