<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from __future__ import with_statement <EOL> import functools <EOL> from twisted . internet import defer <EOL> from txtorcon . interface import ITorControlProtocol <EOL> class MagicContainer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n ) : <EOL> self . _txtorcon_name = n <EOL> self . attrs = { } <EOL> self . _setup = False <EOL> def _setup_complete ( self ) : <EOL> self . _setup = True <EOL> def _add_attribute ( self , n , v ) : <EOL> self . attrs [ n ] = v <EOL> def __repr__ ( self ) : <EOL> return object . __getattribute__ ( self , '<STR_LIT>' ) <EOL> def __getitem__ ( self , idx ) : <EOL> return list ( object . __getattribute__ ( self , '<STR_LIT>' ) . items ( ) ) [ idx ] [ <NUM_LIT:1> ] <EOL> def __len__ ( self ) : <EOL> return len ( object . __getattribute__ ( self , '<STR_LIT>' ) ) <EOL> def __dir__ ( self ) : <EOL> return list ( object . __getattribute__ ( self , '<STR_LIT>' ) . keys ( ) ) <EOL> def __getattribute__ ( self , name ) : <EOL> sup = super ( MagicContainer , self ) <EOL> if sup . __getattribute__ ( '<STR_LIT>' ) is False : <EOL> return sup . __getattribute__ ( name ) <EOL> attrs = sup . __getattribute__ ( '<STR_LIT>' ) <EOL> if name == '<STR_LIT>' : <EOL> return list ( attrs . keys ( ) ) <EOL> else : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> return sup . __getattribute__ ( name ) <EOL> try : <EOL> return attrs [ name ] <EOL> except KeyError : <EOL> if name in [ '<STR_LIT>' ] : <EOL> return object . __getattribute__ ( self , name ) <EOL> raise AttributeError ( name ) <EOL> def dump ( self , prefix ) : <EOL> prefix = prefix + '<STR_LIT:.>' + object . __getattribute__ ( self , '<STR_LIT>' ) <EOL> for x in list ( object . __getattribute__ ( self , '<STR_LIT>' ) . values ( ) ) : <EOL> x . dump ( prefix ) <EOL> class ConfigMethod ( object ) : <EOL> def __init__ ( self , info_key , protocol , takes_arg = False ) : <EOL> self . info_key = info_key <EOL> self . proto = protocol <EOL> self . takes_arg = takes_arg <EOL> def dump ( self , prefix ) : <EOL> n = self . info_key . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) <EOL> n = n . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> s = '<STR_LIT>' % ( n , '<STR_LIT>' if self . takes_arg else '<STR_LIT>' ) <EOL> return s <EOL> def __call__ ( self , * args ) : <EOL> if self . takes_arg : <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % self . info_key <EOL> ) <EOL> req = '<STR_LIT>' % ( self . info_key , str ( args [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> if len ( args ) != <NUM_LIT:0> : <EOL> raise TypeError ( '<STR_LIT>' % self . info_key ) <EOL> req = self . info_key <EOL> def stripper ( key , arg ) : <EOL> return arg . strip ( ) [ len ( key ) + <NUM_LIT:1> : ] . strip ( ) <EOL> d = self . proto . get_info_raw ( req ) <EOL> d . addCallback ( functools . partial ( stripper , req ) ) <EOL> return d <EOL> def __str__ ( self ) : <EOL> arg = '<STR_LIT>' <EOL> if self . takes_arg : <EOL> arg = '<STR_LIT>' <EOL> return '<STR_LIT>' % ( self . info_key . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) , arg ) <EOL> class TorInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , control , errback = None ) : <EOL> self . _setup = False <EOL> self . attrs = { } <EOL> '''<STR_LIT>''' <EOL> self . protocol = ITorControlProtocol ( control ) <EOL> self . errback = errback <EOL> self . post_bootstrap = defer . Deferred ( ) <EOL> if self . protocol . post_bootstrap : <EOL> self . protocol . post_bootstrap . addCallback ( self . bootstrap ) <EOL> else : <EOL> self . bootstrap ( ) <EOL> def _add_attribute ( self , n , v ) : <EOL> self . attrs [ n ] = v <EOL> def __getitem__ ( self , idx ) : <EOL> sup = super ( TorInfo , self ) <EOL> if sup . __getattribute__ ( '<STR_LIT>' ) is True : <EOL> return list ( object . __getattribute__ ( self , '<STR_LIT>' ) . items ( ) ) [ idx ] [ <NUM_LIT:1> ] <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def __len__ ( self ) : <EOL> sup = super ( TorInfo , self ) <EOL> if sup . __getattribute__ ( '<STR_LIT>' ) is True : <EOL> return len ( object . __getattribute__ ( self , '<STR_LIT>' ) ) <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def __dir__ ( self ) : <EOL> sup = super ( TorInfo , self ) <EOL> if sup . __getattribute__ ( '<STR_LIT>' ) is True : <EOL> return list ( sup . __getattribute__ ( '<STR_LIT>' ) . keys ( ) ) <EOL> return list ( sup . __getattribute__ ( '<STR_LIT>' ) . keys ( ) ) <EOL> def __getattribute__ ( self , name ) : <EOL> sup = super ( TorInfo , self ) <EOL> if sup . __getattribute__ ( '<STR_LIT>' ) is False : <EOL> return sup . __getattribute__ ( name ) <EOL> attrs = sup . __getattribute__ ( '<STR_LIT>' ) <EOL> if name == '<STR_LIT>' : <EOL> return list ( attrs . keys ( ) ) <EOL> else : <EOL> try : <EOL> return attrs [ name ] <EOL> except KeyError : <EOL> if name == '<STR_LIT>' : <EOL> return object . __getattribute__ ( self , name ) <EOL> raise AttributeError ( name ) <EOL> def bootstrap ( self , * args ) : <EOL> d = self . protocol . get_info_raw ( "<STR_LIT>" ) <EOL> d . addCallback ( self . _do_setup ) <EOL> if self . errback : <EOL> d . addErrback ( self . errback ) <EOL> d . addCallback ( self . _setup_complete ) <EOL> return d <EOL> def dump ( self ) : <EOL> for x in object . __getattribute__ ( self , '<STR_LIT>' ) . values ( ) : <EOL> x . dump ( '<STR_LIT>' ) <EOL> def _do_setup ( self , data ) : <EOL> added_magic = [ ] <EOL> for line in data . split ( '<STR_LIT:\n>' ) : <EOL> if line == "<STR_LIT>" or line . strip ( ) == '<STR_LIT>' : <EOL> continue <EOL> ( name , documentation ) = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if name == '<STR_LIT>' : <EOL> continue <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> bits = name [ : - <NUM_LIT:2> ] . split ( '<STR_LIT:/>' ) <EOL> takes_arg = True <EOL> else : <EOL> bits = name . split ( '<STR_LIT:/>' ) <EOL> takes_arg = False <EOL> mine = self <EOL> for bit in bits [ : - <NUM_LIT:1> ] : <EOL> bit = bit . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> if bit in mine . attrs : <EOL> mine = mine . attrs [ bit ] <EOL> if not isinstance ( mine , MagicContainer ) : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % ( bit , name ) <EOL> ) <EOL> else : <EOL> c = MagicContainer ( bit ) <EOL> added_magic . append ( c ) <EOL> mine . _add_attribute ( bit , c ) <EOL> mine = c <EOL> n = bits [ - <NUM_LIT:1> ] . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> if n in mine . attrs : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % ( n , name ) <EOL> ) <EOL> mine . _add_attribute ( n , ConfigMethod ( '<STR_LIT:/>' . join ( bits ) , <EOL> self . protocol , takes_arg ) ) <EOL> for c in added_magic : <EOL> c . _setup_complete ( ) <EOL> return None <EOL> def _setup_complete ( self , * args ) : <EOL> pb = self . post_bootstrap <EOL> self . _setup = True <EOL> pb . callback ( self ) </s>
