def load_module(self, module_name): """If the module was located it then is loaded by this function. It is also a part of PEP-0302 interface Loading means first preprocessing of shell python code if it is not processed already and the addition to system path and import. :param module_name: the name of the module to load :return: the module imported. This function assumes that it will import a module anyway since find_module already found the module """ sys.meta_path.remove(self) if module_name.find('.') == -1: spy_module_path = locator.locate_spy_module(module_name) spy_file_path = locator.locate_spy_file(module_name) if spy_module_path is not None: new_module_path = preprocessor.preprocess_module(spy_module_path) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) elif spy_file_path is not None: new_module_path = preprocessor.preprocess_file(spy_file_path, is_root_script=False) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) else: raise ImportError("Unexpected error occured in importer. Neither shellpy module not file was found") else: module = import_module(module_name) sys.meta_path.insert(0, self) return module
def load_module(self, module_name): """If the module was located it then is loaded by this function. It is also a part of PEP-0302 interface Loading means first preprocessing of shell python code if it is not processed already and the addition to system path and import. :param module_name: the name of the module to load :return: the module imported. This function assumes that it will import a module anyway since find_module already found the module """ sys.meta_path.remove(self) if module_name.find(".") == -1: spy_module_path = locator.locate_spy_module(module_name) spy_file_path = locator.locate_spy_file(module_name) if spy_module_path is not None: new_module_path = preprocessor.preprocess_module(spy_module_path) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) elif spy_file_path is not None: new_module_path = preprocessor.preprocess_file(spy_file_path, is_root_script=False) new_module_pythonpath = os.path.split(new_module_path)[0] if new_module_pythonpath not in sys.path: sys.path.append(new_module_pythonpath) module = import_module(module_name) else: raise ImportError("Unexpected error occured in importer. Neither shellpy module not file was found") else: module = import_module(module_name) sys.meta_path.insert(0, self) return module
def find_module(self, module_name, package_path): """This function is part of interface defined in import hooks PEP-0302 Given the name of the module its goal is to locate it. If shellpy module with the name was found, self is returned as Loader for this module. Otherwise None is returned and standard python import works as expected :param module_name: the module to locate :param package_path: part of interface, not used, see PEP-0302 :return: self if shellpy module was found, None if not """ if module_name.find('.') == -1: root_module_name = module_name else: root_module_name = module_name.split('.')[0] spy_module_path = locator.locate_spy_module(root_module_name) spy_file_path = locator.locate_spy_file(root_module_name) if spy_module_path is not None or spy_file_path is not None: return self else: return None
def find_module(self, module_name, package_path): """This function is part of interface defined in import hooks PEP-0302 Given the name of the module its goal is to locate it. If shellpy module with the name was found, self is returned as Loader for this module. Otherwise None is returned and standard python import works as expected :param module_name: the module to locate :param package_path: part of interface, not used, see PEP-0302 :return: self if shellpy module was found, None if not """ if module_name.find(".") == -1: root_module_name = module_name else: root_module_name = module_name.split(".")[0] spy_module_path = locator.locate_spy_module(root_module_name) spy_file_path = locator.locate_spy_file(root_module_name) if spy_module_path is not None or spy_file_path is not None: return self else: return None
def test_modules(self): self.assertIsNotNone(locator.locate_spy_module('testmodulespy')) self.assertIsNone(locator.locate_spy_module('testmodulepy')) self.assertIsNone(locator.locate_spy_module('testmoduleempty')) self.assertIsNone(locator.locate_spy_module('testfilepy')) self.assertIsNone(locator.locate_spy_module('non_existent_file'))