diff options
author | Magnus Hagander | 2023-08-25 07:56:10 +0000 |
---|---|---|
committer | Magnus Hagander | 2023-08-25 07:56:10 +0000 |
commit | f755c6379d042f99bb7780c784047164062a977a (patch) | |
tree | d6c1e884ce5a3fc7ffe321a8b175fb8bc880c75c /tools/deploystatic/deploystatic.py | |
parent | db6c77d069d7522f8c211c5ce27a8e6e6ed5f2f5 (diff) |
Support YAML as well as JSON for context files
Previously only json as supported for the template integration for
context.json and context.override.d. This adds support for yaml as well
both for a context.yaml file, and for putting yaml files in
context.override.d. If both a json and yaml exists with the same name
(in the root or in the override), then the json will be loaded first and
then the yaml merged on top of it.
YAML has a few features that are really useful for the context file such
as comments and easier on the eyes multi-line string handling, but
fundamentally the handling is exactly the same.
If the `yaml` module is not importable, then yaml files are simply
ignored.
Diffstat (limited to 'tools/deploystatic/deploystatic.py')
-rwxr-xr-x | tools/deploystatic/deploystatic.py | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/tools/deploystatic/deploystatic.py b/tools/deploystatic/deploystatic.py index 04cd242f..ac92d3a9 100755 --- a/tools/deploystatic/deploystatic.py +++ b/tools/deploystatic/deploystatic.py @@ -27,6 +27,13 @@ from datetime import datetime, date, time import dateutil.parser +try: + import yaml + _has_yaml = True +except ImportError: + _has_yaml = False + + # # Some useful filters. We include them inline in this file to make it # standalone useful. @@ -179,9 +186,12 @@ class JinjaTarLoader(jinja2.BaseLoader): # Optionally load a JSON context -def load_context(jsondata): - if jsondata: - return json.loads(jsondata.decode('utf8')) +def load_context(data, filetype): + if data: + if filetype == 'json': + return json.loads(data.decode('utf8')) + else: + return yaml.safe_load(data.decode('utf8')) else: return {} @@ -373,7 +383,9 @@ if __name__ == "__main__": env.filters.update(global_filters) # If there is a context json, load it as well - context = load_context(source.readfile('templates/context.json')) + context = {} + deep_update_context(context, load_context(source.readfile('templates/context.json'), 'json')) + deep_update_context(context, load_context(source.readfile('templates/context.yaml'), 'yaml')) # Fetch the current git revision if this is coming out of a git repository context['githash'] = git_revision @@ -381,8 +393,8 @@ if __name__ == "__main__": # Load contexts in override directory, if any if source.isdir('templates/context.override.d'): for f in sorted(source.listfiles('templates/context.override.d')): - if f.endswith('.json'): - deep_update_context(context, load_context(source.readfile(os.path.join('templates/context.override.d', f)))) + if f.endswith('.json') or (_has_yaml and f.endswith('.yaml')): + deep_update_context(context, load_context(source.readfile(os.path.join('templates/context.override.d', f)), os.path.splitext(f)[1][1:])) knownfiles = [] knownfiles = _deploy_static(source, args.destpath) |