Exemplo n.º 1
0
 def render_jinja2(self, filename, **kwargs) -> Response:
     fullname = os.path.join(self.folder, filename)
     if os.path.isfile(fullname):
         with open(fullname) as f:
             return Response(Jinja2Template(f.read()).stream(**kwargs))
     else:
         raise Exception('Template {} Not Exists', format(filename))
Exemplo n.º 2
0
 def _process_via_jinja2(self, variables):
     log.info("Rendering template '%s' with jinja2", self.file_name)
     template = Jinja2Template(self.content)
     rendered_txt = template.render(**variables)
     if not rendered_txt.strip():
         log.info("Template '%s' is empty after jinja2 processing", self.file_name)
         return None
     return self._load_content(rendered_txt)
Exemplo n.º 3
0
def create_nginx_file(nginx_file, nginx_tmpl, context):
    if not jinja2_loaded:
        print "Could not found Jinja2 module"
        sys.exit(1)
    with open(nginx_tmpl, 'rb') as nf:
        t = nf.read()
    template = Jinja2Template(t)
    with open(nginx_file, 'wb') as configfile:
        configfile.write(template.render(context).encode('utf-8'))
Exemplo n.º 4
0
 def renderjinja2(obj, s=s):
     tvars = {
         'here': obj,
         'obj': obj,
         'container': obj.parentNode,
         'config': obj.ownerDocument.config,
         'context': obj.ownerDocument.context,
         'templates': obj.renderer
     }
     return Jinja2Template(s).render(tvars)
Exemplo n.º 5
0
def putfunc(engine):

    template = request.values.get('tpl')
    if not template:
        template = '%s'

    injection = request.values.get('inj')
    if engine == 'mako':
        return MakoTemplates(template % injection).render()
    elif engine == 'jinja2':
        return Jinja2Template(template % injection).render()
Exemplo n.º 6
0
def headerfunc(engine):

    template = request.headers.get('tpl')
    if not template:
        template = '%s'

    injection = request.headers.get('User-Agent')

    if engine == 'mako':
        return MakoTemplates(template % injection).render()
    elif engine == 'jinja2':
        return Jinja2Template(template % injection).render()
Exemplo n.º 7
0
def limited(engine):
    template = request.values.get('tpl')
    if not template:
        template = '%s'

    length = int(request.values.get('limit', 6))

    injection = request.values.get('inj', '')
    if len(injection) > length:
        return 'Inj too long'

    if engine == 'mako':
        return MakoTemplates(template % injection).render()
    elif engine == 'jinja2':
        return Jinja2Template(template % injection).render()
Exemplo n.º 8
0
 def render(self, variables):
     stylesheets = [
         style.as_css for style in self.styles if style.source is not None
     ]
     if self.base_style.source is not None:
         stylesheets = [self.base_style.as_css] + stylesheets
     template = Jinja2Template(self.source)
     context: dict = {
         var.name: var.compiled_content
         for var in variables
         if not var.is_list or var.parent_id is not None
     }
     context.update(
         {var.name: var.as_list
          for var in variables if var.is_list})
     source = template.render(**context)
     if "codehilite" in source:
         stylesheets.append(CSS(string=HtmlFormatter().get_style_defs()))
     html = HTML(string=source, base_url=".", url_fetcher=url_fetcher)
     return html.write_pdf(stylesheets=stylesheets)
Exemplo n.º 9
0
 def render(self, param_context={}):
     this_context = deepcopy(self.context)
     this_context.update(param_context)
     with open(self.tmpl_path, 'r') as template_file:
         return Jinja2Template(template_file.read()).render(this_context)
Exemplo n.º 10
0
            dst = template['dst']
            logging.debug("Template source :" + src)
            logging.debug("Template destination :" + dst)

            filedata = ""
            with open (os.path.join(args.workingdir,src), "r") as myfile:
                filedata=myfile.read()

            for e in engines:
                if e == 'jinja2':
                    logging.debug("Processign Jinja template")
                    try:
                        if filedata[-1:] != '\n':
                            logging.warn(src + " does not contain a newline at the end of file, " + dst + " might appear mangled.")

                        template = Jinja2Template(filedata)
                        filedata = template.render(template_variables)
                    except Exception,e:
                        logging.debug( "Error: ", str(e) )

            if 'collect' in data:
                logging.debug("Found a collect directive")
                for match in data['collect']:

                    if re.search(match['regex'], src, re.MULTILINE):
                        logging.debug("Collecting " + src + " into " + match['dst'])

                        if match['dst'] in collect:
                            collect[match['dst']] = collect[match['dst']] + "\n" + filedata
                        else:
                            collect[match['dst']] = filedata
 def render_expression_record(self, expression, record):
     template = Jinja2Template(expression)
     return template.render(record)
 def render_expression(self, expression, attributes):
     record = dict((x.attribute.name, x.value) for x in attributes)
     template = Jinja2Template(expression)
     return template.render(record)
Exemplo n.º 13
0
 def parametrize(self, value):
     """ Return the value with template substitution """
     template = Template(value)
     return Jinja2Template(template.safe_substitute(**self.mapping),
                           variable_start_string='{!',
                           variable_end_string='!}').render(**self.context)