<commit_before>"""Error-message handling.

For copyright, license, and warranty, see bottom of file.
"""

import sys
from schevo.lib import optimize

from StringIO import StringIO
from traceback import print_exception

import gtk

from schevo.placeholder import Placeholder
import schevo.error
from schevo.label import label

from atfg.gtk.constants import MONO_FONT


BULLET = u'\u2022 '


class FriendlyErrorDialog(object):

    def __init__(self, parent, always_ignore=True):
        self.parent = parent
        self.always_ignore = always_ignore

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            show_error(self.parent, exc_type, exc_val, exc_tb)
        if hasattr(sys, 'frozen'):
            # Ignore exception if running in py2exe environment.
            return self.always_ignore
        else:
            return False


def dereference(obj):
    if isinstance(obj, Placeholder) and obj.entity is not None:
        return obj.entity
    else:
        return obj


def escape(s):
    s = (unicode(s)
         .replace('&', '&amp;')
         .replace('<', '&lt;')
         .replace('>', '&gt;')
         )
    return s


def show_error(parent, exc_type, exc_val, exc_tb):
    try:
        # Override for specific error types.
        if issubclass(exc_type, schevo.error.DeleteRestricted):
            markup = [
                u'The object was not deleted from the database.\n'
                u'\n'
                u'It is referred to by the following objects,\n'
                u'which require you to delete them first before\n'
                u'deleting this object.\n'
                u'\n'
                ]
            for entity, ref_entity, ref_field_name in exc_val.restrictions:
                markup.append(BULLET + '<b>%s</b>\n'
                              % escape(dereference(entity)))
        elif issubclass(exc_type, schevo.error.FieldReadonly):
            markup = [
                u'The <b>%s</b> field is readonly and cannot be changed.'
                % escape(label(exc_val.field))
                ]<commit_msg>Sort restrictions before displaying them<commit_after>"""Error-message handling.

For copyright, license, and warranty, see bottom of file.
"""

import sys
from schevo.lib import optimize

from StringIO import StringIO
from traceback import print_exception

import gtk

from schevo.placeholder import Placeholder
import schevo.error
from schevo.label import label

from atfg.gtk.constants import MONO_FONT


BULLET = u'\u2022 '


class FriendlyErrorDialog(object):

    def __init__(self, parent, always_ignore=True):
        self.parent = parent
        self.always_ignore = always_ignore

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            show_error(self.parent, exc_type, exc_val, exc_tb)
        if hasattr(sys, 'frozen'):
            # Ignore exception if running in py2exe environment.
            return self.always_ignore
        else:
            return False


def dereference(obj):
    if isinstance(obj, Placeholder) and obj.entity is not None:
        return obj.entity
    else:
        return obj


def escape(s):
    s = (unicode(s)
         .replace('&', '&amp;')
         .replace('<', '&lt;')
         .replace('>', '&gt;')
         )
    return s


def show_error(parent, exc_type, exc_val, exc_tb):
    try:
        # Override for specific error types.
        if issubclass(exc_type, schevo.error.DeleteRestricted):
            markup = [
                u'The object was not deleted from the database.\n'
                u'\n'
                u'It is referred to by the following objects,\n'
                u'which require you to delete them first before\n'
                u'deleting this object.\n'
                u'\n'
                ]
            restrictions = sorted(exc_val.restrictions)
            for entity, ref_entity, ref_field_name in restrictions:
                markup.append(BULLET + '<b>%s</b>\n'
                              % escape(dereference(entity)))
        elif issubclass(exc_type, schevo.error.FieldReadonly):
            markup = [
                u'The <b>%s</b> field is readonly and cannot be changed.'
                % escape(label(exc_val.field))
                ]
{'fraction_non_alphanumeric': 0.09266498045527707, 'fraction_numerical': 0.002299379167624741, 'mean_word_length': 2.059071729957806, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2928666', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1239, 'n_words': 366}