File: _old_pkgutil_code.py

package info (click to toggle)
xdoctest 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,512 kB
  • sloc: python: 10,963; sh: 815; cpp: 33; makefile: 19
file content (30 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

def _pkgutil_submodule_names(modpath, with_pkg=False, with_mod=True):
    """
    Ignore:
        x = sorted(submodule_paths(modname_to_modpath('ubelt')))
        y = sorted(_pkgutil_submodule_names(modname_to_modpath('ubelt')))
        x = [modpath_to_modname(p, hide_init=False, hide_main=False) for p in x]
        print('x = {!r}'.format(x))
        print('y = {!r}'.format(y))

    Notes:
        this will take into account pyc files, we choose not to.
    """
    package_name = modpath_to_modname(modpath)
    if isfile(modpath):
        # If input is a file, just return it
        yield package_name
    else:
        # Otherwise, if it is a package, find sub-packages and sub-modules
        import pkgutil
        # dont use the pkgutil version, as it is incompatible with pytest
        prefix = package_name + '.'
        walker = pkgutil.walk_packages([modpath], prefix=prefix,
                                       onerror=lambda x: None)  # nocover
        for importer, modname, ispkg in walker:
            if not ispkg and with_mod:
                yield modname
            elif ispkg and with_pkg:
                yield modname