summaryrefslogtreecommitdiff
path: root/postgresqleu/confreg/contextutil.py
diff options
context:
space:
mode:
authorMagnus Hagander2020-02-09 15:59:32 +0000
committerMagnus Hagander2020-02-09 15:59:32 +0000
commitc26521cfd921e7fe251ed065fe7712ef3472c7aa (patch)
tree4ecf6e23264490c86b0806a88ae33421365d33b1 /postgresqleu/confreg/contextutil.py
parent19ea4a3a0b94efaf459ba461d5907e1cb0eaf23c (diff)
Replace context.override.json with a directory context.override.d
Instead of loading a single override file in context.override.json, load every file called *.json in the directory context.override.d. This is useful for example for the case of downloading the schedule data into a file and then using it for testing, instead of having to "stitch" it into place in an existing file. NOTE! For test cases using context.override.json today, just create a context.override.d and move the existing override file into this directory, and things should work like before again. NOTE! As context.override.json is not supposed to be committed to the git repository, this should not affect any production installs, but will affect most local test setups.
Diffstat (limited to 'postgresqleu/confreg/contextutil.py')
-rw-r--r--postgresqleu/confreg/contextutil.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/postgresqleu/confreg/contextutil.py b/postgresqleu/confreg/contextutil.py
new file mode 100644
index 00000000..a0f7e480
--- /dev/null
+++ b/postgresqleu/confreg/contextutil.py
@@ -0,0 +1,59 @@
+import os
+import json
+import logging
+import copy
+
+
+# XXX: keep in sync with deploystatic.py!
+def deep_update_context(target, source):
+ for k, v in source.items():
+ if type(v) == dict:
+ # If this is a dict stored in the dict
+ if k not in target:
+ # Target didn't have it, so copy it over
+ target[k] = copy.deepcopy(v)
+ elif type(target[k]) != dict:
+ # Target had something but it's not a dict, so overwrite it
+ target[k] = copy.deepcopy(v)
+ else:
+ deep_update_context(target[k], v)
+ else:
+ target[k] = copy.copy(v)
+
+
+def _load_context_file(filename):
+ try:
+ with open(filename, encoding='utf8') as f:
+ return json.load(f)
+ except ValueError as e:
+ # Malformatted JSON -- pass it through as an exception
+ raise
+ except Exception:
+ # Any other error, just ignore it (?)
+ return {}
+
+
+def load_base_context(rootdir):
+ if os.path.isfile(os.path.join(rootdir, 'templates/context.json')):
+ return _load_context_file(os.path.join(rootdir, 'templates/context.json'))
+ return {}
+
+
+def load_override_context(rootdir):
+ # Load contexts in override directory, if any
+ c = {}
+ if os.path.isdir(os.path.join(rootdir, 'templates/context.override.d')):
+ for fn in sorted(os.listdir(os.path.join(rootdir, 'templates/context.override.d'))):
+ if fn.endswith('.json'):
+ try:
+ with open(os.path.join(rootdir, 'templates/context.override.d', fn)) as f:
+ deep_update_context(c, json.load(f))
+ except Exception as e:
+ logging.getLogger(__name__).warning(
+ 'Failed to load context file {}: {}'.format(os.path.join(rootdir, 'templates/context.override.d', fn), e)
+ )
+ return c
+
+
+def update_with_override_context(context, rootdir):
+ deep_update_context(context, load_override_context(rootdir))