src.util_mdtf module¶
Common functions and classes used in multiple places in the MDTF code.
-
class
src.util_mdtf.ConfigManager(*args, **kwargs)[source]¶ Bases:
src.util.Singleton
-
class
src.util_mdtf._PathManager(d, code_root=None, unittest=False)[source]¶ Bases:
src.util.NameSpaceSingletonholding root paths for the MDTF code. These are set in thepathssection ofdefaults.jsonc.
-
class
src.util_mdtf.TempDirManager(*args, **kwargs)[source]¶ Bases:
src.util.Singleton-
_prefix= 'MDTF_temp_'¶
-
-
class
src.util_mdtf.VariableTranslator(*args, **kwargs)[source]¶ Bases:
src.util.Singleton
-
src.util_mdtf.setenv(varname, varvalue, env_dict, verbose=0, overwrite=True)[source]¶ Wrapper to set environment variables.
-
class
src.util_mdtf._DoubleBraceTemplate(template)[source]¶ Bases:
string.TemplatePrivate class used by
append_html_template()to do string templating with double curly brackets as delimiters, since single brackets are also used in css.See `https://docs.python.org/3.7/library/string.html#string.Template`_ and `https://stackoverflow.com/a/34362892`__.
-
flags= 64¶
-
delimiter= '{{'¶
-
pattern= re.compile("\n \\{\\{(?: # match delimiter itself, but don't include it\n # Alternatives for what to do with string following delimiter:\n # case 1) text is an escaped double, re.VERBOSE)¶
-
-
src.util_mdtf.append_html_template(template_file, target_file, template_dict={}, create=True, append=True)[source]¶ Perform subtitutions on template_file and write result to target_file.
Variable substitutions are done with custom templating, replacing double curly bracket-delimited keys with their values in template_dict. For example, if template_dict is {‘A’: ‘foo’}, all occurrences of the string {{A}} in template_file are replaced with the string foo. Spaces between the braces and variable names are ignored.
Double-curly-bracketed strings that don’t correspond to keys in template_dict are ignored (instead of raising a KeyError.)
Double curly brackets are chosen as the delimiter to match the default syntax of, eg, django and jinja2. Using single curly braces leads to conflicts with CSS syntax.
- Parameters
template_file – Path to template file.
target_file – Destination path for result.
template_dict –
dictof variable name-value pairs. Both names and values must be strings.create – Boolean, default True. If true, create target_file if it doesn’t exist, otherwise raise an OSError.
append – Boolean, default True. If target_file exists and this is true, append the substituted contents of template_file to it. If false, overwrite target_file with the substituted contents of template_file.