PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/make_rst.py

https://gitlab.com/tlunet/casper
Python | 202 lines | 172 code | 24 blank | 6 comment | 28 complexity | 8cdd367e840b48fb4948632cc2318ca1 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Apr 22 11:32:47 2015
  4. @author: t.lunet
  5. """
  6. import os
  7. import sys
  8. import inspect
  9. import glob
  10. def list_dir(package_dir,doc_dir,code_dir):
  11. tab_elt = os.listdir(package_dir)
  12. if '__init__.py' not in tab_elt:
  13. print("Not a python package directory")
  14. return
  15. print("Python Package directory = ",tab_elt)
  16. tab_elt.remove('__init__.py')
  17. if '__init__.pyc' in tab_elt :
  18. tab_elt.remove('__init__.pyc')
  19. tab_packages = []
  20. tab_modules = []
  21. for elt in tab_elt:
  22. if not elt.startswith('.'):
  23. if os.path.isdir(package_dir+'/'+elt):
  24. tab_packages += [elt]
  25. elif elt.endswith('.py'):
  26. tab_modules += [elt[:-3]]
  27. package_name = package_dir.replace('/','.')
  28. print("Packages = ",tab_packages)
  29. write_package_rst(package_name,tab_packages,tab_modules,doc_dir,code_dir)
  30. for elt in tab_packages:
  31. list_dir(package_dir+'/'+elt,doc_dir,code_dir)
  32. print("Modules = ",tab_modules)
  33. for elt in tab_modules:
  34. list_module(package_dir+'/'+elt,doc_dir)
  35. def list_module(module_name, doc_dir):
  36. module_name = module_name.replace('/', '.')
  37. module = module_name
  38. exec('import {0} as module'.format(module))
  39. print("Python module = ", module_name)
  40. tab_classes = inspect.getmembers(module, isclassof(module))
  41. tab_classes = [elt[1].__module__+'.'+elt[0] for elt in tab_classes]
  42. print("Classes = ", tab_classes)
  43. for classe in tab_classes:
  44. write_classe(classe, doc_dir)
  45. tab_functions = inspect.getmembers(module, isfunctionof(module))
  46. tab_functions = [elt[1].__module__+'.'+elt[0] for elt in tab_functions]
  47. print("Functions = ", tab_functions)
  48. for function in tab_functions:
  49. write_function(function, doc_dir)
  50. write_module_rst(module_name, tab_classes, tab_functions,
  51. doc_dir)
  52. def isclassof(mod):
  53. def func(obj):
  54. if hasattr(mod, '__name__'):
  55. return inspect.isclass(obj) and obj.__module__ == mod.__name__
  56. else:
  57. return False
  58. return func
  59. def isfunctionof(mod):
  60. def func(obj):
  61. return inspect.isfunction(obj) and \
  62. (obj.__module__ == mod.__name__ or
  63. obj.__module__ == mod.__package__+'.base')
  64. return func
  65. def write_module_rst(module_name, tab_classes, tab_functions,
  66. doc_dir):
  67. f = open(doc_dir+module_name+'.rst', 'w')
  68. f.write(module_name+' module\n' +
  69. '='*(len(module_name)+7) +
  70. '\n\n' +
  71. '.. automodule:: '+module_name+'\n' +
  72. '.. toctree::\n' +
  73. ' :maxdepth: 1\n\n')
  74. for classe in tab_classes:
  75. f.write(' '+classe+'\n')
  76. for function in tab_functions:
  77. f.write(' '+function+'\n')
  78. f.write('\n\n' +
  79. 'Indices and tables\n' +
  80. '------------------\n' +
  81. '\n' +
  82. '* :ref:`genindex`\n' +
  83. '* :ref:`modindex`\n' +
  84. '* :ref:`search`')
  85. f.close()
  86. def write_package_rst(package_name, tab_packages, tab_modules, doc_dir,
  87. code_dir):
  88. if package_name == code_dir:
  89. f = open(doc_dir+'index.rst', 'w')
  90. f.write('.. automodule:: '+package_name+'\n' +
  91. ' :members:\n' +
  92. ' :undoc-members:\n' +
  93. ' :private-members:\n' +
  94. '.. toctree::\n' +
  95. ' :maxdepth: 1\n' +
  96. '\n')
  97. else:
  98. f = open(doc_dir+package_name+'.rst', 'w')
  99. f.write(package_name+' package\n' +
  100. '='*(len(package_name)+8) +
  101. '\n\n' +
  102. '.. automodule:: '+package_name +
  103. '\n\n' +
  104. '.. toctree::\n' +
  105. ' :maxdepth: 1\n\n')
  106. for package in tab_packages:
  107. f.write(' '+package_name+'.'+package+'\n')
  108. for module in tab_modules:
  109. f.write(' '+package_name+'.'+module+'\n')
  110. f.write('\n\n' +
  111. 'Indices and tables\n' +
  112. '------------------\n' +
  113. '\n' +
  114. '* :ref:`genindex`\n' +
  115. '* :ref:`modindex`\n' +
  116. '* :ref:`search`')
  117. f.close()
  118. def write_classe(classe_name, doc_dir):
  119. f = open(doc_dir+classe_name+'.rst', 'w')
  120. f.write(classe_name+' class\n' +
  121. '='*(len(classe_name)+6) +
  122. '\n\n' +
  123. '.. autoclass:: '+classe_name+'\n' +
  124. ' :members:\n' +
  125. ' :private-members:\n' +
  126. ' :undoc-members:\n' +
  127. ' :inherited-members:\n' +
  128. ' :show-inheritance:')
  129. f.write('\n\n' +
  130. 'Indices and tables\n' +
  131. '------------------\n' +
  132. '\n' +
  133. '* :ref:`genindex`\n' +
  134. '* :ref:`modindex`\n' +
  135. '* :ref:`search`')
  136. f.close()
  137. def write_function(function_name, doc_dir):
  138. f = open(doc_dir+function_name+'.rst', 'w')
  139. f.write(function_name+' function\n' +
  140. '='*(len(function_name)+9) +
  141. '\n\n' +
  142. '.. autofunction:: '+function_name+'\n')
  143. f.write('\n\nIndices and tables\n' +
  144. '------------------\n' +
  145. '\n' +
  146. '* :ref:`genindex`\n' +
  147. '* :ref:`modindex`\n' +
  148. '* :ref:`search`')
  149. f.close()
  150. def write_attr(attr_name, doc_dir):
  151. f = open(doc_dir+attr_name+'.rst', 'w')
  152. f.write(attr_name+' function\n' +
  153. '='*(len(attr_name)+9) +
  154. '\n\n' +
  155. '.. autoattribute:: '+attr_name+'\n')
  156. f.write('\n\nIndices and tables\n' +
  157. '------------------\n' +
  158. '\n' +
  159. '* :ref:`genindex`\n' +
  160. '* :ref:`modindex`\n' +
  161. '* :ref:`search`')
  162. f.close()
  163. def main(code_dir, doc_dir):
  164. code_dir = code_dir
  165. doc_dir = doc_dir
  166. if not doc_dir.endswith('/'):
  167. doc_dir += '/'
  168. for old_rst in glob.glob(doc_dir+'*.rst'):
  169. os.remove(old_rst)
  170. if code_dir.endswith('.py'):
  171. list_module(code_dir[:-3], doc_dir)
  172. elif os.path.isdir(code_dir):
  173. list_dir(code_dir, doc_dir, code_dir)
  174. else:
  175. raise ValueError('code_dir not package or module')
  176. if __name__ == "__main__":
  177. main(sys.argv[1], sys.argv[2])