Skip to content

Commit aced52b

Browse files
committed
Avoid a global by placing the "global" template store in request context.
1 parent 0e3d381 commit aced52b

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

mig/lib/templates/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
_all_template_dirs = [
4747
TEMPLATES_DIR,
4848
]
49-
_global_store = None
5049

5150

5251
def _clear_global_store():
@@ -147,15 +146,16 @@ def populated(template_dirs, cache_dir=None, context=None):
147146

148147

149148
def init_global_templates(configuration, _templates_dirs=template_dirs):
150-
global _global_store
149+
_context = configuration.context()
151150

152-
if _global_store is not None:
153-
return _global_store
151+
try:
152+
return configuration.context(namespace='templates')
153+
except KeyError as exc:
154+
pass
154155

155-
_global_store = TemplateStore.populated(
156+
store = TemplateStore.populated(
156157
_templates_dirs(),
157158
cache_dir=cache_dir(),
158159
context=_FormatContext(configuration)
159160
)
160-
161-
return _global_store
161+
return configuration.context_set(store, namespace='templates')

mig/shared/configuration.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ def __init__(self, config_file, verbose=False, skip_log=False,
716716
disable_auth_log=False):
717717
self.config_file = config_file
718718
self.mig_server_id = None
719+
self._context = None
719720

720721
configuration_options = copy.deepcopy(_CONFIGURATION_DEFAULTS)
721722

@@ -727,6 +728,23 @@ def __init__(self, config_file, verbose=False, skip_log=False,
727728
disable_auth_log=disable_auth_log,
728729
_config_file=config_file)
729730

731+
def context(self, namespace=None):
732+
if self._context is None:
733+
self._context = {}
734+
if namespace is None:
735+
return self._context
736+
try:
737+
return self._context[namespace]
738+
except KeyError:
739+
raise
740+
741+
def context_set(self, value, namespace=None):
742+
assert namespace is not None
743+
744+
context = self.context()
745+
context[namespace] = value
746+
return value
747+
730748
def reload_config(self, verbose, skip_log=False, disable_auth_log=False,
731749
_config_file=None):
732750
"""Re-read and parse configuration file. Optional skip_log arg

tests/test_mig_shared_configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _is_method(value):
4141

4242
def _to_dict(obj):
4343
return {k: v for k, v in inspect.getmembers(obj)
44-
if not (k.startswith('__') or _is_method(v))}
44+
if not (k.startswith('_') or _is_method(v))}
4545

4646

4747
class MigSharedConfiguration(MigTestCase):

tests/test_mig_wsgibin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050

5151

5252
def _force_test_templates(configuration):
53-
from mig.lib.templates import init_global_templates, _clear_global_store
54-
_clear_global_store()
53+
from mig.lib.templates import init_global_templates
5554
test_tmpl_dir = os.path.join(TEST_DATA_DIR, 'templates')
55+
# populate current context with a template store backed onto test templates
5656
init_global_templates(configuration, _templates_dirs=lambda: [test_tmpl_dir])
5757

5858

0 commit comments

Comments
 (0)