Skip to content

Commit

Permalink
Merge pull request astropy/astropy-helpers#148 from embray/sphinx-1.3…
Browse files Browse the repository at this point in the history
…-fixes

sphinx.ext.automodsumm broken with Sphinx 1.3
  • Loading branch information
embray committed Mar 30, 2015
2 parents 2a83c47 + e4f6be5 commit d66a547
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
17 changes: 10 additions & 7 deletions sphinx_automodapi/autodoc_enhancements.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ def type_object_attrgetter(obj, attr, *defargs):
of autodoc.
"""

if attr in obj.__dict__ and isinstance(obj.__dict__[attr], property):
# Note, this should only be used for properties--for any other type of
# descriptor (classmethod, for example) this can mess up existing
# expectcations of what getattr(cls, ...) returns
return obj.__dict__[attr]
else:
return getattr(obj, attr, *defargs)
for base in obj.__mro__:
if attr in base.__dict__:
if isinstance(base.__dict__[attr], property):
# Note, this should only be used for properties--for any other
# type of descriptor (classmethod, for example) this can mess
# up existing expectations of what getattr(cls, ...) returns
return base.__dict__[attr]
break

return getattr(obj, attr, *defargs)


def setup(app):
Expand Down
24 changes: 19 additions & 5 deletions sphinx_automodapi/automodapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@

from .utils import find_mod_objs

if sys.version_info[0] == 3:
text_type = str
else:
text_type = unicode


automod_templ_modheader = """
{modname} {pkgormod}
Expand Down Expand Up @@ -296,18 +301,24 @@ def automodapi_replace(sourcestr, app, dotoctree=True, docname=None,
if app.config.automodapi_writereprocessed:
# sometimes they are unicode, sometimes not, depending on how
# sphinx has processed things
if isinstance(newsourcestr, unicode):
if isinstance(newsourcestr, text_type):
ustr = newsourcestr
else:
ustr = newsourcestr.decode(app.config.source_encoding)

if docname is None:
with open(os.path.join(app.srcdir, 'unknown.automodapi'), 'a') as f:
f.write('\n**NEW DOC**\n\n')
f.write(ustr.encode('utf8'))
f.write(ustr)
else:
with open(os.path.join(app.srcdir, docname + '.automodapi'), 'w') as f:
f.write(ustr.encode('utf8'))
env = app.builder.env
# Determine the filename associated with this doc (specifically
# the extension)
filename = docname + os.path.splitext(env.doc2path(docname))[1]
filename += '.automodapi'

with open(os.path.join(app.srcdir, filename), 'w') as f:
f.write(ustr)

return newsourcestr
else:
Expand All @@ -330,8 +341,11 @@ def _mod_info(modname, toskip=[], onlylocals=True):
break

# find_mod_objs has already imported modname
# TODO: There is probably a cleaner way to do this, though this is pretty
# reliable for all Python versions for most cases that we care about.
pkg = sys.modules[modname]
ispkg = '__init__.' in os.path.split(pkg.__name__)[1]
ispkg = (hasattr(pkg, '__file__') and isinstance(pkg.__file__, str) and
os.path.split(pkg.__file__)[1].startswith('__init__.py'))

return ispkg, hascls, hasfunc

Expand Down
33 changes: 23 additions & 10 deletions sphinx_automodapi/automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,23 @@ def run(self):

self.content = cont

#for some reason, even though ``currentmodule`` is substituted in, sphinx
#doesn't necessarily recognize this fact. So we just force it
#internally, and that seems to fix things
# for some reason, even though ``currentmodule`` is substituted in,
# sphinx doesn't necessarily recognize this fact. So we just force
# it internally, and that seems to fix things
env.temp_data['py:module'] = modname

#can't use super because Sphinx/docutils has trouble
#return super(Autosummary,self).run()
# can't use super because Sphinx/docutils has trouble return
# super(Autosummary,self).run()
nodelist.extend(Autosummary.run(self))

return self.warnings + nodelist
finally: # has_content = False for the Automodsumm
self.content = []

def get_items(self, names):
self.genopt['imported-members'] = True
return Autosummary.get_items(self, names)


#<-------------------automod-diagram stuff------------------------------------>
class Automoddiagram(InheritanceDiagram):
Expand Down Expand Up @@ -220,10 +225,12 @@ def run(self):
#<---------------------automodsumm generation stuff--------------------------->
def process_automodsumm_generation(app):
env = app.builder.env
ext = app.config.source_suffix

filestosearch = [x + ext for x in env.found_docs
if os.path.isfile(env.doc2path(x))]\
filestosearch = []
for docname in env.found_docs:
filename = env.doc2path(docname)
if os.path.isfile(filename):
filestosearch.append(docname + os.path.splitext(filename)[1])

liness = []
for sfn in filestosearch:
Expand All @@ -238,10 +245,11 @@ def process_automodsumm_generation(app):
f.write('\n')

for sfn, lines in zip(filestosearch, liness):
suffix = os.path.splitext(sfn)[1]
if len(lines) > 0:
generate_automodsumm_docs(lines, sfn, builder=app.builder,
warn=app.warn, info=app.info,
suffix=app.config.source_suffix,
suffix=suffix,
base_path=app.srcdir)

#_automodsummrex = re.compile(r'^(\s*)\.\. automodsumm::\s*([A-Za-z0-9_.]+)\s*'
Expand Down Expand Up @@ -281,14 +289,16 @@ def automodsumm_to_autosummary_lines(fn, app):
"""

fullfn = os.path.join(app.builder.env.srcdir, fn)

with open(fullfn) as fr:
if 'astropy_helpers.sphinx.ext.automodapi' in app._extensions:
from astropy_helpers.sphinx.ext.automodapi import automodapi_replace
# Must do the automodapi on the source to get the automodsumm
# that might be in there
filestr = automodapi_replace(fr.read(), app, True, fn, False)
docname = os.path.splitext(fn)[0]
filestr = automodapi_replace(fr.read(), app, True, docname, False)
else:
filestr = fr.read()

Expand Down Expand Up @@ -353,6 +363,9 @@ def automodsumm_to_autosummary_lines(fn, app):
continue
newlines.append(allindent + nm)

# add one newline at the end of the autosummary block
newlines.append('')

return newlines


Expand Down
6 changes: 4 additions & 2 deletions sphinx_automodapi/tests/test_automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def warn(self, msg, loc):
automodsumm_to_autosummary_lines
generate_automodsumm_docs
process_automodsumm_generation
setup"""
setup
"""


def test_ams_to_asmry(tmpdir):
Expand Down Expand Up @@ -97,7 +98,8 @@ def test_ams_to_asmry(tmpdir):
.. autosummary::
:p:
pilot"""
pilot
"""


def test_ams_cython(tmpdir, cython_testpackage):
Expand Down

0 comments on commit d66a547

Please sign in to comment.