<commit_before># Module:   errors
# Date:     11th February 2009
# Author:   <NAME>, prologic at shortcircuit dot net dot au

"""Errors

This module implements a set of standard HTTP Errors.
"""

from cgi import escape as _escape
from urlparse import urljoin as _urljoin

import utils
from constants import DEFAULT_ERROR_MESSAGE, RESPONSES, SERVER_VERSION


class BaseError(object):

    def __init__(self, request, response):
        self.request = request
        self.response = response

        response.clear()
        response.close = True
        response.headers.add_header("Connection", "close")

    def __nonzero__(self):
        return True

class HTTPError(BaseError):

    def __init__(self, request, response, status, message=None, error=None):
        super(HTTPError, self).__init__(request, response)

        self.status = status

        short, long = RESPONSES.get(status, ("???", "???",))
        self.message = message = message or short

        self.error = error

        s = DEFAULT_ERROR_MESSAGE % {
            "status": "%s %s" % (status, short),
            "message": _escape(message),
            "traceback": error or "",
            "version": SERVER_VERSION}

        response.body = s
        response.status = "%s %s" % (status, short)

    def __repr__(self):
        name = self.__class__.__name__
        return "<%s (%d, %s)>" % (name, self.status, repr(self.message))

class Forbidden(HTTPError):

    def __init__(self, request, response, message=None):
        super(Forbidden, self).__init__(request, response, 403, message)

class NotFound(HTTPError):

    def __init__(self, request, response, message=None):
        super(NotFound, self).__init__(request, response, 404, message)

class Redirect(HTTPError):

    def __init__(self, request, response, urls, status=None):
        if isinstance(urls, basestring):
            urls = [urls]
        
        abs_urls = []
        for url in urls:
            # Note that urljoin will "do the right thing" whether url is:
            #  1. a complete URL with host (e.g. "http://www.example.com/test")
            #  2. a URL relative to root (e.g. "/dummy")
            #  3. a URL relative to the current path
            # Note that any query string in request is discarded.
            url = _urljoin(utils.url(request), url)<commit_msg>lib.web.errors: BaseError deprecated. HTTPError is now an Event. Tidied up constructor<commit_after># Module:   errors
# Date:     11th February 2009
# Author:   <NAME>, prologic at shortcircuit dot net dot au

"""Errors

This module implements a set of standard HTTP Errors.
"""

from cgi import escape as _escape
from urlparse import urljoin as _urljoin

from circuits import Event

import utils
from constants import DEFAULT_ERROR_MESSAGE, RESPONSES, SERVER_VERSION


class HTTPError(Event):

    def __init__(self, request, response, status, message=None, error=None):
        super(HTTPError, self).__init__(request, response,
                status, message=message, error=error)

        response.clear()
        response.close = True
        response.headers.add_header("Connection", "close")

        self.request = request
        self.response = response
        self.status = status

        short, long = RESPONSES.get(status, ("???", "???",))
        self.message = message = message or short

        self.error = error

        response.status = "%s %s" % (status, short)

        response.body = DEFAULT_ERROR_MESSAGE % {
            "status": "%s %s" % (status, short),
            "message": _escape(message),
            "traceback": error or "",
            "version": SERVER_VERSION}

    def __nonzero__(self):
        return True

    def __repr__(self):
        name = self.__class__.__name__
        return "<%s (%d, %s)>" % (name, self.status, repr(self.message))

class Forbidden(HTTPError):

    def __init__(self, request, response, message=None):
        super(Forbidden, self).__init__(request, response, 403, message)

class NotFound(HTTPError):

    def __init__(self, request, response, message=None):
        super(NotFound, self).__init__(request, response, 404, message)

class Redirect(HTTPError):

    def __init__(self, request, response, urls, status=None):
        if isinstance(urls, basestring):
            urls = [urls]
        
        abs_urls = []
        for url in urls:
            # Note that urljoin will "do the right thing" whether url is:
            #  1. a complete URL with host (e.g. "http://www.example.com/test")
            #  2. a URL relative to root (e.g. "/dummy")
            #  3. a URL relative to the current path
            # Note that any query string in request is discarded.
            url = _urljoin(utils.url(request), url)
{'fraction_non_alphanumeric': 0.11115798354777473, 'fraction_numerical': 0.006327778949588694, 'mean_word_length': 2.619847328244275, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '327506', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1360, 'n_words': 428}