<commit_before>from tornado import web, escape
from july.template import JulyTemplateLoader
from july.cache import cache

__all__ = ["JulyHandler", "ApiHandler", "run_server"]


class JulyHandler(web.RequestHandler):
    """July Handler

    Subclass JulyHandler to make an app, it provides a way to organize a July
    App, and will support more features in the future.

    If you don't want the in app template feature, set app_template=False::

        class HomeHandler(JulyHandler):
            app_template = False

            def get(self):
                self.write('hello world')
    """
    app_template = True

    def _get_app(self):
        if hasattr(self, '_july_app'):
            return self._july_app
        if '__july_apps__' in self.settings:
            app = self.settings['__july_apps__'].get(self.__module__, None)
            self._july_app = app
        else:
            self._july_app = None
        return self._july_app

    def create_template_loader(self, template_path):
        app = self._get_app()
        if app and self.app_template:
            kwargs = {}
            if 'autoescape' in self.settings:
                kwargs['autoescape'] = self.settings['autoescape']

            return JulyTemplateLoader(template_path, app, **kwargs)
        return super(JulyHandler, self).create_template_loader(template_path)

    def on_finish(self):
        #: clear flash message
        key = '%s_flash_message' % self.xsrf_token
        cache.delete(key)

    def flash_message(self, msg=None, category=None):
        """flash_message provide an easy way to communicate with users.

        create message in your handler::

            class HomeHandler(JulyHandler):
                def get(self):
                    self.flash_message('thanks')
                    self.render('home.html')

        and get messages in ``home.html``::

            <ul>
                {% for category, message in flash_message() $}
                <li>{{category}}: {{message}}</li>
                {% end %}
            </ul>
        """
        def get_category_message(messages, category):
            for cat, msg in messages:
                if cat == category:
                    yield (cat, msg)

        #: use xsrf token or not ?
        key = '%s_flash_message' % self.xsrf_token
        if msg is None:
            messages = cache.get(key)
            if messages is None:
                return []
            if category is not None:
                return get_category_message(messages, category)
            return messages
        message = (category, msg)
        messages = cache.get(key)
        if isinstance(messages, list):
            messages.append(message)
        else:
            messages = [message]
        cache.set(key, messages, 600)
        return message

    def render_string(self, template_name, **kwargs):
        #: add application filters
        if '__july_filters__' in self.settings:
            kwargs.update(self.settings['__july_filters__'])

        #: add application global variables
        if '__july_global__' in self.settings:
            assert "g" not in kwargs, "g is a reserved keyword."
            kwargs["g"] = self.settings['__july_global__']

        #: add app filters
        app = self._get_app()
        if app and '__july_filters__' in app.settings:
            kwargs.update(app.settings['__july_filters__'])

        #: flash message support
        kwargs['flash_message'] = self.flash_message<commit_msg>reverse redirect makes life easier<commit_after>from tornado import web, escape
from july.template import JulyTemplateLoader
from july.cache import cache

__all__ = ["JulyHandler", "ApiHandler", "run_server"]


class JulyHandler(web.RequestHandler):
    """July Handler

    Subclass JulyHandler to make an app, it provides a way to organize a July
    App, and will support more features in the future.

    If you don't want the in app template feature, set app_template=False::

        class HomeHandler(JulyHandler):
            app_template = False

            def get(self):
                self.write('hello world')
    """
    app_template = True

    def _get_app(self):
        if hasattr(self, '_july_app'):
            return self._july_app
        if '__july_apps__' in self.settings:
            app = self.settings['__july_apps__'].get(self.__module__, None)
            self._july_app = app
        else:
            self._july_app = None
        return self._july_app

    def create_template_loader(self, template_path):
        app = self._get_app()
        if app and self.app_template:
            kwargs = {}
            if 'autoescape' in self.settings:
                kwargs['autoescape'] = self.settings['autoescape']

            return JulyTemplateLoader(template_path, app, **kwargs)
        return super(JulyHandler, self).create_template_loader(template_path)

    def on_finish(self):
        #: clear flash message
        key = '%s_flash_message' % self.xsrf_token
        cache.delete(key)

    def flash_message(self, msg=None, category=None):
        """flash_message provide an easy way to communicate with users.

        create message in your handler::

            class HomeHandler(JulyHandler):
                def get(self):
                    self.flash_message('thanks')
                    self.render('home.html')

        and get messages in ``home.html``::

            <ul>
                {% for category, message in flash_message() $}
                <li>{{category}}: {{message}}</li>
                {% end %}
            </ul>
        """
        def get_category_message(messages, category):
            for cat, msg in messages:
                if cat == category:
                    yield (cat, msg)

        #: use xsrf token or not ?
        key = '%s_flash_message' % self.xsrf_token
        if msg is None:
            messages = cache.get(key)
            if messages is None:
                return []
            if category is not None:
                return get_category_message(messages, category)
            return messages
        message = (category, msg)
        messages = cache.get(key)
        if isinstance(messages, list):
            messages.append(message)
        else:
            messages = [message]
        cache.set(key, messages, 600)
        return message

    def reverse_redirect(self, name, *args):
        self.redirect(self.reverse_url(name, *args))

    def render_string(self, template_name, **kwargs):
        #: add application filters
        if '__july_filters__' in self.settings:
            kwargs.update(self.settings['__july_filters__'])

        #: add application global variables
        if '__july_global__' in self.settings:
            assert "g" not in kwargs, "g is a reserved keyword."
            kwargs["g"] = self.settings['__july_global__']

        #: add app filters
        app = self._get_app()
        if app and '__july_filters__' in app.settings:
            kwargs.update(app.settings['__july_filters__'])

        #: flash message support
        kwargs['flash_message'] = self.flash_message
{'fraction_non_alphanumeric': 0.09394279304542905, 'fraction_numerical': 0.0008412787436904094, 'mean_word_length': 2.0379045996592846, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3974087', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1869, 'n_words': 541}