sphinx-autoapi/autoapi/mappers/javascript.py

142 lines
3.8 KiB
Python
Raw Normal View History

import json
import subprocess
import os
import sphinx.util.logging
from .base import PythonMapperBase, SphinxMapperBase
LOGGER = sphinx.util.logging.getLogger(__name__)
2015-06-10 21:23:50 +00:00
class JavaScriptSphinxMapper(SphinxMapperBase):
2019-01-27 05:20:45 +00:00
"""Auto API domain handler for Javascript
Parses directly from Javascript files.
:param app: Sphinx application passed in as part of the extension
2019-01-27 05:20: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
:param path: Path of file to read
2019-01-27 05:20:45 +00:00
"""
# TODO support JSON here
# TODO sphinx way of reporting errors in logs?
2019-01-27 05:20:45 +00:00
subcmd = "jsdoc"
if os.name == "nt":
subcmd = ".".join([subcmd, "cmd"])
try:
2019-01-27 05:20:45 +00:00
parsed_data = json.loads(subprocess.check_output([subcmd, "-X", path]))
return parsed_data
except IOError:
2019-01-27 05:20:45 +00:00
LOGGER.warning("Error reading file: {0}".format(path))
except TypeError:
2019-01-27 05:20:45 +00:00
LOGGER.warning("Error reading file: {0}".format(path))
return None
2015-06-10 18:36:18 +00:00
# Subclassed to iterate over items
2017-11-05 23:29:39 +00:00
def map(self, options=None):
2019-01-27 05:20:45 +00:00
"""Trigger find of serialized sources and build objects"""
for path, data in self.paths.items():
for item in data:
for obj in self.create_class(item, options):
obj.jinja_env = self.jinja_env
self.add_object(obj)
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 Javascript data
Data keys handled here:
type
Set the object class
consts, types, vars, funcs
Recurse into :py:meth:`create_class` to create child object
instances
:param data: dictionary data from godocjson output
2019-01-27 05:20:45 +00:00
"""
obj_map = dict((cls.type, cls) for cls in ALL_CLASSES)
try:
2019-01-27 05:20:45 +00:00
cls = obj_map[data["kind"]]
2015-06-23 03:25:25 +00:00
except (KeyError, TypeError):
2019-01-27 05:20:45 +00:00
LOGGER.warning("Unknown Type: %s" % data)
else:
# Recurse for children
2015-06-10 18:36:18 +00:00
obj = cls(data, jinja_env=self.jinja_env)
2019-01-27 05:20:45 +00:00
if "children" in data:
for child_data in data["children"]:
for child_obj in self.create_class(child_data, options=options):
obj.children.append(child_obj)
yield obj
2015-06-10 21:23:50 +00:00
class JavaScriptPythonMapper(PythonMapperBase):
2019-01-27 05:20:45 +00:00
language = "javascript"
2015-06-10 18:36:18 +00:00
def __init__(self, obj, **kwargs):
2019-01-27 05:20:45 +00:00
"""
Map JSON data into Python object.
This is the standard object that will be rendered into the templates,
so we try and keep standard naming to keep templates more re-usable.
2019-01-27 05:20:45 +00:00
"""
2015-06-10 21:23:50 +00:00
super(JavaScriptPythonMapper, self).__init__(obj, **kwargs)
2019-01-27 05:20:45 +00:00
self.name = obj.get("name")
self.id = self.name
# Second level
2019-01-27 05:20:45 +00:00
self.docstring = obj.get("description", "")
# self.docstring = obj.get('comment', '')
2019-01-27 05:20:45 +00:00
self.imports = obj.get("imports", [])
self.children = []
self.parameters = map(
2019-01-27 05:20:45 +00:00
lambda n: {"name": n["name"], "type": n["type"][0]}, obj.get("param", [])
)
# Language Specific
pass
2015-06-10 21:23:50 +00:00
class JavaScriptClass(JavaScriptPythonMapper):
2019-01-27 05:20:45 +00:00
type = "class"
ref_directive = "class"
top_level_object = True
2015-06-10 21:23:50 +00:00
class JavaScriptFunction(JavaScriptPythonMapper):
2019-01-27 05:20:45 +00:00
type = "function"
ref_type = "func"
2015-06-10 21:23:50 +00:00
class JavaScriptData(JavaScriptPythonMapper):
2019-01-27 05:20:45 +00:00
type = "data"
ref_directive = "data"
2015-06-10 21:23:50 +00:00
class JavaScriptMember(JavaScriptPythonMapper):
2019-01-27 05:20:45 +00:00
type = "member"
ref_directive = "member"
2015-06-10 21:23:50 +00:00
class JavaScriptAttribute(JavaScriptPythonMapper):
2019-01-27 05:20:45 +00:00
type = "attribute"
ref_directive = "attr"
2018-06-06 06:15:45 +00:00
ALL_CLASSES = [
JavaScriptFunction,
JavaScriptClass,
JavaScriptData,
JavaScriptAttribute,
JavaScriptMember,
]