must record creation rank as an instance attribute
importsys"""Generates the chapter that list all the modules in CubicWebin order to pull all the docstring."""classModuleGenerator:HEADER=""".. -*- coding: utf-8 -*-============CubicWeb API============"""EXCLUDE_DIRS=('test','tests','examples','data','doc','.hg','migration')def__init__(self,output_fn,mod_names):self.mod_names=mod_namesself.fn=open(output_fn,'w')self.fn.write(self.HEADER)defdone(self):self.fn.close()defgen_module(self,mod_name):mod_entry=""":mod:`%s`%s.. automodule:: %s :members:"""%(mod_name,'='*(len(':mod:``'+mod_name)),mod_name)self.fn.write(mod_entry)deffind_modules(self):importosmodules=[]formod_nameinself.mod_names:forroot,dirs,filesinos.walk(mod_name):ifself.keep_module(root):fornameinfiles:ifname=="__init__.py":ifself.format_mod_name(root,mod_name)notinmodules:modules.append(self.format_mod_name(root,mod_name))else:ifname.endswith(".py")andname!="__pkginfo__.py"and"__init__.py"infiles:filename=root+'/'+name.split('.py')[0]ifself.format_mod_name(filename,mod_name)notinmodules:modules.append(self.format_mod_name(filename,mod_name))returnmodulesdefgen_modules(self):formoduleinself.find_modules():self.gen_module(module)defformat_mod_name(self,path,mod_name):mod_root=mod_name.split('/')[-1]mod_end=path.split(mod_root)[-1]returnmod_root+mod_end.replace('/','.')defkeep_module(self,mod_end):""" Filter modules in order to exclude specific package directories. """fordirinself.EXCLUDE_DIRS:ifmod_end.find(dir)!=-1:returnFalsereturnTrueUSAGE="""Two arguments required: generate_modules [cubicweb-root] [file-out][cubicweb-root] : full path to cubicweb forest[file-out] : rest file containing the list of modules for Sphinx"""defgenerate_modules_file(args):iflen(args)!=2:printUSAGEsys.exit()CW_ROOT=args[0]OUTPUT=args[1]modules=(CW_ROOT+'/cubicweb', \CW_ROOT+'/indexer', \CW_ROOT+'/logilab', \CW_ROOT+'/rql', \CW_ROOT+'/yams')mg=ModuleGenerator(CW_ROOT+'/cubicweb/doc/book/en/'+OUTPUT,modules)mg.find_modules()mg.gen_modules()mg.done()printargsif__name__=='__main__':generate_modules_file(sys.argv[1:])