def render(self, context, jinja_env=None, autoescape=False):
        if autoescape:
            if not jinja_env:
                jinja_env = Environment(autoescape=autoescape)
            else:
                jinja_env.autoescape = autoescape

        # Body
        xml_src = self.build_xml(context, jinja_env)

        # fix tables if needed
        tree = self.fix_tables(xml_src)

        self.map_tree(tree)

        # Headers
        headers = self.build_headers_footers_xml(context, self.HEADER_URI,
                                                 jinja_env)
        for relKey, xml in headers:
            self.map_headers_footers_xml(relKey, xml)

        # Footers
        footers = self.build_headers_footers_xml(context, self.FOOTER_URI,
                                                 jinja_env)
        for relKey, xml in footers:
            self.map_headers_footers_xml(relKey, xml)
Example #2
0
def generate_templates(
        project, html_template, text_template, current_date, current_time_utc):
    '''Generates the templates using Jinja2 templates

    :param html_template: The filename of the HTML template in the templates
    folder
    :param text_template: The filename of the text template in the templates
    folder
    :param current_date: The current date.
    '''
    env = Environment(
        loader=FileSystemLoader('templates'), trim_blocks=True,
        lstrip_blocks=True, autoescape=True)

    env.filters['last_comment'] = last_comment
    env.filters['most_recent_comments'] = most_recent_comments
    env.filters['comments_within_lookback'] = comments_within_lookback
    env.filters['as_date'] = as_date

    log.info('Rendering HTML Template')
    html = env.get_template(html_template)
    rendered_html = premailer.transform(html.render(
        project=project, current_date=current_date,
        current_time_utc=current_time_utc))

    log.info('Rendering Text Template')
    env.autoescape = False
    plaintext = env.get_template(text_template)
    rendered_plaintext = plaintext.render(
        project=project, current_date=current_date,
        current_time_utc=current_time_utc)

    return (rendered_html, rendered_plaintext)
def generate_templates(project, html_template, text_template, current_date, current_time_utc, skip_inline_css=False):
    """Generates the templates using Jinja2 templates

    :param html_template: The filename of the HTML template in the templates
    folder
    :param text_template: The filename of the text template in the templates
    folder
    :param current_date: The current date.
    """
    env = Environment(loader=FileSystemLoader("templates"), trim_blocks=True, lstrip_blocks=True, autoescape=True)

    env.filters["last_comment"] = last_comment
    env.filters["most_recent_comments"] = most_recent_comments
    env.filters["comments_within_lookback"] = comments_within_lookback
    env.filters["as_date"] = as_date

    log.info("Rendering HTML Template")
    html = env.get_template(html_template)
    if skip_inline_css:
        rendered_html = html.render(project=project, current_date=current_date, current_time_utc=current_time_utc)
    else:
        rendered_html = premailer.transform(
            html.render(project=project, current_date=current_date, current_time_utc=current_time_utc)
        )

    log.info("Rendering Text Template")
    env.autoescape = False
    plaintext = env.get_template(text_template)
    rendered_plaintext = plaintext.render(project=project, current_date=current_date, current_time_utc=current_time_utc)

    return (rendered_html, rendered_plaintext)
Example #4
0
def environment(**options):
    env = Environment(**options, extensions=['jinja2.ext.i18n'])
    env.autoescape = True
    env.install_gettext_translations(translation, newstyle=True)

    env.globals.update(context['globals'])
    env.filters.update(context['filters'])

    return env
Example #5
0
def environment(**options):
    env = Environment(**options)
    env.autoescape = True

    env.globals.update({
        'url': reverse,
    })
    # env.filters.update(context['filters'])

    return env
Example #6
0
def environment(**options):
    _load_django_default_filters()
    _load_templatetags()

    env = Environment(**options)
    env.globals.update(_local_env['globals'])
    env.filters.update(_local_env['filters'])
    env.tests.update(_local_env['tests'])
    env.install_gettext_translations(translation)
    env.autoescape = select_autoescape(
        disabled_extensions=('txt.jinja2',),
        default_for_string=False,
        default=True)
    return env
def generate_templates(project,
                       html_template,
                       text_template,
                       current_date,
                       current_time_utc,
                       skip_inline_css=False):
    '''Generates the templates using Jinja2 templates

    :param html_template: The filename of the HTML template in the templates
    folder
    :param text_template: The filename of the text template in the templates
    folder
    :param current_date: The current date.
    '''
    env = Environment(loader=FileSystemLoader('templates'),
                      trim_blocks=True,
                      lstrip_blocks=True,
                      autoescape=True)

    env.filters['last_comment'] = last_comment
    env.filters['most_recent_comments'] = most_recent_comments
    env.filters['comments_within_lookback'] = comments_within_lookback
    env.filters['as_date'] = as_date

    log.info('Rendering HTML Template')
    html = env.get_template(html_template)
    if skip_inline_css:
        rendered_html = html.render(project=project,
                                    current_date=current_date,
                                    current_time_utc=current_time_utc)
    else:
        rendered_html = premailer.transform(
            html.render(project=project,
                        current_date=current_date,
                        current_time_utc=current_time_utc))

    log.info('Rendering Text Template')
    env.autoescape = False
    plaintext = env.get_template(text_template)
    rendered_plaintext = plaintext.render(project=project,
                                          current_date=current_date,
                                          current_time_utc=current_time_utc)

    return (rendered_html, rendered_plaintext)
def get_jinja_env(template_data):
    env = Environment(loader=PackageLoader('cvbuilder', 'templates'))

    # Change start/end strings if necessary
    # For TEX templates, this is _always_ necessary
    if "strings" in template_data or "tex" in template_data["language"]:
        try:
            strings = template_data["strings"]
        except KeyError:
            print "\nWarning: Start/end strings are necessary in TEX templates"
            exit()

        if "variable_start" in strings:
            env.variable_start_string = strings["variable_start"]
        elif "variable_begin" in strings:
            env.variable_start_string = strings["variable_begin"]
        if "variable_end"   in strings:
            env.variable_end_string   = strings["variable_end"]

        if "block_start" in strings:
            env.block_start_string = strings["block_start"]
        elif "block_begin" in strings:
            env.block_start_string = strings["block_begin"]
        if "block_end"   in strings:
            env.block_end_string   = strings["block_end"]

        if "comment_start" in strings:
            env.comment_start_string = strings["comment_start"]
        elif "comment_begin" in strings:
            env.comment_start_string = strings["comment_begin"]
        if "comment_end"   in strings:
            env.comment_end_string   = strings["comment_end"]

    if "html" in template_data["language"]:
        env.autoescape = True

    env.filters['datetimeformat'] = datetimeformat
    env.filters['escape_tex']     = escape_tex
    env.filters['bibtex']         = bibtex

    return env
Example #9
0
def create_report_environment():
    # prepare jinja2 environment
    from orun.reports.engines.chrome.filters import localize, linebreaks, groupby
    from orun.reports.engines.chrome.extension import ReportExtension
    from orun.reports.engines.chrome.utils import avg, total, to_list, COUNT, AVG, SUM
    env = Environment()
    env.autoescape = False
    env.add_extension(ReportExtension)
    env.finalize = localize
    # env.undefined = SilentUndefined
    env.filters['localize'] = localize
    env.filters['linebreaks'] = linebreaks
    # env.globals['static_fs'] = self.static_fs
    env.filters['total'] = total
    env.globals['avg'] = avg
    env.globals['sum'] = sum
    env.globals['count'] = len
    env.globals['COUNT'] = COUNT
    env.globals['SUM'] = SUM
    env.globals['AVG'] = AVG
    from orun.reports.data import DataSource
    env.globals['create_datasource'] = lambda sql: DataSource(sql)
Example #10
0
    def render(self, context, jinja_env=None, autoescape=False):
        # init template working attributes
        self.render_init()

        if autoescape:
            if not jinja_env:
                jinja_env = Environment(autoescape=autoescape)
            else:
                jinja_env.autoescape = autoescape

        # Body
        xml_src = self.build_xml(context, jinja_env)

        # fix tables if needed
        tree = self.fix_tables(xml_src)

        # fix docPr ID's
        self.fix_docpr_ids(tree)

        # Replace body xml tree
        self.map_tree(tree)

        # Headers
        headers = self.build_headers_footers_xml(context, self.HEADER_URI,
                                                 jinja_env)
        for relKey, xml in headers:
            self.map_headers_footers_xml(relKey, xml)

        # Footers
        footers = self.build_headers_footers_xml(context, self.FOOTER_URI,
                                                 jinja_env)
        for relKey, xml in footers:
            self.map_headers_footers_xml(relKey, xml)

        # set rendered flag
        self.is_rendered = True
Example #11
0
        rank += 1

    session.commit()

    del _sessionmaker
    del _create_engine
    del _ut
    del session

    log.log([], LVL_ALWAYS, PyLogger.INFO, 'Done updating user ranks...')
    q.put("Done updating user rank")

if __name__ == '__main__':
    jinjaenv = Environment(loader=PackageLoader('stats', 'templates'))
    jinjaenv.autoescape = True
    wt = WebTool()
    ut = UserTool(Session)
    st = ScriptTool(Session)
    ct = CommitTool(Session)
    vt = VariableTool(Session)
    gt = GraphTool()


    # Set it to zero so we schedule it right away. (the first time)
    last_rank_time = 0
    last_rank_process = None
    last_rank_queue = None

    from log import PyLogger
Example #12
0
                if ws_count == -1:
                    ws_count = re.search(r'[\S]', line).start()
                ret.append(line[ws_count:] if len(line) >= ws_count else line)

            if '// BEGIN_CODE' in line:
                add_line = True

    if len(ret) == 0:
        print "Error did not read any code from %s. Make sure you have a BEGIN_CODE marker" % file_name

    return ret


env = Environment(loader=FileSystemLoader('templates'))
env.autoescape = True

# go through and "fix-up" MENU_ITEMS
for item in list(MENU_ITEMS):
    if 'name' not in item:
        print "Error couldn't find name in item: %s" % str(item)
        MENU_ITEMS.remove(item)

    if 'page' not in item:
        print "Error, no page found in item: %s" % str(item)
        MENU_ITEMS.remove(item)
        continue

    if item['page'] is None:
        continue
Example #13
0
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
# import json
import yaml
'''
Quick python3 script to render HTML from a json file of slides
containing the data to be visualized, using the Jinja template engine
'''

env = Environment(loader=FileSystemLoader(""))
env.autoescape = False  # allows HTML tags to be read in from strings
env.lstrip_blocks = True  # strip the whitespace from jinja template lines
env.trim_blocks = True
'''
def is_string(l): return isinstance(l,str)
env.filters["is_string"] = is_string
'''

templatefile = "viz_template.html"
datafile = "viz.yaml"
outfile = "viz.html"

try:
    with open(datafile, "r") as f:
        data = f.read()
    # data = json.loads(data)
    data = yaml.load(data)
    template = env.get_template(templatefile)
    html = template.render(data=data)
    with open(outfile, "w") as f:
        f.write(html)
    print("wrote %s" % outfile)
Example #14
0
        rank += 1

    session.commit()

    del _sessionmaker
    del _create_engine
    del _ut
    del session

    log.log([], LVL_ALWAYS, PyLogger.INFO, 'Done updating user ranks...')
    q.put("Done updating user rank")


if __name__ == '__main__':
    jinjaenv = Environment(loader=PackageLoader('stats', 'templates'))
    jinjaenv.autoescape = True
    wt = WebTool()
    ut = UserTool(Session)
    st = ScriptTool(Session)
    ct = CommitTool(Session)
    vt = VariableTool(Session)
    gt = GraphTool()

    # Set it to zero so we schedule it right away. (the first time)
    last_rank_time = 0
    last_rank_process = None
    last_rank_queue = None

    from log import PyLogger

    log = PyLogger()