<commit_before># -*- coding: utf-8 -*-


# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <<EMAIL>>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""Mako templates rendering"""


import email.header
import json
import os

import mako.lookup

from .. import conf


custom_lookups = {}  # custom TemplateLookups, inited by function load_environment
default_lookup = None  # default TemplateLookup, inited by function load_environment
dirs = None  # Sequence of templates directories, inited by function load_environment
js = lambda x: json.dumps(x, encoding = 'utf-8', ensure_ascii = False)


def get_default_lookup():
    global default_lookup
    if default_lookup is None:
        default_lookup = mako.lookup.TemplateLookup(
            default_filters = ['h'],
            directories = dirs,
#            error_handler = handle_mako_error,
            input_encoding = 'utf-8',
            module_directory = os.path.join(conf['cache_dir'], 'templates'),
            strict_undefined = False,
            )
    return default_lookup


def get_lookup(custom_name):
    # return mobile_lookup if ctx.mobile else lookup
    if custom_name is None or conf['customs_dir'] is None:
        return get_default_lookup()
    custom_templates_dir = os.path.join(conf['customs_dir'], custom_name, 'templates')
    if os.path.exists(custom_templates_dir):
        if custom_name not in custom_lookups:
            custom_lookups[custom_name] = mako.lookup.TemplateLookup(
                default_filters = ['h'],
                directories = [custom_templates_dir] + dirs,
#                error_handler = handle_mako_error,
                input_encoding = 'utf-8',
                module_directory = os.path.join(conf['cache_dir'], 'templates-{}'.format(custom_name)),
                strict_undefined = False,
                )
        return custom_lookups[custom_name]
    if custom_name in custom_lookups:
        del custom_lookups[custom_name]
    return get_default_lookup()


def get_template(template_path, custom_name = None):
    assert template_path.startswith('/')
    return get_lookup(custom_name).get_template(template_path)


def qp(s, encoding = 'utf-8'):
    assert isinstance(s, unicode)
    quoted_words = []
    for word in s.split(' '):
        try:
            word = str(word)
        except UnicodeEncodeError:
            word = str(email.header.Header(word.encode(encoding), encoding))
        quoted_words.append(word)
    return u' '.join(quoted_words)


def render(ctx, template_path, custom_name = None, **kw):
    return get_template(template_path, custom_name = custom_name).render_unicode(
        _ = ctx.translator.ugettext,
        ctx = ctx,
        js = js,
        N_ = lambda message: message,
        qp = qp,
        req = ctx.req,
        **kw).strip()


def render_def(ctx, template_path, def_name, custom_name = None, **kw):
    return get_template(template_path, custom_name = custom_name).get_def(def_name).render_unicode(
        _ = ctx.translator.ugettext,
        ctx = ctx,
        js = js,
        N_ = lambda message: message,
        qp = qp,
        req = ctx.req,
        **kw).strip()<commit_msg>Don't fail when rendering an error template.<commit_after># -*- coding: utf-8 -*-


# OpenFisca -- A versatile microsimulation software
# By: OpenFisca Team <<EMAIL>>
#
# Copyright (C) 2011, 2012, 2013, 2014 OpenFisca Team
# https://github.com/openfisca
#
# This file is part of OpenFisca.
#
# OpenFisca is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenFisca is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""Mako templates rendering"""


import email.header
import json
import os

import mako.lookup

from .. import conf


custom_lookups = {}  # custom TemplateLookups, inited by function load_environment
default_lookup = None  # default TemplateLookup, inited by function load_environment
dirs = None  # Sequence of templates directories, inited by function load_environment
js = lambda x: json.dumps(x, encoding = 'utf-8', ensure_ascii = False)


def get_default_lookup():
    global default_lookup
    if default_lookup is None:
        default_lookup = mako.lookup.TemplateLookup(
            default_filters = ['h'],
            directories = dirs,
#            error_handler = handle_mako_error,
            input_encoding = 'utf-8',
            module_directory = os.path.join(conf['cache_dir'], 'templates'),
            strict_undefined = False,
            )
    return default_lookup


def get_lookup(custom_name):
    # return mobile_lookup if ctx.mobile else lookup
    if custom_name is None or conf['customs_dir'] is None:
        return get_default_lookup()
    custom_templates_dir = os.path.join(conf['customs_dir'], custom_name, 'templates')
    if os.path.exists(custom_templates_dir):
        if custom_name not in custom_lookups:
            custom_lookups[custom_name] = mako.lookup.TemplateLookup(
                default_filters = ['h'],
                directories = [custom_templates_dir] + dirs,
#                error_handler = handle_mako_error,
                input_encoding = 'utf-8',
                module_directory = os.path.join(conf['cache_dir'], 'templates-{}'.format(custom_name)),
                strict_undefined = False,
                )
        return custom_lookups[custom_name]
    if custom_name in custom_lookups:
        del custom_lookups[custom_name]
    return get_default_lookup()


def get_template(template_path, custom_name = None):
    assert template_path.startswith('/')
    return get_lookup(custom_name).get_template(template_path)


def qp(s, encoding = 'utf-8'):
    assert isinstance(s, unicode)
    quoted_words = []
    for word in s.split(' '):
        try:
            word = str(word)
        except UnicodeEncodeError:
            word = str(email.header.Header(word.encode(encoding), encoding))
        quoted_words.append(word)
    return u' '.join(quoted_words)


def render(ctx, template_path, custom_name = None, **kw):
    return get_template(template_path, custom_name = custom_name).render_unicode(
        _ = ctx.translator.ugettext,
        ctx = ctx,
        js = js,
        N_ = lambda message: message,
        qp = qp,
        node = None,
        req = ctx.req,
        **kw).strip()


def render_def(ctx, template_path, def_name, custom_name = None, **kw):
    return get_template(template_path, custom_name = custom_name).get_def(def_name).render_unicode(
        _ = ctx.translator.ugettext,
        ctx = ctx,
        js = js,
        N_ = lambda message: message,
        qp = qp,
        node = None,
        req = ctx.req,
        **kw).strip()
{'fraction_non_alphanumeric': 0.09438031206393505, 'fraction_numerical': 0.0055816313586198145, 'mean_word_length': 2.9498997995991982, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4028920', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2255, 'n_words': 679}