2.9. fill_jinja_template module
This utility fills in a user-supplied Jinja template from either a YAML file, or command line arguments.
The user configuration file and commandline arguments should be YAML-formatted. This script will support a single- or two-level YAML config file. For example:
- expt1:
date_first_cycl: !datetime 2019043000 date_last_cycl: !datetime 2019050100 cycl_freq: !!str 12:00:00
- expt2:
date_first_cycl: !datetime 2019061012 date_last_cycl: !datetime 2019061212 cycl_freq: !!str 12:00:00
date_first_cycl: !datetime 2019043000 date_last_cycl: !datetime 2019050100 cycl_freq: !!str 12:00:00
In Case 1, provide the name of the file and the section title, e.g. expt2, to the -c command line argument. Only provide the name of the file in -c option if it’s configured as in Case 2 above.
Supported YAML Tags:
The script supports additional YAML configuration tags.
- !datetime Converts an input string formatted as YYYYMMDDHH[mm[ss]] to a
Python datetime object
!join Uses os.path.join to join a list as a path.
Expected behavior:
The template file is required. Script fails if not provided.
Command line arguments in the -u setting override the -c settings.
- fill_jinja_template.config_exists(arg)[source]
Checks whether the config file exists and if it contains the input section. Returns the config as a Python dict.
- fill_jinja_template.file_exists(arg)[source]
Checks whether a file exists, and returns the path if it does.
- fill_jinja_template.fill_jinja_template(argv)[source]
Loads a Jinja template, determines its necessary undefined variables, retrives them from user supplied settings, and renders the final result.
- fill_jinja_template.load_config(arg)[source]
Check to ensure that the provided config file exists. If it does, load it with YAML’s safe loader and return the resulting dict.
- fill_jinja_template.load_str(arg)[source]
Load a dict string safely using YAML. Return the resulting dict.
- fill_jinja_template.parse_args(argv)[source]
Function maintains the arguments accepted by this script. Please see Python’s argparse documenation for more information about settings of each argument.
- fill_jinja_template.path_ok(arg)[source]
Check whether the path to the file exists, and is writeable. Return the path if it passes all checks, otherwise raise an error.
- fill_jinja_template.to_datetime(loader, node)[source]
Converts a date string with format YYYYMMDDHH[MM[SS]] to a datetime object.
- fill_jinja_template.update_dict(dest, newdict, quiet=False)[source]
Overwrites all values in dest dictionary section with key/value pairs from newdict. Does not support multi-layer update.
Turn off print statements with quiet=True.
- Input:
dest A dict that is to be updated. newdict A dict containing sections and keys corresponding to
those in dest and potentially additional ones, that will be used to update the dest dict.
quiet An optional boolean flag to turn off output.
- Output:
None
- Result:
The dest dict is updated in place.