<commit_before>"""Symplate, the Simple pYthon teMPLATE renderer.

See README.md or https://github.com/benhoyt/symplate for documentation.

"""

from __future__ import with_statement

import os
import sys

__version__ = '0.9'


def html_filter(s):
    """Escape special HTML/XML characters in given ASCII or unicode string
    (this is the default output filter in templates).
    """
    # slight performance boost by feeding correct string type into replace()
    if isinstance(s, unicode):
        return (s.replace(u'&', u'&amp;')
                 .replace(u'<', u'&lt;')
                 .replace(u'>', u'&gt;')
                 .replace(u"'", u'&#39;')
                 .replace(u'"', u'&#34;'))
    else:
        return (s.replace('&', '&amp;')
                 .replace('<', '&lt;')
                 .replace('>', '&gt;')
                 .replace("'", '&#39;')
                 .replace('"', '&#34;'))


class Error(Exception):
    """A Symplate template or syntax error."""

    def __init__(self, msg, template, line_num):
        super(Error, self).__init__(msg, template, line_num)
        self.msg = msg
        self.template = template
        self.line_num = line_num
        lines = template.splitlines()
        if 0 <= line_num - 1 < len(lines):
            self.line = lines[line_num - 1]
        else:
            self.line = ''

    def __str__(self):
        return '%s, line %d: %s' % (self.msg, self.line_num, self.line.strip())

    def __repr__(self):
        return 'symplate.Error<%r>' % str(self)


class Renderer(object):
    """Symplate renderer class. See __init__'s docs for more info."""

    def __init__(self, template_dir, output_dir=None, extension='.symp',
                 check_mtime=False, modify_path=True, preamble='',
                 default_filter='symplate.html_filter'):
        """Initialize a Renderer instance.

        * template_dir:   directory your Symplate source files are in (the
                          only required argument)
        * output_dir:     directory compiled template (.py) files should go
                          into, default is {template_dir}/../symplouts
        * extension:      file extension for templates (set to '' if you want
                          to specify explictly when calling render)
        * check_mtime:    True means check template file's mtime on render(),
                          which is slower and usually only used for debugging
        * modify_path:    True means add output_dir/.. to sys.path for
                          importing compiled template
        * preamble:       extra code to include at top of compiled template,
                          such as imports
        * default_filter: if a string, use directly as Python expression for
                          default filter; otherwise this must be a function
                          that takes a single filename argument (e.g., if you
                          want to use the file extension to determine the
                          default filter)
        """
        self.template_dir = os.path.abspath(template_dir)
        if output_dir is None:
            output_dir = os.path.abspath(os.path.join(self.template_dir, '..',
                                                      'symplouts'))
        self.output_dir = output_dir
        self.extension = extension
        self.check_mtime = check_mtime
        self.preamble = preamble
        self.default_filter = default_filter

        self._module_cache = {}
        if modify_path:
            path_dir = os.path.abspath(os.path.join(output_dir, '..'))
            sys.path.insert(0, path_dir)

    def _get_default_filter(self, filename):
        """Return Python expression string to use as default filter."""
        if isinstance(self.default_filter, basestring):
            return self.default_filter
        else:
            return self.default_filter(filename)

    def _compile_text(self, text, indent, template, line_num):
        """Compile the text parts of a template (the parts not inside {%...%}
        blocks) at given indent level and return list of Python source output
        lines.
        """
        writes = []
        add_write = writes.append<commit_msg>Only modify sys.path in Renderer.__init__ if path isn't in there already<commit_after>"""Symplate, the Simple pYthon teMPLATE renderer.

See README.md or https://github.com/benhoyt/symplate for documentation.

"""

from __future__ import with_statement

import os
import sys

__version__ = '0.9'


def html_filter(s):
    """Escape special HTML/XML characters in given ASCII or unicode string
    (this is the default output filter in templates).
    """
    # slight performance boost by feeding correct string type into replace()
    if isinstance(s, unicode):
        return (s.replace(u'&', u'&amp;')
                 .replace(u'<', u'&lt;')
                 .replace(u'>', u'&gt;')
                 .replace(u"'", u'&#39;')
                 .replace(u'"', u'&#34;'))
    else:
        return (s.replace('&', '&amp;')
                 .replace('<', '&lt;')
                 .replace('>', '&gt;')
                 .replace("'", '&#39;')
                 .replace('"', '&#34;'))


class Error(Exception):
    """A Symplate template or syntax error."""

    def __init__(self, msg, template, line_num):
        super(Error, self).__init__(msg, template, line_num)
        self.msg = msg
        self.template = template
        self.line_num = line_num
        lines = template.splitlines()
        if 0 <= line_num - 1 < len(lines):
            self.line = lines[line_num - 1]
        else:
            self.line = ''

    def __str__(self):
        return '%s, line %d: %s' % (self.msg, self.line_num, self.line.strip())

    def __repr__(self):
        return 'symplate.Error<%r>' % str(self)


class Renderer(object):
    """Symplate renderer class. See __init__'s docs for more info."""

    def __init__(self, template_dir, output_dir=None, extension='.symp',
                 check_mtime=False, modify_path=True, preamble='',
                 default_filter='symplate.html_filter'):
        """Initialize a Renderer instance.

        * template_dir:   directory your Symplate source files are in (the
                          only required argument)
        * output_dir:     directory compiled template (.py) files should go
                          into, default is {template_dir}/../symplouts
        * extension:      file extension for templates (set to '' if you want
                          to specify explictly when calling render)
        * check_mtime:    True means check template file's mtime on render(),
                          which is slower and usually only used for debugging
        * modify_path:    True means add output_dir/.. to sys.path for
                          importing compiled template
        * preamble:       extra code to include at top of compiled template,
                          such as imports
        * default_filter: if a string, use directly as Python expression for
                          default filter; otherwise this must be a function
                          that takes a single filename argument (e.g., if you
                          want to use the file extension to determine the
                          default filter)
        """
        self.template_dir = os.path.abspath(template_dir)
        if output_dir is None:
            output_dir = os.path.abspath(os.path.join(self.template_dir, '..',
                                                      'symplouts'))
        self.output_dir = output_dir
        self.extension = extension
        self.check_mtime = check_mtime
        self.preamble = preamble
        self.default_filter = default_filter

        self._module_cache = {}
        if modify_path:
            path_dir = os.path.abspath(os.path.join(output_dir, '..'))
            if path_dir not in sys.path:
                sys.path.insert(0, path_dir)

    def _get_default_filter(self, filename):
        """Return Python expression string to use as default filter."""
        if isinstance(self.default_filter, basestring):
            return self.default_filter
        else:
            return self.default_filter(filename)

    def _compile_text(self, text, indent, template, line_num):
        """Compile the text parts of a template (the parts not inside {%...%}
        blocks) at given indent level and return list of Python source output
        lines.
        """
        writes = []
        add_write = writes.append
{'fraction_non_alphanumeric': 0.10421052631578948, 'fraction_numerical': 0.0032748538011695906, 'mean_word_length': 1.9174343227567383, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2036623', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2263, 'n_words': 716}