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

__init__(cli_obj=None, pod_info_tuple=None, unittest=False)[source]

Initialize self. See help(type(self)) for accurate signature.

class src.util_mdtf._PathManager(d, code_root=None, unittest=False)[source]

Bases: src.util.NameSpace

Singleton holding root paths for the MDTF code. These are set in the paths section of defaults.jsonc.

__init__(d, code_root=None, unittest=False)[source]

Initialize self. See help(type(self)) for accurate signature.

parse(d, paths_to_parse=[], env=None)[source]
_init_path(key, d, env=None)[source]
model_paths(case, overwrite=False)[source]
pod_paths(pod, case)[source]
class src.util_mdtf.TempDirManager(*args, **kwargs)[source]

Bases: src.util.Singleton

_prefix = 'MDTF_temp_'
__init__(temp_root=None)[source]

Initialize self. See help(type(self)) for accurate signature.

make_tempdir(hash_obj=None)[source]
rm_tempdir(path)[source]
cleanup()[source]
exception src.util_mdtf.ConventionError[source]

Bases: Exception

class src.util_mdtf.VariableTranslator(*args, **kwargs)[source]

Bases: src.util.Singleton

__init__(unittest=False, verbose=0)[source]

Initialize self. See help(type(self)) for accurate signature.

toCF(convention, varname_in)[source]
fromCF(convention, varname_in)[source]
src.util_mdtf.get_available_programs(verbose=0)[source]
src.util_mdtf.setenv(varname, varvalue, env_dict, verbose=0, overwrite=True)[source]

Wrapper to set environment variables.

Parameters
  • varname (str) – Variable name to define

  • varvalue – Value to assign. Coerced to type str before being set.

  • env_dict (dict) – Copy of

  • verbose (int, optional) – Logging verbosity level. Default 0.

  • overwrite (bool) – If set to False, do not overwrite the values of previously-set variables.

src.util_mdtf.check_required_envvar(*varlist)[source]
src.util_mdtf.check_required_dirs(already_exist=[], create_if_nec=[], verbose=1)[source]
src.util_mdtf.bump_version(path, new_v=None, extra_dirs=[])[source]
class src.util_mdtf._DoubleBraceTemplate(template)[source]

Bases: string.Template

Private 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_dictdict of 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.