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
Beispiel #2
0
    def find_spec(self, fullname, path, target=None):
        # print('------分割---------')
        # print("fullname:", fullname)
        # print("path:", path)

        # 如果 fullname 是 pack 那么 last_mile 就是 'pack'
        # 如果 fullname 是 pack.music 那么 last_mile 就是 'music'
        last_mile = fullname.split('.')[-1]

        if path == None:
            # 导入的是 顶级包,例如:import pack
            serach_path = sys.path

        else:
            # 如果导入的是模块或者子包
            serach_path = path

        for path_temp in serach_path:
            full_path = os.path.join(path_temp, last_mile)
            init_full_path = os.path.join(full_path, '__init__.py')
            module_full_path = full_path + '.py'

            if os.path.exists(full_path) and os.path.isfile(init_full_path):
                # 导入 包
                return ModuleSpec(fullname,
                                  loader=loader,
                                  origin=init_full_path,
                                  is_package=True)

            if os.path.isfile(module_full_path):
                # 导入 模块
                return ModuleSpec(fullname,
                                  loader=loader,
                                  origin=module_full_path,
                                  is_package=False)
 def find_spec(self, fullname, path=None, target=None):
     if not fullname.startswith('calibre_extensions'):
         return
     parts = fullname.split('.')
     if parts[0] != 'calibre_extensions':
         return
     if len(parts) > 2:
         return
     is_package = len(parts) == 1
     extension_name = None if is_package else parts[1]
     path = os.path.join(plugins_loc, '__init__.py')
     if extension_name:
         if extension_name not in self.calibre_extensions:
             return
         for suffix in EXTENSION_SUFFIXES:
             path = os.path.join(plugins_loc, extension_name + suffix)
             if os.path.exists(path):
                 break
         else:
             return
         return ModuleSpec(fullname,
                           ExtensionFileLoader(fullname, path),
                           is_package=is_package,
                           origin=path)
     return ModuleSpec(fullname,
                       ExtensionsPackageLoader(self.calibre_extensions),
                       is_package=is_package,
                       origin=path)
Beispiel #4
0
    def find_spec(self, module_name: str, path: list[str], target: str = None):
        if path is None:
            paths = [
                path for path in map(Path, sys.path)
                if path.exists() and path.is_dir()
            ]
        else:
            paths = list(map(Path, path))
        for try_path in paths:
            log.debug(f"Finding module {module_name} in {try_path}")
            last_module_name = module_name.rpartition(".")[2]

            fullpath = try_path / (last_module_name + self.suffix)
            if fullpath.exists():
                log.debug(f"Found module '{module_name}' in {fullpath}")
                loader = ExtensionModuleLoader(fullpath)
                return ModuleSpec(module_name, loader, origin=fullpath)

            fullpath = try_path / last_module_name
            if fullpath.is_dir() and (fullpath /
                                      f"__init__{self.suffix}").exists():
                log.debug(f"Found package '{module_name}' in {fullpath}")
                loader = ExtensionPackageLoader(fullpath)
                return ModuleSpec(module_name,
                                  loader,
                                  origin=fullpath,
                                  is_package=True)
Beispiel #5
0
 def find_spec(self, fullname, paths=None, target=None):
     if self._paths is None:
         self._paths = []
         self._paths = self._zip.namelist()
     spec = None
     if fullname.replace(".", "/") + '/__init__.py' in self._paths:
         full_uri = self._zip_path + '/' + fullname.replace(".", "/")
         try:
             loader = PackageLoader(self._zip, full_uri)
             loader.load_module(fullname)
             spec = ModuleSpec(fullname, loader, origin=paths)
         except ImportError as ie:
             spec = None
         except Exception as e:
             spec = None
     elif fullname.replace(".", "/") + '.py' in self._paths:
         full_uri = self._zip_path + '/' + fullname.replace(".", "/")
         try:
             loader = ModuleLoader(self._zip, full_uri)
             spec = ModuleSpec(fullname, loader, origin=paths)
         except ImportError as ie:
             spec = None
         except Exception as e:
             spec = None
     else:
         pass
     return spec
Beispiel #6
0
    def find_spec(self, fullname, path, target=None):
        if path is None:
            path = sys.path
            import_path = fullname

        else:
            import_path = fullname.split('.')[-1]

        split_path = import_path.split('.')
        file_name = "{}.hivemap".format(split_path[-1])

        # Search all paths to find hivemap
        for root in path:
            directory = join(root, *split_path[:-1])

            if not isdir(directory):
                continue

            if file_name in listdir(directory):
                file_path = join(directory, file_name)
                break

        else:
            return None

        loader = HivemapModuleLoader(fullname, file_path)
        self._loaders.append(loader)

        spec = ModuleSpec(fullname, loader, origin=file_path, loader_state=None, is_package=False)
        spec.has_location = True

        return spec
Beispiel #7
0
    def find_spec(self, fullname, _path=None, _target=None):
        if not self._in_capsule(fullname):
            # If the capsule directory name is not in the fullname, it's
            # not our job to import it
            return None

        if fullname == self._capsule_dir_name:
            # If the capsule root directory is being loaded, return a
            # modulespec
            return ModuleSpec(name=fullname, loader=None, is_package=True)

        # Get rid of the capsule name prefix
        pruned_fullname = _remove_capsule_name(self._capsule_dir_name,
                                               fullname)
        package_path = _package_fullname_to_path(pruned_fullname)
        module_path = _module_fullname_to_path(pruned_fullname)

        if package_path in self._zip_file.namelist():
            # If a directory exists with a name that matches the import, we
            # assume it is a package import.
            return ModuleSpec(name=fullname, loader=None, is_package=True)
        elif module_path in self._zip_file.namelist():
            # If a .py file exists with a name that matches the import, we
            # assume it is a module import
            module_file_path = self._capsule_dir_path / module_path
            loader = ZipModuleLoader(zip_file=self._zip_file,
                                     module_file_path=module_file_path,
                                     capsule_dir_name=self._capsule_dir_name)
            return ModuleSpec(name=fullname, loader=loader)

        raise ImportError(f"Problem while importing {fullname}")
Beispiel #8
0
    def find_spec(cls, fullname, paths=None, target=None):
        """Try to find a spec for the specified module.

        :param fullname: module full name
        :param paths: the search paths
        :param target: the target
        :return: a module spec
        """
        from importlib.machinery import ModuleSpec

        # TODO: read PEP 420
        last_module = fullname.split('.')[-1]
        if paths is None:
            paths = [cls.__path__]
        for path in paths:
            full_path = os.path.join(path, last_module)

            init_path = os.path.join(full_path, '__init__.py')
            module_path = full_path + '.py'

            if os.path.exists(init_path) and not os.path.exists(module_path):
                return ModuleSpec(fullname, _loader, origin=init_path)
            elif os.path.exists(module_path):
                return ModuleSpec(fullname, _loader, origin=module_path)
        return None
Beispiel #9
0
def _patch_spec(
        fullname,
        spec: ModuleSpec):  #loader_class, fullname, path, smsl, target):
    spec.name = fullname
    if isinstance(spec.loader, abc_FileLoader):
        spec.loader = spec.loader.__class__(fullname, spec.loader.path)
    return spec
 def find_spec(self, fullname, paths=None, target=None):
     relative_path = fullname.replace(".", "/")
     base_path = None
     full_path = None
     for path in sys.path:
         path_dir = Path(path)
         base_path = path_dir.resolve()
         abs_path = base_path.joinpath(relative_path)
         # 如果已经编译,那么跳过
         if path_dir.is_dir():
             find_files = [
                 i for i in path_dir.iterdir() if i.match(f"{relative_path}_pb2.py")
             ]
             if len(find_files) > 0:
                 full_path = str(find_files[0])
                 loader = PyProtoBufferImportLoader(full_path)
                 spec = ModuleSpec(fullname, loader, origin=paths)
                 return spec
         # 判断是否是proto模块
         if abs_path.with_suffix(".proto").exists():
             full_path = abs_path.with_suffix(".proto")
             loader = ProtoBufferImportLoader(full_path)
             spec = ModuleSpec(fullname, loader, origin=paths)
             return spec
     else:
         # 都不是就跳过
         return None
Beispiel #11
0
 def from_path(cls, path: str, compiler: Compiler) -> FileSource:
     """create an instance with a spec inferred from a path"""
     spec = ModuleSpec("__main__",
                       SourceFileLoader("__main__", path),
                       origin=path,
                       is_package=False)
     spec.has_location = True
     return cls(io.FileIO(path, "r"), spec, compiler)
Beispiel #12
0
    def find_spec(
        self,
        fullname: str,
        path,  # Optional[List[str]] # MyPy complains this is incompatible with supertype
        target: types.ModuleType = None,
    ) -> Optional[ModuleSpec]:
        """Find the ModuleSpec for the specified Basilisp module.

        Returns None if the module is not a Basilisp module to allow import processing to continue."""
        package_components = fullname.split(".")
        if not path:
            path = sys.path
            module_name = package_components
        else:
            module_name = [package_components[-1]]

        for entry in path:
            root_path = os.path.join(entry, *module_name)
            filenames = [
                f"{os.path.join(root_path, '__init__')}.lpy",
                f"{root_path}.lpy",
            ]
            for filename in filenames:
                if os.path.isfile(filename):
                    state = {
                        "fullname": fullname,
                        "filename": filename,
                        "path": entry,
                        "target": target,
                        "cache_filename": _cache_from_source(filename),
                    }
                    logger.debug(
                        f"Found potential Basilisp module '{fullname}' in file '{filename}'"
                    )
                    is_package = filename.endswith(
                        "__init__.lpy") or _is_package(root_path)
                    spec = ModuleSpec(
                        fullname,
                        self,
                        origin=filename,
                        loader_state=state,
                        is_package=is_package,
                    )
                    # The Basilisp loader can find packages regardless of
                    # submodule_search_locations, but the Python loader cannot.
                    # Set this to the root path to allow the Python loader to
                    # load submodules of Basilisp "packages".
                    if is_package:
                        assert (
                            spec.submodule_search_locations is not None
                        ), "Package module spec must have submodule_search_locations list"
                        spec.submodule_search_locations.append(root_path)
                    return spec
            if os.path.isdir(root_path):
                if _is_namespace_package(root_path):
                    return ModuleSpec(fullname, None, is_package=True)
        return None
Beispiel #13
0
    def find_spec(self, fullname, path, target=None):
        key = fullname.replace('.', '/') + '.py'
        if self.db.exists(key):
            return ModuleSpec(fullname, self, origin=key)

        key = fullname.replace('.', '/') + '/__init__.py'

        if self.db.exists(key):
            return ModuleSpec(fullname, self, origin=key)
Beispiel #14
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
Beispiel #15
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
Beispiel #16
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
Beispiel #17
0
 def test_file_shebang(self) -> None:
     """test the handling of shebangs in files"""
     path = "./tests/embedding/shebang.pyhp"
     with open(path, "r") as file:
         code = self.compiler.compile_file(file)
     builder = self.compiler.builder()
     spec = ModuleSpec("__main__", None, origin=path, is_package=False)
     spec.has_location = True
     with open(path, "r") as file:
         file.readline()  # discard shebang
         self.compiler.parser.build(file.read(), builder, 1)
     self.assertEqual(code, builder.code(spec))
Beispiel #18
0
    def test_file(self) -> None:
        """test the compilation of files"""
        path = "./tests/embedding/syntax.pyhp"
        with open(path, "r") as file:
            code = self.compiler.compile_file(file)

        builder = self.compiler.builder()
        spec = ModuleSpec("__main__", None, origin=path, is_package=False)
        spec.has_location = True
        with open(path, "r") as file:
            self.compiler.parser.build(file.read(), builder)
        self.assertEqual(code, builder.code(spec))
Beispiel #19
0
 def find_spec(self,
               fullname: str,
               path: Optional[Sequence[Union[bytes, str]]],
               target: Optional[ModuleType] = None) -> Optional[ModuleSpec]:
     if fullname not in self.sources:
         return None
     is_package, _ = self.sources[fullname]
     spec = ModuleSpec(name=fullname,
                       loader=self,
                       origin=self.origin,
                       is_package=is_package)
     spec.has_location = False
     return spec
Beispiel #20
0
def get_yapypy_module_spec_from_path(names, module_path):
    with Path(module_path).open('r') as fr:
        spec = ModuleSpec(names, YAPyPyLoader(names, module_path))
        __source__ = fr.read()
        result = parse(__source__, module_path)
        # pprint(result.result)
        check_parsing_complete(__source__, result.tokens, result.state)
        __bytecode__ = py_compile(result.result,
                                  filename=module_path,
                                  is_entrypoint=False)
        spec.__source__ = __source__
        spec.__bytecode__ = __bytecode__
        return spec
Beispiel #21
0
    def find_spec(self, fullname, path, target=None):
        try:
            if not fullname.startswith(
                    self.message_root) and not fullname.startswith(
                        self.enum_root):
                return None
            name_path = fullname.split('.')
            if len(name_path) == 2:
                # olympe.messages
                origin = os.path.join(self.olympe_root,
                                      fullname.replace('.', '/'))
                spec = ModuleSpec(fullname,
                                  self,
                                  origin=origin,
                                  is_package=False)
                # olympe.enums
                return spec
            elif len(name_path) == 3:
                # olympe.messages.<feature_name>
                # olympe.enums.<feature_name>
                type_, feature_name, class_name = name_path[1], name_path[
                    2], None
            elif len(name_path) == 4:
                # olympe.messages.<feature_name>.<class_or_message>
                # olympe.enums.<feature_name>.<class_or_enum>
                type_, feature_name, class_name = name_path[1:]
            else:
                return None

            if feature_name not in self.messages.by_feature:
                # feature name does not exists
                return None

            if (class_name is not None and class_name
                    not in self.messages.by_feature[feature_name]):
                # class name, message or enum does not exist
                return None

            is_package = class_name is None

            origin = os.path.join(self.olympe_root, fullname.replace('.', '/'))
            spec = ModuleSpec(fullname,
                              self,
                              origin=origin,
                              is_package=is_package)

            return spec
        except Exception:
            traceback.print_exc()
            return None
Beispiel #22
0
def test_wrap_module():
    my_module = types.ModuleType('my_module', 'my doc string')
    my_module.foo = 'foo'
    my_module.bar = 'bar'
    my_module.__spec__ = ModuleSpec('my_module', loader=None)
    assert 'foo' in my_module.__dict__
    assert 'bar' in my_module.__dict__
    assert 'zoo' not in my_module.__dict__

    with pytest.raises(AssertionError, match='deadline should match vX.Y'):
        deprecate_attributes(my_module,
                             {'foo': ('invalid', 'use bar instead')})

    # temporarily update sys.modules so deprecate_attributes can find my_module
    sys.modules['my_module'] = my_module
    wrapped = deprecate_attributes('my_module',
                                   {'foo': ('v0.6', 'use bar instead')})
    assert wrapped is sys.modules.pop('my_module')
    # Dunder methods
    assert wrapped.__doc__ == 'my doc string'
    assert wrapped.__name__ == 'my_module'
    assert wrapped.__spec__ is my_module.__spec__
    # Verify __spec__ setter in the wrapped module
    wrapped.__spec__ = ModuleSpec('my_module', loader=None)
    assert my_module.__spec__ is wrapped.__spec__
    # Test dict is correct.
    assert 'foo' in wrapped.__dict__
    assert 'bar' in wrapped.__dict__
    assert 'zoo' not in wrapped.__dict__

    # Deprecation capability.
    with cirq.testing.assert_deprecated(
            '_compat_test.py:',
            'foo was used but is deprecated.',
            'will be removed in cirq v0.6',
            'use bar instead',
            deadline='v0.6',
    ):
        _ = wrapped.foo

    with pytest.raises(
            ValueError,
            match=
            'During testing using Cirq deprecated functionality is not allowed'
    ):
        _ = wrapped.foo

    with cirq.testing.assert_logs(count=0):
        _ = wrapped.bar
Beispiel #23
0
 def with_inferred_spec(cls, fd: io.FileIO,
                        compiler: Compiler) -> FileSource:
     """create an instance with a spec inferred from the file"""
     if isinstance(fd.name, str):
         spec = ModuleSpec("__main__",
                           SourceFileLoader("__main__", fd.name),
                           origin=fd.name,
                           is_package=False)
         spec.has_location = True
     else:
         spec = ModuleSpec("__main__",
                           None,
                           origin=f"<fd {fd.name}>",
                           is_package=False)
     return cls(fd, spec, compiler)
Beispiel #24
0
 def test_equal(self) -> None:
     """test if equality between generic code objetcs works"""
     builder = bytecode.ByteCodeBuilder(-1)
     builder.add_code("print(1)", 0)
     builder.add_text("X", 0)
     spec = ModuleSpec("test", None, origin="this test", is_package=False)
     code = builder.code(spec)
     code2 = builder.code(spec)
     code3 = builder.code(
         ModuleSpec("X", None, origin="this test", is_package=False))
     builder.add_text("Y", 0)
     code4 = builder.code(spec)
     self.assertEqual(code, code2)
     self.assertNotEqual(code, code3)
     self.assertNotEqual(code, code4)
Beispiel #25
0
    def find_spec(self, fullname, path, target=None):
        spec = super(TracedModuleImporter,
                     self).find_spec(fullname, path, target)
        if spec is not None:
            if spec.origin == self.traced_file:
                self.record_module(fullname)
                return ModuleSpec(fullname, self, origin=self.traced_file)
            if self.traced_file is None and self.traced.startswith(fullname):
                self.original_loaders[fullname] = spec.loader
                spec.loader = self
                return spec

        if fullname == self.traced:
            return ModuleSpec(fullname, self, origin=self.traced_file)
        return None
Beispiel #26
0
 def find_spec(klass, full_name, paths=None, target=None):
     if full_name.startswith("dbmodule"):
         x = full_name.split(".")
         if len(x) < 5:
             full_path = os.path.join(full_name, "__init__.dbpy")
             return ModuleSpec(full_name,
                               DBPackageLoader(),
                               origin=full_path)
         else:
             full_path = full_name.replace(".", os.sep) + ".dbpy"
             return ModuleSpec(full_name,
                               DBModuleLoader(),
                               origin=full_path)
     else:
         return None
Beispiel #27
0
    def load_module(self, fullname):
        """ Loads and returns the Python module for the given enaml path.
        If a module already exisist in sys.path, the existing module is
        reused, otherwise a new one is created.

        Only used in Python 2.

        """
        if fullname in sys.modules:
            pre_exists = True
        else:
            pre_exists = False

        mod = self.create_module(
            ModuleSpec(fullname, self, origin=self.file_info.src_path))

        code, _ = self.get_code()

        try:
            self.exec_module(mod, code)
        except Exception:
            if not pre_exists:
                del sys.modules[fullname]
            raise

        return mod
Beispiel #28
0
 def find_spec(self, fullname, path=None, target=None):  # ModuleSpecを返す。
     if path is None:
         baseurl = self._baseurl
     else:
         if not path[0].startswith(self._baseurl):
             return None
         baseurl = path[0]
     parts = fullname.split('.')
     basename = parts[-1]
     if basename not in self._links:  # Check link cache
         self._links[baseurl] = _get_links(self._simplefileaccess, baseurl)
     if basename in self._links[
             baseurl]:  # Check if it's a package。パッケージの時。
         fullurl = "/".join((self._baseurl, basename))
         loader = UrlPackageLoader(self._simplefileaccess, fullurl)
         try:  # Attempt to load the package (which accesses __init__.py)
             loader.load_module(fullname)
             self._links[fullurl] = _get_links(self._simplefileaccess,
                                               fullurl)
             self._loaders[fullurl] = UrlModuleLoader(
                 self._simplefileaccess, fullurl)
         except ImportError:
             loader = None
         return loader
     filename = "".join((basename, '.py'))
     if filename in self._links[baseurl]:  # A normal module
         return ModuleSpec(fullname, self._loaders[baseurl], origin=path)
     else:
         return None
Beispiel #29
0
    def find_spec(cls, fullname, path=None, target=None):
        soname = 'libpy.' + fullname + '.so'
        path = os.path.join(sys._native_library_dir, soname)

        if os.path.exists(path):
            loader = _bootstrap_external.ExtensionFileLoader(fullname, path)
            return ModuleSpec(fullname, loader, origin=path)
Beispiel #30
0
 def create_module(self, spec: ModuleSpec):
     state = spec.loader_state
     cmake = tool.CMake(spec, state)
     cmake.configure()
     cmake.build()
     spec = ModuleSpec(spec.name, loader=self, origin=cmake.output)
     return super().create_module(spec)
Beispiel #31
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