Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # docstring.py
- # Created by pira on 2017/08/11.
- #
- #coding: utf-8
- u"""Create documents for MarkDown from docstring.
- """
- import inspect
- import importlib
- import os
- #出力するfilename
- filename = 'docstring.md'
- # current directoryにあるpythonファイルのlistを生成
- python_files = list()
- for file in os.listdir('.'):
- name, ext = os.path.splitext(file)
- if ext == '.py':
- if name == 'docstring':
- pass
- else:
- python_files.append(name)
- print('Modules')
- print(python_files)
- #(module名,moduleのdocstring,関数と関数のdocstringのlist)のlistを作成
- modules = list()
- for module_name in python_files:
- module = importlib.import_module(module_name)
- module_doc = inspect.getdoc(module) or ''
- functions_and_docs = []
- functions = inspect.getmembers(module, inspect.isfunction)
- for func_name, func_object in functions:
- func_doc = inspect.getdoc(func_object) or ''
- func_and_doc = (func_name, func_doc)
- functions_and_docs.append(func_and_doc)
- modules.append( (module_name, module_doc, functions_and_docs) )
- #print(modules)
- #docsをMarkdownで見られるdocumentっぽく出力(HTMLタグも使用)
- docs = ''
- for module_name, module_doc, functions_and_docs in modules:
- docs += '<details><summary><strong>' + module_name + '</strong> - '+ module_doc + '</summary>\n\n'
- for func_name, func_doc in functions_and_docs:
- docs += '* <strong>' + func_name + '()</strong>\n'
- docs += func_doc + '\n'
- docs += '</details>\n\n'
- #file出力
- with open(filename, 'w') as fp:
- fp.write(docs)
- print('\nExport ' + filename + '.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement