Python Code Examples for load module

60 Python code examples are found related to "load module". These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Project: parglare   Author: igordejanovic   File: common.py    License: MIT License 6 votes vote down vote up
def load_python_module(mod_name, mod_path):
    """
    Loads Python module from an arbitrary location.
    See https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path  # noqa
    """
    if sys.version_info >= (3, 5):
        import importlib.util
        spec = importlib.util.spec_from_file_location(
            mod_name, mod_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
    elif sys.version_info >= (3, 3):
        from importlib.machinery import SourceFileLoader
        module = SourceFileLoader(
            mod_name, mod_path).load_module()
    else:
        import imp
        module = imp.load_source(mod_name, mod_path)

    return module 
Example 2
Project: CVE-2017-7494   Author: joxeankoret   File: cve_2017_7494.py    License: GNU General Public License v3.0 6 votes vote down vote up
def load_module(self, module):
    if int(self.sambaOld) == 1:
	  module = '\\\PIPE\\' + module  
    
    log("Trying to load module %s" % module)
    stringbinding = r'ncacn_np:%s[\pipe\%s]' % (self.sambaTarget, module)
    sb = transport.DCERPCStringBinding(stringbinding)
    na = sb.get_network_address()
    rpctransport = transport.SMBTransport(na, filename = module, smb_connection = self.smb)
    dce = rpctransport.get_dce_rpc()

    try:
      dce.connect()
      return True
    except KeyboardInterrupt:
      print "Aborted."
      sys.exit(0)
    except:
      log("Error: %s" % str(sys.exc_info()[1]))
      return False 
Example 3
Project: python2017   Author: bpgc-cte   File: abc.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        """Return the loaded module.

        The module must be added to sys.modules and have import-related
        attributes set properly.  The fullname is a str.

        ImportError is raised on failure.

        This method is deprecated in favor of loader.exec_module(). If
        exec_module() exists then it is used to provide a backwards-compatible
        functionality for this method.

        """
        if not hasattr(self, 'exec_module'):
            raise ImportError
        return _bootstrap._load_module_shim(self, fullname) 
Example 4
Project: PokemonGo-DesktopMap   Author: mchristopher   File: loader.py    License: MIT License 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 5
Project: NoobSec-Toolkit   Author: krintoxi   File: bottle.py    License: GNU General Public License v2.0 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        packname, modname = fullname.rsplit('.', 1)
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 6
def load_module(prefix, epoch, data_names, data_shapes):
    """
    Loads the model from checkpoint specified by prefix and epoch, binds it
    to an executor, and sets its parameters and returns a mx.mod.Module
    """
    sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)

    # We don't need CTC loss for prediction, just a simple softmax will suffice.
    # We get the output of the layer just before the loss layer ('pred_fc') and add softmax on top
    pred_fc = sym.get_internals()['pred_fc_output']
    sym = mx.sym.softmax(data=pred_fc)

    mod = mx.mod.Module(symbol=sym, context=mx.cpu(), data_names=data_names, label_names=None)
    mod.bind(for_training=False, data_shapes=data_shapes)
    mod.set_params(arg_params, aux_params, allow_missing=False)
    return mod 
Example 7
Project: CTFCrackTools-V2   Author: Acmesec   File: loader.py    License: GNU General Public License v3.0 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 8
Project: royal-chaos   Author: KTH   File: Context.py    License: MIT License 6 votes vote down vote up
def load_module(path,encoding=None):
	try:
		return cache_modules[path]
	except KeyError:
		pass
	module=imp.new_module(WSCRIPT_FILE)
	try:
		code=Utils.readf(path,m='rU',encoding=encoding)
	except EnvironmentError:
		raise Errors.WafError('Could not read the file %r'%path)
	module_dir=os.path.dirname(path)
	sys.path.insert(0,module_dir)
	try:exec(compile(code,path,'exec'),module.__dict__)
	finally:sys.path.remove(module_dir)
	cache_modules[path]=module
	return module 
Example 9
Project: ufora   Author: ufora   File: ModuleLevelObjectIndex.py    License: Apache License 2.0 6 votes vote down vote up
def loadModule(self, modulename, module):
        if modulename in self.modulesVisited:
            return
        self.modulesVisited.add(modulename)

        #ignore all modules that are not part of the base python installation.
        #this is a crude way of deciding which modules are going to be available
        #on both the host and the server.
        if modulename not in sys.builtin_module_names and (
                not hasattr(module, '__file__') or not os.path.abspath(module.__file__).startswith(sys.prefix)
                ):
            return

        if module is not None:
            d = dict(module.__dict__)
            for leafItemName, leafItemValue in d.iteritems():
                self.module_objects_by_name["modules", modulename, leafItemName] = leafItemValue
                self.modules_and_names_by_object[id(leafItemValue)] = ("modules", modulename, leafItemName)
            self.modules_and_names_by_object[id(module)] = ("modules", modulename)
            self.module_objects_by_name["modules", modulename] = module 
Example 10
Project: opsbro   Author: naparuba   File: bottle.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        modname = fullname.rsplit('.', 1)[1]
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 11
Project: nlgserv   Author: mnestis   File: bottle.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        modname = fullname.rsplit('.', 1)[1]
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 12
Project: aws-servicebroker   Author: awslabs   File: bottle.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        modname = fullname.rsplit('.', 1)[1]
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 13
Project: Sepia   Author: S4kur4   File: loader.py    License: GNU General Public License v2.0 6 votes vote down vote up
def loadModule():
    _name = conf.MODULE_NAME #加载的脚本名
    msg = 'Load script: %s' % _name
    logger.info(msg)
    #在脚本文件夹中查找_name文件
    fp, pathname, description = imp.find_module(os.path.splitext(_name)[0], [paths.SCRIPT_PATH])
    try:
        th.module_obj = imp.load_module("_", fp, pathname, description) #加载找到的模块
        for each in ESSENTIAL_MODULE_METHODS: #ESSENTIAL_MODULE_METHODS=['poc']
            if not hasattr(th.module_obj, each): #如果模块中不存在'poc'方法就提醒并退出
                errorMsg = "Can't find essential method:'%s()' in current script,Please modify your script/PoC".format(each)
                sys.exit(logger.error(errorMsg))
    except ImportError, e: #模块加载失败就抛出异常
        errorMsg = "Your current scipt [%s.py] caused this exception\n%s\n%s" \
                   % (_name, '[Error Msg]: ' + str(e), 'Maybe you can download this module from pip or easy_install')
        sys.exit(logger.error(errorMsg)) 
Example 14
Project: apex-sigma-core   Author: lu-ci   File: modman.py    License: GNU General Public License v3.0 6 votes vote down vote up
def load_module(self, root, module_data):
        """
        Loads a given modules commands, events, and information.
        :param root: The module origin path.
        :type root: str
        :param module_data: Document containing the module's details.
        :type module_data: dict
        :return:
        :rtype:
        """
        if self.init:
            self.log.info(f'Loading the {module_data.get("name")} Module')
        self.load_category(module_data)
        if 'commands' in module_data:
            self.load_commands(root, module_data)
        if self.bot.cfg.dsc.bot:
            if not self.bot.cfg.pref.music_only:
                if 'events' in module_data:
                    self.load_events(root, module_data) 
Example 15
Project: apm-python-agent-principle   Author: mozillazg   File: _hook.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        print('load_module {}'.format(fullname))
        if fullname in sys.modules:
            return sys.modules[fullname]

        # 先从 sys.meta_path 中删除自定义的 finder
        # 防止下面执行 import_module 的时候再次触发此 finder
        # 从而出现递归调用的问题
        finder = sys.meta_path.pop(0)
        # 导入 module
        module = importlib.import_module(fullname)

        module_hook(fullname, module)

        sys.meta_path.insert(0, finder)
        return module 
Example 16
Project: SalesforceXyTools   Author: exiahuang   File: bottle.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        packname, modname = fullname.rsplit('.', 1)
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 17
Project: catch   Author: broadinstitute   File: dynamic_load.py    License: MIT License 6 votes vote down vote up
def load_module_from_path(path):
    """Load Python module in the given path.

    Args:
        path: path to .py file

    Returns:
        Python module (before returning, this also executes
        the module)
    """
    path = os.path.abspath(path)

    # Use the filename (without extension) as the module name
    _, filename = os.path.split(path)
    module_name, _ = os.path.splitext(filename)

    spec = importlib.util.spec_from_file_location(module_name, path)
    module = importlib.util.module_from_spec(spec)

    # Execute the module
    spec.loader.exec_module(module)

    return module 
Example 18
Project: jawfish   Author: war-and-code   File: loader.py    License: MIT License 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception as e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass)
        return tests 
Example 19
Project: sporco   Author: bwohlberg   File: __init__.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_module(name):
    """Load the named module without registering it in ``sys.modules``.

    Parameters
    ----------
    name : string
      Module name

    Returns
    -------
    mod : module
      Loaded module
    """

    spec = importlib.util.find_spec(name)
    mod = importlib.util.module_from_spec(spec)
    mod.__spec__ = spec
    mod.__loader__ = spec.loader
    spec.loader.exec_module(mod)
    return mod 
Example 20
Project: gimp-plugin-export-layers   Author: khalim19   File: loader.py    License: GNU General Public License v3.0 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all test cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 21
Project: D-VAE   Author: muhanzhang   File: util.py    License: MIT License 6 votes vote down vote up
def load_module_state(model, state_name):
    pretrained_dict = torch.load(state_name)
    model_dict = model.state_dict()

    # to delete, to correct grud names
    '''
    new_dict = {}
    for k, v in pretrained_dict.items():
        if k.startswith('grud_forward'):
            new_dict['grud'+k[12:]] = v
        else:
            new_dict[k] = v
    pretrained_dict = new_dict
    '''

    # 1. filter out unnecessary keys
    pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}

    # 2. overwrite entries in the existing state dict
    model_dict.update(pretrained_dict) 
    # 3. load the new state dict
    model.load_state_dict(pretrained_dict)
    return 
Example 22
Project: ironpython3   Author: IronLanguages   File: loader.py    License: Apache License 2.0 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception as e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass)
        return tests 
Example 23
Project: Computable   Author: ktraunmueller   File: loader.py    License: MIT License 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 24
Project: ezcf   Author: laike9m   File: type_yaml.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        mod = super(YamlLoader, self).load_module(fullname)

        with codecs.open(self.cfg_file, 'r', 'utf-8') as f:
            try:
                for doc in yaml.load_all(f, yaml.Loader):
                    if isinstance(doc, dict):
                        mod.__dict__.update(doc)
            except yaml.YAMLError:
                self.e = "YAMLError"
                self.err_msg = sys.exc_info()[1]

        if self.e == "YAMLError":
            err_msg = "\nYaml file not valid: "
            err_msg += self.cfg_file + '\n'
            err_msg += str(self.err_msg)
            raise InvalidYamlError(err_msg)

        return mod 
Example 25
Project: formica   Author: theserverlessway   File: loader.py    License: MIT License 6 votes vote down vote up
def load_module(self, module_path, element_key, element_value):
        path_elements = module_path.split("::")
        dir_pattern = re.compile(fnmatch.translate(path_elements.pop(0)), re.IGNORECASE)
        matched_dirs = [dir for dir in os.listdir(self.path) if dir_pattern.match(dir)]
        matched_dir = module_path
        if matched_dirs:
            matched_dir = matched_dirs[0]

        module_path = self.path + "/" + "/".join([matched_dir] + path_elements)

        file_name = "*"

        if not os.path.isdir(module_path):
            file_name = module_path.split("/")[-1]
            module_path = "/".join(module_path.split("/")[:-1])

        properties = element_value.get("Properties", {})
        properties["module_name"] = element_key
        vars = self.merge_variables(properties)

        loader = Loader(module_path, file_name, vars)
        loader.load()
        self.merge(loader.template_dictionary(), file=file_name) 
Example 26
Project: RevitBatchProcessor   Author: bvn-architecture   File: loader.py    License: GNU General Public License v3.0 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 27
Project: py3_demo_on_vnpy_ctp   Author: vvipi   File: __init__.py    License: MIT License 6 votes vote down vote up
def loadStrategyModule(moduleName):
    """使用importlib动态载入模块"""
    try:
        module = importlib.import_module(moduleName)
        
        # 遍历模块下的对象,只有名称中包含'Strategy'的才是策略类
        for k in dir(module):
            if 'Strategy' in k:
                v = module.__getattribute__(k)
                STRATEGY_CLASS[k] = v
    except:
        print ('-' * 20)
        print ('Failed to import strategy file %s:' %moduleName)
        traceback.print_exc()    


# 遍历strategy目录下的文件 
Example 28
Project: BinderFilter   Author: dxwu   File: ihooks.py    License: MIT License 6 votes vote down vote up
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 
Example 29
Project: ufora   Author: ufora   File: PureImplementationMappings.py    License: Apache License 2.0 6 votes vote down vote up
def try_load_pure_module(self, module_name):
        try:
            # first try to load a pyfora pure module, if one exists
            importlib.import_module("pyfora.pure_modules.pure_" + module_name)
            self.addMappingsForModule(module_name)
        except ImportError:
            pass

        pyfora_path = os.getenv('PYFORAPATH')
        if pyfora_path is None:
            return

        for mod in [module_name, "pure_" + module_name]:
            path = os.path.join(pyfora_path, mod)
            if os.path.exists(path) or os.path.exists(path + '.py'):
                try:
                    load_args = imp.find_module(mod, pyfora_path)
                    imp.load_module("pyfora.user_pure_modules.pure_" + mod, *load_args)
                    self.addMappingsForModule(module_name)
                except ImportError:
                    pass 
Example 30
Project: lokun-record   Author: benediktkr   File: bottle.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        packname, modname = fullname.rsplit('.', 1)
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 31
Project: Splunking-Crime   Author: nccgroup   File: ihooks.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
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 
Example 32
Project: pyparsing   Author: pyparsing   File: statemachine.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        assert fullname in self._found
        if fullname in sys.modules:
            module = sys.modules[fullname]
        else:
            sys.modules[fullname] = module = types.ModuleType(fullname)
        data = None
        with open(self._found[fullname]) as f:
            data = f.read()

        module.__dict__.clear()
        module.__file__ = self._found[fullname]
        module.__name__ = fullname
        module.__loader__ = self
        self.process_filedata(module, data)
        return module 
Example 33
Project: python   Author: Yeah-Kun   File: abc.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(self, fullname):
        """Return the loaded module.

        The module must be added to sys.modules and have import-related
        attributes set properly.  The fullname is a str.

        ImportError is raised on failure.

        This method is deprecated in favor of loader.exec_module(). If
        exec_module() exists then it is used to provide a backwards-compatible
        functionality for this method.

        """
        if not hasattr(self, 'exec_module'):
            raise ImportError
        return _bootstrap._load_module_shim(self, fullname) 
Example 34
Project: teye_scanner_for_book   Author: imiyoo2010   File: bottle.py    License: GNU General Public License v3.0 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        packname, modname = fullname.rsplit('.', 1)
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 35
Project: arnold-usd   Author: Autodesk   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        modname = fullname.rsplit('.', 1)[1]
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 36
Project: scylla   Author: acaceres2176   File: abc.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(self, fullname):
        """Return the loaded module.

        The module must be added to sys.modules and have import-related
        attributes set properly.  The fullname is a str.

        ImportError is raised on failure.

        This method is deprecated in favor of loader.exec_module(). If
        exec_module() exists then it is used to provide a backwards-compatible
        functionality for this method.

        """
        if not hasattr(self, 'exec_module'):
            raise ImportError
        return _bootstrap._load_module_shim(self, fullname) 
Example 37
Project: sqlobject   Author: sqlobject   File: moduleloader.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def load_module_from_name(filename, module_name):
    if module_name in sys.modules:
        return sys.modules[module_name]
    init_filename = os.path.join(os.path.dirname(filename), '__init__.py')
    if not os.path.exists(init_filename):
        try:
            f = open(init_filename, 'w')
        except (OSError, IOError) as e:
            raise IOError(
                'Cannot write __init__.py file into directory %s (%s)\n'
                % (os.path.dirname(filename), e))
        f.write('#\n')
        f.close()
    if module_name in sys.modules:
        return sys.modules[module_name]
    if '.' in module_name:
        parent_name = '.'.join(module_name.split('.')[:-1])
        base_name = module_name.split('.')[-1]
        load_module_from_name(os.path.dirname(filename), parent_name)
    else:
        base_name = module_name
    return load_module_from_file(base_name, module_name, filename) 
Example 38
Project: anvio   Author: merenlab   File: programs.py    License: GNU General Public License v3.0 6 votes vote down vote up
def load_as_module(self, path):
        """
        Importing the program as a module has the advantage of grabbing the meta info as python
        objects directly instead of parsing the file lines as a string object. if is not a Python
        file, self.module is None.

        Taken from stackoverflow user Ciro Santilli:
        https://stackoverflow.com/questions/2601047/import-a-python-module-without-the-py-extension/56090741#56090741
        """
        try:
            module_name = os.path.basename(path).replace('-', '_')
            spec = importlib.util.spec_from_loader(
                module_name,
                importlib.machinery.SourceFileLoader(module_name, path)
            )
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)
            sys.modules[module_name] = module
            return module
        except:
            return None 
Example 39
Project: CTFCrackTools   Author: Acmesec   File: ihooks.py    License: GNU General Public License v3.0 6 votes vote down vote up
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 
Example 40
Project: pySINDy   Author: luckystarufo   File: modutils.py    License: MIT License 6 votes vote down vote up
def load_module_from_name(dotted_name, path=None, use_sys=True):
    """Load a Python module from its name.

    :type dotted_name: str
    :param dotted_name: python name of a module or package

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type use_sys: bool
    :param use_sys:
      boolean indicating whether the sys.modules dictionary should be
      used or not


    :raise ImportError: if the module or package is not found

    :rtype: module
    :return: the loaded module
    """
    return load_module_from_modpath(dotted_name.split("."), path, use_sys) 
Example 41
Project: ironpython2   Author: IronLanguages   File: loader.py    License: Apache License 2.0 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all test cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 42
Project: 802.11ah-ns3   Author: MOSAIC-UA   File: Context.py    License: GNU General Public License v2.0 6 votes vote down vote up
def load_module(path,encoding=None):
	try:
		return cache_modules[path]
	except KeyError:
		pass
	module=imp.new_module(WSCRIPT_FILE)
	try:
		code=Utils.readf(path,m='rU',encoding=encoding)
	except EnvironmentError:
		raise Errors.WafError('Could not read the file %r'%path)
	module_dir=os.path.dirname(path)
	sys.path.insert(0,module_dir)
	exec(compile(code,path,'exec'),module.__dict__)
	sys.path.remove(module_dir)
	cache_modules[path]=module
	return module 
Example 43
Project: meddle   Author: glmcdona   File: ihooks.py    License: MIT License 6 votes vote down vote up
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 
Example 44
Project: locality-sensitive-hashing   Author: singhj   File: base.py    License: MIT License 6 votes vote down vote up
def loadTestsFromModule(self, module, path=None):
        """Return iterable of tests in a module. May be a
        generator. Each item returned must be a runnable
        unittest.TestCase (or subclass) instance.
        Return None if your plugin cannot
        collect any tests from module.

        :param module: The module object
        :type module: python module
        :param path: the path of the module to search, to distinguish from
            namespace package modules

            .. note::

               NEW. The ``path`` parameter will only be passed by nose 0.11
               or above.
        """
        pass 
Example 45
Project: unity-python   Author: exodrifter   File: ihooks.py    License: MIT License 6 votes vote down vote up
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 
Example 46
Project: CTFCrackTools-V2   Author: Acmesec   File: ihooks.py    License: GNU General Public License v3.0 6 votes vote down vote up
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 
Example 47
Project: Flask-P2P   Author: chalasr   File: base.py    License: MIT License 6 votes vote down vote up
def load_config_from_module_name_or_filename(self, location):
        """
        Loads the configuration file: the file is a python file, otherwise raise an RuntimeError
        Exception or stop the process if the configuration file contains a syntax error.
        """

        try:
            cfg = self.get_config_from_module_name(module_name=location)
        except ImportError:
            cfg = self.get_config_from_filename(filename=location)

        for k, v in cfg.items():
            # Ignore unknown names
            if k not in self.cfg.settings:
                continue
            try:
                self.cfg.set(k.lower(), v)
            except:
                print("Invalid value for %s: %s\n" % (k, v), file=sys.stderr)
                sys.stderr.flush()
                raise

        return cfg 
Example 48
Project: canape   Author: ctxis   File: loader.py    License: GNU General Public License v3.0 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 49
Project: MobulaOP   Author: wkcn   File: loader.py    License: MIT License 6 votes vote down vote up
def load_module(name, pathname):
        """Load Module.

        Paramters
        ---------
        name: str
            the name of module.
        pathname:
            the name of path.

        Returns
        -------
        Module
        """
        spec = importlib.util.spec_from_file_location(name, pathname)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        return module 
Example 50
Project: training_results_v0.6   Author: mlperf   File: lstm_ocr_infer.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(prefix, epoch, data_names, data_shapes):
    """
    Loads the model from checkpoint specified by prefix and epoch, binds it
    to an executor, and sets its parameters and returns a mx.mod.Module
    """
    sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)

    # We don't need CTC loss for prediction, just a simple softmax will suffice.
    # We get the output of the layer just before the loss layer ('pred_fc') and add softmax on top
    pred_fc = sym.get_internals()['pred_fc_output']
    sym = mx.sym.softmax(data=pred_fc)

    mod = mx.mod.Module(symbol=sym, context=mx.cpu(), data_names=data_names, label_names=None)
    mod.bind(for_training=False, data_shapes=data_shapes)
    mod.set_params(arg_params, aux_params, allow_missing=False)
    return mod 
Example 51
Project: ppci   Author: windelbouwman   File: codepage.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def load_code_as_module(source_file, reporter=None):
    """ Load c3 code as a module """

    from ..api import c3c

    # Compile a simple function
    march = get_current_arch()
    if march is None:
        raise NotImplementedError(sys.platform)

    obj = c3c(
        [source_file], [], march, debug=True, opt_level=2, reporter=reporter
    )

    # Convert obj to executable module
    m = Mod(obj)
    return m 
Example 52
Project: oss-ftp   Author: aliyun   File: ihooks.py    License: MIT License 6 votes vote down vote up
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 
Example 53
Project: oss-ftp   Author: aliyun   File: loader.py    License: MIT License 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, case.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception, e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass) 
Example 54
Project: contrail-server-manager   Author: Juniper   File: bottle.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        packname, modname = fullname.rsplit('.', 1)
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 55
Project: pibooth   Author: pibooth   File: utils.py    License: MIT License 6 votes vote down vote up
def load_module(path):
    """Load a Python module dynamically.
    """
    if not osp.isfile(path):
        raise ValueError("Invalid Python module path '{}'".format(path))

    dirname, filename = osp.split(path)
    modname = osp.splitext(filename)[0]

    if dirname not in sys.path:
        sys.path.append(dirname)

    for hook in sys.meta_path:
        loader = hook.find_module(modname, [dirname])
        if loader:
            return loader.load_module(modname)

    LOGGER.warning("Can not load Python module '%s' from '%s'", modname, path) 
Example 56
Project: Imogen   Author: CedricGuillemet   File: abc.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        """Return the loaded module.

        The module must be added to sys.modules and have import-related
        attributes set properly.  The fullname is a str.

        ImportError is raised on failure.

        This method is deprecated in favor of loader.exec_module(). If
        exec_module() exists then it is used to provide a backwards-compatible
        functionality for this method.

        """
        if not hasattr(self, 'exec_module'):
            raise ImportError
        return _bootstrap._load_module_shim(self, fullname) 
Example 57
Project: luscan-devel   Author: blackye   File: loader.py    License: GNU General Public License v2.0 6 votes vote down vote up
def loadTestsFromModule(self, module, use_load_tests=True):
        """Return a suite of all tests cases contained in the given module"""
        tests = []
        for name in dir(module):
            obj = getattr(module, name)
            if isinstance(obj, type) and issubclass(obj, unittest.TestCase):
                tests.append(self.loadTestsFromTestCase(obj))

        load_tests = getattr(module, 'load_tests', None)
        tests = self.suiteClass(tests)
        if use_load_tests and load_tests is not None:
            try:
                return load_tests(self, tests, None)
            except Exception as e:
                return _make_failed_load_tests(module.__name__, e,
                                               self.suiteClass)
        return tests 
Example 58
Project: aws-mock-metadata   Author: dump247   File: bottle.py    License: MIT License 6 votes vote down vote up
def load_module(self, fullname):
        if fullname in sys.modules: return sys.modules[fullname]
        modname = fullname.rsplit('.', 1)[1]
        realname = self.impmask % modname
        __import__(realname)
        module = sys.modules[fullname] = sys.modules[realname]
        setattr(self.module, modname, module)
        module.__loader__ = self
        return module






###############################################################################
# Common Utilities #############################################################
############################################################################### 
Example 59
Project: SNIPER-mxnet   Author: mahyarnajibi   File: lstm_ocr_infer.py    License: Apache License 2.0 6 votes vote down vote up
def load_module(prefix, epoch, data_names, data_shapes):
    """
    Loads the model from checkpoint specified by prefix and epoch, binds it
    to an executor, and sets its parameters and returns a mx.mod.Module
    """
    sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)

    # We don't need CTC loss for prediction, just a simple softmax will suffice.
    # We get the output of the layer just before the loss layer ('pred_fc') and add softmax on top
    pred_fc = sym.get_internals()['pred_fc_output']
    sym = mx.sym.softmax(data=pred_fc)

    mod = mx.mod.Module(symbol=sym, context=mx.cpu(), data_names=data_names, label_names=None)
    mod.bind(for_training=False, data_shapes=data_shapes)
    mod.set_params(arg_params, aux_params, allow_missing=False)
    return mod 
Example 60
Project: tbot   Author: Rahix   File: loader.py    License: GNU General Public License v3.0 6 votes vote down vote up
def load_module(p: pathlib.Path) -> types.ModuleType:
    """
    Load a python module from a path.

    :param pathlib.Path p: Path to ``<module>.py``
    :rtype: Module
    """
    import importlib.util

    if not p.is_file():
        raise FileNotFoundError(f"The module {str(p)!r} does not exist")
    default_sys_path = sys.path
    try:
        module_spec = importlib.util.spec_from_file_location(
            name=p.stem, location=str(p)
        )
        module = importlib.util.module_from_spec(module_spec)
        if not isinstance(module_spec.loader, importlib.abc.Loader):
            raise TypeError(f"Invalid module spec {module_spec!r}")
        sys.path = default_sys_path + [str(p.parent)]
        module_spec.loader.exec_module(module)
    finally:
        sys.path = default_sys_path

    return module