<commit_before>import os
import time

from twill.commands import get_browser, show
from twill.errors import TwillAssertionError


__all__ = ('show_on_error', )


def show_on_error(func, clsname=None):
    """
    On ``TwillAssertionError``, show last page got by twill.

    By sets up ``TWILL_ERROR_DIR`` environment var all error pages would be
    saved on it.
    """
    def test_wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except TwillAssertionError:
            saved, showed = False, False

            if 'TWILL_ERROR_DIR' in os.environ:
                dirname = os.environ['TWILL_ERROR_DIR']

                if not os.path.isdir(dirname):
                    try:
                        os.mkdir(dirname)
                    except:
                        print 'Cannot create directory at %r.' % dirname
                        show()

                        showed = True

                if not showed:
                    timestamp = int(time.time())

                    if clsname is None:
                        filename = '%s.%s' % (func.__module__, func.__name__)
                    else:
                        filename = '%s.%s.%s' % (func.__module__,
                                                 clsname,
                                                 func.__name__)

                    filename = '%s-%d.html' % (filename, timestamp)
                    filename = os.path.join(dirname, filename)

                    try:
                        file = open(filename, 'w')
                        file.write(get_browser().get_html())
                        file.close()
                    except:
                        print 'Cannot write to file at %r.' % filename
                        show()

                        showed = True
                    else:
                        print 'Output saved to %r.' % filename
                        saved = True

            if not saved and not showed:
                show()

            raise

    test_wrapper.__name__ = func.__name__
    test_wrapper.__doc__ = func.__doc__
    test_wrapper.__dict__.update(func.__dict__)

    return test_wrapper<commit_msg>Now saves html output in ``show_on_error`` decorator via ``twill.commands.save_html`` function.<commit_after>import os
import time

from twill.commands import save_html, show
from twill.errors import TwillAssertionError


__all__ = ('show_on_error', )


def show_on_error(func, clsname=None):
    """
    On ``TwillAssertionError``, show last page got by twill.

    By sets up ``TWILL_ERROR_DIR`` environment var all error pages would be
    saved on it.
    """
    def test_wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except TwillAssertionError:
            saved, showed = False, False

            if 'TWILL_ERROR_DIR' in os.environ:
                dirname = os.environ['TWILL_ERROR_DIR']

                if not os.path.isdir(dirname):
                    try:
                        os.mkdir(dirname)
                    except:
                        print 'Cannot create directory at %r.' % dirname
                        show()

                        showed = True

                if not showed:
                    timestamp = int(time.time())

                    if clsname is None:
                        filename = '%s.%s' % (func.__module__, func.__name__)
                    else:
                        filename = '%s.%s.%s' % (func.__module__,
                                                 clsname,
                                                 func.__name__)

                    filename = '%s-%d.html' % (filename, timestamp)
                    filename = os.path.join(dirname, filename)

                    try:
                        save_html(filename)
                    except:
                        print 'Cannot write to file at %r.' % filename
                        show()

                        showed = True
                    else:
                        print 'Output saved to %r.' % filename
                        saved = True

            if not saved and not showed:
                show()

            raise

    test_wrapper.__name__ = func.__name__
    test_wrapper.__doc__ = func.__doc__
    test_wrapper.__dict__.update(func.__dict__)

    return test_wrapper
{'fraction_non_alphanumeric': 0.0820912634716808, 'fraction_numerical': 0.0, 'mean_word_length': 1.1626177491323748, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '748912', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1004, 'n_words': 292}