sphinxutils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of logilab-common.
  5. #
  6. # logilab-common is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU Lesser General Public License as published by the Free
  8. # Software Foundation, either version 2.1 of the License, or (at your option) any
  9. # later version.
  10. #
  11. # logilab-common is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License along
  17. # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
  18. """Sphinx utils
  19. ModuleGenerator: Generate a file that lists all the modules of a list of
  20. packages in order to pull all the docstring.
  21. This should not be used in a makefile to systematically generate sphinx
  22. documentation!
  23. Typical usage:
  24. >>> from logilab.common.sphinxutils import ModuleGenerator
  25. >>> mgen = ModuleGenerator('logilab common', '/home/adim/src/logilab/common')
  26. >>> mgen.generate('api_logilab_common.rst', exclude_dirs=('test',))
  27. """
  28. import os, sys
  29. import os.path as osp
  30. import inspect
  31. from logilab.common import STD_BLACKLIST
  32. from logilab.common.shellutils import globfind
  33. from logilab.common.modutils import load_module_from_file, modpath_from_file
  34. def module_members(module):
  35. members = []
  36. for name, value in inspect.getmembers(module):
  37. if getattr(value, '__module__', None) == module.__name__:
  38. members.append( (name, value) )
  39. return sorted(members)
  40. def class_members(klass):
  41. return sorted([name for name in vars(klass)
  42. if name not in ('__doc__', '__module__',
  43. '__dict__', '__weakref__')])
  44. class ModuleGenerator:
  45. file_header = """.. -*- coding: utf-8 -*-\n\n%s\n"""
  46. module_def = """
  47. :mod:`%s`
  48. =======%s
  49. .. automodule:: %s
  50. :members: %s
  51. """
  52. class_def = """
  53. .. autoclass:: %s
  54. :members: %s
  55. """
  56. def __init__(self, project_title, code_dir):
  57. self.title = project_title
  58. self.code_dir = osp.abspath(code_dir)
  59. def generate(self, dest_file, exclude_dirs=STD_BLACKLIST):
  60. """make the module file"""
  61. self.fn = open(dest_file, 'w')
  62. num = len(self.title) + 6
  63. title = "=" * num + "\n %s API\n" % self.title + "=" * num
  64. self.fn.write(self.file_header % title)
  65. self.gen_modules(exclude_dirs=exclude_dirs)
  66. self.fn.close()
  67. def gen_modules(self, exclude_dirs):
  68. """generate all modules"""
  69. for module in self.find_modules(exclude_dirs):
  70. modname = module.__name__
  71. classes = []
  72. modmembers = []
  73. for objname, obj in module_members(module):
  74. if inspect.isclass(obj):
  75. classmembers = class_members(obj)
  76. classes.append( (objname, classmembers) )
  77. else:
  78. modmembers.append(objname)
  79. self.fn.write(self.module_def % (modname, '=' * len(modname),
  80. modname,
  81. ', '.join(modmembers)))
  82. for klass, members in classes:
  83. self.fn.write(self.class_def % (klass, ', '.join(members)))
  84. def find_modules(self, exclude_dirs):
  85. basepath = osp.dirname(self.code_dir)
  86. basedir = osp.basename(basepath) + osp.sep
  87. if basedir not in sys.path:
  88. sys.path.insert(1, basedir)
  89. for filepath in globfind(self.code_dir, '*.py', exclude_dirs):
  90. if osp.basename(filepath) in ('setup.py', '__pkginfo__.py'):
  91. continue
  92. try:
  93. module = load_module_from_file(filepath)
  94. except: # module might be broken or magic
  95. dotted_path = modpath_from_file(filepath)
  96. module = type('.'.join(dotted_path), (), {}) # mock it
  97. yield module
  98. if __name__ == '__main__':
  99. # example :
  100. title, code_dir, outfile = sys.argv[1:]
  101. generator = ModuleGenerator(title, code_dir)
  102. # XXX modnames = ['logilab']
  103. generator.generate(outfile, ('test', 'tests', 'examples',
  104. 'data', 'doc', '.hg', 'migration'))