Esempio n. 1
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
Esempio n. 2
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)
Esempio n. 3
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))
Esempio n. 4
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))
Esempio n. 5
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
Esempio n. 6
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)
Esempio n. 7
0
    def find_spec(cls, fullname, path=None, target=None):
        """ Finds the given Enaml module and returns an importer, or
        None if the module is not found.

        """
        loader = cls.locate_module(fullname, path)
        if loader is not None:
            if not isinstance(loader, AbstractEnamlImporter):
                msg = 'Enaml imports received invalid loader object %s'
                raise ImportError(msg % loader)

            spec = ModuleSpec(fullname, loader,
                              origin=loader.file_info.src_path)
            spec.cached = loader.file_info.cache_path
            spec.has_location = True

            return spec
Esempio n. 8
0
 def test_code(self) -> None:
     """test ZIPSource.code"""
     with open("tests/embedding/syntax.pyhp", "r") as fd, \
             zipfile.ZipFile("tests/embedding.zip") as file, \
             zipfiles.ZIPSource(file, file.getinfo("syntax.pyhp"), compiler) as source:
         code1 = source.code()
         spec = ModuleSpec("__main__",
                           zipfiles.ZIPLoader("__main__", file,
                                              file.getinfo("syntax.pyhp")),
                           origin=os.path.join("tests/embedding.zip",
                                               "syntax.pyhp"),
                           is_package=False)
         spec.has_location = True
         code2 = compiler.compile_raw(fd.read(), spec)
         code3 = source.code(
         )  # check if the source can be read multiple times
         self.assertEqual(code1, code2)
         self.assertEqual(code1, code3)
    def find_spec(cls, fullname, path=None, target=None):
        """Find a spec for a given module.
        
        Because we only deal with our inlined module, we don't have to care about path or target.
        The import machinery also takes care of fully resolving all names, so we just have to deal with the fullnames.
        """
        if fullname in cls.inlined_modules:
            # We have inlined this module, so return the spec
            ms = ModuleSpec(fullname,
                            cls,
                            origin=cls.get_filename(fullname),
                            is_package=cls.is_package(fullname))
            ms.has_location = True
            if cls.namespace_packages and ms.submodule_search_locations is not None:
                for p in _sys.path:
                    ms.submodule_search_locations.append(
                        _os.path.join(p, _os.path.dirname(ms.origin)))
            return ms

        return None
Esempio n. 10
0
File: util.py Progetto: Deric-W/PyHP
 def compile_file(self,
                  file: TextIO,
                  loader: Optional[Loader] = None) -> Code:
     """compile a text stream into a code object"""
     builder = self.builder()
     spec = ModuleSpec("__main__",
                       loader,
                       origin=file.name,
                       is_package=False)
     spec.has_location = True
     first_line = file.readline()
     try:
         if first_line.startswith("#!"):  # shebang, remove first line
             # line offset of 1 to compensate for removed shebang
             self.parser.build(file.read(), builder, 1)
         else:
             self.parser.build(first_line + file.read(), builder)
     except SyntaxError as e:  # change filename
         e.filename = spec.origin
         raise e
     return builder.code(spec)