<commit_before>#!/usr/bin/python
# coding:utf8

import os
import time
import traceback

from quixote.publish import SessionPublisher
from quixote.qwip import QWIP
from quixote.session import SessionManager

from code.libs.gzipper import make_gzip_middleware
from code.libs.permdir import get_tmpdir
from code.libs.auth.check_auth import check_auth
from code.libs.import_obj import import_obj_set
from code.libs.template import st
from code.views.util import is_mobile_device

import code.views as controllers
from code.models.user import User


PERFORMANCE_METRIC_MARKER = '<!-- _performtips_ -->'


def show_performance_metric(request, output):
    idx = output.find(PERFORMANCE_METRIC_MARKER)
    if idx > 0:
        pt = int((time.time() - request.start_time) * 1000)
        cls = pt > 250 and 'red' or pt > 100 and 'orange' or 'green'
        block = '<li class="hidden-phone"><a href="?_dae_profile=1" style="color:%s"> %d ms </a></li>' % (cls, pt)
        output = (output[:idx] + block + output[idx + len(PERFORMANCE_METRIC_MARKER):])
    return output


class CodePublisher(SessionPublisher):

    def __init__(self, *args, **kwargs):
        SessionPublisher.__init__(self, *args, **kwargs)
        display = 'html' if os.environ.get('DOUBAN_PRODUCTION') else 'plain'
        self.configure(DISPLAY_EXCEPTIONS=display,
                       UPLOAD_DIR=get_tmpdir() + '/upload/')

    def start_request(self, request):
        SessionPublisher.start_request(self, request)
        os.environ['SQLSTORE_SOURCE'] = request.get_url()

        resp = request.response
        resp.set_content_type('text/html; charset=utf-8')
        resp.set_header('Pragma', 'no-cache')
        resp.set_header('Cache-Control', 'must-revalidate, no-cache, private')
        # FIXME: quixote with origin?
        resp.set_header('Access-Control-Allow-Origin', '*')
        request.enable_ajax = False
        request.browser = request.guess_browser_version()
        request.method = request.get_method()
        request.url = request.get_path()
        request.is_mobile = is_mobile_device(request)
        request.start_time = time.time()
        request.user = User.check_session(request)

        import_obj_set("request", request)

    def try_publish(self, request, path):
        output = SessionPublisher.try_publish(self, request, path)
        output = show_performance_metric(request, output)
        return output

    def _generate_cgitb_error(self, request, original_response,
                              exc_type, exc_value, tb):
        traceback.print_exc()
        return st('/errors/500.html', **locals())


def create_publisher():
    return CodePublisher(controllers,
                         session_mgr=SessionManager())


app = make_gzip_middleware(QWIP(create_publisher()))<commit_msg>Remove quixote session publisher.<commit_after>#!/usr/bin/python
# coding:utf8

import os
import time
import traceback
from quixote.publish import Publisher
from quixote.qwip import QWIP

from code import views as controllers
from code.libs.gzipper import make_gzip_middleware
from code.libs.permdir import get_tmpdir
from code.libs.auth.check_auth import check_auth
from code.libs.import_obj import import_obj_set
from code.libs.template import st
from code.models.user import User
from code.views.util import is_mobile_device


PERFORMANCE_METRIC_MARKER = '<!-- _performtips_ -->'


def show_performance_metric(request, output):
    idx = output.find(PERFORMANCE_METRIC_MARKER)
    if idx > 0:
        pt = int((time.time() - request.start_time) * 1000)
        cls = pt > 250 and 'red' or pt > 100 and 'orange' or 'green'
        block = '<li class="hidden-phone"><span style="color:%s"> %d ms </span></li>' % (cls, pt)
        output = (output[:idx] + block + output[idx + len(PERFORMANCE_METRIC_MARKER):])
    return output


class CODEPublisher(Publisher):

    def __init__(self, *args, **kwargs):
        Publisher.__init__(self, *args, **kwargs)
        display = 'html' if os.environ.get('DOUBAN_PRODUCTION') else 'plain'
        self.configure(DISPLAY_EXCEPTIONS=display,
                       UPLOAD_DIR=get_tmpdir() + '/upload/')

    def start_request(self, request):
        Publisher.start_request(self, request)
        os.environ['SQLSTORE_SOURCE'] = request.get_url()

        resp = request.response
        resp.set_content_type('text/html; charset=utf-8')
        resp.set_header('Pragma', 'no-cache')
        resp.set_header('Cache-Control', 'must-revalidate, no-cache, private')
        # FIXME: quixote with origin?
        resp.set_header('Access-Control-Allow-Origin', '*')
        request.enable_ajax = False
        request.browser = request.guess_browser_version()
        request.method = request.get_method()
        request.url = request.get_path()
        request.is_mobile = is_mobile_device(request)
        request.start_time = time.time()
        request.user = User.check_session(request)

        import_obj_set("request", request)

    def try_publish(self, request, path):
        output = Publisher.try_publish(self, request, path)
        output = show_performance_metric(request, output)
        return output

    def _generate_cgitb_error(self, request, original_response,
                              exc_type, exc_value, tb):
        traceback.print_exc()
        return st('/errors/500.html', **locals())


def create_publisher():
    return CODEPublisher(controllers)


app = make_gzip_middleware(QWIP(create_publisher()))
{'fraction_non_alphanumeric': 0.11247033047288661, 'fraction_numerical': 0.006025196275333212, 'mean_word_length': 3.361464968152866, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '6279224', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1658, 'n_words': 362}