From 8817f991a59d0071530b094cb8ba01af64ebd3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 1 Jun 2018 14:00:44 +0200 Subject: [PATCH] Use power of LazyInitialise to murder TFW/TAOENV global state --- lib/envvars/__init__.py | 21 ++++++++++++++++----- lib/tao/config/envvars.py | 4 ++-- lib/tfw/config/envvars.py | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) 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