lbry-sdk/scripts/generate_json_api.py

76 lines
2.3 KiB
Python
Raw Normal View History

import os
import re
import json
import inspect
2018-12-12 18:18:32 +01:00
from lbrynet.extras.daemon.Daemon import Daemon
SECTIONS = re.compile("(.*?)Usage:(.*?)Options:(.*?)Returns:(.*)", re.DOTALL)
REQUIRED_OPTIONS = re.compile("\(<(.*?)>.*?\)")
ARGUMENT_NAME = re.compile("--([^=]+)")
ARGUMENT_TYPE = re.compile("\s*\((.*?)\)(.*)")
def get_api(obj):
docstr = inspect.getdoc(obj).strip()
try:
description, usage, options, returns = SECTIONS.search(docstr).groups()
except:
raise ValueError(f"Doc string format error for {obj.__name__}.")
required = re.findall(REQUIRED_OPTIONS, usage)
arguments = []
for line in options.splitlines():
line = line.strip()
if not line:
continue
if line.startswith('--'):
arg, desc = line.split(':', 1)
arg_name = ARGUMENT_NAME.search(arg).group(1)
arg_type, arg_desc = ARGUMENT_TYPE.search(desc).groups()
arguments.append({
'name': arg_name.strip(),
'type': arg_type.strip(),
'description': [arg_desc.strip()],
'is_required': arg_name in required
})
elif line == 'None':
continue
else:
arguments[-1]['description'].append(line.strip())
for arg in arguments:
arg['description'] = ' '.join(arg['description'])
2019-01-21 20:32:44 +01:00
name = obj.__name__[len('jsonrpc_'):]
return {
2019-01-21 20:32:44 +01:00
'name': name,
'description': description.strip(),
'arguments': arguments,
2019-01-21 20:32:44 +01:00
'returns': returns.strip(),
'examples': [{
2019-01-21 22:17:41 +01:00
'title': f"performing {name} operation",
2019-01-21 20:32:44 +01:00
'curl': f"""curl -d'{{"method": "{name}"}}' http://localhost:5279/""",
'lbrynet': f'lbrynet {name} --some-arg=foo',
'python': f'requests.post("http://localhost:5279", json={{"method": "{name}"}}).json()',
'output': returns.strip()
}]
}
def write_api(f):
apis = []
for method_name in sorted(Daemon.callable_methods.keys()):
apis.append(get_api(Daemon.callable_methods[method_name]))
json.dump(apis, f, indent=4)
if __name__ == '__main__':
parent = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
html_file = os.path.join(parent, 'docs', 'api.json')
with open(html_file, 'w+') as f:
write_api(f)