<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import __builtin__ <EOL> class TreeNode ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass ) : <EOL> """<STR_LIT>""" <EOL> self . klass = klass <EOL> self . name = klass . __name__ <EOL> self . children = [ ] <EOL> self . parents = [ ] <EOL> self . level = None <EOL> def add_parent ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( parent , TreeNode ) <EOL> if parent not in self . parents : <EOL> self . parents . append ( parent ) <EOL> def add_child ( self , child ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( child , TreeNode ) <EOL> if child not in self . children : <EOL> self . children . append ( child ) <EOL> def get_level ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . level : <EOL> if self . parents : <EOL> self . level = max ( [ x . get_level ( ) for x in self . parents ] ) + <NUM_LIT:1> <EOL> else : <EOL> self . level = <NUM_LIT:0> <EOL> return self . level <EOL> def get_ancestors ( self ) : <EOL> """<STR_LIT>""" <EOL> def _get_ancestors ( node , ancestors ) : <EOL> ancestors . extend ( node . parents ) <EOL> for p in node . parents : <EOL> _get_ancestors ( p , ancestors ) <EOL> ancestors = [ ] <EOL> _get_ancestors ( self , ancestors ) <EOL> return ancestors <EOL> class ClassTree ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , modules ) : <EOL> """<STR_LIT>""" <EOL> self . nodes = { } <EOL> self . tree = [ [ ] ] <EOL> if not hasattr ( modules , '<STR_LIT>' ) : <EOL> self . modules = [ modules ] <EOL> else : <EOL> self . modules = modules <EOL> def __iter__ ( self ) : <EOL> return iter ( self . nodes . values ( ) ) <EOL> def _generate_hierarchy ( self , klass ) : <EOL> """<STR_LIT>""" <EOL> node = self . get_node ( klass . __name__ , create = <NUM_LIT:1> ) <EOL> for base in klass . __bases__ : <EOL> base_node = self . get_node_from_class ( base , create = <NUM_LIT:1> ) <EOL> node . add_parent ( base_node ) <EOL> base_node . add_child ( node ) <EOL> self . _generate_hierarchy ( base ) <EOL> def get_class ( self , name ) : <EOL> """<STR_LIT>""" <EOL> klass = None <EOL> for m in self . modules : <EOL> if hasattr ( m , name ) : <EOL> return getattr ( m , name ) <EOL> if hasattr ( __builtin__ , name ) : <EOL> klass = getattr ( __builtin__ , name ) <EOL> if not klass : <EOL> try : <EOL> klass = self . nodes [ name ] . klass <EOL> except KeyError : <EOL> raise KeyError , "<STR_LIT>" % name <EOL> return klass <EOL> def add_node ( self , klass ) : <EOL> """<STR_LIT>""" <EOL> name = klass . __name__ <EOL> if not self . nodes . has_key ( name ) : <EOL> node = TreeNode ( klass ) <EOL> self . nodes [ name ] = node <EOL> return node <EOL> def get_node ( self , name , create = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if self . nodes . has_key ( name ) : <EOL> return self . nodes [ name ] <EOL> elif create : <EOL> return self . add_node ( self . get_class ( name ) ) <EOL> def get_node_from_class ( self , cls , create = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> name = cls . __name__ <EOL> if self . nodes . has_key ( name ) : <EOL> return self . nodes [ name ] <EOL> elif create : <EOL> return self . add_node ( cls ) <EOL> def create ( self , class_names = None ) : <EOL> """<STR_LIT>""" <EOL> if class_names is None : <EOL> class_names = [ ] <EOL> for m in self . modules : <EOL> class_names . extend ( dir ( m ) ) <EOL> for name in class_names : <EOL> klass = self . get_class ( name ) <EOL> if klass and hasattr ( klass , '<STR_LIT>' ) : <EOL> self . _generate_hierarchy ( klass ) <EOL> for node in self : <EOL> d = node . get_level ( ) <EOL> while len ( self . tree ) <= d : <EOL> self . tree . append ( [ ] ) <EOL> self . tree [ d ] . append ( node ) <EOL> def _comp ( x , y ) : <EOL> return cmp ( x . name , y . name ) <EOL> for nodes in self . tree : <EOL> nodes . sort ( _comp ) </s>
