Esempio n. 1
0
def instantiate(template, values, destination):
    """
    Instantiate a templated file with a set of values and
    place it in a target destination.

    :param template: the path to the templated file
    :type template: ``str``
    :param values: the context dict to be used when
                   instantiating the templated file
    :type values: ``dict``
    :param destination: the path to the destination directory/file to
                        write the instantiated templated file to
    :type destination: ``str``

    :return: ``None``
    :rtype: ``NoneType``
    """
    makedirs(os.path.dirname(destination))
    if os.path.isdir(destination):
        destination = os.path.join(destination, os.path.basename(template))
    with open(destination, "w") as target:
        with open(template, "r") as source:
            try:
                tmplt = Template(source.read())
                target.write(tmplt.substitute(values))
            except Exception as exc:
                raise Exception(
                    "On reading/writing template: {} - of file {}".format(
                        exc, template))
Esempio n. 2
0
 def __init__(self, driver, value):
     """
     Create a new context value
     """
     self.driver, self.value = driver, Template(value)