<commit_before># -*- coding: utf-8 -
#
# Copyright (c) 2008, 2009 <NAME> <<EMAIL>> 
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""
exception classes.
"""
import restkit
import warnings

try:
    import webob
except ImportError:
    webob = False
    
class deprecated_property(object):
    """
    Wraps a decorator, with a deprecation warning or error
    """
    def __init__(self, decorator, attr, message, warning=True):
        self.decorator = decorator
        self.attr = attr
        self.message = message
        self.warning = warning

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        self.warn()
        return self.decorator.__get__(obj, type)

    def __set__(self, obj, value):
        self.warn()
        self.decorator.__set__(obj, value)

    def __delete__(self, obj):
        self.warn()
        self.decorator.__delete__(obj)

    def __repr__(self):
        return '<Deprecated attribute %s: %r>' % (
            self.attr,
            self.decorator)

    def warn(self):
        if not self.warning:
            raise DeprecationWarning(
                'The attribute %s is deprecated: %s' % (self.attr, self.message))
        else:
            warnings.warn(
                'The attribute %s is deprecated: %s' % (self.attr, self.message),
                DeprecationWarning,
                stacklevel=3)
                
                    
class SimpleResourceError(Exception):
    """ default error class """
    def __init__(self, msg=None, http_code=None, response=None):
        self.msg = msg or ''
        self.status = http_code
        self.response = response
        Exception.__init__(self)
        
    def _status_int__get(self):
        """
        The status as an integer
        """
        return self.status
    def _status_int__set(self, http_code):
        self.status = http_code
    status_int = property(_status_int__get, _status_int__set, doc=_status_int__get.__doc__)
        
    status_code = deprecated_property(
        status_int, 'status_code', 'use .status_int instead',
        warning=False)
        
    def _get_message(self):
        return self.msg
    def _set_message(self, msg):
        self.msg = msg or ''
    message = property(_get_message, _set_message)    
    
    def __str__(self):
        if self.msg:
            return self.msg
        try:
            return str(self.__dict__)
        except (NameError, ValueError, KeyError), e:
            return 'Unprintable exception %s: %s' \
                % (self.__class__.__name__, str(e))
                

ResourceError = None
if webob:
    import webob.exc
    class WSGIResourceError(webob.exc.WSGIHTTPException):

        def __init__(self, msg=None, http_code=None, response=None):
            webob.exc.WSGIHTTPException.__init__(self)
            
            http_code = http_code or 500
            klass = webob.exc.status_map[http_code]
            self.code = http_code
            self.title = klass.title
            self.status = '%s %s' % (self.code, self.title)
            self.explanation = msg
            self.response = response
            
            # default params
            #self.status_int = http_code
            self.msg = msg

        def _get_message(self):
            return self.explanation
        def _set_message(self, msg):
            self.explanation = msg or ''
        message = property(_get_message, _set_message)
    ResourceError = WSGIResourceError
else:
    WSGIResourceError = None
    ResourceError = SimpleResourceError
                
def use_simple_exception():
    global ResourceError, SimpleResourceError
    ResourceError = SimpleResourceError
    
def use_wsgi_exception():
    global ResourceError, WSGIResourceError
    if webob:
        ResourceError = WSGIResourceError
    else:
        raise ImportError("webob cannot be imported.")

class ResourceNotFound(ResourceError):
    """Exception raised when no resource was found at the given url. 
    """

class Unauthorized(ResourceError):
    """Exception raised when an authorization is required to access to
    the resource specified.
    """

class RequestFailed(ResourceError):
    """Exception raised when an unexpected HTTP error is received in response
    to a request.
    

    The request failed, meaning the remote HTTP server returned a code 
    other than success, unauthorized, or NotFound.

    The exception message attempts to extract the error

    You can get the status code by e.http_code, or see anything about the 
    response via e.response. For example, the entire result body (which is 
    probably an HTML error page) is e.response.body.
    """
    
class RedirectLimit(Exception):
    """Exception raised when the redirection limit is reached."""

class RequestError(Exception):
    """Exception raised when a request is malformed"""
    
class InvalidUrl(Exception):
    """
    Not a valid url for use with this software.
    """
    
class TransportError(Exception):
    """Error raised by a transport """
    
class ResponseError(Exception):
    """ Error raised while getting response or decompressing response stream"""
    
<commit_msg>deprecate status_coe for old version of webob too<commit_after># -*- coding: utf-8 -
#
# Copyright (c) 2008, 2009 <NAME> <<EMAIL>> 
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""
exception classes.
"""
import restkit
import warnings

try:
    import webob
except ImportError:
    webob = False
    
class deprecated_property(object):
    """
    Wraps a decorator, with a deprecation warning or error
    """
    def __init__(self, decorator, attr, message, warning=True):
        self.decorator = decorator
        self.attr = attr
        self.message = message
        self.warning = warning

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        self.warn()
        return self.decorator.__get__(obj, type)

    def __set__(self, obj, value):
        self.warn()
        self.decorator.__set__(obj, value)

    def __delete__(self, obj):
        self.warn()
        self.decorator.__delete__(obj)

    def __repr__(self):
        return '<Deprecated attribute %s: %r>' % (
            self.attr,
            self.decorator)

    def warn(self):
        if not self.warning:
            raise DeprecationWarning(
                'The attribute %s is deprecated: %s' % (self.attr, self.message))
        else:
            warnings.warn(
                'The attribute %s is deprecated: %s' % (self.attr, self.message),
                DeprecationWarning,
                stacklevel=3)
                
                    
class SimpleResourceError(Exception):
    """ default error class """
    def __init__(self, msg=None, http_code=None, response=None):
        self.msg = msg or ''
        self.status = http_code
        self.response = response
        Exception.__init__(self)
        
    def _status_int__get(self):
        """
        The status as an integer
        """
        return self.status
    def _status_int__set(self, http_code):
        self.status = http_code
    status_int = property(_status_int__get, _status_int__set, doc=_status_int__get.__doc__)
        
    status_code = deprecated_property(
        status_int, 'status_code', 'use .status_int instead',
        warning=False)
        
    def _get_message(self):
        return self.msg
    def _set_message(self, msg):
        self.msg = msg or ''
    message = property(_get_message, _set_message)    
    
    def __str__(self):
        if self.msg:
            return self.msg
        try:
            return str(self.__dict__)
        except (NameError, ValueError, KeyError), e:
            return 'Unprintable exception %s: %s' \
                % (self.__class__.__name__, str(e))
                

ResourceError = None
if webob:
    import webob.exc
    class WSGIResourceError(webob.exc.WSGIHTTPException):

        def __init__(self, msg=None, http_code=None, response=None):
            webob.exc.WSGIHTTPException.__init__(self)
            
            http_code = http_code or 500
            klass = webob.exc.status_map[http_code]
            self.code = http_code
            self.title = klass.title
            self.status = '%s %s' % (self.code, self.title)
            self.explanation = msg
            self.response = response
            # default params
            self.msg = msg

        def _status_int__get(self):
            """
            The status as an integer
            """
            return int(self.status.split()[0])
        def _status_int__set(self, value):
            self.status = value
        status_int = property(_status_int__get, _status_int__set, doc=_status_int__get.__doc__)
        
        status_code = deprecated_property(
            status_int, 'status_code', 'use .status_int instead',
            warning=False)

        def _get_message(self):
            return self.explanation
        def _set_message(self, msg):
            self.explanation = msg or ''
        message = property(_get_message, _set_message)
    ResourceError = WSGIResourceError
else:
    WSGIResourceError = None
    ResourceError = SimpleResourceError
                
def use_simple_exception():
    global ResourceError, SimpleResourceError
    ResourceError = SimpleResourceError
    
def use_wsgi_exception():
    global ResourceError, WSGIResourceError
    if webob:
        ResourceError = WSGIResourceError
    else:
        raise ImportError("webob cannot be imported.")

class ResourceNotFound(ResourceError):
    """Exception raised when no resource was found at the given url. 
    """

class Unauthorized(ResourceError):
    """Exception raised when an authorization is required to access to
    the resource specified.
    """

class RequestFailed(ResourceError):
    """Exception raised when an unexpected HTTP error is received in response
    to a request.
    

    The request failed, meaning the remote HTTP server returned a code 
    other than success, unauthorized, or NotFound.

    The exception message attempts to extract the error

    You can get the status code by e.http_code, or see anything about the 
    response via e.response. For example, the entire result body (which is 
    probably an HTML error page) is e.response.body.
    """
    
class RedirectLimit(Exception):
    """Exception raised when the redirection limit is reached."""

class RequestError(Exception):
    """Exception raised when a request is malformed"""
    
class InvalidUrl(Exception):
    """
    Not a valid url for use with this software.
    """
    
class TransportError(Exception):
    """Error raised by a transport """
    
class ResponseError(Exception):
    """ Error raised while getting response or decompressing response stream"""
    

{'fraction_non_alphanumeric': 0.08055624125730272, 'fraction_numerical': 0.002221673660824488, 'mean_word_length': 2.428490832157969, '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': '3780274', 'n_tokens_mistral': 3321, 'n_tokens_neox': 3192, 'n_words': 1087}