|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import inspect |
| 3 | +import json |
| 4 | + |
| 5 | +import terminusdb_client |
| 6 | +from numpydoc.docscrape import FunctionDoc, ClassDoc |
| 7 | + |
| 8 | +def parameter_to_document(param): |
| 9 | + return {'@type': 'Parameter', 'type': param.type, 'name': param.name, 'summary': "\n".join(param.desc)} |
| 10 | + |
| 11 | +classes_to_scan = ['Client']#, 'WOQLQuery'] |
| 12 | +classes = [] |
| 13 | + |
| 14 | +for name, obj in inspect.getmembers(terminusdb_client): |
| 15 | + if inspect.isclass(obj) and 'terminusdb' in str(obj): |
| 16 | + if name in classes_to_scan: |
| 17 | + docstring_class = ClassDoc(obj) |
| 18 | + class_params = [parameter_to_document(x) for x in docstring_class['Attributes']] |
| 19 | + functions = [] |
| 20 | + for func in obj.__dict__.values(): |
| 21 | + if callable(func) and func.__doc__: |
| 22 | + docstring_function = FunctionDoc(func) |
| 23 | + parameters = [parameter_to_document(x) for x in docstring_function['Parameters']] |
| 24 | + examples = '\n'.join(docstring_function['Examples']) |
| 25 | + if len(docstring_function['Returns']) > 0: |
| 26 | + ret = docstring_function['Returns'][0] # Functions always have one return |
| 27 | + returns = {'@type': 'Returns', 'name': '', 'type': ret.type} |
| 28 | + if len(ret.desc) > 0: |
| 29 | + returns['summary'] = "\n".join(ret.desc) |
| 30 | + else: |
| 31 | + returns = {'@type': 'Returns', 'name': '', 'type': 'void'} |
| 32 | + function_obj = {'@type': 'Definition', |
| 33 | + 'name': func.__name__, |
| 34 | + 'parameters': parameters, |
| 35 | + 'returns': returns, |
| 36 | + 'examples': [examples] if examples != "" else None, |
| 37 | + 'summary': "\n".join(docstring_function['Summary'])} |
| 38 | + functions.append(function_obj) |
| 39 | + class_obj = {'@type': 'Class', 'name': name, 'summary': "\n".join(docstring_class['Summary']), 'memberVariables': class_params, 'memberFunctions': functions} |
| 40 | + classes.append(class_obj) |
| 41 | + |
| 42 | +application = { |
| 43 | + '@type': 'Application', |
| 44 | + 'version': terminusdb_client.__version__.__version__, |
| 45 | + 'name': terminusdb_client.__version__.__title__, |
| 46 | + 'summary': terminusdb_client.__version__.__description__, |
| 47 | + 'language': 'Python', |
| 48 | + 'license': terminusdb_client.__version__.__license__, |
| 49 | + 'modules': [ |
| 50 | + { |
| 51 | + '@type': 'Module', |
| 52 | + 'name': 'terminusdb_client', |
| 53 | + 'classes': classes |
| 54 | + } |
| 55 | + ]} |
| 56 | + |
| 57 | +print(json.dumps(application)) |
0 commit comments