mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 10:31:31 +00:00
Use power of LazyInitialise to murder TFW/TAOENV global state
This commit is contained in:
parent
06c2fc97ad
commit
8817f991a5
@ -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:
|
||||||
for envvar in environ.keys()
|
def __init__(self, prefix, tuple_name):
|
||||||
if envvar.startswith(prefix)}
|
self._prefix = prefix
|
||||||
return namedtuple(tuple_name, envvars)(**envvars)
|
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()
|
||||||
|
if envvar.startswith(self._prefix)}
|
||||||
|
return namedtuple(self._tuple_name, envvars)(**envvars)
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user