<s> from twisted . web . resource import Resource <EOL> import simplejson <EOL> from twisted . internet . defer import DeferredList <EOL> from twisted . web import server <EOL> import traceback <EOL> from datetime import datetime <EOL> from twisted . python . failure import Failure <EOL> import cStringIO , gzip <EOL> class DataResource ( Resource ) : <EOL> isLeaf = True <EOL> def __init__ ( self , spider ) : <EOL> self . spider = spider <EOL> Resource . __init__ ( self ) <EOL> def render ( self , request ) : <EOL> request . setHeader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if len ( request . postpath ) > <NUM_LIT:0> : <EOL> if request . postpath [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> return simplejson . dumps ( self . spider . getServerData ( ) ) <EOL> elif request . postpath [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> return simplejson . dumps ( self . spider . getExposedFunctionDetails ( ) ) <EOL> elif request . postpath [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if "<STR_LIT>" in request . args : <EOL> get_deferreds = [ ] <EOL> for uuid in request . args [ "<STR_LIT>" ] : <EOL> get_deferreds . append ( self . spider . get ( uuid ) ) <EOL> d = DeferredList ( get_deferreds , consumeErrors = True ) <EOL> d . addCallback ( self . _getCallback , request . args [ "<STR_LIT>" ] ) <EOL> d . addCallback ( self . _successResponse ) <EOL> d . addErrback ( self . _errorResponse ) <EOL> d . addCallback ( self . _immediateResponse , request ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> return self . _errorResponse ( Failure ( exc_value = Exception ( "<STR_LIT>" ) ) ) <EOL> elif request . postpath [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if "<STR_LIT>" in request . args : <EOL> d = self . spider . showReservation ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> d . addCallback ( self . _successResponse ) <EOL> d . addErrback ( self . _errorResponse ) <EOL> d . addCallback ( self . _immediateResponse , request ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> return self . _errorResponse ( Failure ( exc_value = Exception ( "<STR_LIT>" ) ) ) <EOL> elif request . postpath [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if "<STR_LIT>" in request . args : <EOL> d = self . spider . deleteReservation ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> d . addCallback ( self . _successResponse ) <EOL> d . addErrback ( self . _errorResponse ) <EOL> d . addCallback ( self . _immediateResponse , request ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> return self . _errorResponse ( Failure ( exc_value = Exception ( "<STR_LIT>" ) ) ) <EOL> elif request . postpath [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if "<STR_LIT>" in request . args : <EOL> d = self . spider . deleteFunctionReservations ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> d . addCallback ( self . _successResponse ) <EOL> d . addErrback ( self . _errorResponse ) <EOL> d . addCallback ( self . _immediateResponse , request ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> return self . _errorResponse ( Failure ( exc_value = Exception ( "<STR_LIT>" ) ) ) <EOL> elif request . postpath [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if "<STR_LIT>" in request . args : <EOL> d = self . spider . queryByUUID ( request . args [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> d . addCallback ( self . _successResponse ) <EOL> d . addErrback ( self . _errorResponse ) <EOL> d . addCallback ( self . _immediateResponse , request ) <EOL> return server . NOT_DONE_YET <EOL> else : <EOL> return self . _errorResponse ( Failure ( exc_value = Exception ( "<STR_LIT>" ) ) ) <EOL> def _getCallback ( self , data , uuids ) : <EOL> response = { } <EOL> for i in range ( <NUM_LIT:0> , len ( uuids ) ) : <EOL> if data [ i ] [ <NUM_LIT:0> ] == True : <EOL> response [ uuids [ i ] ] = data [ i ] [ <NUM_LIT:1> ] <EOL> else : <EOL> response [ uuids [ i ] ] = { "<STR_LIT:error>" : str ( data [ i ] [ <NUM_LIT:1> ] . value ) } <EOL> return response <EOL> def _successResponse ( self , data ) : <EOL> return simplejson . dumps ( data ) <EOL> def _errorResponse ( self , error ) : <EOL> reason = str ( error . value ) <EOL> tb = traceback . format_exc ( traceback . extract_tb ( error . tb ) ) <EOL> return simplejson . dumps ( { "<STR_LIT:error>" : reason , "<STR_LIT>" : tb } ) <EOL> def _immediateResponse ( self , data , request ) : <EOL> encoding = request . getHeader ( "<STR_LIT>" ) <EOL> if encoding and "<STR_LIT>" in encoding : <EOL> zbuf = cStringIO . StringIO ( ) <EOL> zfile = gzip . GzipFile ( None , '<STR_LIT:wb>' , <NUM_LIT:9> , zbuf ) <EOL> if isinstance ( data , unicode ) : <EOL> zfile . write ( unicode ( data ) . encode ( "<STR_LIT:utf-8>" ) ) <EOL> elif isinstance ( data , str ) : <EOL> zfile . write ( unicode ( data , '<STR_LIT:utf-8>' ) . encode ( "<STR_LIT:utf-8>" ) ) <EOL> else : <EOL> zfile . write ( unicode ( data ) . encode ( "<STR_LIT:utf-8>" ) ) <EOL> zfile . close ( ) <EOL> request . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> request . write ( zbuf . getvalue ( ) ) <EOL> else : <EOL> request . write ( data ) <EOL> request . finish ( ) </s>
