<commit_before># -*- coding: utf-8 -*-
'''
Coldsweat - Web app machinery

Copyright (c) 2013— <NAME>
Portions are copyright (c) 2013 <NAME>
License: MIT (see LICENSE.md for details)
'''
import sys, os, re, time, codecs, cgi, string, urllib, time

from wsgiref.util import application_uri
from wsgiref.headers import Headers

from datetime import datetime
from traceback import format_tb
import urlparse
from webob import Request
import tempita

from utilities import *
from coldsweat import log, config, VERSION_STRING, installation_dir


ENCODING = 'utf-8'

#@@FIXME Use httplib.responses http://docs.python.org/2/library/httplib.html

HTTP_OK = '200 OK'
HTTP_NOT_FOUND = '404 Not Found'
HTTP_SEE_OTHER = '303 See Other'
# HTTP_MOVED_PERMANENTLY = '301 Moved Permanently'
HTTP_INTERNAL_SERVER_ERROR = '500 Internal Server Error'
HTTP_FORBIDDEN = '403 Forbidden'
HTTP_UNAUTHORIZED = '401 Unauthorized'
HTTP_BAD_REQUEST = '400 Bad Request'

#BASE_DIR = os.path.dirname(os.path.abspath(__file__))
 
 
# ------------------------------------------------------
# Response
# ------------------------------------------------------

URI_MAP = []
    
# Decorator
def view(pattern='^$', method='GET'):    
    
    def _(handler):         
        URI_MAP.append((re.compile(pattern, re.U), method.upper(), handler))
        return handler   
    return _   
    
def dispatch_request(environ, start_response):

    request = Request(environ) 

    output_headers = Headers([('Content-Type', 'text/html; charset=%s' % ENCODING)])

    template_symbols = {
        'base_uri': get_base_uri(environ), #@@FIXME: Use reuqest.application_url instead
        'encoding': ENCODING,
        'version_string': VERSION_STRING
    }
    
    # Setup common template symbols
    filler = fill_template_namespace(**template_symbols)

    try:
        path_info = request.path_info
    except KeyError:
        path_info = ''
    
    def find_view():
    
        for re, method, handler in URI_MAP:        
            match = re.match(path_info)
            if match and (method == request.method):                    
                return handler, match.groups()
    
        raise NotFoundError  
    
    def response(lines):
        for line in lines:
            yield encode(line)

    try:                    
        handler, args = find_view()
                   
        if not args:
            args = ()

        status, headers, body = handler(request, filler, *args)

        for name, value in headers:
            if name == 'Content-Type':
                output_headers[name] = value # Replace default value
            else:
                output_headers.add_header(name, value)

    except NotFoundError, exc:     
        status = str(exc)
        if exc.body:
            message = exc.body
        else:
            message = u'Resource <i>%s</i> could not be found on this server.' % path_info
                    
        body = make_page('404.html', filler, {'message': message})

    #output_headers['Content-Length'] = str(len(body))

    #@@NOTE: Looks wrong to me. However, WSGI handlers.py uses StringType 
    #  instead of str while asserting for k and v types
    start_response(status, [(str(k), str(v)) for k, v in output_headers.items()])    
    #start_response(status, str(output_headers))    

    return response([body])


# ------------------------------------------------------
# Misc. utilities
# ------------------------------------------------------

def get_base_uri(environ):
    _, filename = os.path.split(environ.get('SCRIPT_FILENAME', ''))    
    return application_uri(environ).replace('/%s' % filename, '')
    
class HTTPError(Exception):
    def __init__(self, status, headers=[], body=u''):
        self.headers = headers
        self.body = body
        super(HTTPError, self).__init__(status)

class NotFoundError(HTTPError):
    def __init__(self, headers=[], body=u''): 
        super(NotFoundError, self).__init__(HTTP_NOT_FOUND, headers, body)
                

def redirect_to(uri, headers=None):    
    output_headers = [('Location', uri)]
    if headers: 
        output_headers.extend(headers)
    return HTTP_SEE_OTHER, output_headers, u''
 

# ------------------------------------------------------
# Templates
# ------------------------------------------------------

def fill_template_namespace(**kwargs):            
    d = kwargs.copy()
    def _(t, **kwargs):
        d.update(kwargs)        
        return t.substitute(d)
    return _


def get_template(filename, from_template):
    """
    Load an inherited template
    """
    return tempita.Template.from_filename(os.path.join(installation_dir, 'coldsweat/templates', filename))
        
def fill_template(source, **kwargs):    
    t = tempita.Template(source, get_template=get_template)    
    return t.substitute(kwargs)


def load_template(filename):            
    return tempita.Template.from_filename(os.path.join(installation_dir, 'coldsweat/templates', filename), get_template=get_template)


def make_page(filename, filler, symbols):
<commit_msg>Added TEMPLATE_DIR var.<commit_after># -*- coding: utf-8 -*-
'''
Coldsweat - Web app machinery

Copyright (c) 2013— <NAME>
Portions are copyright (c) 2013 <NAME>
License: MIT (see LICENSE.md for details)
'''
import sys, os, re, time, codecs, cgi, string, urllib, time

from wsgiref.util import application_uri
from wsgiref.headers import Headers

from datetime import datetime
from traceback import format_tb
import urlparse
from webob import Request
import tempita

from utilities import *
from coldsweat import log, config, VERSION_STRING, installation_dir


ENCODING = 'utf-8'

#@@FIXME Use httplib.responses http://docs.python.org/2/library/httplib.html

HTTP_OK = '200 OK'
HTTP_NOT_FOUND = '404 Not Found'
HTTP_SEE_OTHER = '303 See Other'
# HTTP_MOVED_PERMANENTLY = '301 Moved Permanently'
HTTP_INTERNAL_SERVER_ERROR = '500 Internal Server Error'
HTTP_FORBIDDEN = '403 Forbidden'
HTTP_UNAUTHORIZED = '401 Unauthorized'
HTTP_BAD_REQUEST = '400 Bad Request'

TEMPLATE_DIR = os.path.join(installation_dir, 'coldsweat/templates')
 
# ------------------------------------------------------
# Response
# ------------------------------------------------------

URI_MAP = []
    
# Decorator
def view(pattern='^$', method='GET'):    
    
    def _(handler):         
        URI_MAP.append((re.compile(pattern, re.U), method.upper(), handler))
        return handler   
    return _   
    
def dispatch_request(environ, start_response):

    request = Request(environ) 

    output_headers = Headers([('Content-Type', 'text/html; charset=%s' % ENCODING)])

    template_symbols = {
        'base_uri': get_base_uri(environ), #@@FIXME: Use reuqest.application_url instead
        'encoding': ENCODING,
        'version_string': VERSION_STRING
    }
    
    # Setup common template symbols
    filler = fill_template_namespace(**template_symbols)

    try:
        path_info = request.path_info
    except KeyError:
        path_info = ''
    
    def find_view():
    
        for re, method, handler in URI_MAP:        
            match = re.match(path_info)
            if match and (method == request.method):                    
                return handler, match.groups()
    
        raise NotFoundError  
    
    def response(lines):
        for line in lines:
            yield encode(line)

    try:                    
        handler, args = find_view()
                   
        if not args:
            args = ()

        status, headers, body = handler(request, filler, *args)

        for name, value in headers:
            if name == 'Content-Type':
                output_headers[name] = value # Replace default value
            else:
                output_headers.add_header(name, value)

    except NotFoundError, exc:     
        status = str(exc)
        if exc.body:
            message = exc.body
        else:
            message = u'Resource <i>%s</i> could not be found on this server.' % path_info
                    
        body = make_page('404.html', filler, {'message': message})

    #output_headers['Content-Length'] = str(len(body))

    #@@NOTE: Looks wrong to me. However, WSGI handlers.py uses StringType 
    #  instead of str while asserting for k and v types
    start_response(status, [(str(k), str(v)) for k, v in output_headers.items()])    
    #start_response(status, str(output_headers))    

    return response([body])


# ------------------------------------------------------
# Misc. utilities
# ------------------------------------------------------

def get_base_uri(environ):
    _, filename = os.path.split(environ.get('SCRIPT_FILENAME', ''))    
    return application_uri(environ).replace('/%s' % filename, '')
    
class HTTPError(Exception):
    def __init__(self, status, headers=[], body=u''):
        self.headers = headers
        self.body = body
        super(HTTPError, self).__init__(status)

class NotFoundError(HTTPError):
    def __init__(self, headers=[], body=u''): 
        super(NotFoundError, self).__init__(HTTP_NOT_FOUND, headers, body)
                

def redirect_to(uri, headers=None):    
    output_headers = [('Location', uri)]
    if headers: 
        output_headers.extend(headers)
    return HTTP_SEE_OTHER, output_headers, u''
 

# ------------------------------------------------------
# Templates
# ------------------------------------------------------

def fill_template_namespace(**kwargs):            
    d = kwargs.copy()
    def _(t, **kwargs):
        d.update(kwargs)        
        return t.substitute(d)
    return _


def get_template(filename, from_template):
    """
    Load an inherited template
    """
    return tempita.Template.from_filename(os.path.join(TEMPLATE_DIR, filename))
        
def fill_template(source, **kwargs):    
    t = tempita.Template(source, get_template=get_template)    
    return t.substitute(kwargs)


def load_template(filename):            
    return tempita.Template.from_filename(os.path.join(TEMPLATE_DIR, filename), get_template=get_template)


def make_page(filename, filler, symbols):

{'fraction_non_alphanumeric': 0.16604071950978455, 'fraction_numerical': 0.0075113658825854916, 'mean_word_length': 2.1484131922837584, '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': '4691338', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2869, 'n_words': 717}