diff --git a/fgs/__main__.py b/fgs/__main__.py index 501fed3730199097468b42f9b159f3e318f53267..54bca9a1cd983cfc77afde2cf89964afc260af99 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 0000000000000000000000000000000000000000..007d969162aca88a5837460e1ee02bcce1679f3e --- /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 01c4631d04b679bed43b9f2a5fcea874083893a0..938f1e6610ba738e3bef8636cf46ef8659c4ce01 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 ebeb32c980a23c9c0a830cdc1e30490162757ff5..3b18f31412c25fd996868ae40b7a8054a17fa3ff 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)