2015-04-08 05:54:53 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
from epyparse import parsed
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-06-01 21:14:15 +00:00
|
|
|
from .base import AutoAPIBase, AutoAPIDomain
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
|
|
|
|
class PythonDomain(AutoAPIDomain):
|
|
|
|
|
|
|
|
'''Auto API domain handler for Python
|
|
|
|
|
|
|
|
Parses directly from Python files.
|
|
|
|
|
|
|
|
:param app: Sphinx application passed in as part of the extension
|
|
|
|
'''
|
|
|
|
|
2015-06-06 23:11:49 +00:00
|
|
|
def read_file(self, path, **kwargs):
|
|
|
|
'''Read file input into memory, returning deserialized objects
|
|
|
|
|
|
|
|
:param path: Path of file to read
|
|
|
|
'''
|
|
|
|
# TODO support JSON here
|
|
|
|
# TODO sphinx way of reporting errors in logs?
|
|
|
|
|
|
|
|
try:
|
|
|
|
parsed_data = parsed(path)
|
|
|
|
return parsed_data
|
|
|
|
except IOError:
|
2015-06-10 18:01:06 +00:00
|
|
|
self.app.warn('Error reading file: {0}'.format(path))
|
2015-06-06 23:11:49 +00:00
|
|
|
except TypeError:
|
2015-06-10 18:01:06 +00:00
|
|
|
self.app.warn('Error reading file: {0}'.format(path))
|
2015-06-06 23:11:49 +00:00
|
|
|
return None
|
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
def create_class(self, data):
|
|
|
|
'''Return instance of class based on Roslyn type property
|
|
|
|
|
|
|
|
Data keys handled here:
|
|
|
|
|
|
|
|
type
|
|
|
|
Set the object class
|
|
|
|
|
|
|
|
items
|
|
|
|
Recurse into :py:meth:`create_class` to create child object
|
|
|
|
instances
|
|
|
|
|
2015-05-31 05:03:19 +00:00
|
|
|
:param data: dictionary data of epydoc output
|
2015-04-21 05:54:32 +00:00
|
|
|
'''
|
2015-05-31 05:03:19 +00:00
|
|
|
obj_map = dict((cls.type, cls) for cls
|
|
|
|
in [PythonClass, PythonFunction, PythonModule])
|
|
|
|
try:
|
|
|
|
cls = obj_map[data['type']]
|
|
|
|
except KeyError:
|
|
|
|
self.app.warn("Unknown Type: %s" % data['type'])
|
|
|
|
else:
|
2015-06-06 23:11:49 +00:00
|
|
|
obj = cls(data, jinja_env=self.jinja_env)
|
2015-05-31 05:03:19 +00:00
|
|
|
if 'children' in data:
|
|
|
|
for child_data in data['children']:
|
2015-06-01 21:14:21 +00:00
|
|
|
for child_obj in self.create_class(child_data):
|
|
|
|
obj.children.append(child_obj)
|
2015-05-31 05:03:19 +00:00
|
|
|
yield obj
|
2015-04-21 05:54:32 +00:00
|
|
|
|
2015-04-08 05:54:53 +00:00
|
|
|
|
|
|
|
class PythonBase(AutoAPIBase):
|
|
|
|
|
|
|
|
language = 'python'
|
|
|
|
|
2015-06-10 18:01:06 +00:00
|
|
|
def __init__(self, obj, **kwargs):
|
|
|
|
super(PythonBase, self).__init__(obj, **kwargs)
|
2015-06-06 23:11:49 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
# Always exist
|
|
|
|
self.id = obj['fullname']
|
2015-06-06 23:11:49 +00:00
|
|
|
self.name = self.obj.get('fullname', self.id)
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
# Optional
|
|
|
|
self.imports = obj.get('imports', [])
|
|
|
|
self.children = []
|
|
|
|
self.parameters = obj.get('params', [])
|
|
|
|
self.docstring = obj.get('docstring', '')
|
2015-06-06 23:11:49 +00:00
|
|
|
self.methods = obj.get('methods', [])
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
# For later
|
|
|
|
self.item_map = defaultdict(list)
|
2015-04-08 05:54:53 +00:00
|
|
|
|
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
class PythonFunction(PythonBase):
|
|
|
|
type = 'function'
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
|
|
|
|
class PythonModule(PythonBase):
|
|
|
|
type = 'module'
|
|
|
|
|
|
|
|
|
|
|
|
class PythonClass(PythonBase):
|
|
|
|
type = 'class'
|