lbry-sdk/scripts/gen_api_docs.py

69 lines
1.9 KiB
Python
Raw Normal View History

2016-12-09 04:05:31 +01:00
# -*- coding: utf-8 -*-
# Generate docs: python gen_api_docs.py
# See docs: pip install mkdocs; mkdocs serve
# Push docs: mkdocs gh-deploy
import inspect
import os.path as op
import re
import sys
from lbrynet.lbrynet_daemon.Daemon import Daemon
def _name(obj):
if hasattr(obj, '__name__'):
return obj.__name__
elif inspect.isdatadescriptor(obj):
return obj.fget.__name__
def _anchor(name):
anchor = name.lower().replace(' ', '-')
anchor = re.sub(r'[^\w\- ]', '', anchor)
return anchor
_docstring_header_pattern = re.compile(r'^([^\n]+)\n[\-\=]{3,}$', flags=re.MULTILINE)
_docstring_parameters_pattern = re.compile(r'^([^ \n]+) \: ([^\n]+)$', flags=re.MULTILINE)
def _replace_docstring_header(paragraph):
"""Process NumPy-like function docstrings."""
# Replace Markdown headers in docstrings with light headers in bold.
paragraph = re.sub(_docstring_header_pattern, r'*\1*', paragraph)
paragraph = re.sub(_docstring_parameters_pattern, r'\n* `\1` (\2)\n', paragraph)
return paragraph
def _doc(obj):
docstr = (inspect.getdoc(obj) or '').strip()
2017-06-09 18:14:03 +02:00
return _replace_docstring_header(docstr)
2016-12-09 04:05:31 +01:00
def _link(name, anchor=None):
return "[{name}](#{anchor})".format(name=name, anchor=anchor or _anchor(name))
def main():
curdir = op.dirname(op.realpath(__file__))
2017-06-09 18:14:03 +02:00
cli_doc_path = op.realpath(op.join(curdir, '..', 'docs', 'cli.md'))
2016-12-09 04:05:31 +01:00
# toc = ''
doc = ''
# Table of contents
2017-06-09 18:14:03 +02:00
for method_name in sorted(Daemon.callable_methods.keys()):
method = Daemon.callable_methods[method_name]
2016-12-09 04:05:31 +01:00
# toc += '* ' + _link(method_name, _anchor(method_name)) + "\n"
2017-06-09 18:14:03 +02:00
doc += '## ' + method_name + "\n\n```text\n" + _doc(method) + "\n```\n\n"
2016-12-09 04:05:31 +01:00
2017-06-09 18:14:03 +02:00
text = "# LBRY Command Line Documentation\n\n" + doc
with open(cli_doc_path, 'w+') as f:
2016-12-09 04:05:31 +01:00
f.write(text)
if __name__ == '__main__':
sys.exit(main())