Esempio n. 1
0
 def spec(self, fullname, node, origin=None, pkg=True):
     spec = ModuleSpec(fullname, self, origin=str(origin), is_package=pkg)
     spec._node = node
     if pkg:
         if hasattr(node, 'path'):
             spec.submodule_search_locations = [str(node.path)]
         else:
             spec.submodule_search_locations = []
     return spec
    def find_spec(cls, name, path, target=None):  # pragma: no cover
        match = cls.expr.match(name)
        try:
            # Construct the name for the actual module to load
            new_name = f"message_data.{match.group('name')}"
        except AttributeError:
            # `match` was None; unsupported. Let the importlib defaults take over.
            return None

        # Get an import spec for the message_data submodule
        spec = util.find_spec(new_name)
        if not spec:
            return None

        # Create a new spec that loads message_data.model.foo as if it were
        # message_ix_models.model.foo
        new_spec = ModuleSpec(
            name=name,
            # Create a new loader that loads from the actual file with the desired name
            loader=SourceFileLoader(fullname=name, path=spec.origin),
            origin=spec.origin,
        )
        # These can't be passed through the constructor
        new_spec.submodule_search_locations = spec.submodule_search_locations

        return new_spec
Esempio n. 3
0
 def find_spec(self, fullname, path=None, target=None):
     code = self.code_map.get(fullname)
     if code is None:
         return None
     loader = self
     spec = ModuleSpec(fullname, loader)
     if not code:
         spec.submodule_search_locations = [
             str(pathlib.Path(__file__).parent)
         ]
     return spec
Esempio n. 4
0
    def find_spec(self, name, path=None, module=None):

        # if name space matches, create a namespace module which searches the desired directories.
        if name == self.namespace:
            spec = ModuleSpec(name, None, is_package=True)
            spec.submodule_search_locations = self.dirs

        else:
            spec = None

        # print(f"NamespaceCreator.find_spec: name={name} path={path} spec={spec}")
        return spec
Esempio n. 5
0
    def find_spec(self, fullname, path=None, target=None):
        """Try to find a loader for the specified module, or the namespace
        package portions. Returns (loader, list-of-portions)."""
        builtin_spec = BuiltinImporter.find_spec(fullname, path, target)
        if builtin_spec:
            return builtin_spec

        is_namespace = False
        parts = fullname.split('.')
        tail_module = parts[-1]

        # Check if the module is the name of a directory (and thus a package).
        for dirobj in self.dirobjs_from_path(path):
            if dirobj.isdir(tail_module):
                base_path = os.path.join(self.path, *parts)
                for suffix, loader_class in self._loaders:
                    init_filename = '__init__' + suffix
                    full_path = os.path.join(base_path, init_filename)
                    if dirobj.isfile(full_path):
                        _loader = loader_class(fullname, full_path,
                                               dirobj)
                        return spec_from_file_location(
                            fullname, full_path,
                            loader=_loader,
                            submodule_search_locations=[base_path])
                else:
                    # If a namespace package, return the path if we don't
                    #  find a module in the next section.
                    is_namespace = dirobj.isdir(tail_module)
            # Check for a file w/ a proper suffix exists.
            for suffix, loader_class in self._loaders:
                fn = tail_module + suffix
                full_path = os.path.join(dirobj.name, fn)
                if dirobj.isfile(fn):
                    _loader = loader_class(fullname, full_path, dirobj)
                    return spec_from_file_location(
                        fullname, full_path,
                        loader=_loader,
                        submodule_search_locations=None)

            if is_namespace:
                spec = ModuleSpec(fullname, None)
                spec.submodule_search_locations = [base_path]
                return spec
        return None
Esempio n. 6
0
    def find_spec(self, modulename, target):
        tail_modulename = modulename.rpartition('.')[2]

        try:
            path = tail_modulename + PACKAGE_SUFFIX
            tree_entry = self.get_tree_entry(path)
            if tree_entry.type == 'blob':
                spec = ModuleSpec(modulename,
                                  GitLoader(tree_entry, self.repo, self.commit_sha),
                                  is_package=True,
                                  origin=path)

                spec.submodule_search_locations = [os.path.join(self.repopath, tail_modulename)]
                return spec
        except KeyError:
            pass

        try:
            path = tail_modulename + SOURCE_SUFFIX
            tree_entry = self.get_tree_entry(path)
            if tree_entry.type == 'blob':
                return ModuleSpec(modulename, GitLoader(tree_entry, self.repo, self.commit_sha), origin=path)
        except KeyError:
            pass
Esempio n. 7
0
def make_namespace_spec(plugin_name: str) -> ModuleSpec:
    """Makes a namespace spec."""
    namespace = get_plugin_namespace(plugin_name)
    spec = ModuleSpec(name=namespace, loader=None)
    spec.submodule_search_locations = []
    return spec