<s> from os . path import exists , join , abspath , sep , splitext , dirname <EOL> from json import dumps <EOL> from provy . core import Role <EOL> import os <EOL> import fnmatch <EOL> import inspect <EOL> class RoleDoc ( object ) : <EOL> def __init__ ( self , role , name , module , docs ) : <EOL> self . role = role <EOL> self . name = name <EOL> self . module = module <EOL> self . fullname = "<STR_LIT>" % ( module , name ) <EOL> self . docs = docs <EOL> self . methods = [ ] <EOL> self . parse_methods ( role ) <EOL> def parse_methods ( self , role ) : <EOL> for name , member in inspect . getmembers ( role ) : <EOL> if not inspect . ismethod ( member ) or name . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> if not member . __module__ == role . __module__ : <EOL> continue <EOL> if not member . __doc__ : <EOL> print "<STR_LIT>" % ( name , role . __name__ ) <EOL> self . add_method ( NameDoc ( name , member . __doc__ ) ) <EOL> def add_method ( self , method_doc ) : <EOL> self . methods . append ( method_doc ) <EOL> def to_dict ( self ) : <EOL> obj = { <EOL> '<STR_LIT>' : self . name , <EOL> '<STR_LIT>' : self . fullname , <EOL> '<STR_LIT>' : self . module , <EOL> '<STR_LIT>' : self . docs and self . docs . strip ( ) or None , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> for method in self . methods : <EOL> obj [ '<STR_LIT>' ] . append ( method . to_dict ( ) ) <EOL> return obj <EOL> class NameDoc ( object ) : <EOL> def __init__ ( self , name , doc ) : <EOL> self . name = name <EOL> self . doc = doc <EOL> def to_dict ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . name , <EOL> '<STR_LIT>' : self . doc and self . doc . strip ( ) or None <EOL> } <EOL> def main ( ) : <EOL> path = "<STR_LIT>" <EOL> source_path = join ( os . curdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not exists ( dirname ( path ) ) : <EOL> os . makedirs ( dirname ( path ) ) <EOL> root_namespace = '<STR_LIT>' <EOL> roles_to_document = { <EOL> '<STR_LIT>' : RoleDoc ( Role , '<STR_LIT>' , '<STR_LIT>' , Role . __doc__ ) <EOL> } <EOL> for root , dirs , files in os . walk ( source_path ) : <EOL> for file_name in files : <EOL> if file_name == "<STR_LIT>" : <EOL> continue <EOL> if not fnmatch . fnmatch ( file_name , '<STR_LIT>' ) : <EOL> continue <EOL> module_path = '<STR_LIT>' % ( root_namespace , <EOL> get_namespace_for ( root ) , <EOL> splitext ( file_name ) [ <NUM_LIT:0> ] ) <EOL> module = import_module ( module_path ) <EOL> for name , member in inspect . getmembers ( module ) : <EOL> if not inspect . isclass ( member ) or not issubclass ( member , Role ) : <EOL> continue <EOL> if member . __module__ != module_path : <EOL> continue <EOL> if not member . __doc__ : <EOL> print "<STR_LIT>" % ( member . __module__ , name ) <EOL> roles_to_document [ module_path ] = RoleDoc ( member , <EOL> name , <EOL> member . __module__ , <EOL> member . __doc__ ) <EOL> tree = { } <EOL> for full_name , role_doc in roles_to_document . iteritems ( ) : <EOL> role = role_doc . role <EOL> name = role_doc . name <EOL> current = tree <EOL> module = __import__ ( role . __module__ ) <EOL> for part in role . __module__ . split ( '<STR_LIT:.>' ) : <EOL> if hasattr ( module , part ) : <EOL> module = getattr ( module , part ) <EOL> if not part in current : <EOL> if not module . __doc__ : <EOL> print "<STR_LIT>" % module . __name__ <EOL> current [ part ] = { <EOL> '<STR_LIT>' : module . __name__ , <EOL> '<STR_LIT>' : module . __doc__ and module . __doc__ . strip ( ) or None <EOL> } <EOL> if part == role . __module__ . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] : <EOL> current [ part ] [ role_doc . name ] = role_doc . to_dict ( ) <EOL> current = current [ part ] <EOL> contents = dumps ( tree , sort_keys = True , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> f . write ( contents ) <EOL> def import_module ( module_path ) : <EOL> module = __import__ ( module_path ) <EOL> return reduce ( getattr , module_path . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] , module ) <EOL> def get_namespace_for ( directory ) : <EOL> source_path = abspath ( join ( os . curdir , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> diff = abspath ( directory ) . replace ( source_path , '<STR_LIT>' ) <EOL> namespace = '<STR_LIT:.>' . join ( [ module for module in diff . split ( sep ) if module ] ) <EOL> return namespace <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
