def set_option(self, optname, value, action=None, optdict=None): """overridden from configuration.OptionsProviderMixin to handle some special options """ if optname in self._options_methods or optname in self._bw_options_methods: if value: try: meth = self._options_methods[optname] except KeyError: meth = self._bw_options_methods[optname] warn("%s is deprecated, replace it by %s" % (optname, optname.split("-")[0]), DeprecationWarning) value = check_csv(None, optname, value) if isinstance(value, (list, tuple)): for _id in value: meth(_id) else: meth(value) elif optname == "output-format": if value.lower() in REPORTER_OPT_MAP: self.set_reporter(REPORTER_OPT_MAP[value.lower()]()) else: module = load_module_from_name(get_module_part(value)) class_name = value.split(".")[-1] reporter_class = getattr(module, class_name) self.set_reporter(reporter_class()) try: BaseRawChecker.set_option(self, optname, value, action, optdict) except UnsupportedAction: print >> sys.stderr, "option %s can't be read from config file" % optname
def astng_from_module_name(self, modname, context_file=None): """given a module name, return the astng object""" if modname in self.astng_cache: return self.astng_cache[modname] if modname == '__main__': from logilab.astng.builder import ASTNGBuilder return ASTNGBuilder(self).string_build('', modname) old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath = self.file_from_module_name(modname, context_file) if filepath is not None and not is_python_source(filepath): module = self.zip_import_data(filepath) if module is not None: return module if filepath is None or not is_python_source(filepath): try: module = load_module_from_name(modname) except Exception as ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise ASTNGBuildingException(msg) return self.astng_from_module(module, modname) return self.astng_from_file(filepath, modname, fallback=False) finally: os.chdir(old_cwd)
def astng_from_module_name(self, modname, context_file=None): """given a module name, return the astng object""" if modname in self._cache: return self._cache[modname] old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath = self.file_from_module_name(modname, context_file) if filepath is not None and not is_python_source(filepath): data, zmodname = zip_import_data(filepath) if data is not None: from logilab.astng.builder import ASTNGBuilder try: return ASTNGBuilder(self).string_build(data, zmodname, filepath) except (SyntaxError, KeyboardInterrupt, SystemExit): raise if filepath is None or not is_python_source(filepath): try: module = load_module_from_name(modname) # catch SystemError as well, we may get that on badly # initialized C-module except (SystemError, ImportError), ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise ASTNGBuildingException(msg) return self.astng_from_module(module, modname) return self.astng_from_file(filepath, modname, fallback=False)
def callnew(*args, **kwargs): from logilab.common.modutils import load_module_from_name message = "object %s has been moved to module %s" % (objname, modpath) warn(message, DeprecationWarning, stacklevel=2) m = load_module_from_name(modpath) return getattr(m, objname)(*args, **kwargs)
def astng_from_module_name(self, modname, context_file=None): """given a module name, return the astng object""" old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath = self.file_from_module_name(modname, context_file) if filepath is not None and not is_python_source(filepath): try: return self._cache[filepath] except KeyError: data, zmodname = zip_import_data(filepath) if data is not None: from logilab.astng.builder import ASTNGBuilder try: astng = ASTNGBuilder(self).string_build(data, zmodname, filepath) except (SyntaxError, KeyboardInterrupt, SystemExit): raise self._cache[filepath] = astng return astng if filepath is None or not is_python_source(filepath): try: module = load_module_from_name(modname) except ImportError, ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise ASTNGBuildingException(msg) return self.astng_from_module(module, modname) return self.astng_from_file(filepath, modname, fallback=False)
def cb_plug_class(self, option, opt_name, value, parser): """optik callback to plug the aspect on a class""" parts = value.split('.') modulename, klassname = '.'.join(parts[:-1]), parts[-1] module = load_module_from_name(modulename) klass = getattr(module, klassname) #self._weaved[modulename] = module weaver.weave_methods(klass, LoggerAspect, sys.stderr, self.config)
def load_plugin_modules(self, modnames): """take a list of module names which are pylint plugins and load and register them """ for modname in modnames: if modname in self._dynamic_plugins: continue self._dynamic_plugins.append(modname) module = load_module_from_name(modname) module.register(self)
def _load_reporter(self): name = self._reporter_name.lower() if name in self._reporters: self.set_reporter(self._reporters[name]()) else: qname = self._reporter_name module = load_module_from_name(get_module_part(qname)) class_name = qname.split('.')[-1] reporter_class = getattr(module, class_name) self.set_reporter(reporter_class())
def load_file(self, filepath, modname): """ load registrable objects (if any) from a python file """ from logilab.common.modutils import load_module_from_name if modname in self._loadedmods: return self._loadedmods[modname] = {} mdate = self._mdate(filepath) if mdate is None: return # backup file, see _mdate implementation elif "flymake" in filepath: # flymake + pylint in use, don't consider these they will corrupt the registry return # set update time before module loading, else we get some reloading # weirdness in case of syntax error or other error while importing the # module self._lastmodifs[filepath] = mdate # load the module module = load_module_from_name(modname) self.load_module(module)
def ast_from_module_name(self, modname, context_file=None): """given a module name, return the astroid object""" if modname in self.astroid_cache: return self.astroid_cache[modname] if modname == '__main__': from astroid.builder import AstroidBuilder return AstroidBuilder(self).string_build('', modname) old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath = self.file_from_module_name(modname, context_file) if filepath is not None and not is_python_source(filepath): module = self.zip_import_data(filepath) if module is not None: return module if filepath is None or not is_python_source(filepath): try: module = load_module_from_name(modname) except Exception, ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise AstroidBuildingException(msg) return self.ast_from_module(module, modname) return self.ast_from_file(filepath, modname, fallback=False)
def callnew(*args, **kwargs): from logilab.common.modutils import load_module_from_name message = "object %s has been moved to module %s" % (objname, modpath) self.warn(version, message) m = load_module_from_name(modpath) return getattr(m, objname)(*args, **kwargs)
def cb_plug_module(self, option, opt_name, value, parser): """optik callback to plug the aspect on a module""" module = load_module_from_name(value) #self._weaved[value] = module weaver.weave_module(module, LoggerAspect, sys.stderr, self.config)
def test_knownValues_load_module_from_name_2(self): self.assertEqual(modutils.load_module_from_name('os.path'), path)
def test_knownValues_load_module_from_name_1(self): self.assertEqual(modutils.load_module_from_name('sys'), sys)