def get_package_for_document(self, doc_uri: str): url = urllib.parse.urlparse(doc_uri) fullname = os.path.abspath(url.path) for package in self.packages: if fullname.startswith(package.path): return package raise OrcinusError(f"Not found file ‘{url.path}’ in packages")
def convert_module_name(filename, path): fullname = os.path.abspath(filename) if not fullname.startswith(path): raise OrcinusError(f"Not found file `{filename}` in package `{path}`") module_name = fullname[len(path):] module_name, _ = os.path.splitext(module_name) module_name = module_name.strip(os.path.sep) module_name = module_name.replace(os.path.sep, '.') return module_name
def load_document(self, module_name: str) -> Document: """ Load document for module :param module_name: Module name :return: Document """ for package in self.packages: doc_uri = convert_filename(module_name, package.path) try: return package.get_or_create_document(doc_uri) except IOError: logger.debug( f"Not found module `{module_name}` in file `{doc_uri}`") pass # Continue raise OrcinusError(f'Not found module {module_name}')
def create_document(self, doc_uri, source=None, version=None) -> Document: """ Create new document """ url = urllib.parse.urlparse(doc_uri) name = self.get_module_name(url.path) if source is None: try: with open(url.path, 'r', encoding='utf-8') as stream: source = stream.read() except IOError: raise OrcinusError( f"Not found file ‘{url.path}’ in package ‘{self.name}’") document = Document(self, doc_uri, name=name, source=source, version=version) self.documents[doc_uri] = document self.modules[name] = document self.workspace.on_document_create(document=document) return document
def get_package_for_module(self, name: str): for package in self.packages: if package.has_module(name): return package raise OrcinusError(f"Not found module ‘{name}’ in packages")
def get_module_name(self, filename: str) -> str: fullname = os.path.abspath(filename) if fullname.startswith(self.path): return convert_module_name(fullname, self.path) raise OrcinusError(f"Not found file ‘{filename}’ in packages")
def verify(self): try: self.__llvm_ref = binding.parse_assembly(str(self.llvm_module)) self.__llvm_ref.verify() except RuntimeError as ex: raise OrcinusError(str(ex))