2016-08-25 23:24:14 +00:00
|
|
|
import os
|
2015-04-08 05:54:53 +00:00
|
|
|
from collections import defaultdict
|
2016-08-25 23:24:14 +00:00
|
|
|
import textwrap
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-06-10 18:53:09 +00:00
|
|
|
from .base import PythonMapperBase, SphinxMapperBase
|
2016-06-09 22:43:55 +00:00
|
|
|
from ..utils import slugify
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2016-06-09 22:43:55 +00:00
|
|
|
from pydocstyle import parse
|
2015-08-05 17:09:07 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class PythonSphinxMapper(SphinxMapperBase):
|
2015-04-21 05:54:32 +00:00
|
|
|
|
|
|
|
'''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
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
2016-06-09 22:43:55 +00:00
|
|
|
parsed_data = parse(open(path), path)
|
2015-06-06 23:11:49 +00:00
|
|
|
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-07-07 23:19:25 +00:00
|
|
|
except ImportError:
|
|
|
|
self.app.warn('Error reading file: {0}'.format(path))
|
2015-06-06 23:11:49 +00:00
|
|
|
return None
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
def create_class(self, data, options=None, **kwargs):
|
2016-06-09 22:43:55 +00:00
|
|
|
'''
|
|
|
|
Create a class from the passed in data
|
2015-04-21 05:54:32 +00:00
|
|
|
|
2016-06-09 22:43:55 +00:00
|
|
|
:param data: dictionary data of pydocstyle 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
|
2016-08-25 23:24:14 +00:00
|
|
|
in [PythonClass, PythonFunction, PythonModule, PythonMethod, PythonPackage])
|
2015-05-31 05:03:19 +00:00
|
|
|
try:
|
2016-06-09 22:43:55 +00:00
|
|
|
cls = obj_map[data.kind]
|
2015-05-31 05:03:19 +00:00
|
|
|
except KeyError:
|
2016-06-09 22:43:55 +00:00
|
|
|
self.app.warn("Unknown Type: %s" % data.kind)
|
2015-05-31 05:03:19 +00:00
|
|
|
else:
|
2016-08-25 23:24:14 +00:00
|
|
|
path = kwargs.get('path')
|
|
|
|
obj = cls(data, jinja_env=self.jinja_env,
|
|
|
|
options=self.app.config.autoapi_options, path=path
|
|
|
|
)
|
2016-06-09 22:43:55 +00:00
|
|
|
for child_data in data.children:
|
2016-08-25 23:24:14 +00:00
|
|
|
for child_obj in self.create_class(child_data, options=options, path=path):
|
2016-06-09 22:43:55 +00:00
|
|
|
obj.children.append(child_obj)
|
|
|
|
self.add_object(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
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class PythonPythonMapper(PythonMapperBase):
|
2015-04-08 05:54:53 +00:00
|
|
|
|
|
|
|
language = 'python'
|
|
|
|
|
2015-06-10 18:01:06 +00:00
|
|
|
def __init__(self, obj, **kwargs):
|
2015-06-10 21:23:50 +00:00
|
|
|
super(PythonPythonMapper, self).__init__(obj, **kwargs)
|
2015-06-06 23:11:49 +00:00
|
|
|
|
2016-08-25 23:24:14 +00:00
|
|
|
# Properly name the object with dot notation
|
|
|
|
if self.top_level_object:
|
|
|
|
name = self.path.relative.split('.')[0].replace('/', '.')
|
|
|
|
else:
|
|
|
|
name = '.'.join([
|
|
|
|
os.path.dirname(self.path.relative).replace('/', '.'),
|
|
|
|
obj.name
|
|
|
|
])
|
2016-06-09 22:43:55 +00:00
|
|
|
self.id = slugify(name)
|
|
|
|
self.name = name
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
# Optional
|
|
|
|
self.children = []
|
2016-06-09 22:43:55 +00:00
|
|
|
try:
|
|
|
|
args = obj.source.split('\n')[0]
|
|
|
|
args = args.split('(')[1]
|
|
|
|
args = args.split(')')[0]
|
|
|
|
self.args = args.split(',')
|
|
|
|
except:
|
|
|
|
args = ''
|
|
|
|
self.docstring = obj.docstring or ''
|
2016-08-25 23:24:14 +00:00
|
|
|
self.docstring = textwrap.dedent(self.docstring)
|
2016-06-09 22:43:55 +00:00
|
|
|
self.docstring = self.docstring.replace("'''", '').replace('"""', '')
|
|
|
|
if getattr(obj, 'parent'):
|
|
|
|
self.inheritance = [obj.parent.name]
|
|
|
|
else:
|
|
|
|
self.inheritance = ''
|
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
|
|
|
|
2016-08-25 23:24:14 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return 'Python {type}: {name}'.format(name=self.name, type=self.type)
|
|
|
|
|
2015-06-10 20:33:42 +00:00
|
|
|
@property
|
|
|
|
def undoc_member(self):
|
|
|
|
return self.docstring == ''
|
|
|
|
|
|
|
|
@property
|
|
|
|
def private_member(self):
|
2015-06-10 20:58:52 +00:00
|
|
|
return self.short_name[0] == '_'
|
2015-06-10 20:33:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def special_member(self):
|
2015-06-10 20:58:52 +00:00
|
|
|
return self.short_name[0:2] == '__'
|
2015-06-10 20:33:42 +00:00
|
|
|
|
2015-06-10 20:58:52 +00:00
|
|
|
@property
|
|
|
|
def display(self):
|
|
|
|
if self.undoc_member and 'undoc-members' not in self.options:
|
2015-06-10 20:33:42 +00:00
|
|
|
return False
|
2015-06-10 20:58:52 +00:00
|
|
|
if self.private_member and 'private-members' not in self.options:
|
2015-06-10 20:33:42 +00:00
|
|
|
return False
|
2015-06-10 20:58:52 +00:00
|
|
|
if self.special_member and 'special-members' not in self.options:
|
2015-06-10 20:33:42 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class PythonFunction(PythonPythonMapper):
|
2015-04-21 05:54:32 +00:00
|
|
|
type = 'function'
|
2015-04-08 05:54:53 +00:00
|
|
|
|
2015-04-21 05:54:32 +00:00
|
|
|
|
2016-06-09 22:43:55 +00:00
|
|
|
class PythonMethod(PythonPythonMapper):
|
|
|
|
type = 'method'
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class PythonModule(PythonPythonMapper):
|
2015-04-21 05:54:32 +00:00
|
|
|
type = 'module'
|
2015-06-10 20:13:34 +00:00
|
|
|
top_level_object = True
|
2015-04-21 05:54:32 +00:00
|
|
|
|
|
|
|
|
2016-08-25 23:24:14 +00:00
|
|
|
class PythonPackage(PythonPythonMapper):
|
|
|
|
type = 'package'
|
|
|
|
top_level_object = True
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class PythonClass(PythonPythonMapper):
|
2015-04-21 05:54:32 +00:00
|
|
|
type = 'class'
|