<s> """<STR_LIT>""" <EOL> from twisted . internet import defer <EOL> from twisted . python . failure import Failure <EOL> from twisted . spread import pb <EOL> from twisted . protocols import basic <EOL> from twisted . internet import interfaces <EOL> from zope . interface import implements <EOL> class LocalMethod : <EOL> def __init__ ( self , local , name ) : <EOL> self . local = local <EOL> self . name = name <EOL> def __call__ ( self , * args , ** kw ) : <EOL> return self . local . callRemote ( self . name , * args , ** kw ) <EOL> class LocalAsRemote : <EOL> """<STR_LIT>""" <EOL> reportAllTracebacks = <NUM_LIT:1> <EOL> def callRemote ( self , name , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' + name ) : <EOL> return getattr ( self , '<STR_LIT>' + name ) ( * args , ** kw ) <EOL> try : <EOL> method = getattr ( self , "<STR_LIT>" + name ) <EOL> return defer . succeed ( method ( * args , ** kw ) ) <EOL> except : <EOL> f = Failure ( ) <EOL> if self . reportAllTracebacks : <EOL> f . printTraceback ( ) <EOL> return defer . fail ( f ) <EOL> def remoteMethod ( self , name ) : <EOL> return LocalMethod ( self , name ) <EOL> class LocalAsyncForwarder : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , forwarded , interfaceClass , failWhenNotImplemented = <NUM_LIT:0> ) : <EOL> assert interfaceClass . providedBy ( forwarded ) <EOL> self . forwarded = forwarded <EOL> self . interfaceClass = interfaceClass <EOL> self . failWhenNotImplemented = failWhenNotImplemented <EOL> def _callMethod ( self , method , * args , ** kw ) : <EOL> return getattr ( self . forwarded , method ) ( * args , ** kw ) <EOL> def callRemote ( self , method , * args , ** kw ) : <EOL> if self . interfaceClass . queryDescriptionFor ( method ) : <EOL> result = defer . maybeDeferred ( self . _callMethod , method , * args , ** kw ) <EOL> return result <EOL> elif self . failWhenNotImplemented : <EOL> return defer . fail ( <EOL> Failure ( NotImplementedError , <EOL> "<STR_LIT>" % method ) ) <EOL> else : <EOL> return defer . succeed ( None ) <EOL> class Pager : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , collector , callback = None , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if callable ( callback ) : <EOL> self . callback = callback <EOL> self . callbackArgs = args <EOL> self . callbackKeyword = kw <EOL> else : <EOL> self . callback = None <EOL> self . _stillPaging = <NUM_LIT:1> <EOL> self . collector = collector <EOL> collector . broker . registerPageProducer ( self ) <EOL> def stillPaging ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _stillPaging : <EOL> self . collector . callRemote ( "<STR_LIT>" ) <EOL> if self . callback is not None : <EOL> self . callback ( * self . callbackArgs , ** self . callbackKeyword ) <EOL> return self . _stillPaging <EOL> def sendNextPage ( self ) : <EOL> """<STR_LIT>""" <EOL> self . collector . callRemote ( "<STR_LIT>" , self . nextPage ( ) ) <EOL> def nextPage ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def stopPaging ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _stillPaging = <NUM_LIT:0> <EOL> class StringPager ( Pager ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , collector , st , chunkSize = <NUM_LIT> , callback = None , * args , ** kw ) : <EOL> self . string = st <EOL> self . pointer = <NUM_LIT:0> <EOL> self . chunkSize = chunkSize <EOL> Pager . __init__ ( self , collector , callback , * args , ** kw ) <EOL> def nextPage ( self ) : <EOL> val = self . string [ self . pointer : self . pointer + self . chunkSize ] <EOL> self . pointer += self . chunkSize <EOL> if self . pointer >= len ( self . string ) : <EOL> self . stopPaging ( ) <EOL> return val <EOL> class FilePager ( Pager ) : <EOL> """<STR_LIT>""" <EOL> implements ( interfaces . IConsumer ) <EOL> def __init__ ( self , collector , fd , callback = None , * args , ** kw ) : <EOL> self . chunks = [ ] <EOL> Pager . __init__ ( self , collector , callback , * args , ** kw ) <EOL> self . startProducing ( fd ) <EOL> def startProducing ( self , fd ) : <EOL> self . deferred = basic . FileSender ( ) . beginFileTransfer ( fd , self ) <EOL> self . deferred . addBoth ( lambda x : self . stopPaging ( ) ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> self . producer = producer <EOL> if not streaming : <EOL> self . producer . resumeProducing ( ) <EOL> def unregisterProducer ( self ) : <EOL> self . producer = None <EOL> def write ( self , chunk ) : <EOL> self . chunks . append ( chunk ) <EOL> def sendNextPage ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . chunks : <EOL> return <EOL> val = self . chunks . pop ( <NUM_LIT:0> ) <EOL> self . producer . resumeProducing ( ) <EOL> self . collector . callRemote ( "<STR_LIT>" , val ) <EOL> class CallbackPageCollector ( pb . Referenceable ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , callback ) : <EOL> self . pages = [ ] <EOL> self . callback = callback <EOL> def remote_gotPage ( self , page ) : <EOL> self . pages . append ( page ) <EOL> def remote_endedPaging ( self ) : <EOL> self . callback ( self . pages ) <EOL> def getAllPages ( referenceable , methodName , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> referenceable . callRemote ( methodName , CallbackPageCollector ( d . callback ) , * args , ** kw ) <EOL> return d </s>
