python类load_module()的实例源码

runtestHelpers.py 文件源码 项目:core-framework 作者: RedhawkSDR 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def loadModule(filename):
    if filename == '':
        raise RuntimeError, 'Empty filename cannot be loaded'
    print "Loading module %s" % (filename)
    searchPath, file = os.path.split(filename)
    if not searchPath in sys.path: 
        sys.path.append(searchPath)
        sys.path.append(os.path.normpath(searchPath+"/../"))
    moduleName, ext = os.path.splitext(file)
    fp, pathName, description = imp.find_module(moduleName, [searchPath,])

    try:
        module = imp.load_module(moduleName, fp, pathName, description)
    finally:
        if fp:
            fp.close()

    return module
ihooks.py 文件源码 项目:kinect-2-libras 作者: inessadl 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def import_it(self, partname, fqname, parent, force_load=0):
        if not partname:
            # completely empty module name should only happen in
            # 'from . import' or __import__("")
            return parent
        if not force_load:
            try:
                return self.modules[fqname]
            except KeyError:
                pass
        try:
            path = parent and parent.__path__
        except AttributeError:
            return None
        partname = str(partname)
        stuff = self.loader.find_module(partname, path)
        if not stuff:
            return None
        fqname = str(fqname)
        m = self.loader.load_module(fqname, stuff)
        if parent:
            setattr(parent, partname, m)
        return m
pydoc.py 文件源码 项目:kinect-2-libras 作者: inessadl 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def synopsis(filename, cache={}):
    """Get the one-line summary out of a module file."""
    mtime = os.stat(filename).st_mtime
    lastupdate, result = cache.get(filename, (0, None))
    if lastupdate < mtime:
        info = inspect.getmoduleinfo(filename)
        try:
            file = open(filename)
        except IOError:
            # module can't be opened, so skip it
            return None
        if info and 'b' in info[2]: # binary modules have to be imported
            try: module = imp.load_module('__temp__', file, filename, info[1:])
            except: return None
            result = (module.__doc__ or '').splitlines()[0]
            del sys.modules['__temp__']
        else: # text modules can be directly examined
            result = source_synopsis(file)
            file.close()
        cache[filename] = (mtime, result)
    return result
pydoc.py 文件源码 项目:kinect-2-libras 作者: inessadl 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def importfile(path):
    """Import a Python source file or compiled file given its path."""
    magic = imp.get_magic()
    file = open(path, 'r')
    if file.read(len(magic)) == magic:
        kind = imp.PY_COMPILED
    else:
        kind = imp.PY_SOURCE
    file.close()
    filename = os.path.basename(path)
    name, ext = os.path.splitext(filename)
    file = open(path, 'r')
    try:
        module = imp.load_module(name, file, path, (ext, 'r', kind))
    except:
        raise ErrorDuringImport(path, sys.exc_info())
    file.close()
    return module
setup.py 文件源码 项目:abusehelper 作者: Exploit-install 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def install_other(subdir):
    cwd = os.getcwd()
    path = os.path.join(cwd, subdir)

    try:
        os.chdir(path)
    except OSError, error:
        if error.errno not in (errno.ENOENT, errno.ENOTDIR):
            raise
        print >> sys.stderr, "Could not find directory %r" % path
        return

    try:
        module_info = imp.find_module("setup", ["."])
        imp.load_module("setup", *module_info)
    finally:
        os.chdir(cwd)
__init__.py 文件源码 项目:health-mosconi 作者: GNUHealth-Mosconi 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def get(prop):
    db_type = name()
    modname = 'trytond.backend.%s' % db_type
    if modname not in sys.modules:
        try:
            __import__(modname)
        except ImportError:
            if not pkg_resources:
                raise
            ep, = pkg_resources.iter_entry_points('trytond.backend', db_type)
            mod_path = os.path.join(ep.dist.location,
                *ep.module_name.split('.')[:-1])
            fp, pathname, description = imp.find_module(db_type, [mod_path])
            imp.load_module(modname, fp, pathname, description)
    module = sys.modules[modname]
    return getattr(module, prop)
UTscapy.py 文件源码 项目:CyberScan 作者: medbenali 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def import_module(name):
    name = os.path.realpath(name)
    thepath = os.path.dirname(name)
    name = os.path.basename(name)
    if name.endswith(".py"):
        name = name[:-3]
    f,path,desc = imp.find_module(name,[thepath])

    try:
        return imp.load_module(name, f, path, desc)
    finally:
        if f:
            f.close()


#### INTERNAL/EXTERNAL FILE EMBEDDING ####
UTscapy.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def import_module(name):
    name = os.path.realpath(name)
    thepath = os.path.dirname(name)
    name = os.path.basename(name)
    if name.endswith(".py"):
        name = name[:-3]
    f,path,desc = imp.find_module(name,[thepath])

    try:
        return imp.load_module(name, f, path, desc)
    finally:
        if f:
            f.close()


#### INTERNAL/EXTERNAL FILE EMBEDDING ####
ihooks.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m
ihooks.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def import_it(self, partname, fqname, parent, force_load=0):
        if not partname:
            # completely empty module name should only happen in
            # 'from . import' or __import__("")
            return parent
        if not force_load:
            try:
                return self.modules[fqname]
            except KeyError:
                pass
        try:
            path = parent and parent.__path__
        except AttributeError:
            return None
        partname = str(partname)
        stuff = self.loader.find_module(partname, path)
        if not stuff:
            return None
        fqname = str(fqname)
        m = self.loader.load_module(fqname, stuff)
        if parent:
            setattr(parent, partname, m)
        return m
pydoc.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def importfile(path):
    """Import a Python source file or compiled file given its path."""
    magic = imp.get_magic()
    file = open(path, 'r')
    if file.read(len(magic)) == magic:
        kind = imp.PY_COMPILED
    else:
        kind = imp.PY_SOURCE
    file.close()
    filename = os.path.basename(path)
    name, ext = os.path.splitext(filename)
    file = open(path, 'r')
    try:
        module = imp.load_module(name, file, path, (ext, 'r', kind))
    except:
        raise ErrorDuringImport(path, sys.exc_info())
    file.close()
    return module
PluginManager.py 文件源码 项目:rudra 作者: 7h3rAm 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def load_plugin(self, plugin, found_plugin):
    try:
      module = imp.load_module(plugin, found_plugin[0], found_plugin[1], found_plugin[2])
    except ImportError as err:
      print "[i] ImportError for '{0}'".format(found_plugin[0], err)
      print traceback.print_exc()
      return
    except TypeError as err:
      print "[i] TypeError for '{0}'".format(found_plugin[0], err)
      print traceback.format_exc()
      return
    except Exception as err:
      print "[i] Misc. Error for '{0}'".format(found_plugin[0], err)
      print traceback.format_exc()
      return

    return module
UTscapy.py 文件源码 项目:CVE-2016-6366 作者: RiskSense-Ops 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def import_module(name):
    name = os.path.realpath(name)
    thepath = os.path.dirname(name)
    name = os.path.basename(name)
    if name.endswith(".py"):
        name = name[:-3]
    f,path,desc = imp.find_module(name,[thepath])

    try:
        return imp.load_module(name, f, path, desc)
    finally:
        if f:
            f.close()


#### INTERNAL/EXTERNAL FILE EMBEDDING ####
_manager.py 文件源码 项目:gprime 作者: GenealogyCollective 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def reload(self, module, pdata):
        """
        Reloads modules that might not be in the path.
        """
        try:
            import imp
            fp, pathname, description = imp.find_module(pdata.mod_name, [pdata.fpath])
            try:
                module = imp.load_module(pdata.mod_name, fp, pathname,description)
            finally:
                if fp:
                    fp.close()
        except:
            if pdata.mod_name in sys.modules:
                del sys.modules[pdata.mod_name]
            module = self.import_plugin(pdata)
        return module
visualizationdesigner.py 文件源码 项目:coquery 作者: gkunter 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def get_visualizer_module(name):
    # try to import the specified visualization module:
    visualizer_path = os.path.join(options.cfg.base_path, "visualizer")
    try:
        find = imp.find_module(name, [visualizer_path])
        module = imp.load_module(name, *find)
        return module
    except Exception as e:
        print(e)
        msg = "<code style='color: darkred'>{type}: {code}</code>".format(
            type=type(e).__name__, code=sys.exc_info()[1])
        logger.error(msg)
        QtWidgets.QMessageBox.critical(
            None, "Visualization error – Coquery",
            VisualizationModuleError(name, msg).error_message)
        return None
__init__.py 文件源码 项目:hakkuframework 作者: 4shadoww 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def find_module(self, fullname, path=None):
        logger.debug('Running find_module: {0}...'.format(fullname))
        if '.' in fullname:
            parent, child = fullname.rsplit('.', 1)
            if path is None:
                loader = self.find_module(parent, path)
                mod = loader.load_module(parent)
                path = mod.__path__
            fullname = child

        # Perhaps we should try using the new importlib functionality in Python
        # 3.3: something like this?
        # thing = importlib.machinery.PathFinder.find_module(fullname, path)
        try:
            self.found = imp.find_module(fullname, path)
        except Exception as e:
            logger.debug('Py2Fixer could not find {0}')
            logger.debug('Exception was: {0})'.format(fullname, e))
            return None
        self.kind = self.found[-1][-1]
        if self.kind == imp.PKG_DIRECTORY:
            self.pathname = os.path.join(self.found[1], '__init__.py')
        elif self.kind == imp.PY_SOURCE:
            self.pathname = self.found[1]
        return self
__init__.py 文件源码 项目:hakkuframework 作者: 4shadoww 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _find_and_load_module(self, name, path=None):
        """
        Finds and loads it. But if there's a . in the name, handles it
        properly.
        """
        bits = name.split('.')
        while len(bits) > 1:
            # Treat the first bit as a package
            packagename = bits.pop(0)
            package = self._find_and_load_module(packagename, path)
            try:
                path = package.__path__
            except AttributeError:
                # This could be e.g. moves.
                flog.debug('Package {0} has no __path__.'.format(package))
                if name in sys.modules:
                    return sys.modules[name]
                flog.debug('What to do here?')

        name = bits[0]
        module_info = imp.find_module(name, path)
        return imp.load_module(name, *module_info)
UTscapy.py 文件源码 项目:hakkuframework 作者: 4shadoww 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def import_module(name):
    name = os.path.realpath(name)
    thepath = os.path.dirname(name)
    name = os.path.basename(name)
    if name.endswith(".py"):
        name = name[:-3]
    f,path,desc = imp.find_module(name,[thepath])

    try:
        return imp.load_module(name, f, path, desc)
    finally:
        if f:
            f.close()


#### INTERNAL/EXTERNAL FILE EMBEDDING ####
class_loader.py 文件源码 项目:relaax 作者: deeplearninc 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def import_module(cls, path, cname):
        if path is None:
            pname, _ = cname.rsplit('.', 1)
        else:
            mpath, mname = os.path.split(path)

            if os.path.isdir(path):
                pname, _ = cname.rsplit('.', 1)
            elif os.path.isfile(path):
                mname, _ = os.path.splitext(mname)
                pname = None
            else:
                raise ImportError("No module named %s" % path)

            module = cls.load_module(mname, mpath)

        if pname:
            module = importlib.import_module(pname)

        return module
test_examples.py 文件源码 项目:parglare 作者: igordejanovic 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_examples():

    examples_pat = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                '../../examples/*/*.py')

    # Filter out __init__.py
    examples = [f for f in glob.glob(examples_pat)
                if not any([x in f for x in ['__init__.py',
                                             'molecular']])]
    for e in examples:
        example_dir = os.path.dirname(e)
        sys.path.insert(0, example_dir)
        (module_name, _) = os.path.splitext(os.path.basename(e))
        (module_file, module_path, desc) = \
            imp.find_module(module_name, [example_dir])

        m = imp.load_module(module_name, module_file, module_path, desc)

        if hasattr(m, 'main'):
            m.main(debug=False)
utils.py 文件源码 项目:isar 作者: ilbers 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def load_plugins(logger, plugins, pluginpath):
    def load_plugin(name):
        logger.debug('Loading plugin %s' % name)
        fp, pathname, description = imp.find_module(name, [pluginpath])
        try:
            return imp.load_module(name, fp, pathname, description)
        finally:
            if fp:
                fp.close()

    logger.debug('Loading plugins from %s...' % pluginpath)

    expanded = (glob.glob(os.path.join(pluginpath, '*' + ext))
                for ext in python_extensions)
    files = itertools.chain.from_iterable(expanded)
    names = set(os.path.splitext(os.path.basename(fn))[0] for fn in files)
    for name in names:
        if name != '__init__':
            plugin = load_plugin(name)
            if hasattr(plugin, 'plugin_init'):
                obj = plugin.plugin_init(plugins)
                plugins.append(obj or plugin)
            else:
                plugins.append(plugin)
setup.py 文件源码 项目:backrefs 作者: facelessuser 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def get_version():
    """Get version and version_info without importing the entire module."""

    devstatus = {
        'alpha': '3 - Alpha',
        'beta': '4 - Beta',
        'candidate': '4 - Beta',
        'final': '5 - Production/Stable'
    }
    path = os.path.join(os.path.dirname(__file__), 'backrefs')
    fp, pathname, desc = imp.find_module('__init__', [path])
    try:
        v = imp.load_module('__init__', fp, pathname, desc)
        return v.version, devstatus[v.version_info[3]]
    except Exception:
        print(traceback.format_exc())
    finally:
        fp.close()
setup.py 文件源码 项目:backrefs 作者: facelessuser 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def get_unicodedata():
    """Download the unicodedata version for the given Python version."""

    import unicodedata

    fail = False
    path = os.path.join(os.path.dirname(__file__), 'tools')
    fp, pathname, desc = imp.find_module('unidatadownload', [path])
    try:
        unidatadownload = imp.load_module('unidatadownload', fp, pathname, desc)
        unidatadownload.get_unicodedata(unicodedata.unidata_version, no_zip=True)
    except Exception:
        print(traceback.format_exc())
        fail = True
    finally:
        fp.close()

    assert not fail, "Failed to obtain unicodedata!"
setup.py 文件源码 项目:backrefs 作者: facelessuser 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def generate_unicode_table():
    """Generate the unicode table for the given Python version."""

    fail = False
    path = os.path.join(os.path.dirname(__file__), 'tools')
    fp, pathname, desc = imp.find_module('unipropgen', [path])
    try:
        unipropgen = imp.load_module('unipropgen', fp, pathname, desc)
        unipropgen.build_unicode_property_table(
            os.path.join(
                os.path.dirname(__file__),
                'backrefs', 'uniprops', 'unidata'
            )
        )
    except Exception:
        print(traceback.format_exc())
        fail = True
    finally:
        fp.close()

    assert not fail, "Failed uniprops.py generation!"
plugin_manager.py 文件源码 项目:OpenPoGoBot 作者: tehp 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def load_plugin(self, plugin_name):
        plugins = self.get_available_plugins()
        if plugin_name in plugins:
            if plugin_name not in self.loaded_plugins:
                module = imp.load_module(self.main_module, *plugins[plugin_name]['info'])
                self.loaded_plugins[plugin_name] = {
                    'name': plugin_name,
                    'info': plugins[plugin_name]['info'],
                    'module': module
                }
                self.log('Loaded plugin "%s".' % plugin_name, color="green")
            else:
                self.log('Plugin "%s" was already loaded!' % plugin_name, color="yellow")
        else:
            self.log('Cannot locate plugin "%s"!' % plugin_name, color="red")
            raise Exception('Cannot locate plugin "%s"' % plugin_name)
all.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __import__(name, globals=None, locals=None, fromlist=None):
    """An alternative to the import function so that we can import
    modules defined as strings.

    This code was taken from: http://docs.python.org/lib/examples-imp.html
    """
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.
    module_name = name.split('.')[-1]
    module_path = os.path.join(EXAMPLE_DIR, *name.split('.')[:-1])

    fp, pathname, description = imp.find_module(module_name, [module_path])

    try:
        return imp.load_module(module_name, fp, pathname, description)
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close()
test_import.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_imp_module(self):
        # Verify that the imp module can correctly load and find .py files
        # XXX (ncoghlan): It would be nice to use support.CleanImport
        # here, but that breaks because the os module registers some
        # handlers in copy_reg on import. Since CleanImport doesn't
        # revert that registration, the module is left in a broken
        # state after reversion. Reinitialising the module contents
        # and just reverting os.environ to its previous state is an OK
        # workaround
        orig_path = os.path
        orig_getenv = os.getenv
        with EnvironmentVarGuard():
            x = imp.find_module("os")
            self.addCleanup(x[0].close)
            new_os = imp.load_module("os", *x)
            self.assertIs(os, new_os)
            self.assertIs(orig_path, new_os.path)
            self.assertIsNot(orig_getenv, new_os.getenv)
pydoc.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def synopsis(filename, cache={}):
    """Get the one-line summary out of a module file."""
    mtime = os.stat(filename).st_mtime
    lastupdate, result = cache.get(filename, (None, None))
    if lastupdate is None or lastupdate < mtime:
        info = inspect.getmoduleinfo(filename)
        try:
            file = tokenize.open(filename)
        except IOError:
            # module can't be opened, so skip it
            return None
        if info and 'b' in info[2]: # binary modules have to be imported
            try: module = imp.load_module('__temp__', file, filename, info[1:])
            except: return None
            result = (module.__doc__ or '').splitlines()[0]
            del sys.modules['__temp__']
        else: # text modules can be directly examined
            result = source_synopsis(file)
            file.close()
        cache[filename] = (mtime, result)
    return result
pydoc.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def importfile(path):
    """Import a Python source file or compiled file given its path."""
    magic = imp.get_magic()
    with open(path, 'rb') as file:
        if file.read(len(magic)) == magic:
            kind = imp.PY_COMPILED
        else:
            kind = imp.PY_SOURCE
        file.seek(0)
        filename = os.path.basename(path)
        name, ext = os.path.splitext(filename)
        try:
            module = imp.load_module(name, file, path, (ext, 'r', kind))
        except:
            raise ErrorDuringImport(path, sys.exc_info())
    return module
testmodule.py 文件源码 项目:fandango 作者: tango-controls 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def get_submodules(self,module=None,nr=0):
    print('get_submodules(%s)'%module)
    try:
      module = module or self.module
      if fn.isString(module):
        m = self.load_module(module)
      else:
        m,module = module,module.__name__
      result = set()
      l = getattr(m,'__test__',dir(m))
      print m,l
      l = list(l)
      for o in l:
        o = o.split('.')[-1]
        n = getattr(m,o)
        if self.is_module(n) and module == n.__package__:
          o = module+'.'+o
          result.add(o)
          if nr<10:
            result = result.union(self.get_submodules(n,nr=nr+1))
    except Exception,e:
      print('get_submodules(%s)'%module)
      traceback.print_exc()
      raise e
    return result


问题


面经


文章

微信
公众号

扫码关注公众号