<s> import gettext <EOL> import os <EOL> import unittest <EOL> import six <EOL> import sys <EOL> import types <EOL> import logging <EOL> from nose import result <EOL> from nose import core <EOL> from nose import config <EOL> class _AnsiColorizer ( object ) : <EOL> """<STR_LIT>""" <EOL> _colors = dict ( black = <NUM_LIT:30> , red = <NUM_LIT> , green = <NUM_LIT:32> , yellow = <NUM_LIT> , <EOL> blue = <NUM_LIT> , magenta = <NUM_LIT> , cyan = <NUM_LIT> , white = <NUM_LIT> ) <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def supported ( cls , stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> if not stream . isatty ( ) : <EOL> return False <EOL> try : <EOL> import curses <EOL> except ImportError : <EOL> return False <EOL> else : <EOL> try : <EOL> try : <EOL> return curses . tigetnum ( "<STR_LIT>" ) > <NUM_LIT:2> <EOL> except curses . error : <EOL> curses . setupterm ( ) <EOL> return curses . tigetnum ( "<STR_LIT>" ) > <NUM_LIT:2> <EOL> except : <EOL> return False <EOL> supported = classmethod ( supported ) <EOL> def write ( self , text , color ) : <EOL> """<STR_LIT>""" <EOL> color = self . _colors [ color ] <EOL> self . stream . write ( '<STR_LIT>' % ( color , text ) ) <EOL> class _Win32Colorizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> from win32console import GetStdHandle , STD_OUT_HANDLE <EOL> from win32console import FOREGROUND_RED , FOREGROUND_BLUE <EOL> from win32console import FOREGROUND_GREEN , FOREGROUND_INTENSITY <EOL> red , green , blue , bold = ( FOREGROUND_RED , FOREGROUND_GREEN , <EOL> FOREGROUND_BLUE , FOREGROUND_INTENSITY ) <EOL> self . stream = stream <EOL> self . screenBuffer = GetStdHandle ( STD_OUT_HANDLE ) <EOL> self . _colors = { <EOL> '<STR_LIT>' : red | green | blue , <EOL> '<STR_LIT>' : red | bold , <EOL> '<STR_LIT>' : green | bold , <EOL> '<STR_LIT>' : blue | bold , <EOL> '<STR_LIT>' : red | green | bold , <EOL> '<STR_LIT>' : red | blue | bold , <EOL> '<STR_LIT>' : green | blue | bold , <EOL> '<STR_LIT>' : red | green | blue | bold } <EOL> def supported ( cls , stream = sys . stdout ) : <EOL> try : <EOL> import win32console <EOL> screenBuffer = win32console . GetStdHandle ( <EOL> win32console . STD_OUT_HANDLE ) <EOL> except ImportError : <EOL> return False <EOL> import pywintypes <EOL> try : <EOL> screenBuffer . SetConsoleTextAttribute ( <EOL> win32console . FOREGROUND_RED | <EOL> win32console . FOREGROUND_GREEN | <EOL> win32console . FOREGROUND_BLUE ) <EOL> except pywintypes . error : <EOL> return False <EOL> else : <EOL> return True <EOL> supported = classmethod ( supported ) <EOL> def write ( self , text , color ) : <EOL> color = self . _colors [ color ] <EOL> self . screenBuffer . SetConsoleTextAttribute ( color ) <EOL> self . stream . write ( text ) <EOL> self . screenBuffer . SetConsoleTextAttribute ( self . _colors [ '<STR_LIT>' ] ) <EOL> class _NullColorizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def supported ( cls , stream = sys . stdout ) : <EOL> return True <EOL> supported = classmethod ( supported ) <EOL> def write ( self , text , color ) : <EOL> self . stream . write ( text ) <EOL> class RyuTestResult ( result . TextTestResult ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> result . TextTestResult . __init__ ( self , * args , ** kw ) <EOL> self . _last_case = None <EOL> self . colorizer = None <EOL> stdout = sys . __stdout__ <EOL> sys . stdout = sys . __stdout__ <EOL> for colorizer in [ _Win32Colorizer , _AnsiColorizer , _NullColorizer ] : <EOL> if colorizer . supported ( ) : <EOL> self . colorizer = colorizer ( self . stream ) <EOL> break <EOL> sys . stdout = stdout <EOL> def getDescription ( self , test ) : <EOL> return str ( test ) <EOL> def addSuccess ( self , test ) : <EOL> unittest . TestResult . addSuccess ( self , test ) <EOL> if self . showAll : <EOL> self . colorizer . write ( "<STR_LIT:OK>" , '<STR_LIT>' ) <EOL> self . stream . writeln ( ) <EOL> elif self . dots : <EOL> self . stream . write ( '<STR_LIT:.>' ) <EOL> self . stream . flush ( ) <EOL> def addFailure ( self , test , err ) : <EOL> unittest . TestResult . addFailure ( self , test , err ) <EOL> if self . showAll : <EOL> self . colorizer . write ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> self . stream . writeln ( ) <EOL> elif self . dots : <EOL> self . stream . write ( '<STR_LIT:F>' ) <EOL> self . stream . flush ( ) <EOL> def addError ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> stream = getattr ( self , '<STR_LIT>' , None ) <EOL> ec , ev , tb = err <EOL> try : <EOL> exc_info = self . _exc_info_to_string ( err , test ) <EOL> except TypeError : <EOL> exc_info = self . _exc_info_to_string ( err ) <EOL> for cls , ( storage , label , isfail ) in self . errorClasses . items ( ) : <EOL> if result . isclass ( ec ) and issubclass ( ec , cls ) : <EOL> if isfail : <EOL> test . passwd = False <EOL> storage . append ( ( test , exc_info ) ) <EOL> if stream is not None : <EOL> if self . showAll : <EOL> message = [ label ] <EOL> detail = result . _exception_detail ( err [ <NUM_LIT:1> ] ) <EOL> if detail : <EOL> message . append ( detail ) <EOL> stream . writeln ( "<STR_LIT>" . join ( message ) ) <EOL> elif self . dots : <EOL> stream . write ( label [ : <NUM_LIT:1> ] ) <EOL> return <EOL> self . errors . append ( ( test , exc_info ) ) <EOL> test . passed = False <EOL> if stream is not None : <EOL> if self . showAll : <EOL> self . colorizer . write ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> self . stream . writeln ( ) <EOL> elif self . dots : <EOL> stream . write ( '<STR_LIT:E>' ) <EOL> def startTest ( self , test ) : <EOL> unittest . TestResult . startTest ( self , test ) <EOL> current_case = test . test . __class__ . __name__ <EOL> if self . showAll : <EOL> if current_case != self . _last_case : <EOL> self . stream . writeln ( current_case ) <EOL> self . _last_case = current_case <EOL> self . stream . write ( <EOL> '<STR_LIT>' % str ( test . test ) . ljust ( <NUM_LIT> ) ) <EOL> self . stream . flush ( ) <EOL> class RyuTestRunner ( core . TextTestRunner ) : <EOL> def _makeResult ( self ) : <EOL> return RyuTestResult ( self . stream , <EOL> self . descriptions , <EOL> self . verbosity , <EOL> self . config ) <EOL> def run_tests ( c = None ) : <EOL> logger = logging . getLogger ( ) <EOL> hdlr = logging . StreamHandler ( ) <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> hdlr . setFormatter ( formatter ) <EOL> logger . addHandler ( hdlr ) <EOL> logger . setLevel ( logging . DEBUG ) <EOL> if not c : <EOL> return True <EOL> runner = RyuTestRunner ( stream = c . stream , <EOL> verbosity = c . verbosity , <EOL> config = c ) <EOL> return not core . run ( config = c , testRunner = runner ) <EOL> def add_method ( cls , method_name , method ) : <EOL> """<STR_LIT>""" <EOL> method . func_name = method_name <EOL> method . __name__ = method_name <EOL> if six . PY3 : <EOL> methodtype = types . MethodType ( method , cls ) <EOL> else : <EOL> methodtype = types . MethodType ( method , None , cls ) <EOL> setattr ( cls , method_name , methodtype ) </s>
