Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.9 KiB

5 years ago
  1. import sys
  2. import types
  3. from pydoc import safeimport, resolve, ErrorDuringImport
  4. from pkgutil import iter_modules
  5. from elpy import compat
  6. # Types we want to recurse into (nodes).
  7. CONTAINER_TYPES = (type, types.ModuleType)
  8. # Types of attributes we can get documentation for (leaves).
  9. PYDOC_TYPES = (type,
  10. types.FunctionType,
  11. types.BuiltinFunctionType,
  12. types.BuiltinMethodType,
  13. types.MethodType,
  14. types.ModuleType)
  15. if not compat.PYTHON3: # pragma: nocover
  16. # Python 2 old style classes
  17. CONTAINER_TYPES = tuple(list(CONTAINER_TYPES) + [types.ClassType])
  18. PYDOC_TYPES = tuple(list(PYDOC_TYPES) + [types.ClassType])
  19. def get_pydoc_completions(modulename):
  20. """Get possible completions for modulename for pydoc.
  21. Returns a list of possible values to be passed to pydoc.
  22. """
  23. modulename = compat.ensure_not_unicode(modulename)
  24. modulename = modulename.rstrip(".")
  25. if modulename == "":
  26. return sorted(get_modules())
  27. candidates = get_completions(modulename)
  28. if candidates:
  29. return sorted(candidates)
  30. needle = modulename
  31. if "." in needle:
  32. modulename, part = needle.rsplit(".", 1)
  33. candidates = get_completions(modulename)
  34. else:
  35. candidates = get_modules()
  36. return sorted(candidate for candidate in candidates
  37. if candidate.startswith(needle))
  38. def get_completions(modulename):
  39. modules = set("{0}.{1}".format(modulename, module)
  40. for module in get_modules(modulename))
  41. try:
  42. module, name = resolve(modulename)
  43. except ImportError:
  44. return modules
  45. if isinstance(module, CONTAINER_TYPES):
  46. modules.update("{0}.{1}".format(modulename, name)
  47. for name in dir(module)
  48. if not name.startswith("_") and
  49. isinstance(getattr(module, name),
  50. PYDOC_TYPES))
  51. return modules
  52. def get_modules(modulename=None):
  53. """Return a list of modules and packages under modulename.
  54. If modulename is not given, return a list of all top level modules
  55. and packages.
  56. """
  57. modulename = compat.ensure_not_unicode(modulename)
  58. if not modulename:
  59. try:
  60. return ([modname for (importer, modname, ispkg)
  61. in iter_modules()
  62. if not modname.startswith("_")] +
  63. list(sys.builtin_module_names))
  64. except OSError:
  65. # Bug in Python 2.6, see #275
  66. return list(sys.builtin_module_names)
  67. try:
  68. module = safeimport(modulename)
  69. except ErrorDuringImport:
  70. return []
  71. if module is None:
  72. return []
  73. if hasattr(module, "__path__"):
  74. return [modname for (importer, modname, ispkg)
  75. in iter_modules(module.__path__)
  76. if not modname.startswith("_")]
  77. return []