2015-06-10 18:03:45 +00:00
|
|
|
import json
|
|
|
|
import subprocess
|
2015-05-29 22:22:06 +00:00
|
|
|
|
2019-01-27 01:11:12 +00:00
|
|
|
import sphinx.util.logging
|
|
|
|
|
2015-06-10 18:53:09 +00:00
|
|
|
from .base import PythonMapperBase, SphinxMapperBase
|
2015-05-29 22:22:06 +00:00
|
|
|
|
2019-01-27 01:11:12 +00:00
|
|
|
LOGGER = sphinx.util.logging.getLogger(__name__)
|
|
|
|
|
2015-05-29 22:22:06 +00:00
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoSphinxMapper(SphinxMapperBase):
|
2015-05-29 22:22:06 +00:00
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Auto API domain handler for Go
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
Parses directly from Go files.
|
|
|
|
|
|
|
|
:param app: Sphinx application passed in as part of the extension
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
2015-05-29 22:22:06 +00:00
|
|
|
|
2017-11-09 17:56:42 +00:00
|
|
|
def load(self, patterns, dirs, ignore=None):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
2015-06-10 18:03:45 +00:00
|
|
|
Load objects from the filesystem into the ``paths`` dictionary.
|
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
2015-09-23 23:00:43 +00:00
|
|
|
for _dir in dirs:
|
|
|
|
data = self.read_file(_dir)
|
|
|
|
if data:
|
|
|
|
self.paths[_dir] = data
|
2015-06-10 18:03:45 +00:00
|
|
|
|
|
|
|
def read_file(self, path, **kwargs):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Read file input into memory, returning deserialized objects
|
2015-06-10 18:03:45 +00:00
|
|
|
|
|
|
|
:param path: Path of file to read
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
2015-06-10 18:03:45 +00:00
|
|
|
# TODO support JSON here
|
|
|
|
# TODO sphinx way of reporting errors in logs?
|
|
|
|
|
|
|
|
try:
|
2019-01-27 05:20:45 +00:00
|
|
|
parsed_data = json.loads(subprocess.check_output(["godocjson", path]))
|
2015-06-10 18:03:45 +00:00
|
|
|
return parsed_data
|
|
|
|
except IOError:
|
2019-01-27 05:20:45 +00:00
|
|
|
LOGGER.warning("Error reading file: {0}".format(path))
|
2015-06-10 18:03:45 +00:00
|
|
|
except TypeError:
|
2019-01-27 05:20:45 +00:00
|
|
|
LOGGER.warning("Error reading file: {0}".format(path))
|
2015-06-10 18:03:45 +00:00
|
|
|
return None
|
|
|
|
|
2017-11-09 17:56:42 +00:00
|
|
|
def create_class(self, data, options=None, **kwargs):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Return instance of class based on Go data
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
Data keys handled here:
|
|
|
|
|
2015-06-10 18:03:45 +00:00
|
|
|
_type
|
2015-05-29 22:22:06 +00:00
|
|
|
Set the object class
|
|
|
|
|
2015-05-31 01:32:43 +00:00
|
|
|
consts, types, vars, funcs
|
2015-05-29 22:22:06 +00:00
|
|
|
Recurse into :py:meth:`create_class` to create child object
|
|
|
|
instances
|
|
|
|
|
2015-05-30 23:28:59 +00:00
|
|
|
:param data: dictionary data from godocjson output
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
|
|
|
_type = kwargs.get("_type")
|
|
|
|
obj_map = dict((cls.type, cls) for cls in ALL_CLASSES)
|
2015-05-31 05:03:19 +00:00
|
|
|
try:
|
2015-06-10 18:03:45 +00:00
|
|
|
# Contextual type data from children recursion
|
|
|
|
if _type:
|
2019-01-27 05:20:45 +00:00
|
|
|
LOGGER.debug("Forcing Go Type %s" % _type)
|
2015-06-10 18:03:45 +00:00
|
|
|
cls = obj_map[_type]
|
|
|
|
else:
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = obj_map[data["type"]]
|
2015-06-06 20:44:01 +00:00
|
|
|
except KeyError:
|
2019-01-27 05:20:45 +00:00
|
|
|
LOGGER.warning("Unknown Type: %s" % data)
|
2015-05-31 05:03:19 +00:00
|
|
|
else:
|
2019-01-27 05:20:45 +00:00
|
|
|
if cls.inverted_names and "names" in data:
|
2015-05-31 05:03:19 +00:00
|
|
|
# Handle types that have reversed names parameter
|
2019-01-27 05:20:45 +00:00
|
|
|
for name in data["names"]:
|
2015-05-31 05:03:19 +00:00
|
|
|
data_inv = {}
|
|
|
|
data_inv.update(data)
|
2019-01-27 05:20:45 +00:00
|
|
|
data_inv["name"] = name
|
|
|
|
if "names" in data_inv:
|
|
|
|
del data_inv["names"]
|
2015-05-31 05:03:19 +00:00
|
|
|
for obj in self.create_class(data_inv):
|
|
|
|
yield obj
|
|
|
|
else:
|
|
|
|
# Recurse for children
|
2015-06-10 18:03:45 +00:00
|
|
|
obj = cls(data, jinja_env=self.jinja_env)
|
2019-01-27 05:20:45 +00:00
|
|
|
for child_type in ["consts", "types", "vars", "funcs"]:
|
2015-05-31 05:03:19 +00:00
|
|
|
for child_data in data.get(child_type, []):
|
2019-01-27 05:20:45 +00:00
|
|
|
obj.children += list(
|
|
|
|
self.create_class(
|
|
|
|
child_data,
|
|
|
|
_type=child_type.replace("consts", "const")
|
|
|
|
.replace("types", "type")
|
|
|
|
.replace("vars", "variable")
|
|
|
|
.replace("funcs", "func"),
|
|
|
|
)
|
|
|
|
)
|
2015-05-31 05:03:19 +00:00
|
|
|
yield obj
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoPythonMapper(PythonMapperBase):
|
2015-05-29 22:22:06 +00:00
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
language = "go"
|
2015-05-31 05:03:19 +00:00
|
|
|
inverted_names = False
|
2015-05-29 22:22:06 +00:00
|
|
|
|
2015-06-10 18:03:45 +00:00
|
|
|
def __init__(self, obj, **kwargs):
|
2015-06-10 21:23:50 +00:00
|
|
|
super(GoPythonMapper, self).__init__(obj, **kwargs)
|
2019-01-27 05:20:45 +00:00
|
|
|
self.name = obj.get("name") or obj.get("packageName")
|
2015-05-31 05:03:19 +00:00
|
|
|
self.id = self.name
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
# Second level
|
2019-01-27 05:20:45 +00:00
|
|
|
self.imports = obj.get("imports", [])
|
2015-05-29 22:22:06 +00:00
|
|
|
self.children = []
|
2015-05-31 01:32:43 +00:00
|
|
|
self.parameters = map(
|
2019-01-27 05:20:45 +00:00
|
|
|
lambda n: {"name": n["name"], "type": n["type"].lstrip("*")},
|
|
|
|
obj.get("parameters", []),
|
2015-05-31 01:32:43 +00:00
|
|
|
)
|
2019-01-27 05:20:45 +00:00
|
|
|
self.docstring = obj.get("doc", "")
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
# Go Specific
|
2019-01-27 05:20:45 +00:00
|
|
|
self.notes = obj.get("notes", {})
|
|
|
|
self.filenames = obj.get("filenames", [])
|
|
|
|
self.bugs = obj.get("bugs", [])
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-01-27 05:20:45 +00:00
|
|
|
return "<{cls} {id}>".format(cls=self.__class__.__name__, id=self.id)
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def short_name(self):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Shorten name property"""
|
|
|
|
return self.name.split(".")[-1]
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def namespace(self):
|
2019-01-27 05:20:45 +00:00
|
|
|
pieces = self.id.split(".")[:-1]
|
2015-05-29 22:22:06 +00:00
|
|
|
if pieces:
|
2019-01-27 05:20:45 +00:00
|
|
|
return ".".join(pieces)
|
2018-04-21 19:48:06 +00:00
|
|
|
return None
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ref_type(self):
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ref_directive(self):
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def methods(self):
|
2019-01-27 05:20:45 +00:00
|
|
|
return self.obj.get("methods", [])
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoVariable(GoPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "var"
|
2015-05-31 05:03:19 +00:00
|
|
|
inverted_names = True
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoMethod(GoPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "method"
|
|
|
|
ref_directive = "meth"
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoConstant(GoPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "const"
|
2015-05-31 05:03:19 +00:00
|
|
|
inverted_names = True
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoFunction(GoPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "func"
|
|
|
|
ref_type = "function"
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoPackage(GoPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "package"
|
|
|
|
ref_directive = "pkg"
|
2015-06-10 20:13:34 +00:00
|
|
|
top_level_object = True
|
2015-05-29 22:22:06 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class GoType(GoPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "type"
|
2015-06-06 20:44:01 +00:00
|
|
|
|
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
ALL_CLASSES = [GoConstant, GoFunction, GoPackage, GoVariable, GoType, GoMethod]
|