<s> """<STR_LIT>""" <EOL> import sys <EOL> import imp <EOL> import tokenize <EOL> from token import NAME , DEDENT , OP <EOL> from operator import itemgetter <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> _modules = { } <EOL> class Object : <EOL> def __init__ ( self , module , name , file , lineno , parent ) : <EOL> self . module = module <EOL> self . name = name <EOL> self . file = file <EOL> self . lineno = lineno <EOL> self . parent = parent <EOL> self . objects = { } <EOL> def _addobject ( self , name , obj ) : <EOL> self . objects [ name ] = obj <EOL> class Class ( Object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , module , name , super , file , lineno , parent = None ) : <EOL> Object . __init__ ( self , module , name , file , lineno , parent ) <EOL> if super is None : <EOL> super = [ ] <EOL> self . super = super <EOL> self . methods = { } <EOL> def _addmethod ( self , name , lineno ) : <EOL> self . methods [ name ] = lineno <EOL> class Function ( Object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , module , name , file , lineno , parent = None ) : <EOL> Object . __init__ ( self , module , name , file , lineno , parent ) <EOL> def _newfunction ( ob , name , lineno ) : <EOL> '''<STR_LIT>''' <EOL> return Function ( ob . module , name , ob . file , lineno , ob ) <EOL> def _newclass ( ob , name , super , lineno ) : <EOL> '''<STR_LIT>''' <EOL> return Class ( ob . module , name , super , ob . file , lineno , ob ) <EOL> def readmodule ( module , path = None ) : <EOL> '''<STR_LIT>''' <EOL> res = { } <EOL> for key , value in _readmodule ( module , path or [ ] ) . items ( ) : <EOL> if isinstance ( value , Class ) : <EOL> res [ key ] = value <EOL> return res <EOL> def readmodule_ex ( module , path = None ) : <EOL> '''<STR_LIT>''' <EOL> return _readmodule ( module , path or [ ] ) <EOL> def _readmodule ( module , path , inpackage = None ) : <EOL> '''<STR_LIT>''' <EOL> if inpackage is not None : <EOL> fullmodule = "<STR_LIT>" % ( inpackage , module ) <EOL> else : <EOL> fullmodule = module <EOL> if fullmodule in _modules : <EOL> return _modules [ fullmodule ] <EOL> dict = { } <EOL> if module in sys . builtin_module_names and inpackage is None : <EOL> _modules [ module ] = dict <EOL> return dict <EOL> i = module . rfind ( '<STR_LIT:.>' ) <EOL> if i >= <NUM_LIT:0> : <EOL> package = module [ : i ] <EOL> submodule = module [ i + <NUM_LIT:1> : ] <EOL> parent = _readmodule ( package , path , inpackage ) <EOL> if inpackage is not None : <EOL> package = "<STR_LIT>" % ( inpackage , package ) <EOL> return _readmodule ( submodule , parent [ '<STR_LIT>' ] , package ) <EOL> f = None <EOL> if inpackage is not None : <EOL> f , fname , ( _s , _m , ty ) = imp . find_module ( module , path ) <EOL> else : <EOL> f , fname , ( _s , _m , ty ) = imp . find_module ( module , path + sys . path ) <EOL> if ty == imp . PKG_DIRECTORY : <EOL> dict [ '<STR_LIT>' ] = [ fname ] <EOL> path = [ fname ] + path <EOL> f , fname , ( _s , _m , ty ) = imp . find_module ( '<STR_LIT>' , [ fname ] ) <EOL> _modules [ fullmodule ] = dict <EOL> if ty != imp . PY_SOURCE : <EOL> f . close ( ) <EOL> return dict <EOL> stack = [ ] <EOL> g = tokenize . generate_tokens ( f . readline ) <EOL> try : <EOL> for tokentype , token , start , _end , _line in g : <EOL> if tokentype == DEDENT : <EOL> lineno , thisindent = start <EOL> while stack and stack [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] >= thisindent : <EOL> del stack [ - <NUM_LIT:1> ] <EOL> elif token == '<STR_LIT>' : <EOL> lineno , thisindent = start <EOL> while stack and stack [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] >= thisindent : <EOL> del stack [ - <NUM_LIT:1> ] <EOL> tokentype , meth_name , start = g . next ( ) [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> if tokentype != NAME : <EOL> continue <EOL> cur_func = None <EOL> if stack : <EOL> cur_obj = stack [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if isinstance ( cur_obj , Object ) : <EOL> cur_func = _newfunction ( cur_obj , meth_name , lineno ) <EOL> cur_obj . _addobject ( meth_name , cur_func ) <EOL> if isinstance ( cur_obj , Class ) : <EOL> cur_func = _newfunction ( cur_obj , meth_name , lineno ) <EOL> cur_obj . _addmethod ( meth_name , lineno ) <EOL> else : <EOL> cur_func = Function ( fullmodule , meth_name , fname , lineno ) <EOL> dict [ meth_name ] = cur_func <EOL> stack . append ( ( cur_func , thisindent ) ) <EOL> elif token == '<STR_LIT:class>' : <EOL> lineno , thisindent = start <EOL> while stack and stack [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] >= thisindent : <EOL> del stack [ - <NUM_LIT:1> ] <EOL> tokentype , class_name , start = g . next ( ) [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> if tokentype != NAME : <EOL> continue <EOL> tokentype , token , start = g . next ( ) [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> inherit = None <EOL> if token == '<STR_LIT:(>' : <EOL> names = [ ] <EOL> level = <NUM_LIT:1> <EOL> super = [ ] <EOL> while True : <EOL> tokentype , token , start = g . next ( ) [ <NUM_LIT:0> : <NUM_LIT:3> ] <EOL> if token in ( '<STR_LIT:)>' , '<STR_LIT:U+002C>' ) and level == <NUM_LIT:1> : <EOL> n = "<STR_LIT>" . join ( super ) <EOL> if n in dict : <EOL> n = dict [ n ] <EOL> else : <EOL> c = n . split ( '<STR_LIT:.>' ) <EOL> if len ( c ) > <NUM_LIT:1> : <EOL> m = c [ - <NUM_LIT:2> ] <EOL> c = c [ - <NUM_LIT:1> ] <EOL> if m in _modules : <EOL> d = _modules [ m ] <EOL> if c in d : <EOL> n = d [ c ] <EOL> names . append ( n ) <EOL> super = [ ] <EOL> if token == '<STR_LIT:(>' : <EOL> level += <NUM_LIT:1> <EOL> elif token == '<STR_LIT:)>' : <EOL> level -= <NUM_LIT:1> <EOL> if level == <NUM_LIT:0> : <EOL> break <EOL> elif token == '<STR_LIT:U+002C>' and level == <NUM_LIT:1> : <EOL> pass <EOL> elif tokentype in ( NAME , OP ) and level == <NUM_LIT:1> : <EOL> super . append ( token ) <EOL> inherit = names <EOL> if stack : <EOL> cur_obj = stack [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if isinstance ( cur_obj , ( Class , Function ) ) : <EOL> cur_class = _newclass ( cur_obj , class_name , inherit , <EOL> lineno ) <EOL> cur_obj . _addobject ( class_name , cur_class ) <EOL> else : <EOL> cur_class = Class ( fullmodule , class_name , inherit , <EOL> fname , lineno ) <EOL> dict [ class_name ] = cur_class <EOL> stack . append ( ( cur_class , thisindent ) ) <EOL> elif token == '<STR_LIT>' and start [ <NUM_LIT:1> ] == <NUM_LIT:0> : <EOL> modules = _getnamelist ( g ) <EOL> for mod , _mod2 in modules : <EOL> try : <EOL> if inpackage is None : <EOL> _readmodule ( mod , path ) <EOL> else : <EOL> try : <EOL> _readmodule ( mod , path , inpackage ) <EOL> except ImportError : <EOL> _readmodule ( mod , [ ] ) <EOL> except : <EOL> pass <EOL> elif token == '<STR_LIT>' and start [ <NUM_LIT:1> ] == <NUM_LIT:0> : <EOL> mod , token = _getname ( g ) <EOL> if not mod or token != "<STR_LIT>" : <EOL> continue <EOL> names = _getnamelist ( g ) <EOL> try : <EOL> d = _readmodule ( mod , path , inpackage ) <EOL> except : <EOL> continue <EOL> for n , n2 in names : <EOL> if n in d : <EOL> dict [ n2 or n ] = d [ n ] <EOL> elif n == '<STR_LIT:*>' : <EOL> for n in d : <EOL> if n [ <NUM_LIT:0> ] != '<STR_LIT:_>' : <EOL> dict [ n ] = d [ n ] <EOL> except StopIteration : <EOL> pass <EOL> f . close ( ) <EOL> return dict <EOL> def _getnamelist ( g ) : <EOL> names = [ ] <EOL> while True : <EOL> name , token = _getname ( g ) <EOL> if not name : <EOL> break <EOL> if token == '<STR_LIT>' : <EOL> name2 , token = _getname ( g ) <EOL> else : <EOL> name2 = None <EOL> names . append ( ( name , name2 ) ) <EOL> while token != "<STR_LIT:U+002C>" and "<STR_LIT:\n>" not in token : <EOL> token = g . next ( ) [ <NUM_LIT:1> ] <EOL> if token != "<STR_LIT:U+002C>" : <EOL> break <EOL> return names <EOL> def _getname ( g ) : <EOL> parts = [ ] <EOL> tokentype , token = g . next ( ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> if tokentype != NAME and token != '<STR_LIT:*>' : <EOL> return ( None , token ) <EOL> parts . append ( token ) <EOL> while True : <EOL> tokentype , token = g . next ( ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> if token != '<STR_LIT:.>' : <EOL> break <EOL> tokentype , token = g . next ( ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> if tokentype != NAME : <EOL> break <EOL> parts . append ( token ) <EOL> return ( "<STR_LIT:.>" . join ( parts ) , token ) <EOL> def _main ( ) : <EOL> import os <EOL> mod = sys . argv [ <NUM_LIT:1> ] <EOL> if os . path . exists ( mod ) : <EOL> path = [ os . path . dirname ( mod ) ] <EOL> mod = os . path . basename ( mod ) <EOL> if mod . lower ( ) . endswith ( "<STR_LIT>" ) : <EOL> mod = mod [ : - <NUM_LIT:3> ] <EOL> else : <EOL> path = [ ] <EOL> dict = readmodule_ex ( mod , path ) <EOL> lineno_key = lambda a : getattr ( a , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> objs = dict . values ( ) <EOL> objs . sort ( key = lineno_key , reverse = True ) <EOL> indent_level = <NUM_LIT:2> <EOL> while objs : <EOL> obj = objs . pop ( ) <EOL> if isinstance ( obj , list ) : <EOL> continue <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> obj . indent = <NUM_LIT:0> <EOL> if isinstance ( obj , Object ) : <EOL> new_objs = obj . objects . values ( ) <EOL> new_objs . sort ( key = lineno_key , reverse = True ) <EOL> for ob in new_objs : <EOL> ob . indent = obj . indent + indent_level <EOL> objs . extend ( new_objs ) <EOL> if isinstance ( obj , Class ) : <EOL> print "<STR_LIT>" % ( '<STR_LIT:U+0020>' * obj . indent , obj . name , <EOL> obj . super , obj . lineno ) <EOL> elif isinstance ( obj , Function ) : <EOL> print "<STR_LIT>" % ( '<STR_LIT:U+0020>' * obj . indent , obj . name , obj . lineno ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> _main ( ) </s>
