Skip to content

Commit

Permalink
Handle template themesConfig section in tenant config template
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitblanc authored Feb 19, 2024
1 parent c6f065f commit 07e4e6f
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions src/config_generator/config_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ def __init__(self, config, logger, config_file_dir):
self.tenant = config.get('config', {}).get('tenant', 'default')
self.logger.debug("Using tenant '%s'" % self.tenant)

# Handle themesConfig in tenantConfig.json
themes_config = config.get("themesConfig", None)
if isinstance(themes_config, str):
try:
if not os.path.isabs(themes_config):
themes_config = os.path.join(config_file_dir, themes_config)
with open(themes_config, encoding='utf-8') as f:
config["themesConfig"] = json.load(f)
except:
msg = "Failed to read themes configuration %s" % themes_config
self.logger.error(msg)
raise Exception(msg)
elif not isinstance(themes_config, dict):
msg = "Missing or invalid themes configuration in tenantConfig.json"
self.logger.error(msg)
raise Exception(msg)

if config.get('template', None):
config_template_path = config.get('template')
if not os.path.isabs(config_template_path):
Expand All @@ -145,11 +162,31 @@ def __init__(self, config, logger, config_file_dir):
config_template_data = fh.read().replace('$tenant$', self.tenant)
config_template = json.loads(config_template_data, object_pairs_hook=OrderedDict)

# Handle themesConfig if it has also been templated
themes_config_template = config_template.get("themesConfig", None)
if isinstance(themes_config_template, str):
try:
if not os.path.isabs(themes_config_template):
themes_config_template_path = os.path.join(os.path.dirname(config_template_path), themes_config_template)
with open(themes_config_template_path, encoding='utf-8') as f:
config_template["themesConfig"] = json.load(f)
except:
msg = "Failed to read themes configuration %s" % themes_config_template_path
self.logger.error(msg)
raise Exception(msg)
elif not isinstance(themes_config_template, dict):
msg = "No themes configuration in templated tenantConfig.json"
self.logger.debug(msg)
raise Exception(msg)

config_services = dict(map(lambda entry: (entry["name"], entry), config.get("services", [])))
config_template_services = dict(map(lambda entry: (entry["name"], entry), config_template.get("services", [])))

config = deepmerge.always_merger.merge(config_template, config)
config["services"] = list(deepmerge.always_merger.merge(config_template_services, config_services).values())

# Get themesConfig from config because it could have been merged with a template
themes_config = config.get("themesConfig")
except Exception as e:
self.logger.warning("Failed to merge config template %s: %s." % (config_template_path, str(e)))

Expand Down Expand Up @@ -199,23 +236,6 @@ def __init__(self, config, logger, config_file_dir):
self.logger.error(msg)
raise Exception(msg)

themes_config = config.get("themesConfig", None)

if isinstance(themes_config, str):
try:
if not os.path.isabs(themes_config):
themes_config = os.path.join(config_file_dir, themes_config)
with open(themes_config, encoding='utf-8') as f:
themes_config = json.load(f)
except:
msg = "Failed to read themes configuration %s" % themes_config
self.logger.error(msg)
raise Exception(msg)
elif not isinstance(themes_config, dict):
msg = "Missing or invalid themes configuration in tenantConfig.json"
self.logger.error(msg)
raise Exception(msg)

# Preprocess QGS projects
self.preprocess_qgs_projects(generator_config, self.tenant)

Expand Down

0 comments on commit 07e4e6f

Please sign in to comment.