Skip to content

Commit

Permalink
add scanfilter parameter, move _iter_modules out of method scope, add…
Browse files Browse the repository at this point in the history
…resses #99
  • Loading branch information
poke1024 committed Dec 23, 2019
1 parent 86eac27 commit 30d127e
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,23 @@ def __lt__(self, other):
return self.refname < other.refname


def _iter_modules(paths):
"""
Custom implementation of `pkgutil.iter_modules()`
because that one doesn't play well with namespace packages.
See: https://github.com/pypa/setuptools/issues/83
"""
from os.path import isdir, join, splitext
for pth in paths:
for file in os.listdir(pth):
if file.startswith(('.', '__pycache__', '__init__.py')):
continue
if file.endswith(_SOURCE_SUFFIXES):
yield splitext(file)[0]
if isdir(join(pth, file)) and '.' not in file:
yield file


class Module(Doc):
"""
Representation of a module's documentation.
Expand All @@ -491,7 +508,8 @@ class Module(Doc):
__slots__ = ('supermodule', 'doc', '_context', '_is_inheritance_linked')

def __init__(self, module: Union[ModuleType, str], *, docfilter: Callable[[Doc], bool] = None,
supermodule: 'Module' = None, context: Context = None):
supermodule: 'Module' = None, context: Context = None,
scanfilter: Callable[[str], bool] = None):
"""
Creates a `Module` documentation object given the actual
module Python object.
Expand Down Expand Up @@ -563,23 +581,7 @@ def is_from_this_module(obj):
# If the module is a package, scan the directory for submodules
if self.is_package:

def iter_modules(paths):
"""
Custom implementation of `pkgutil.iter_modules()`
because that one doesn't play well with namespace packages.
See: https://github.com/pypa/setuptools/issues/83
"""
from os.path import isdir, join, splitext
for pth in paths:
for file in os.listdir(pth):
if file.startswith(('.', '__pycache__', '__init__.py')):
continue
if file.endswith(_SOURCE_SUFFIXES):
yield splitext(file)[0]
if isdir(join(pth, file)) and '.' not in file:
yield file

for root in iter_modules(self.obj.__path__):
for root in _iter_modules(self.obj.__path__):
# Ignore if this module was already doc'd.
if root in self.doc:
continue
Expand All @@ -590,6 +592,10 @@ def iter_modules(paths):

assert self.refname == self.name
fullname = "%s.%s" % (self.name, root)

if scanfilter and not scanfilter(fullname):
continue

self.doc[root] = m = Module(import_module(fullname),
docfilter=docfilter, supermodule=self,
context=self._context)
Expand Down

0 comments on commit 30d127e

Please sign in to comment.