Skip to content

Templates

Functions

script_template(template_name)

Load a template of the given name, and fill it in based on the Fabric environment dictionary, storing the result in deploy/.scripts/job-name.sh job-name is loaded from the environment dictionary. Return value is the path of the generated script.

Source code in fabsim/deploy/templates.py
@beartype
def script_template(template_name: str) -> str:
    """
    Load a template of the given name, and fill it in based on the Fabric
    environment dictionary, storing the result in deploy/.scripts/job-name.sh
    job-name is loaded from the environment dictionary.
    Return value is the path of the generated script.
    """
    result = script_template_content(template_name)
    return script_template_save_temporary(result)

template(pattern, number_of_iterations=1)

Low-level templating function, insert env variables into any string pattern - number_of_iterations can be adjusted to allow recurring templating using a single function call.

Source code in fabsim/deploy/templates.py
@beartype
def template(pattern: str, number_of_iterations: Optional[int] = 1) -> str:
    """
    Low-level templating function, insert env variables into any string pattern
        - number_of_iterations can be adjusted to allow recurring
                templating using a single function call.
    """
    # print(env.flee_location)
    try:
        for i in range(0, number_of_iterations):
            # template = Template(pattern).substitute(env)
            template = Template(pattern).safe_substitute(env)
            pattern = template

        return template
    except KeyError as err:
        print("ORIGINAL PATTERN:\n\n{}".format(pattern))
        print(
            "SAFELY SUBSTITUTED PATTERN:\n\n{}".format(
                Template(pattern).safe_substitute(env)
            )
        )
        print("ERROR: FABSIM_TEMPLATE_KEYERROR")
        print(
            "Template variables were not found in FabSim env dictionary: \
            These variables need to be added, with a default value set."
        )
        print(
            "FabSim performed a 'safe_substite' and print the original \
            template and the partially substituted one (both are given above \
            this message). Variables that are missing in the env dictionary \
            will be displayed unsubstituted in the output text. FabSim will \
            now terminate as these errors would result in unpredictable \
            behavior otherwise."
        )

        sys.tracebacklimit = 0
        raise KeyError
        # sys.exit()