<commit_before>#
# Jinja2 (http://jinja.pocoo.org) based template renderer.
#
# Author: <NAME> <ssato redhat.com>
# License: MIT
#
import logging
import os

import anyconfig.compat

LOGGER = logging.getLogger(__name__)
TEMPLATE_SUPPORT = False
try:
    import jinja2
    from jinja2.exceptions import TemplateNotFound

    TEMPLATE_SUPPORT = True

    def tmpl_env(paths):
        return jinja2.Environment(loader=jinja2.FileSystemLoader(paths))

except ImportError:
    LOGGER.warn("Jinja2 is not available on your system, so "
                "template support will be disabled.")

    class TemplateNotFound(RuntimeError):
        pass

    def tmpl_env(*args):
        return None


def make_template_paths(template_file, paths=None):
    """
    :param template_file: Absolute or relative path to the template file
    :param paths: A list of template search paths

    >>> make_template_paths("/path/to/a/template")
    ['.', '/path/to/a']
    >>> make_template_paths("/path/to/a/template", ["/tmp", "."])
    ['/tmp', '.', '/path/to/a']
    """
    tmpldir = os.path.abspath(os.path.dirname(template_file))
    return [os.curdir, tmpldir] if paths is None else paths + [tmpldir]


def render_s(tmpl_s, ctx=None, paths=[os.curdir]):
    """
    Compile and render given template string `tmpl_s` with context `context`.

    :param tmpl_s: Template string
    :param ctx: Context dict needed to instantiate templates
    :param paths: Template search paths

    >>> s = render_s('a = {{ a }}, b = "{{ b }}"', {'a': 1, 'b': 'bbb'})
    >>> if TEMPLATE_SUPPORT:
    ...     assert s == 'a = 1, b = "bbb"'
    """
    env = tmpl_env(paths)

    if env is None:
        return tmpl_s
    else:
        if ctx is None:
            ctx = {}

        return tmpl_env(paths).from_string(tmpl_s).render(**ctx)


def render_impl(template_file, ctx=None, paths=None):
    """
    :param template_file: Absolute or relative path to the template file
    :param ctx: Context dict needed to instantiate templates
    """
    env = tmpl_env(make_template_paths(template_file, paths))

    if env is None:
        return anyconfig.compat.copen(template_file).read()
    else:
        if ctx is None:<commit_msg>simplify the flag of template support, s/TEMPLATE_SUPPORT/SUPPORTED/g<commit_after>#
# Jinja2 (http://jinja.pocoo.org) based template renderer.
#
# Author: <NAME> <ssato redhat.com>
# License: MIT
#
import logging
import os

import anyconfig.compat

LOGGER = logging.getLogger(__name__)
SUPPORTED = False
try:
    import jinja2
    from jinja2.exceptions import TemplateNotFound

    SUPPORTED = True

    def tmpl_env(paths):
        return jinja2.Environment(loader=jinja2.FileSystemLoader(paths))

except ImportError:
    LOGGER.warn("Jinja2 is not available on your system, so "
                "template support will be disabled.")

    class TemplateNotFound(RuntimeError):
        pass

    def tmpl_env(*args):
        return None


def make_template_paths(template_file, paths=None):
    """
    :param template_file: Absolute or relative path to the template file
    :param paths: A list of template search paths

    >>> make_template_paths("/path/to/a/template")
    ['.', '/path/to/a']
    >>> make_template_paths("/path/to/a/template", ["/tmp", "."])
    ['/tmp', '.', '/path/to/a']
    """
    tmpldir = os.path.abspath(os.path.dirname(template_file))
    return [os.curdir, tmpldir] if paths is None else paths + [tmpldir]


def render_s(tmpl_s, ctx=None, paths=[os.curdir]):
    """
    Compile and render given template string `tmpl_s` with context `context`.

    :param tmpl_s: Template string
    :param ctx: Context dict needed to instantiate templates
    :param paths: Template search paths

    >>> s = render_s('a = {{ a }}, b = "{{ b }}"', {'a': 1, 'b': 'bbb'})
    >>> if SUPPORTED:
    ...     assert s == 'a = 1, b = "bbb"'
    """
    env = tmpl_env(paths)

    if env is None:
        return tmpl_s
    else:
        if ctx is None:
            ctx = {}

        return tmpl_env(paths).from_string(tmpl_s).render(**ctx)


def render_impl(template_file, ctx=None, paths=None):
    """
    :param template_file: Absolute or relative path to the template file
    :param ctx: Context dict needed to instantiate templates
    """
    env = tmpl_env(make_template_paths(template_file, paths))

    if env is None:
        return anyconfig.compat.copen(template_file).read()
    else:
        if ctx is None:
{'fraction_non_alphanumeric': 0.12581736189402482, 'fraction_numerical': 0.0036076662908680946, 'mean_word_length': 3.069724770642202, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1418722', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1403, 'n_words': 397}