<commit_before>"""
sentry.interfaces
~~~~~~~~~~~~~~~~~

Interfaces provide an abstraction for how structured data should be
validated and rendered.

:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

import urlparse

from sentry.web.helpers import render_to_string

# unserialization concept is based on pickle
class _EmptyClass(object):
    pass

def unserialize(klass, data):
    value = _EmptyClass()
    value.__class__ = klass
    value.__setstate__(data)
    return value

class Interface(object):
    """
    An interface is a structured represntation of data, which may
    render differently than the default ``extra`` metadata in an event.
    """

    def __init__(self, **kwargs):
        self.attrs = kwargs.keys()
        self.__dict__.update(kwargs)

    def __setstate__(self, data):
        self.__dict__.update(self.unserialize(data))

    def __getstate__(self):
        return self.serialize()

    def unserialize(self, data):
        return data

    def serialize(self):
        return dict((k, self.__dict__[k]) for k in self.attrs)

    def to_html(self, event):
        return ''

    def to_string(self, event):
        return ''

class Message(Interface):
    def __init__(self, message, params):
        self.message = message
        self.params = params

    def serialize(self):
        return {
            'message': self.message,
            'params': self.params,
        }

class Query(Interface):
    def __init__(self, query, engine):
        self.query = query
        self.engine = engine

    def serialize(self):
        return {
            'query': self.query,
            'engine': self.engine,
        }

class Stacktrace(Interface):
    """
    {
        frames: {
            filename: '/real/file/name.py',
            function: 'myfunction',
            vars: {
                key: value
            },
            pre_context: [
                'line1',
                'line2'
            ],
            context_line: 'line3',
            lineno: 7,
            post_context: [
                'line4',
                'line5'
            ],
        }
    }
    """
    def __init__(self, frames):
        self.frames = frames

    def serialize(self):
        return {
            'frames': self.frames,
        }

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/stacktrace.html', {
            'frames': self.frames,
        })

    def to_string(self, event):
        r = ['Stacktrace (most recent call last):']
        for f in self.frames:
            r.append('  File "%(filename)s", line %(lineno)s, in %(function)s\n    %(context_line)s' % f)
        return '\n'.join(r)

    # TODO: abstract this  to some kind of "raw" hook for rendering
    def get_traceback(self, event):
        result = ['Traceback (most recent call last):', '']
        for frame in self.frames:
            result.append('  File "%(filename)s", line %(lineno)s, in %(function)s' % frame)
            result.append('    %s' % frame['context_line'].strip())
            result.append('')

        return '\n'.join(result)

class Exception(Interface):
    def __init__(self, type, value):
        self.type = type
        self.value = value

    def serialize(self):
        return {
            'type': self.type,
            'value': self.value,
        }

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/exception.html', {
            'exception_value': self.value,
            'exception_type': self.type,
        })

class Http(Interface):
    """
    {
        url: 'http://absolute.uri/foo',
        method: 'GET',
        data: {foo: 'bar'},
        query_string: 'hello=world&foo=bar',
        cookies: 'foo=bar',
        env: {REMOTE_ADDR: '192.168.0.1'}
    }
    """<commit_msg>Optional exception type module for Exception interface<commit_after>"""
sentry.interfaces
~~~~~~~~~~~~~~~~~

Interfaces provide an abstraction for how structured data should be
validated and rendered.

:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

import urlparse

from sentry.web.helpers import render_to_string

# unserialization concept is based on pickle
class _EmptyClass(object):
    pass

def unserialize(klass, data):
    value = _EmptyClass()
    value.__class__ = klass
    value.__setstate__(data)
    return value

class Interface(object):
    """
    An interface is a structured represntation of data, which may
    render differently than the default ``extra`` metadata in an event.
    """

    def __init__(self, **kwargs):
        self.attrs = kwargs.keys()
        self.__dict__.update(kwargs)

    def __setstate__(self, data):
        self.__dict__.update(self.unserialize(data))

    def __getstate__(self):
        return self.serialize()

    def unserialize(self, data):
        return data

    def serialize(self):
        return dict((k, self.__dict__[k]) for k in self.attrs)

    def to_html(self, event):
        return ''

    def to_string(self, event):
        return ''

class Message(Interface):
    def __init__(self, message, params):
        self.message = message
        self.params = params

    def serialize(self):
        return {
            'message': self.message,
            'params': self.params,
        }

class Query(Interface):
    def __init__(self, query, engine):
        self.query = query
        self.engine = engine

    def serialize(self):
        return {
            'query': self.query,
            'engine': self.engine,
        }

class Stacktrace(Interface):
    """
    {
        frames: {
            filename: '/real/file/name.py',
            function: 'myfunction',
            vars: {
                key: value
            },
            pre_context: [
                'line1',
                'line2'
            ],
            context_line: 'line3',
            lineno: 7,
            post_context: [
                'line4',
                'line5'
            ],
        }
    }
    """
    def __init__(self, frames):
        self.frames = frames

    def serialize(self):
        return {
            'frames': self.frames,
        }

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/stacktrace.html', {
            'frames': self.frames,
        })

    def to_string(self, event):
        r = ['Stacktrace (most recent call last):']
        for f in self.frames:
            r.append('  File "%(filename)s", line %(lineno)s, in %(function)s\n    %(context_line)s' % f)
        return '\n'.join(r)

    # TODO: abstract this  to some kind of "raw" hook for rendering
    def get_traceback(self, event):
        result = ['Traceback (most recent call last):', '']
        for frame in self.frames:
            result.append('  File "%(filename)s", line %(lineno)s, in %(function)s' % frame)
            result.append('    %s' % frame['context_line'].strip())
            result.append('')

        return '\n'.join(result)

class Exception(Interface):
    def __init__(self, type, value, module=None):
        # The exception type name (e.g. TypeError)
        self.type = type
        # A human readable value for the exception
        self.value = value
        # Optional module of the exception type (e.g. __builtin__)
        self.module = module

    def serialize(self):
        return {
            'type': self.type,
            'value': self.value,
            'module': self.module,
        }

    def to_html(self, event):
        return render_to_string('sentry/partial/interfaces/exception.html', {
            'exception_value': self.value,
            'exception_type': self.type,
            'exception_module': self.module,
        })

class Http(Interface):
    """
    {
        url: 'http://absolute.uri/foo',
        method: 'GET',
        data: {foo: 'bar'},
        query_string: 'hello=world&foo=bar',
        cookies: 'foo=bar',
        env: {REMOTE_ADDR: '192.168.0.1'}
    }
    """
{'fraction_non_alphanumeric': 0.12382483918852054, 'fraction_numerical': 0.004453240969816922, 'mean_word_length': 2.012295081967213, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6096370', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2317, 'n_words': 618}