Ejemplo n.º 1
0
def content_processor_jinja2(item):
    try:
        from jinja2 import Environment, FileSystemLoader
    except ImportError:
        raise TemplateError(
            _("Unable to load Jinja2 (required to render {item}). "
              "You probably have to install it using `pip install Jinja2`.").
            format(item=item.id))

    loader = FileSystemLoader(searchpath=[item.item_data_dir, item.item_dir])
    env = Environment(loader=loader)

    template = env.from_string(item._template_content)

    io.debug("{node}:{bundle}:{item}: rendering with Jinja2...".format(
        bundle=item.bundle.name,
        item=item.id,
        node=item.node.name,
    ))
    start = datetime.now()
    try:
        content = template.render(item=item,
                                  bundle=item.bundle,
                                  node=item.node,
                                  repo=item.node.repo,
                                  **item.attributes['context'])
    except FaultUnavailable:
        raise
    except Exception as e:
        io.debug("".join(format_exception(*exc_info())))
        raise TemplateError(
            _("Error while rendering template for {node}:{bundle}:{item}: {error}"
              ).format(
                  bundle=item.bundle.name,
                  error=e,
                  item=item.id,
                  node=item.node.name,
              ))
    duration = datetime.now() - start
    io.debug("{node}:{bundle}:{item}: rendered in {time}s".format(
        bundle=item.bundle.name,
        item=item.id,
        node=item.node.name,
        time=duration.total_seconds(),
    ))
    return content.encode(item.attributes['encoding'])
Ejemplo n.º 2
0
def content_processor_mako(item):
    template = Template(
        item._template_content.encode('utf-8'),
        input_encoding='utf-8',
        lookup=TemplateLookup(directories=[item.item_data_dir, item.item_dir]),
        output_encoding=item.attributes['encoding'],
    )
    io.debug("{node}:{bundle}:{item}: rendering with Mako...".format(
        bundle=item.bundle.name,
        item=item.id,
        node=item.node.name,
    ))
    start = datetime.now()
    try:
        content = template.render(item=item,
                                  bundle=item.bundle,
                                  node=item.node,
                                  repo=item.node.repo,
                                  **item.attributes['context'])
    except FaultUnavailable:
        raise
    except Exception as e:
        io.debug("".join(format_exception(*exc_info())))
        if isinstance(e, NameError) and str(e) == "Undefined":
            # Mako isn't very verbose here. Try to give a more useful
            # error message - even though we can't pinpoint the excat
            # location of the error. :/
            e = _("Undefined variable (look for '${...}')")
        elif isinstance(e, KeyError):
            e = _("KeyError: {}").format(str(e))
        raise TemplateError(
            _("Error while rendering template for {node}:{bundle}:{item}: {error}"
              ).format(
                  bundle=item.bundle.name,
                  error=e,
                  item=item.id,
                  node=item.node.name,
              ))
    duration = datetime.now() - start
    io.debug("{node}:{bundle}:{item}: rendered in {time}s".format(
        bundle=item.bundle.name,
        item=item.id,
        node=item.node.name,
        time=duration.total_seconds(),
    ))
    return content