Skip to content

Commit cb54a4d

Browse files
committed
add scanfilter parameter, addresses #99
also move _iter_modules out of method scope
1 parent 86eac27 commit cb54a4d

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

pdoc/__init__.py

+29-20
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,23 @@ def __lt__(self, other):
479479
return self.refname < other.refname
480480

481481

482+
def _iter_modules(paths):
483+
"""
484+
Custom implementation of `pkgutil.iter_modules()`
485+
because that one doesn't play well with namespace packages.
486+
See: https://github.com/pypa/setuptools/issues/83
487+
"""
488+
from os.path import isdir, join, splitext
489+
for pth in paths:
490+
for file in os.listdir(pth):
491+
if file.startswith(('.', '__pycache__', '__init__.py')):
492+
continue
493+
if file.endswith(_SOURCE_SUFFIXES):
494+
yield splitext(file)[0]
495+
if isdir(join(pth, file)) and '.' not in file:
496+
yield file
497+
498+
482499
class Module(Doc):
483500
"""
484501
Representation of a module's documentation.
@@ -491,14 +508,18 @@ class Module(Doc):
491508
__slots__ = ('supermodule', 'doc', '_context', '_is_inheritance_linked')
492509

493510
def __init__(self, module: Union[ModuleType, str], *, docfilter: Callable[[Doc], bool] = None,
494-
supermodule: 'Module' = None, context: Context = None):
511+
scanfilter: Callable[[str], bool] = None, supermodule: 'Module' = None,
512+
context: Context = None):
495513
"""
496514
Creates a `Module` documentation object given the actual
497515
module Python object.
498516
499517
`docfilter` is an optional predicate that controls which
500518
sub-objects are documentated (see also: `pdoc.html()`).
501519
520+
`scanfilter` is an optional predicate that controls which
521+
submodules are ignored during parsing.
522+
502523
`supermodule` is the parent `pdoc.Module` this module is
503524
a submodule of.
504525
@@ -563,23 +584,7 @@ def is_from_this_module(obj):
563584
# If the module is a package, scan the directory for submodules
564585
if self.is_package:
565586

566-
def iter_modules(paths):
567-
"""
568-
Custom implementation of `pkgutil.iter_modules()`
569-
because that one doesn't play well with namespace packages.
570-
See: https://github.com/pypa/setuptools/issues/83
571-
"""
572-
from os.path import isdir, join, splitext
573-
for pth in paths:
574-
for file in os.listdir(pth):
575-
if file.startswith(('.', '__pycache__', '__init__.py')):
576-
continue
577-
if file.endswith(_SOURCE_SUFFIXES):
578-
yield splitext(file)[0]
579-
if isdir(join(pth, file)) and '.' not in file:
580-
yield file
581-
582-
for root in iter_modules(self.obj.__path__):
587+
for root in _iter_modules(self.obj.__path__):
583588
# Ignore if this module was already doc'd.
584589
if root in self.doc:
585590
continue
@@ -590,9 +595,13 @@ def iter_modules(paths):
590595

591596
assert self.refname == self.name
592597
fullname = "%s.%s" % (self.name, root)
598+
599+
if scanfilter and not scanfilter(fullname):
600+
continue
601+
593602
self.doc[root] = m = Module(import_module(fullname),
594-
docfilter=docfilter, supermodule=self,
595-
context=self._context)
603+
docfilter=docfilter, scanfilter=scanfilter,
604+
supermodule=self, context=self._context)
596605
# Skip empty namespace packages because they may
597606
# as well be other auxiliary directories
598607
if m.is_namespace and not m.doc:

0 commit comments

Comments
 (0)