<s> import tornado <EOL> import logging <EOL> import httplib <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from octopus . core . framework . wsappframework import WSAppFramework , MainLoopApplication <EOL> from octopus . core . framework . webservice import MappingSet <EOL> from octopus . core . communication . http import Http400 <EOL> from octopus . core . tools import Workload <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> __all__ += [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def queue ( func ) : <EOL> def queued_func ( self , * args , ** kwargs ) : <EOL> return self . queueAndWait ( func , self , * args , ** kwargs ) <EOL> return queued_func <EOL> class ControllerError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ResourceNotFoundError ( ControllerError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> Exception . __init__ ( self , * args ) <EOL> for name , value in kwargs . items ( ) : <EOL> setattr ( self , name , value ) <EOL> class Controller ( object ) : <EOL> def __init__ ( self , framework , root ) : <EOL> self . framework = framework <EOL> self . root = root <EOL> self . mappings = MappingSet ( ) <EOL> def __call__ ( self , request , path , * args ) : <EOL> return self . mappings . match ( request , path , * args ) <EOL> def getDispatchTree ( self ) : <EOL> return self . framework . application . dispatchTree <EOL> def map ( self , pathPattern , methodDict ) : <EOL> self . mappings . add ( ( pathPattern , methodDict ) ) <EOL> class BaseResource ( tornado . web . RequestHandler ) : <EOL> def initialize ( self , framework ) : <EOL> self . framework = framework <EOL> def getDispatchTree ( self ) : <EOL> return self . framework . application . dispatchTree <EOL> def get_error_html ( self , status_code , exception = None , ** kwargs ) : <EOL> message = httplib . responses [ status_code ] <EOL> if exception is not None and isinstance ( exception , tornado . web . HTTPError ) : <EOL> message = exception . log_message <EOL> return "<STR_LIT>" % { <EOL> "<STR_LIT:code>" : status_code , <EOL> "<STR_LIT:message>" : message , <EOL> } <EOL> @ property <EOL> def dispatcher ( self ) : <EOL> return self . framework . application <EOL> def getBodyAsJSON ( self ) : <EOL> try : <EOL> if self . request . body == "<STR_LIT>" or self . request . body is None : <EOL> return "<STR_LIT>" <EOL> return json . loads ( self . request . body ) <EOL> except : <EOL> raise Http400 ( "<STR_LIT>" ) <EOL> def getServerAddress ( self ) : <EOL> server_address = self . request . host . split ( '<STR_LIT::>' ) <EOL> if len ( server_address ) == <NUM_LIT:2> : <EOL> return server_address [ <NUM_LIT:0> ] , server_address [ <NUM_LIT:1> ] <EOL> return server_address [ <NUM_LIT:0> ] , "<STR_LIT>" <EOL> def queueAndWait ( self , func , * args ) : <EOL> workload = Workload ( lambda : func ( * args ) ) <EOL> self . framework . application . queueWorkload ( workload ) <EOL> return workload . wait ( ) <EOL> def writeCallback ( self , chunk ) : <EOL> data = self . request . arguments <EOL> if '<STR_LIT>' in data : <EOL> chunk = ( '<STR_LIT>' % ( data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , chunk ) ) <EOL> self . write ( chunk ) </s>
