MCPcopy Index your code
hub / github.com/pyinvoke/invoke / load

Method load

invoke/loader.py:49–96  ·  view source on GitHub ↗

Load and return collection module identified by ``name``. This method requires a working implementation of `.find` in order to function. In addition to importing the named module, it will add the module's parent directory to the front of `sys.path` to provi

(self, name: Optional[str] = None)

Source from the content-addressed store, hash-verified

47 raise NotImplementedError
48
49 def load(self, name: Optional[str] = None) -> Tuple[ModuleType, str]:
50 """
51 Load and return collection module identified by ``name``.
52
53 This method requires a working implementation of `.find` in order to
54 function.
55
56 In addition to importing the named module, it will add the module's
57 parent directory to the front of `sys.path` to provide normal Python
58 import behavior (i.e. so the loaded module may load local-to-it modules
59 or packages.)
60
61 :returns:
62 Two-tuple of ``(module, directory)`` where ``module`` is the
63 collection-containing Python module object, and ``directory`` is
64 the string path to the directory the module was found in.
65
66 .. versionadded:: 1.0
67 """
68 if name is None:
69 name = self.config.tasks.collection_name
70 spec = self.find(name)
71 if spec and spec.loader and spec.origin:
72 # Typically either tasks.py or tasks/__init__.py
73 source_file = Path(spec.origin)
74 # Will be 'the dir tasks.py is in', or 'tasks/', in both cases this
75 # is what wants to be in sys.path for "from . import sibling"
76 enclosing_dir = source_file.parent
77 # Will be "the directory above the spot that 'import tasks' found",
78 # namely the parent of "your task tree", i.e. "where project level
79 # config files are looked for". So, same as enclosing_dir for
80 # tasks.py, but one more level up for tasks/__init__.py...
81 module_parent = enclosing_dir
82 if spec.parent: # it's a package, so we have to go up again
83 module_parent = module_parent.parent
84 # Get the enclosing dir on the path
85 enclosing_str = str(enclosing_dir)
86 if enclosing_str not in sys.path:
87 sys.path.insert(0, enclosing_str)
88 # Actual import
89 module = module_from_spec(spec)
90 sys.modules[spec.name] = module # so 'from . import xxx' works
91 spec.loader.exec_module(module)
92 # Return the module and the folder it was found in
93 return module, str(module_parent)
94 msg = "ImportError loading {!r}, raising ImportError"
95 debug(msg.format(name))
96 raise ImportError
97
98
99class FilesystemLoader(Loader):

Calls 3

findMethod · 0.95
PathClass · 0.85
strClass · 0.85

Tested by

no test coverage detected