use logilab.common.sphinxutils
authorEmile Anclin <emile.anclin@logilab.fr>
Tue, 05 May 2009 17:18:49 +0200
changeset 1693 49075f57cf2c
parent 1687 9b966d7d7bff
child 1808 aa09e20dd8c0
use logilab.common.sphinxutils
doc/tools/generate_modules.py
--- a/doc/tools/generate_modules.py	Tue May 05 04:52:12 2009 -0700
+++ b/doc/tools/generate_modules.py	Tue May 05 17:18:49 2009 +0200
@@ -1,99 +1,12 @@
+"""generate list of modules for sphinx doc"""
+
 import sys
 
-"""
-Generates the chapter that list all the modules in CubicWeb
-in order to pull all the docstring.
-"""
-
-class ModuleGenerator:
-    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_names
-        self.fn = open(output_fn, 'w')
-        self.fn.write(self.HEADER)
-
-    def done(self):
-        self.fn.close()
-        
-    def gen_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)
-
-    def find_modules(self):
-        import os
-        modules = []
-        for mod_name in self.mod_names:
-            for root, dirs, files in os.walk(mod_name):
-                if self.keep_module(root):
-                    for name in files:
-                        if name == "__init__.py":
-                            if self.format_mod_name(root, mod_name) not in modules:
-                                modules.append(self.format_mod_name(root, mod_name))
-                        else:
-                            if name.endswith(".py") and name != "__pkginfo__.py" and "__init__.py" in files:
-                                filename = root + '/' + name.split('.py')[0]
-                                if self.format_mod_name(filename, mod_name) not in modules:
-                                    modules.append(self.format_mod_name(filename, mod_name))
-        return modules
+EXCLUDE_DIRS = ('test', 'tests', 'examples', 'data', 'doc', '.hg', 'migration')
+if __name__ == '__main__':
 
-    def gen_modules(self):
-        for module in self.find_modules():
-            self.gen_module(module)
-
-    def format_mod_name(self, path, mod_name):
-        mod_root = mod_name.split('/')[-1]
-        mod_end = path.split(mod_root)[-1]
-        return mod_root + mod_end.replace('/', '.')
-
-    def keep_module(self, mod_end):
-        """
-        Filter modules in order to exclude specific package directories.
-        """
-        for dir in self.EXCLUDE_DIRS:
-            if mod_end.find(dir) != -1:
-                return False
-        return True
-
-USAGE = """
-Two arguments required:
-    generate_modules [cubicweb-root] [file-out]
+    from logilab.common.sphinxutils import generate_modules_file
 
-[cubicweb-root] : full path to cubicweb forest
-[file-out] : rest file containing the list of modules for Sphinx
-"""
-def generate_modules_file(args):
-    if len(args) != 2:
-        print USAGE
-        sys.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()
-    print args
-
-
-
-if __name__ == '__main__':
-    generate_modules_file(sys.argv[1:])
-
+    gen = generate_modules_file(sys.argv[1:])
+    gen.set_docdir("cubicweb/doc/book/en")
+    gen.make(['cubicweb', '/indexer', '/logilab', '/rql', '/yams'], EXCLUDE_DIRS)