| 1 |
""" |
| 2 |
cloud_sptheme.ext.autoattribute_search_bases -- monkeypatches autodoc so ``autoattribute`` searches base classes for attr doc. |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import logging; log = logging.getLogger(__name__) |
| 9 |
|
| 10 |
|
| 11 |
from cloud_sptheme import __version__ |
| 12 |
from cloud_sptheme.utils import patchapplier, monkeypatch |
| 13 |
|
| 14 |
__all__ = [ |
| 15 |
"setup", |
| 16 |
] |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
@patchapplier |
| 22 |
def _patch_autoattribute(): |
| 23 |
from sphinx.ext.autodoc import AttributeDocumenter, ModuleAnalyzer, PycodeError |
| 24 |
|
| 25 |
@monkeypatch(AttributeDocumenter) |
| 26 |
def add_content(_wrapped, self, *args, **kwds): |
| 27 |
if not self._datadescriptor and self.analyzer and self.objpath: |
| 28 |
attr_docs = self.analyzer.find_attr_docs() |
| 29 |
key = ('.'.join(self.objpath[:-1]), self.objpath[-1]) |
| 30 |
if key not in attr_docs: |
| 31 |
|
| 32 |
if hasattr(self.parent, "__mro__"): |
| 33 |
for basecls in self.parent.__mro__[1:]: |
| 34 |
try: |
| 35 |
analyzer = ModuleAnalyzer.for_module(basecls.__module__) |
| 36 |
base_attr_docs = analyzer.find_attr_docs() |
| 37 |
except PycodeError as err: |
| 38 |
continue |
| 39 |
|
| 40 |
base_key = (basecls.__name__, self.objpath[-1]) |
| 41 |
if base_key in base_attr_docs: |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
attr_docs[key] = base_attr_docs[base_key] |
| 47 |
break |
| 48 |
return _wrapped(self, *args, **kwds) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
def setup(app): |
| 54 |
|
| 55 |
_patch_autoattribute() |
| 56 |
|
| 57 |
|
| 58 |
return {'version': __version__} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|