<s> """<STR_LIT>""" <EOL> import os , sys , time , re <EOL> import types <EOL> import pprint <EOL> import socket <EOL> import httplib <EOL> import traceback <EOL> from unittest import * <EOL> from unittest import _TextTestResult <EOL> class TerseTestResult ( _TextTestResult ) : <EOL> def printErrors ( self ) : <EOL> if self . errors or self . failures : <EOL> if self . dots or self . showAll : <EOL> self . stream . writeln ( ) <EOL> self . printErrorList ( '<STR_LIT>' , self . errors ) <EOL> self . printErrorList ( '<STR_LIT>' , self . failures ) <EOL> class TerseTestRunner ( TextTestRunner ) : <EOL> """<STR_LIT>""" <EOL> def _makeResult ( self ) : <EOL> return TerseTestResult ( self . stream , self . descriptions , self . verbosity ) <EOL> def run ( self , test ) : <EOL> "<STR_LIT>" <EOL> result = self . _makeResult ( ) <EOL> startTime = time . time ( ) <EOL> test ( result ) <EOL> timeTaken = float ( time . time ( ) - startTime ) <EOL> result . printErrors ( ) <EOL> if not result . wasSuccessful ( ) : <EOL> self . stream . write ( "<STR_LIT>" ) <EOL> failed , errored = map ( len , ( result . failures , result . errors ) ) <EOL> if failed : <EOL> self . stream . write ( "<STR_LIT>" % failed ) <EOL> if errored : <EOL> if failed : self . stream . write ( "<STR_LIT:U+002CU+0020>" ) <EOL> self . stream . write ( "<STR_LIT>" % errored ) <EOL> self . stream . writeln ( "<STR_LIT:)>" ) <EOL> return result <EOL> class ReloadingTestLoader ( TestLoader ) : <EOL> def loadTestsFromName ( self , name , module = None ) : <EOL> """<STR_LIT>""" <EOL> parts = name . split ( '<STR_LIT:.>' ) <EOL> if module is None : <EOL> if not parts : <EOL> raise ValueError ( "<STR_LIT>" % name ) <EOL> else : <EOL> parts_copy = parts [ : ] <EOL> while parts_copy : <EOL> target = "<STR_LIT:.>" . join ( parts_copy ) <EOL> if target in sys . modules : <EOL> module = reload ( sys . modules [ target ] ) <EOL> break <EOL> else : <EOL> try : <EOL> module = __import__ ( target ) <EOL> break <EOL> except ImportError : <EOL> del parts_copy [ - <NUM_LIT:1> ] <EOL> if not parts_copy : <EOL> raise <EOL> parts = parts [ <NUM_LIT:1> : ] <EOL> obj = module <EOL> for part in parts : <EOL> obj = getattr ( obj , part ) <EOL> if type ( obj ) == types . ModuleType : <EOL> return self . loadTestsFromModule ( obj ) <EOL> elif ( isinstance ( obj , ( type , types . ClassType ) ) and <EOL> issubclass ( obj , TestCase ) ) : <EOL> return self . loadTestsFromTestCase ( obj ) <EOL> elif type ( obj ) == types . UnboundMethodType : <EOL> return obj . im_class ( obj . __name__ ) <EOL> elif callable ( obj ) : <EOL> test = obj ( ) <EOL> if not isinstance ( test , TestCase ) and not isinstance ( test , TestSuite ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( obj , test ) ) <EOL> return test <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % obj ) <EOL> try : <EOL> import msvcrt <EOL> def getchar ( ) : <EOL> return msvcrt . getch ( ) <EOL> except ImportError : <EOL> import tty , termios <EOL> def getchar ( ) : <EOL> fd = sys . stdin . fileno ( ) <EOL> old_settings = termios . tcgetattr ( fd ) <EOL> try : <EOL> tty . setraw ( sys . stdin . fileno ( ) ) <EOL> ch = sys . stdin . read ( <NUM_LIT:1> ) <EOL> finally : <EOL> termios . tcsetattr ( fd , termios . TCSADRAIN , old_settings ) <EOL> return ch <EOL> class WebCase ( TestCase ) : <EOL> HOST = "<STR_LIT:127.0.0.1>" <EOL> PORT = <NUM_LIT> <EOL> HTTP_CONN = httplib . HTTPConnection <EOL> def getPage ( self , url , headers = None , method = "<STR_LIT:GET>" , body = None ) : <EOL> """<STR_LIT>""" <EOL> ServerError . on = False <EOL> self . url = url <EOL> result = openURL ( url , headers , method , body , self . HOST , self . PORT , <EOL> self . HTTP_CONN ) <EOL> self . status , self . headers , self . body = result <EOL> self . cookies = [ ( '<STR_LIT>' , v ) for k , v in self . headers <EOL> if k . lower ( ) == '<STR_LIT>' ] <EOL> if ServerError . on : <EOL> raise ServerError ( ) <EOL> return result <EOL> interactive = True <EOL> console_height = <NUM_LIT:30> <EOL> def _handlewebError ( self , msg ) : <EOL> if not self . interactive : <EOL> raise self . failureException ( msg ) <EOL> print <EOL> print "<STR_LIT>" , msg <EOL> p = "<STR_LIT>" <EOL> print p , <EOL> while True : <EOL> i = getchar ( ) . upper ( ) <EOL> if i not in "<STR_LIT>" : <EOL> continue <EOL> print i . upper ( ) <EOL> if i == "<STR_LIT:B>" : <EOL> for x , line in enumerate ( self . body . splitlines ( ) ) : <EOL> if ( x + <NUM_LIT:1> ) % self . console_height == <NUM_LIT:0> : <EOL> print "<STR_LIT>" , <EOL> m = getchar ( ) . lower ( ) <EOL> print "<STR_LIT>" , <EOL> if m == "<STR_LIT:q>" : <EOL> break <EOL> print line <EOL> elif i == "<STR_LIT:H>" : <EOL> pprint . pprint ( self . headers ) <EOL> elif i == "<STR_LIT:S>" : <EOL> print self . status <EOL> elif i == "<STR_LIT>" : <EOL> print self . url <EOL> elif i == "<STR_LIT:I>" : <EOL> return <EOL> elif i == "<STR_LIT:R>" : <EOL> raise self . failureException ( msg ) <EOL> elif i == "<STR_LIT:X>" : <EOL> self . exit ( ) <EOL> print p , <EOL> def exit ( self ) : <EOL> sys . exit ( ) <EOL> def __call__ ( self , result = None ) : <EOL> if result is None : <EOL> result = self . defaultTestResult ( ) <EOL> result . startTest ( self ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> testMethod = getattr ( self , self . _testMethodName ) <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> testMethod = getattr ( self , self . _TestCase__testMethodName ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" % self ) <EOL> try : <EOL> try : <EOL> self . setUp ( ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> result . addError ( self , self . _TestCase__exc_info ( ) ) <EOL> return <EOL> ok = <NUM_LIT:0> <EOL> try : <EOL> testMethod ( ) <EOL> ok = <NUM_LIT:1> <EOL> except self . failureException : <EOL> result . addFailure ( self , self . _TestCase__exc_info ( ) ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> result . addError ( self , self . _TestCase__exc_info ( ) ) <EOL> try : <EOL> self . tearDown ( ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> result . addError ( self , self . _TestCase__exc_info ( ) ) <EOL> ok = <NUM_LIT:0> <EOL> if ok : <EOL> result . addSuccess ( self ) <EOL> finally : <EOL> result . stopTest ( self ) <EOL> def assertStatus ( self , status , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( status , basestring ) : <EOL> if not self . status == status : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' % ( ` self . status ` , ` status ` ) <EOL> self . _handlewebError ( msg ) <EOL> else : <EOL> if not self . status in status : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' % ( ` self . status ` , ` status ` ) <EOL> self . _handlewebError ( msg ) <EOL> def assertHeader ( self , key , value = None , msg = None ) : <EOL> """<STR_LIT>""" <EOL> lowkey = key . lower ( ) <EOL> for k , v in self . headers : <EOL> if k . lower ( ) == lowkey : <EOL> if value is None or str ( value ) == v : <EOL> return <EOL> if msg is None : <EOL> if value is None : <EOL> msg = '<STR_LIT>' % ` key ` <EOL> else : <EOL> msg = '<STR_LIT>' % ( ` key ` , ` value ` ) <EOL> self . _handlewebError ( msg ) <EOL> def assertNoHeader ( self , key , msg = None ) : <EOL> """<STR_LIT>""" <EOL> lowkey = key . lower ( ) <EOL> matches = [ k for k , v in self . headers if k . lower ( ) == lowkey ] <EOL> if matches : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' % ` key ` <EOL> self . _handlewebError ( msg ) <EOL> def assertBody ( self , value , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if value != self . body : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' % ( ` value ` , ` self . body ` ) <EOL> self . _handlewebError ( msg ) <EOL> def assertInBody ( self , value , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if value not in self . body : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' % ` value ` <EOL> self . _handlewebError ( msg ) <EOL> def assertNotInBody ( self , value , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if value in self . body : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' % ` value ` <EOL> self . _handlewebError ( msg ) <EOL> def assertMatchesBody ( self , pattern , msg = None , flags = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if re . search ( pattern , self . body , flags ) is None : <EOL> if msg is None : <EOL> msg = '<STR_LIT>' % ` pattern ` <EOL> self . _handlewebError ( msg ) <EOL> def cleanHeaders ( headers , method , body , host , port ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = [ ] <EOL> found = False <EOL> for k , v in headers : <EOL> if k . lower ( ) == '<STR_LIT:host>' : <EOL> found = True <EOL> break <EOL> if not found : <EOL> headers . append ( ( "<STR_LIT>" , "<STR_LIT>" % ( host , port ) ) ) <EOL> if method in ( "<STR_LIT:POST>" , "<STR_LIT>" ) : <EOL> found = False <EOL> for k , v in headers : <EOL> if k . lower ( ) == '<STR_LIT>' : <EOL> found = True <EOL> break <EOL> if not found : <EOL> headers . append ( ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) ) <EOL> headers . append ( ( "<STR_LIT>" , str ( len ( body or "<STR_LIT>" ) ) ) ) <EOL> return headers <EOL> def openURL ( url , headers = None , method = "<STR_LIT:GET>" , body = None , <EOL> host = "<STR_LIT:127.0.0.1>" , port = <NUM_LIT> , http_conn = httplib . HTTPConnection ) : <EOL> """<STR_LIT>""" <EOL> headers = cleanHeaders ( headers , method , body , host , port ) <EOL> trial = <NUM_LIT:0> <EOL> while trial < <NUM_LIT:10> : <EOL> try : <EOL> conn = http_conn ( host , port ) <EOL> conn . putrequest ( method . upper ( ) , url ) <EOL> for key , value in headers : <EOL> conn . putheader ( key , value ) <EOL> conn . endheaders ( ) <EOL> if body is not None : <EOL> conn . send ( body ) <EOL> response = conn . getresponse ( ) <EOL> status = "<STR_LIT>" % ( response . status , response . reason ) <EOL> outheaders = [ ] <EOL> for line in response . msg . headers : <EOL> key , value = line . split ( "<STR_LIT::>" , <NUM_LIT:1> ) <EOL> outheaders . append ( ( key . strip ( ) , value . strip ( ) ) ) <EOL> outbody = response . read ( ) <EOL> conn . close ( ) <EOL> return status , outheaders , outbody <EOL> except socket . error : <EOL> trial += <NUM_LIT:1> <EOL> if trial >= <NUM_LIT:10> : <EOL> raise <EOL> else : <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> ignored_exceptions = [ ] <EOL> ignore_all = False <EOL> class ServerError ( Exception ) : <EOL> on = False <EOL> def server_error ( exc = None ) : <EOL> """<STR_LIT>""" <EOL> if exc is None : <EOL> exc = sys . exc_info ( ) <EOL> if ignore_all or exc [ <NUM_LIT:0> ] in ignored_exceptions : <EOL> return False <EOL> else : <EOL> ServerError . on = True <EOL> print <EOL> print "<STR_LIT>" . join ( traceback . format_exception ( * exc ) ) <EOL> return True </s>
