From f7e7da275743c1a2df9cdd64728507adc1c687ff Mon Sep 17 00:00:00 2001 From: Jake <j.vondoemming@stud.uni-goettingen.de> Date: Tue, 9 Aug 2022 01:45:46 +0200 Subject: [PATCH] added common.combine() --- fgs/__main__.py | 10 +++++++--- fgs/common.py | 14 ++++++++++++++ fgs/datatypes.py | 5 +++-- fgs/writer.py | 6 ++++-- 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 fgs/common.py diff --git a/fgs/__main__.py b/fgs/__main__.py index 501fed3..54bca9a 100644 --- a/fgs/__main__.py +++ b/fgs/__main__.py @@ -5,6 +5,7 @@ import os import sys +import common import datatypes import reader import generator @@ -19,11 +20,13 @@ def main(): print("Hello World") config = {} with open('../config.json') as f: - config.update(json.loads(f.read())) + config = common.combine(config, json.loads(f.read())) with open('../lang.json') as f: - config['lang'] = json.loads(f.read()) + if 'lang' not in config: + config['lang'] = {} + config['lang'] = common.combine(config['lang'], json.loads(f.read())) with open(CONTENT_DIR + '/config.json') as f: - config.update(json.loads(f.read())) + config = common.combine(config, json.loads(f.read())) print(config) factories = {} @@ -62,6 +65,7 @@ def main(): + def read_static_dir(directory, static_files, subpath = []): print("static_dir: " + directory); for filename in os.listdir(directory): diff --git a/fgs/common.py b/fgs/common.py new file mode 100644 index 0000000..007d969 --- /dev/null +++ b/fgs/common.py @@ -0,0 +1,14 @@ + +def combine(old, new): + if type(old) != type(new): + raise Exception("Cannot combine different types: ", type(old), type(new), old, new) + if isinstance(old, dict): + res = old.copy() + for key, value in new.items(): + if key in res: + res[key] = combine(res[key], value) + else: + res[key] = value + return res + else: + return new diff --git a/fgs/datatypes.py b/fgs/datatypes.py index 01c4631..938f1e6 100644 --- a/fgs/datatypes.py +++ b/fgs/datatypes.py @@ -1,6 +1,8 @@ import datetime from dateutil import parser as dtparser +import common + class DictConverter: def __init__(self, factories): @@ -106,8 +108,7 @@ class Page: if self.lang == deflang: return self._metadata defpage = self._factories['page'].get(self.slug, deflang) - res = defpage.metadata.copy() - res.update(self._metadata) + res = common.combine(defpage.metadata, self._metadata) return res metadata = property(get_metadata, None, None) diff --git a/fgs/writer.py b/fgs/writer.py index ebeb32c..3b18f31 100644 --- a/fgs/writer.py +++ b/fgs/writer.py @@ -2,6 +2,8 @@ from jinja2 import Environment, FileSystemLoader, StrictUndefined import os +import common + class Writer: def __init__(self, config, context, output_dir, theme_dir): @@ -35,7 +37,7 @@ class Writer: # render template context = {} - context.update(self.context) + context = common.combine(context, self.context) context["config"] = localized_config context["theme"] = localized_config['theme'] context["template"] = template @@ -43,7 +45,7 @@ class Writer: context["l"] = lang # current language context["path"] = path context["siteurl"] = siteurl - context.update(extra_context) + context = common.combine(context, extra_context) #print("template: ", template) #print("path: ", path) #print("lang: ", lang) -- GitLab