summaryrefslogtreecommitdiff
path: root/doc/sphinxext/numpydoc/traitsdoc.py
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2013-02-28 20:06:48 -0700
committerCharles Harris <charlesr.harris@gmail.com>2013-02-28 20:06:48 -0700
commit629a2d4daa376e5639ad5106289c77b8137f9f15 (patch)
tree4e5678c1a966b7c24fd0a715982eb30301880ccb /doc/sphinxext/numpydoc/traitsdoc.py
parent4b361f62be7f750dc385d0b7dc7529ad9af5e4ea (diff)
parent9d8722b5bc76ecb2fe74a8e8dd3a7b1c2c83985b (diff)
downloadnumpy-629a2d4daa376e5639ad5106289c77b8137f9f15.tar.gz
Merge branch 'enh-numpydoc'
There were some conflicts with the 2to3 work in numpy. I think I got the fixes right. * enh-numpydoc: DOC: fix doc/source/conf.py to work with Python 3 BUG: numpydoc: check that it works with sub-classes TST: numpydoc: more class tests BUG: numpydoc: fix bugs in attribute docstring extraction + improve presentation TST: numpydoc: add stub test files, to check that files at least import MAINT: always use plot directive from Matplotlib, and prefer Sphinx linkcode ENH: numpydoc: Python 2 & 3 in single codebase, restructure as a package ENH: numpydoc: deal with duplicated signatures DOC: numpydoc/linkcode: mention that the extension will be in Sphinx upstream BUG: numpydoc/linkcode: do not detect linkcode config changes Conflicts: doc/sphinxext/numpydoc/docscrape.py doc/sphinxext/numpydoc/docscrape_sphinx.py doc/sphinxext/numpydoc/linkcode.py doc/sphinxext/numpydoc/phantom_import.py doc/sphinxext/numpydoc/traitsdoc.py
Diffstat (limited to 'doc/sphinxext/numpydoc/traitsdoc.py')
-rw-r--r--doc/sphinxext/numpydoc/traitsdoc.py140
1 files changed, 140 insertions, 0 deletions
diff --git a/doc/sphinxext/numpydoc/traitsdoc.py b/doc/sphinxext/numpydoc/traitsdoc.py
new file mode 100644
index 000000000..e63789436
--- /dev/null
+++ b/doc/sphinxext/numpydoc/traitsdoc.py
@@ -0,0 +1,140 @@
+"""
+=========
+traitsdoc
+=========
+
+Sphinx extension that handles docstrings in the Numpy standard format, [1]
+and support Traits [2].
+
+This extension can be used as a replacement for ``numpydoc`` when support
+for Traits is required.
+
+.. [1] http://projects.scipy.org/numpy/wiki/CodingStyleGuidelines#docstring-standard
+.. [2] http://code.enthought.com/projects/traits/
+
+"""
+
+import inspect
+import os
+import pydoc
+
+from . import docscrape
+from . import docscrape_sphinx
+from .docscrape_sphinx import SphinxClassDoc, SphinxFunctionDoc, SphinxDocString
+
+from . import numpydoc
+
+from . import comment_eater
+
+class SphinxTraitsDoc(SphinxClassDoc):
+ def __init__(self, cls, modulename='', func_doc=SphinxFunctionDoc):
+ if not inspect.isclass(cls):
+ raise ValueError("Initialise using a class. Got %r" % cls)
+ self._cls = cls
+
+ if modulename and not modulename.endswith('.'):
+ modulename += '.'
+ self._mod = modulename
+ self._name = cls.__name__
+ self._func_doc = func_doc
+
+ docstring = pydoc.getdoc(cls)
+ docstring = docstring.split('\n')
+
+ # De-indent paragraph
+ try:
+ indent = min(len(s) - len(s.lstrip()) for s in docstring
+ if s.strip())
+ except ValueError:
+ indent = 0
+
+ for n,line in enumerate(docstring):
+ docstring[n] = docstring[n][indent:]
+
+ self._doc = docscrape.Reader(docstring)
+ self._parsed_data = {
+ 'Signature': '',
+ 'Summary': '',
+ 'Description': [],
+ 'Extended Summary': [],
+ 'Parameters': [],
+ 'Returns': [],
+ 'Raises': [],
+ 'Warns': [],
+ 'Other Parameters': [],
+ 'Traits': [],
+ 'Methods': [],
+ 'See Also': [],
+ 'Notes': [],
+ 'References': '',
+ 'Example': '',
+ 'Examples': '',
+ 'index': {}
+ }
+
+ self._parse()
+
+ def _str_summary(self):
+ return self['Summary'] + ['']
+
+ def _str_extended_summary(self):
+ return self['Description'] + self['Extended Summary'] + ['']
+
+ def __str__(self, indent=0, func_role="func"):
+ out = []
+ out += self._str_signature()
+ out += self._str_index() + ['']
+ out += self._str_summary()
+ out += self._str_extended_summary()
+ for param_list in ('Parameters', 'Traits', 'Methods',
+ 'Returns','Raises'):
+ out += self._str_param_list(param_list)
+ out += self._str_see_also("obj")
+ out += self._str_section('Notes')
+ out += self._str_references()
+ out += self._str_section('Example')
+ out += self._str_section('Examples')
+ out = self._str_indent(out,indent)
+ return '\n'.join(out)
+
+def looks_like_issubclass(obj, classname):
+ """ Return True if the object has a class or superclass with the given class
+ name.
+
+ Ignores old-style classes.
+ """
+ t = obj
+ if t.__name__ == classname:
+ return True
+ for klass in t.__mro__:
+ if klass.__name__ == classname:
+ return True
+ return False
+
+def get_doc_object(obj, what=None, config=None):
+ if what is None:
+ if inspect.isclass(obj):
+ what = 'class'
+ elif inspect.ismodule(obj):
+ what = 'module'
+ elif isinstance(obj, collections.Callable):
+ what = 'function'
+ else:
+ what = 'object'
+ if what == 'class':
+ doc = SphinxTraitsDoc(obj, '', func_doc=SphinxFunctionDoc, config=config)
+ if looks_like_issubclass(obj, 'HasTraits'):
+ for name, trait, comment in comment_eater.get_class_traits(obj):
+ # Exclude private traits.
+ if not name.startswith('_'):
+ doc['Traits'].append((name, trait, comment.splitlines()))
+ return doc
+ elif what in ('function', 'method'):
+ return SphinxFunctionDoc(obj, '', config=config)
+ else:
+ return SphinxDocString(pydoc.getdoc(obj), config=config)
+
+def setup(app):
+ # init numpydoc
+ numpydoc.setup(app, get_doc_object)
+