]>
crepu.dev Git - config.git/blob - djavu-asus/elpa/elpy-20230803.1455/elpy/pydocutils.py
4 from pydoc
import safeimport
, resolve
, ErrorDuringImport
5 from pkgutil
import iter_modules
7 from elpy
import compat
9 # Types we want to recurse into (nodes).
10 CONTAINER_TYPES
= (type, types
.ModuleType
)
11 # Types of attributes we can get documentation for (leaves).
14 types
.BuiltinFunctionType
,
15 types
.BuiltinMethodType
,
18 if not compat
.PYTHON3
: # pragma: nocover
19 # Python 2 old style classes
20 CONTAINER_TYPES
= tuple(list(CONTAINER_TYPES
) + [types
.ClassType
])
21 PYDOC_TYPES
= tuple(list(PYDOC_TYPES
) + [types
.ClassType
])
24 def get_pydoc_completions(modulename
):
25 """Get possible completions for modulename for pydoc.
27 Returns a list of possible values to be passed to pydoc.
30 modulename
= compat
.ensure_not_unicode(modulename
)
31 modulename
= modulename
.rstrip(".")
33 return sorted(get_modules())
34 candidates
= get_completions(modulename
)
36 return sorted(candidates
)
39 modulename
, part
= needle
.rsplit(".", 1)
40 candidates
= get_completions(modulename
)
42 candidates
= get_modules()
43 return sorted(candidate
for candidate
in candidates
44 if candidate
.startswith(needle
))
47 def get_completions(modulename
):
48 modules
= set("{0}.{1}".format(modulename
, module
)
49 for module
in get_modules(modulename
))
52 module
, name
= resolve(modulename
)
55 if isinstance(module
, CONTAINER_TYPES
):
56 modules
.update("{0}.{1}".format(modulename
, name
)
57 for name
in dir(module
)
58 if not name
.startswith("_") and
59 isinstance(getattr(module
, name
),
64 def get_modules(modulename
=None):
65 """Return a list of modules and packages under modulename.
67 If modulename is not given, return a list of all top level modules
71 modulename
= compat
.ensure_not_unicode(modulename
)
74 return ([modname
for (importer
, modname
, ispkg
)
76 if not modname
.startswith("_")] +
77 list(sys
.builtin_module_names
))
79 # Bug in Python 2.6, see #275
80 return list(sys
.builtin_module_names
)
82 module
= safeimport(modulename
)
83 except ErrorDuringImport
:
87 if hasattr(module
, "__path__"):
88 return [modname
for (importer
, modname
, ispkg
)
89 in iter_modules(module
.__path
__)
90 if not modname
.startswith("_")]