<s> """<STR_LIT>""" <EOL> from twisted import copyright <EOL> from twisted . spread import pb <EOL> from twisted . python import log , failure <EOL> from twisted . cred import portal <EOL> from twisted . application import service <EOL> from zope . interface import implements , Interface <EOL> import explorer <EOL> from cStringIO import StringIO <EOL> import string <EOL> import sys <EOL> import traceback <EOL> import types <EOL> class FakeStdIO : <EOL> def __init__ ( self , type_ , list ) : <EOL> self . type = type_ <EOL> self . list = list <EOL> def write ( self , text ) : <EOL> log . msg ( "<STR_LIT>" % ( self . type , string . strip ( str ( text ) ) ) ) <EOL> self . list . append ( ( self . type , text ) ) <EOL> def flush ( self ) : <EOL> pass <EOL> def consolidate ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . list : <EOL> return <EOL> inlist = self . list <EOL> outlist = [ ] <EOL> last_type = inlist [ <NUM_LIT:0> ] <EOL> block_begin = <NUM_LIT:0> <EOL> for i in xrange ( <NUM_LIT:1> , len ( self . list ) ) : <EOL> ( mtype , message ) = inlist [ i ] <EOL> if mtype == last_type : <EOL> continue <EOL> else : <EOL> if ( i - block_begin ) == <NUM_LIT:1> : <EOL> outlist . append ( inlist [ block_begin ] ) <EOL> else : <EOL> messages = map ( lambda l : l [ <NUM_LIT:1> ] , <EOL> inlist [ block_begin : i ] ) <EOL> message = string . join ( messages , '<STR_LIT>' ) <EOL> outlist . append ( ( last_type , message ) ) <EOL> last_type = mtype <EOL> block_begin = i <EOL> class IManholeClient ( Interface ) : <EOL> def console ( list_of_messages ) : <EOL> """<STR_LIT>""" <EOL> def receiveExplorer ( xplorer ) : <EOL> """<STR_LIT>""" <EOL> def listCapabilities ( ) : <EOL> """<STR_LIT>""" <EOL> def runInConsole ( command , console , globalNS = None , localNS = None , <EOL> filename = None , args = None , kw = None , unsafeTracebacks = False ) : <EOL> """<STR_LIT>""" <EOL> output = [ ] <EOL> fakeout = FakeStdIO ( "<STR_LIT>" , output ) <EOL> fakeerr = FakeStdIO ( "<STR_LIT>" , output ) <EOL> errfile = FakeStdIO ( "<STR_LIT>" , output ) <EOL> code = None <EOL> val = None <EOL> if filename is None : <EOL> filename = str ( console ) <EOL> if args is None : <EOL> args = ( ) <EOL> if kw is None : <EOL> kw = { } <EOL> if localNS is None : <EOL> localNS = globalNS <EOL> if ( globalNS is None ) and ( not callable ( command ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> out = sys . stdout <EOL> err = sys . stderr <EOL> sys . stdout = fakeout <EOL> sys . stderr = fakeerr <EOL> try : <EOL> if callable ( command ) : <EOL> val = apply ( command , args , kw ) <EOL> else : <EOL> try : <EOL> code = compile ( command , filename , '<STR_LIT>' ) <EOL> except : <EOL> code = compile ( command , filename , '<STR_LIT>' ) <EOL> if code : <EOL> val = eval ( code , globalNS , localNS ) <EOL> finally : <EOL> sys . stdout = out <EOL> sys . stderr = err <EOL> except : <EOL> ( eType , eVal , tb ) = sys . exc_info ( ) <EOL> fail = failure . Failure ( eVal , eType , tb ) <EOL> del tb <EOL> errfile . write ( pb . failure2Copyable ( fail , unsafeTracebacks ) ) <EOL> if console : <EOL> fakeout . consolidate ( ) <EOL> console ( output ) <EOL> return val <EOL> def _failureOldStyle ( fail ) : <EOL> """<STR_LIT>""" <EOL> import linecache <EOL> tb = [ ] <EOL> for f in fail . frames : <EOL> tb . append ( ( f [ <NUM_LIT:1> ] , f [ <NUM_LIT:2> ] , f [ <NUM_LIT:0> ] , linecache . getline ( f [ <NUM_LIT:1> ] , f [ <NUM_LIT:2> ] ) ) ) <EOL> return { <EOL> '<STR_LIT>' : tb , <EOL> '<STR_LIT>' : traceback . format_exception_only ( fail . type , fail . value ) <EOL> } <EOL> _defaultCapabilities = { <EOL> "<STR_LIT>" : '<STR_LIT>' <EOL> } <EOL> class Perspective ( pb . Avatar ) : <EOL> lastDeferred = <NUM_LIT:0> <EOL> def __init__ ( self , service ) : <EOL> self . localNamespace = { <EOL> "<STR_LIT>" : service , <EOL> "<STR_LIT>" : self , <EOL> "<STR_LIT:_>" : None , <EOL> } <EOL> self . clients = { } <EOL> self . service = service <EOL> def __getstate__ ( self ) : <EOL> state = self . __dict__ . copy ( ) <EOL> state [ '<STR_LIT>' ] = { } <EOL> if state [ '<STR_LIT>' ] . has_key ( "<STR_LIT>" ) : <EOL> del state [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return state <EOL> def attached ( self , client , identity ) : <EOL> """<STR_LIT>""" <EOL> self . clients [ client ] = identity <EOL> host = '<STR_LIT::>' . join ( map ( str , client . broker . transport . getHost ( ) [ <NUM_LIT:1> : ] ) ) <EOL> msg = self . service . welcomeMessage % { <EOL> '<STR_LIT>' : getattr ( identity , '<STR_LIT:name>' , str ( identity ) ) , <EOL> '<STR_LIT:host>' : host , <EOL> '<STR_LIT>' : copyright . longversion , <EOL> } <EOL> client . callRemote ( '<STR_LIT>' , [ ( "<STR_LIT>" , msg ) ] ) <EOL> client . capabilities = _defaultCapabilities <EOL> client . callRemote ( '<STR_LIT>' ) . addCallbacks ( <EOL> self . _cbClientCapable , self . _ebClientCapable , <EOL> callbackArgs = ( client , ) , errbackArgs = ( client , ) ) <EOL> def detached ( self , client , identity ) : <EOL> try : <EOL> del self . clients [ client ] <EOL> except KeyError : <EOL> pass <EOL> def runInConsole ( self , command , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return runInConsole ( command , <EOL> self . console , <EOL> self . service . namespace , <EOL> self . localNamespace , <EOL> str ( self . service ) , <EOL> args = args , <EOL> kw = kw , <EOL> unsafeTracebacks = self . service . unsafeTracebacks ) <EOL> def console ( self , message ) : <EOL> """<STR_LIT>""" <EOL> clients = self . clients . keys ( ) <EOL> origMessage = message <EOL> compatMessage = None <EOL> for client in clients : <EOL> try : <EOL> if not client . capabilities . has_key ( "<STR_LIT>" ) : <EOL> if compatMessage is None : <EOL> compatMessage = origMessage [ : ] <EOL> for i in xrange ( len ( message ) ) : <EOL> if ( ( message [ i ] [ <NUM_LIT:0> ] == "<STR_LIT>" ) and <EOL> isinstance ( message [ i ] [ <NUM_LIT:1> ] , failure . Failure ) ) : <EOL> compatMessage [ i ] = ( <EOL> message [ i ] [ <NUM_LIT:0> ] , <EOL> _failureOldStyle ( message [ i ] [ <NUM_LIT:1> ] ) ) <EOL> client . callRemote ( '<STR_LIT>' , compatMessage ) <EOL> else : <EOL> client . callRemote ( '<STR_LIT>' , message ) <EOL> except pb . ProtocolError : <EOL> self . detached ( client , None ) <EOL> def receiveExplorer ( self , objectLink ) : <EOL> """<STR_LIT>""" <EOL> clients = self . clients . keys ( ) <EOL> for client in clients : <EOL> try : <EOL> client . callRemote ( '<STR_LIT>' , objectLink ) <EOL> except pb . ProtocolError : <EOL> self . detached ( client , None ) <EOL> def _cbResult ( self , val , dnum ) : <EOL> self . console ( [ ( '<STR_LIT:result>' , "<STR_LIT>" % ( dnum , val ) ) ] ) <EOL> return val <EOL> def _cbClientCapable ( self , capabilities , client ) : <EOL> log . msg ( "<STR_LIT>" % ( id ( client ) , capabilities ) ) <EOL> client . capabilities = capabilities <EOL> def _ebClientCapable ( self , reason , client ) : <EOL> reason . trap ( AttributeError ) <EOL> log . msg ( "<STR_LIT>" % <EOL> ( client , ) ) <EOL> def perspective_do ( self , expr ) : <EOL> """<STR_LIT>""" <EOL> log . msg ( "<STR_LIT>" % expr ) <EOL> val = self . runInConsole ( expr ) <EOL> if val is not None : <EOL> self . localNamespace [ "<STR_LIT:_>" ] = val <EOL> from twisted . internet . defer import Deferred <EOL> if isinstance ( val , Deferred ) : <EOL> self . lastDeferred += <NUM_LIT:1> <EOL> self . console ( [ ( '<STR_LIT:result>' , "<STR_LIT>" % self . lastDeferred ) ] ) <EOL> val . addBoth ( self . _cbResult , self . lastDeferred ) <EOL> else : <EOL> self . console ( [ ( "<STR_LIT:result>" , repr ( val ) + '<STR_LIT:\n>' ) ] ) <EOL> log . msg ( "<STR_LIT>" ) <EOL> def perspective_explore ( self , identifier ) : <EOL> """<STR_LIT>""" <EOL> object = self . runInConsole ( identifier ) <EOL> if object : <EOL> expl = explorer . explorerPool . getExplorer ( object , identifier ) <EOL> self . receiveExplorer ( expl ) <EOL> def perspective_watch ( self , identifier ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> object = self . runInConsole ( identifier ) <EOL> if object : <EOL> oLink = self . runInConsole ( self . browser . browseObject , <EOL> object , identifier ) <EOL> self . receiveExplorer ( oLink ) <EOL> self . runInConsole ( self . browser . watchObject , <EOL> object , identifier , <EOL> self . receiveExplorer ) <EOL> class Realm : <EOL> implements ( portal . IRealm ) <EOL> def __init__ ( self , service ) : <EOL> self . service = service <EOL> self . _cache = { } <EOL> def requestAvatar ( self , avatarId , mind , * interfaces ) : <EOL> if pb . IPerspective not in interfaces : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if avatarId in self . _cache : <EOL> p = self . _cache [ avatarId ] <EOL> else : <EOL> p = Perspective ( self . service ) <EOL> p . attached ( mind , avatarId ) <EOL> def detached ( ) : <EOL> p . detached ( mind , avatarId ) <EOL> return ( pb . IPerspective , p , detached ) <EOL> class Service ( service . Service ) : <EOL> welcomeMessage = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self , unsafeTracebacks = False , namespace = None ) : <EOL> self . unsafeTracebacks = unsafeTracebacks <EOL> self . namespace = { <EOL> '<STR_LIT>' : '<STR_LIT>' % ( id ( self ) , ) , <EOL> '<STR_LIT>' : sys <EOL> } <EOL> if namespace : <EOL> self . namespace . update ( namespace ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> dict = self . __dict__ . copy ( ) <EOL> ns = dict [ '<STR_LIT>' ] . copy ( ) <EOL> dict [ '<STR_LIT>' ] = ns <EOL> if ns . has_key ( '<STR_LIT>' ) : <EOL> del ns [ '<STR_LIT>' ] <EOL> return dict </s>
