/usr/lib/python2.7/dist-packages/pyvows/reporting/common.py is in python-pyvows 2.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | # -*- coding: utf-8 -*-
'''Contains the `VowsDefaultReporter` class, which handles output after tests
have been run.
'''
# pyvows testing engine
# https://github.com/heynemann/pyvows
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Bernardo Heynemann heynemann@gmail.com
from __future__ import division, print_function
import re
import traceback
import sys
from pyvows.color import yellow, green, red, bold
__all__ = [
'PROGRESS_SIZE',
'V_EXTRA_VERBOSE',
'V_VERBOSE',
'V_NORMAL',
'V_SILENT',
'ensure_encoded',
'VowsReporter',
]
PROGRESS_SIZE = 50
# verbosity levels
V_EXTRA_VERBOSE = 4
V_VERBOSE = 3
V_NORMAL = 2
V_SILENT = 1
def ensure_encoded(thing, encoding='utf-8'):
'''Ensures proper encoding for unicode characters.
Currently used only for characters `✓` and `✗`.
'''
if isinstance(thing, unicode):
return thing.encode(encoding)
else:
return thing
class VowsReporter(object):
'''Base class for other Reporters to extend. Contains common attributes
and methods.
'''
# Should *only* contain attributes and methods that aren't specific
# to a particular type of report.
HONORED = green('✓')
BROKEN = red('✗')
SKIPPED = '?'
TAB = ' '
def __init__(self, result, verbosity):
self.result = result
self.verbosity = verbosity
self.indent = 1
#-------------------------------------------------------------------------
# String Formatting
#-------------------------------------------------------------------------
def camel_split(self, string):
'''Splits camel-case `string` into separate words.
Example:
self.camel_split('SomeCamelCaseString')
Returns:
'Some camel case string'
'''
return re.sub('((?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z])|(?=[0-9]\b))', ' ', string).strip()
def under_split(self, string):
'''Replaces all underscores in `string` with spaces.'''
return ' '.join(string.split('_'))
def format_traceback(self, traceback_list):
'''Adds the current level of indentation to a traceback (so it matches
the current context's indentation).
'''
# TODO:
# ...Is this a decorator? If so, please add a comment or docstring
# to make it explicit.
def _indent(msg):
if msg.strip().startswith('File'):
return self.indent_msg(msg)
return msg
tb_list = [_indent(tb) for tb in traceback_list]
return ''.join(tb_list)
def format_python_constants(self, msg):
'''Fixes capitalization of Python constants.
Since developers are used to reading `True`, `False`, and `None`
as capitalized words, it makes sense to match that capitalization
in reports.
'''
msg = msg.replace('true', 'True')
msg = msg.replace('false', 'False')
msg = msg.replace('none', 'None')
return msg
def header(self, msg, ruler_character='='):
'''Returns the string `msg` with a text "ruler". Also colorizes as
bright green (when color is available).
'''
ruler = ' {0}'.format(len(msg) * ruler_character)
msg = ' {0}'.format(msg)
msg = '{0}{ruler}{0}{msg}{0}{ruler}{0}'.format(
'\n',
ruler=ruler,
msg=msg)
msg = green(bold(msg))
return msg
def indent_msg(self, msg, indentation=None):
'''Returns `msg` with the indentation specified by `indentation`.
'''
if indentation is not None:
indent = self.TAB * indentation
else:
indent = self.TAB * self.indent
return '{indent}{msg}'.format(
indent=indent,
msg=msg
)
#-------------------------------------------------------------------------
# Printing Methods
#-------------------------------------------------------------------------
def humanized_print(self, msg, indentation=None, file=sys.stdout):
'''Passes `msg` through multiple text filters to make the output
appear more like normal text, then prints it (indented by
`indentation`).
'''
msg = self.under_split(msg)
msg = self.camel_split(msg)
msg = msg.replace(' ', ' ') # normalize spaces if inserted by
# both of the above
msg = msg.capitalize()
msg = self.format_python_constants(msg)
print(self.indent_msg(msg, indentation), file=file)
def print_traceback(self, err_type, err_obj, err_traceback, file=sys.stdout):
'''Prints a color-formatted traceback with appropriate indentation.'''
if isinstance(err_obj, AssertionError):
error_msg = err_obj
else:
error_msg = unicode(err_obj)
print(self.indent_msg(red(error_msg)), file=file)
if self.verbosity >= V_NORMAL:
traceback_msg = traceback.format_exception(err_type, err_obj, err_traceback)
traceback_msg = self.format_traceback(traceback_msg)
traceback_msg = '\n{traceback}'.format(traceback=traceback_msg)
traceback_msg = self.indent_msg(yellow(traceback_msg))
print(traceback_msg, file=file)
|