Skip to content

Commit

Permalink
Fix sphinx-doc#6327: apidoc: Support a python package consisted of __…
Browse files Browse the repository at this point in the history
…init__.so file
  • Loading branch information
tk0miya committed Feb 8, 2020
1 parent 9d301e5 commit cf908b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -15,6 +15,7 @@ Deprecated
* ``sphinx.environment.temp_data['gloss_entries']``
* ``sphinx.environment.BuildEnvironment.indexentries``
* ``sphinx.environment.collectors.indexentries.IndexEntriesCollector``
* ``sphinx.ext.apidoc.INITPY``
* ``sphinx.ext.apidoc.shall_skip()``
* ``sphinx.io.FiletypeNotFoundError``
* ``sphinx.io.get_filetype()``
Expand Down Expand Up @@ -77,6 +78,7 @@ Bugs fixed
* #6559: Wrong node-ids are generated in glossary directive
* #6986: apidoc: misdetects module name for .so file inside module
* #6899: apidoc: private members are not shown even if ``--private`` given
* #6327: apidoc: Support a python package consisted of __init__.so file
* #6999: napoleon: fails to parse tilde in :exc: role
* #7019: gettext: Absolute path used in message catalogs
* #7023: autodoc: nested partial functions are not listed
Expand Down
5 changes: 5 additions & 0 deletions doc/extdev/deprecated.rst
Expand Up @@ -56,6 +56,11 @@ The following is a list of deprecated interfaces.
- 4.0
- ``sphinx.errors.FiletypeNotFoundError``

* - ``sphinx.ext.apidoc.INITPY``
- 2.4
- 4.0
- N/A

* - ``sphinx.ext.apidoc.shall_skip()``
- 2.4
- 4.0
Expand Down
39 changes: 28 additions & 11 deletions sphinx/ext/apidoc.py
Expand Up @@ -29,7 +29,7 @@
import sphinx.locale
from sphinx import __display_version__, package_dir
from sphinx.cmd.quickstart import EXTENSIONS
from sphinx.deprecation import RemovedInSphinx40Warning
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.locale import __
from sphinx.util import rst
from sphinx.util.osutil import FileAvoidWrite, ensuredir
Expand All @@ -46,7 +46,6 @@
'show-inheritance',
]

INITPY = '__init__.py'
PY_SUFFIXES = ('.py', '.pyx') + tuple(EXTENSION_SUFFIXES)

template_dir = path.join(package_dir, 'templates', 'apidoc')
Expand Down Expand Up @@ -81,6 +80,16 @@ def module_join(*modnames: str) -> str:
return '.'.join(filter(None, modnames))


def is_packagedir(dirname: str = None, files: List[str] = None) -> bool:
"""Check given *files* contains __init__ file."""
if files is None and dirname is None:
return False

if files is None:
files = os.listdir(dirname)
return any(f for f in files if is_initpy(f))


def write_file(name: str, text: str, opts: Any) -> None:
"""Write the output file for module/package <name>."""
quiet = getattr(opts, 'quiet', None)
Expand Down Expand Up @@ -149,7 +158,7 @@ def create_package_file(root: str, master_package: str, subroot: str, py_files:
# build a list of sub modules
submodules = [sub.split('.')[0] for sub in py_files
if not is_skipped_module(path.join(root, sub), opts, excludes) and
sub != INITPY]
is_initpy(sub)]
submodules = [module_join(master_package, subroot, modname)
for modname in submodules]
options = copy(OPTIONS)
Expand Down Expand Up @@ -205,7 +214,7 @@ def shall_skip(module: str, opts: Any, excludes: List[str] = []) -> bool:
return True

# Are we a package (here defined as __init__.py, not the folder in itself)
if os.path.basename(module) == INITPY:
if is_initpy(module):
# Yes, check if we have any non-excluded modules at all here
all_skipped = True
basemodule = path.dirname(module)
Expand All @@ -218,8 +227,7 @@ def shall_skip(module: str, opts: Any, excludes: List[str] = []) -> bool:

# skip if it has a "private" name and this is selected
filename = path.basename(module)
if filename != '__init__.py' and filename.startswith('_') and \
not opts.includeprivate:
if is_initpy(filename) and filename.startswith('_') and not opts.includeprivate:
return True
return False

Expand Down Expand Up @@ -266,7 +274,7 @@ def recurse_tree(rootpath: str, excludes: List[str], opts: Any,
implicit_namespaces = getattr(opts, 'implicit_namespaces', False)

# check if the base directory is a package and get its name
if INITPY in os.listdir(rootpath) or implicit_namespaces:
if is_packagedir(rootpath) or implicit_namespaces:
root_package = rootpath.split(path.sep)[-1]
else:
# otherwise, the base is a directory with packages
Expand All @@ -278,11 +286,13 @@ def recurse_tree(rootpath: str, excludes: List[str], opts: Any,
py_files = sorted(f for f in files
if f.endswith(PY_SUFFIXES) and
not is_excluded(path.join(root, f), excludes))
is_pkg = INITPY in py_files
is_namespace = INITPY not in py_files and implicit_namespaces
is_pkg = is_packagedir(None, py_files)
is_namespace = not is_pkg and implicit_namespaces
if is_pkg:
py_files.remove(INITPY)
py_files.insert(0, INITPY)
for f in py_files[:]:
if is_initpy(f):
py_files.remove(f)
py_files.insert(0, f)
elif root != rootpath:
# only accept non-package at toplevel unless using implicit namespaces
if not implicit_namespaces:
Expand Down Expand Up @@ -505,6 +515,13 @@ def main(argv: List[str] = sys.argv[1:]) -> int:
return 0


deprecated_alias('sphinx.ext.apidoc',
{
'INITPY': '__init__.py',
},
RemovedInSphinx40Warning)


# So program can be started with "python -m sphinx.apidoc ..."
if __name__ == "__main__":
main()

0 comments on commit cf908b9

Please sign in to comment.