<commit_before>import os.path

from jinja2 import BaseLoader, Environment, TemplateNotFound

from douglas.renderers.base import RendererBase
from douglas.tools import run_callback


class ThemeLoader(BaseLoader):
    def __init__(self, themepath):
        self.path = themepath

    def get_source(self, environment, template):
        path = os.path.join(self.path, template)
        if not os.path.exists(path):
            raise TemplateNotFound(template)
        mtime = os.path.getmtime(path)
        with file(path) as f:
            source = f.read().decode('utf-8')
        return source, path, lambda: mtime == os.path.getmtime(path)


def guess_autoescape(template_name):
    if template_name is None or '.' not in template_name:
        return False
    return template_name.endswith(('.html', '.htm', 'xml', 'rss'))


class Renderer(RendererBase):
    def build_context(self):
        """Returns a context consisting of filters and variables"""
        parsevars = dict()
        parsevars.update(self._request.config)
        parsevars.update(self._request.data)
        return parsevars

    def get_content_type(self, themedir, theme):
        path = os.path.join(themedir, theme, 'content_type')
        if os.path.exists(path):
            return open(path, 'r').read().strip()
        return 'text/html'

    def render(self, render_headers=True):
        """
        Do final rendering.

        :arg render_headers: whether (True) or not (False) to show the
            headers
        """
        # if we've already rendered, then we don't want to do so again
        if self.rendered:
            return

        config = self._request.get_configuration()
        data = self._request.get_data()

        themedir = config.get('themedir')
        theme = data.get("theme") or "html"<commit_msg>Fix bug in jinjarenderer if themedir is not defined<commit_after>import os.path

from jinja2 import BaseLoader, Environment, TemplateNotFound

from douglas.renderers.base import RendererBase
from douglas.tools import run_callback


class ThemeLoader(BaseLoader):
    def __init__(self, themepath):
        self.path = themepath

    def get_source(self, environment, template):
        path = os.path.join(self.path, template)
        if not os.path.exists(path):
            raise TemplateNotFound(template)
        mtime = os.path.getmtime(path)
        with file(path) as f:
            source = f.read().decode('utf-8')
        return source, path, lambda: mtime == os.path.getmtime(path)


def guess_autoescape(template_name):
    if template_name is None or '.' not in template_name:
        return False
    return template_name.endswith(('.html', '.htm', 'xml', 'rss'))


class Renderer(RendererBase):
    def build_context(self):
        """Returns a context consisting of filters and variables"""
        parsevars = dict()
        parsevars.update(self._request.config)
        parsevars.update(self._request.data)
        return parsevars

    def get_content_type(self, themedir, theme):
        path = os.path.join(themedir, theme, 'content_type')
        if os.path.exists(path):
            return open(path, 'r').read().strip()
        return 'text/html'

    def render(self, render_headers=True):
        """
        Do final rendering.

        :arg render_headers: whether (True) or not (False) to show the
            headers
        """
        # if we've already rendered, then we don't want to do so again
        if self.rendered:
            return

        config = self._request.get_configuration()
        data = self._request.get_data()

        themedir = config.get('themedir', 'themes')
        theme = data.get("theme") or "html"
{'fraction_non_alphanumeric': 0.10679347826086956, 'fraction_numerical': 0.0010869565217391304, 'mean_word_length': 2.6517857142857144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6699700', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1048, 'n_words': 290}