<commit_before># coding: utf-8

"""Fwf web framework.

RequestHandler: 请求处理基类.
Context: 所的请求处理类上下文.

TODO:

1. 模板渲染.
2. 缓存(HTTP 头).
3. gzip等功能实现.

"""

import re
import time
import logging
import datetime
import httplib
from traceback import format_exc
import rawio
import server

from fwf.util import gen_header_date


class RequestHandler(object):
    """Base web request handler.

    """
    def __init__(self, request):
        self.request = request
        self._headers = {}
        self._write = ""
        self._status_code = 200
        self.set_status(self._status_code)


    def head(self, *args, **kwargs):
        raise HTTPError(405)


    def get(self, *args, **kwargs):
        raise HTTPError(405)


    def post(self, *args, **kwargs):
        raise HTTPError(405)


    def finish(self, content):
        response = [b"%s" % self._response_line]
        response.extend([b"%s: %s" % (k, v) for k, v in  self._headers.iteritems()])
        response.append("Content-Length: %d" % len(content))
        response.append("")
        response.append(content)

        logging.info("[%s] %d %s %s" %(
                datetime.datetime.now(),
                self._status_code,
                self.request.url,
                self.request_time()))

        self.request.connection.stream.write("\r\n".join(response),
                                             self.request.connection._on_finish)


    def request_time(self):
        return ("%.4f ms" % (self.request.request_time() * 1000.0))


    def _generate_headers(self):
        self._headers["Server"] = b"Fwf web server"
        self._headers["Content-Type"] = b"text/plain"
        self._headers["Date"] = gen_header_date()


    def set_status(self, status_code):
        assert status_code in httplib.responses, "Response code is not in httplib.responses."
        self._generate_headers()
        self._status_code = status_code
        self._response_line = b"%s %d %s" % (
            self.request.http_version,
            status_code,
            httplib.responses[status_code])


    def write(self, content):
        self.finish(content)


    def view(self, template_name, **kwargs):
        pass



class Context(object):
    def __init__(self, handlers, **settings):
        assert isinstance(handlers, dict), "Handlers must be dict."
        self.settings = settings
        self.handlers = self._handle_url(handlers)


    def _handle_url(self, handlers):
        _handlers = {}
        
        for pattern, handle in handlers.iteritems():<commit_msg>fix up fwf/web.py<commit_after># coding: utf-8

"""Fwf web framework.

RequestHandler: 请求处理基类.
Context: 所的请求处理类上下文.

TODO:

1. 缓存(HTTP 头).
2. gzip等功能实现.

"""

import re
import time
import logging
import datetime
import httplib
from traceback import format_exc
import rawio
import server

from fwf.util import gen_header_date
from fwf.template import Template, TemplateLoader


class RequestHandler(object):
    """Base web request handler.

    """
    def __init__(self, request):
        self.request = request
        self._headers = {}
        self._write = ""
        self._status_code = 200
        self.set_status(self._status_code)


    def head(self, *args, **kwargs):
        raise HTTPError(405)


    def get(self, *args, **kwargs):
        raise HTTPError(405)


    def post(self, *args, **kwargs):
        raise HTTPError(405)


    def finish(self, content):
        response = [b"%s" % self._response_line]
        response.extend([b"%s: %s" % (k, v) for k, v in  self._headers.iteritems()])
        response.append("Content-Length: %d" % len(content))
        response.append("")
        response.append(content)

        logging.info("[%s] %d %s %s" %(
                datetime.datetime.now(),
                self._status_code,
                self.request.url,
                self.request_time()))

        self.request.connection.stream.write("\r\n".join(response),
                                             self.request.connection._on_finish)


    def request_time(self):
        return ("%.4f ms" % (self.request.request_time() * 1000.0))


    def _generate_headers(self):
        self._headers["Server"] = b"Fwf web server"
        self._headers["Content-Type"] = b"text/plain"
        self._headers["Date"] = gen_header_date()


    def set_status(self, status_code):
        assert status_code in httplib.responses, "Response code is not in httplib.responses."
        self._generate_headers()
        self._status_code = status_code
        self._response_line = b"%s %d %s" % (
            self.request.http_version,
            status_code,
            httplib.responses[status_code])


    def write(self, content):
        self.finish(content)


    def view(self, template_name, **kwargs):
        loader = TemplateLoader(template_name)
        html = Template(template_name, loader.read()).generate(**kwargs)
        return html



class Context(object):
    def __init__(self, handlers, **settings):
        assert isinstance(handlers, dict), "Handlers must be dict."
        self.settings = settings
        self.handlers = self._handle_url(handlers)


    def _handle_url(self, handlers):
        _handlers = {}
        
        for pattern, handle in handlers.iteritems():
{'fraction_non_alphanumeric': 0.12105663245914101, 'fraction_numerical': 0.008171797795515013, 'mean_word_length': 2.4020685197155784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7334659', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1608, 'n_words': 368}