diff --git a/lib/envvars/__init__.py b/lib/envvars/__init__.py index 712a926..11faa64 100644 --- a/lib/envvars/__init__.py +++ b/lib/envvars/__init__.py @@ -4,9 +4,20 @@ from collections import namedtuple 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) - for envvar in environ.keys() - if envvar.startswith(prefix)} - return namedtuple(tuple_name, envvars)(**envvars) + +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() + if envvar.startswith(self._prefix)} + return namedtuple(self._tuple_name, envvars)(**envvars) diff --git a/lib/tao/config/envvars.py b/lib/tao/config/envvars.py index da46222..260d057 100644 --- a/lib/tao/config/envvars.py +++ b/lib/tao/config/envvars.py @@ -1,6 +1,6 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # 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 diff --git a/lib/tfw/config/envvars.py b/lib/tfw/config/envvars.py index f239d5c..ef590c6 100644 --- a/lib/tfw/config/envvars.py +++ b/lib/tfw/config/envvars.py @@ -1,6 +1,6 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # 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