SHARE
TWEET

Untitled

a guest Aug 12th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # docstring.py
  3. # Created by pira on 2017/08/11.
  4. #
  5.  
  6. #coding: utf-8
  7. u"""Create documents for MarkDown from docstring.
  8. """
  9.  
  10. import inspect
  11. import importlib
  12. import os
  13.  
  14. #出力するfilename
  15. filename = 'docstring.md'
  16.  
  17. # current directoryにあるpythonファイルのlistを生成
  18. python_files = list()
  19. for file in os.listdir('.'):
  20.     name, ext = os.path.splitext(file)
  21.     if ext == '.py':
  22.         if name == 'docstring':
  23.             pass
  24.         else:
  25.             python_files.append(name)
  26.  
  27. print('Modules')
  28. print(python_files)
  29.  
  30. #(module名,moduleのdocstring,関数と関数のdocstringのlist)のlistを作成
  31. modules = list()
  32.  
  33. for module_name in python_files:
  34.     module = importlib.import_module(module_name)
  35.     module_doc = inspect.getdoc(module) or ''
  36.  
  37.     functions_and_docs = []
  38.     functions = inspect.getmembers(module, inspect.isfunction)
  39.    
  40.     for func_name, func_object in functions:
  41.         func_doc = inspect.getdoc(func_object) or ''
  42.         func_and_doc = (func_name, func_doc)
  43.         functions_and_docs.append(func_and_doc)
  44.  
  45.     modules.append( (module_name, module_doc, functions_and_docs) )
  46.  
  47. #print(modules)
  48.  
  49. #docsをMarkdownで見られるdocumentっぽく出力(HTMLタグも使用)
  50. docs = ''
  51. for module_name, module_doc, functions_and_docs in modules:
  52.     docs += '<details><summary><strong>' + module_name + '</strong> - '+ module_doc + '</summary>\n\n'
  53.    
  54.     for func_name, func_doc in functions_and_docs:
  55.         docs += '* <strong>' + func_name + '()</strong>\n'
  56.         docs += func_doc + '\n'
  57.     docs += '</details>\n\n'
  58.  
  59. #file出力
  60. with open(filename, 'w') as fp:
  61.     fp.write(docs)
  62.     print('\nExport ' + filename + '.')
RAW Paste Data
Top