<s> """<STR_LIT>""" <EOL> import os , traceback <EOL> try : <EOL> import cStringIO as StringIO <EOL> except ImportError : <EOL> import StringIO <EOL> from twisted import copyright <EOL> from twisted . python . compat import execfile <EOL> from twisted . web import http , server , static , resource , html <EOL> rpyNoResource = """<STR_LIT>""" <EOL> class AlreadyCached ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class CacheScanner : <EOL> def __init__ ( self , path , registry ) : <EOL> self . path = path <EOL> self . registry = registry <EOL> self . doCache = <NUM_LIT:0> <EOL> def cache ( self ) : <EOL> c = self . registry . getCachedPath ( self . path ) <EOL> if c is not None : <EOL> raise AlreadyCached ( c ) <EOL> self . recache ( ) <EOL> def recache ( self ) : <EOL> self . doCache = <NUM_LIT:1> <EOL> noRsrc = resource . ErrorPage ( <NUM_LIT> , "<STR_LIT>" , rpyNoResource ) <EOL> def ResourceScript ( path , registry ) : <EOL> """<STR_LIT>""" <EOL> cs = CacheScanner ( path , registry ) <EOL> glob = { '<STR_LIT>' : path , <EOL> '<STR_LIT>' : noRsrc , <EOL> '<STR_LIT>' : registry , <EOL> '<STR_LIT>' : cs . cache , <EOL> '<STR_LIT>' : cs . recache } <EOL> try : <EOL> execfile ( path , glob , glob ) <EOL> except AlreadyCached , ac : <EOL> return ac . args [ <NUM_LIT:0> ] <EOL> rsrc = glob [ '<STR_LIT>' ] <EOL> if cs . doCache and rsrc is not noRsrc : <EOL> registry . cachePath ( path , rsrc ) <EOL> return rsrc <EOL> def ResourceTemplate ( path , registry ) : <EOL> from quixote import ptl_compile <EOL> glob = { '<STR_LIT>' : path , <EOL> '<STR_LIT>' : resource . ErrorPage ( <NUM_LIT> , "<STR_LIT>" , <EOL> rpyNoResource ) , <EOL> '<STR_LIT>' : registry } <EOL> e = ptl_compile . compile_template ( open ( path ) , path ) <EOL> exec e in glob <EOL> return glob [ '<STR_LIT>' ] <EOL> class ResourceScriptWrapper ( resource . Resource ) : <EOL> def __init__ ( self , path , registry = None ) : <EOL> resource . Resource . __init__ ( self ) <EOL> self . path = path <EOL> self . registry = registry or static . Registry ( ) <EOL> def render ( self , request ) : <EOL> res = ResourceScript ( self . path , self . registry ) <EOL> return res . render ( request ) <EOL> def getChildWithDefault ( self , path , request ) : <EOL> res = ResourceScript ( self . path , self . registry ) <EOL> return res . getChildWithDefault ( path , request ) <EOL> class ResourceScriptDirectory ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pathname , registry = None ) : <EOL> resource . Resource . __init__ ( self ) <EOL> self . path = pathname <EOL> self . registry = registry or static . Registry ( ) <EOL> def getChild ( self , path , request ) : <EOL> fn = os . path . join ( self . path , path ) <EOL> if os . path . isdir ( fn ) : <EOL> return ResourceScriptDirectory ( fn , self . registry ) <EOL> if os . path . exists ( fn ) : <EOL> return ResourceScript ( fn , self . registry ) <EOL> return resource . NoResource ( ) <EOL> def render ( self , request ) : <EOL> return resource . NoResource ( ) . render ( request ) <EOL> class PythonScript ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> isLeaf = <NUM_LIT:1> <EOL> def __init__ ( self , filename , registry ) : <EOL> """<STR_LIT>""" <EOL> self . filename = filename <EOL> self . registry = registry <EOL> def render ( self , request ) : <EOL> """<STR_LIT>""" <EOL> request . setHeader ( "<STR_LIT>" , "<STR_LIT>" % copyright . version ) <EOL> namespace = { '<STR_LIT>' : request , <EOL> '<STR_LIT>' : self . filename , <EOL> '<STR_LIT>' : self . registry } <EOL> try : <EOL> execfile ( self . filename , namespace , namespace ) <EOL> except IOError , e : <EOL> if e . errno == <NUM_LIT:2> : <EOL> request . setResponseCode ( http . NOT_FOUND ) <EOL> request . write ( resource . NoResource ( "<STR_LIT>" ) . render ( request ) ) <EOL> except : <EOL> io = StringIO . StringIO ( ) <EOL> traceback . print_exc ( file = io ) <EOL> request . write ( html . PRE ( io . getvalue ( ) ) ) <EOL> request . finish ( ) <EOL> return server . NOT_DONE_YET </s>
