mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
autosummary: refactor generate.py a bit
This commit is contained in:
parent
939012eb1e
commit
bffc8741aa
@ -68,8 +68,6 @@ from sphinx import addnodes, roles
|
||||
from sphinx.util import patfilter
|
||||
from sphinx.util.compat import Directive
|
||||
|
||||
import sphinx.ext.autodoc
|
||||
|
||||
|
||||
# -- autosummary_toc node ------------------------------------------------------
|
||||
|
||||
@ -116,22 +114,23 @@ def get_documenter(obj):
|
||||
"""
|
||||
Get an autodoc.Documenter class suitable for documenting the given object
|
||||
"""
|
||||
reg = sphinx.ext.autodoc.AutoDirective._registry
|
||||
import sphinx.ext.autodoc as autodoc
|
||||
|
||||
if inspect.isclass(obj):
|
||||
if issubclass(obj, Exception):
|
||||
return reg.get('exception')
|
||||
return reg.get('class')
|
||||
return autodoc.ExceptionDocumenter
|
||||
return autodoc.ClassDocumenter
|
||||
elif inspect.ismodule(obj):
|
||||
return reg.get('module')
|
||||
return autodoc.ModuleDocumenter
|
||||
elif inspect.ismethod(obj) or inspect.ismethoddescriptor(obj):
|
||||
return reg.get('method')
|
||||
return autodoc.MethodDocumenter
|
||||
elif (inspect.ismemberdescriptor(obj) or inspect.isgetsetdescriptor(obj)
|
||||
or inspect.isdatadescriptor(obj)):
|
||||
return reg.get('attribute')
|
||||
return autodoc.AttributeDocumenter
|
||||
elif inspect.isroutine(obj):
|
||||
return reg.get('function')
|
||||
return autodoc.FunctionDocumenter
|
||||
else:
|
||||
return reg.get('data')
|
||||
return autodoc.DataDocumenter
|
||||
|
||||
# -- .. autosummary:: ----------------------------------------------------------
|
||||
|
||||
|
@ -25,7 +25,7 @@ import inspect
|
||||
|
||||
from jinja2 import Environment, PackageLoader
|
||||
|
||||
from sphinx.ext.autosummary import import_by_name
|
||||
from sphinx.ext.autosummary import import_by_name, get_documenter
|
||||
from sphinx.util import ensuredir
|
||||
|
||||
# create our own templating environment, for module template only
|
||||
@ -73,17 +73,16 @@ def generate_autosummary_docs(sources, output_dir=None, suffix='.rst',
|
||||
if inspect.ismodule(obj):
|
||||
# XXX replace this with autodoc's API?
|
||||
tmpl = env.get_template('module')
|
||||
functions = [getattr(obj, item).__name__
|
||||
for item in dir(obj)
|
||||
if inspect.isfunction(getattr(obj, item))]
|
||||
classes = [getattr(obj, item).__name__
|
||||
for item in dir(obj)
|
||||
if inspect.isclass(getattr(obj, item))
|
||||
and not issubclass(getattr(obj, item), Exception)]
|
||||
exceptions = [getattr(obj, item).__name__
|
||||
for item in dir(obj)
|
||||
if inspect.isclass(getattr(obj, item))
|
||||
and issubclass(getattr(obj, item), Exception)]
|
||||
|
||||
def get_items(mod, typ):
|
||||
return [getattr(mod, name).__name__
|
||||
for name in dir(mod)
|
||||
if get_documenter(getattr(mod,name)).objtype==typ]
|
||||
|
||||
functions = get_items(obj, 'function')
|
||||
classes = get_items(obj, 'class')
|
||||
exceptions = get_items(obj, 'exception')
|
||||
|
||||
rendered = tmpl.render(name=name,
|
||||
underline='='*len(name),
|
||||
functions=functions,
|
||||
@ -96,19 +95,11 @@ def generate_autosummary_docs(sources, output_dir=None, suffix='.rst',
|
||||
else:
|
||||
f.write('%s\n%s\n\n' % (name, '='*len(name)))
|
||||
|
||||
if inspect.isclass(obj):
|
||||
if issubclass(obj, Exception):
|
||||
f.write(format_modulemember(name, 'autoexception'))
|
||||
else:
|
||||
f.write(format_modulemember(name, 'autoclass'))
|
||||
elif inspect.ismethod(obj) or inspect.ismethoddescriptor(obj):
|
||||
f.write(format_classmember(name, 'automethod'))
|
||||
elif callable(obj):
|
||||
f.write(format_modulemember(name, 'autofunction'))
|
||||
elif hasattr(obj, '__get__'):
|
||||
f.write(format_classmember(name, 'autoattribute'))
|
||||
doc = get_documenter(obj)
|
||||
if doc.objtype in ('method', 'attribute'):
|
||||
f.write(format_classmember(name, 'auto%s' % doc.objtype))
|
||||
else:
|
||||
f.write(format_modulemember(name, 'autofunction'))
|
||||
f.write(format_modulemember(name, 'auto%s' % doc.objtype))
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user