本文整理汇总了Python中pyximport.load_module函数的典型用法代码示例。如果您正苦于以下问题:Python load_module函数的具体用法?Python load_module怎么用?Python load_module使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了load_module函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
def call(self, fun, arg=list()):
'''
Call a function in the load path.
'''
name = fun[:fun.rindex('.')]
try:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(name, fn_, path, desc)
except ImportError:
if self.opts.get('cython_enable', True) is True:
# The module was not found, try to find a cython module
try:
import pyximport
pyximport.install()
for mod_dir in self.module_dirs:
for fn_ in os.listdir(mod_dir):
if name == fn_[:fn_.rindex('.')]:
# Found it, load the mod and break the loop
mod = pyximport.load_module(
name, os.path.join(mod_dir, fn_)
)
return getattr(
mod, fun[fun.rindex('.') + 1:])(*arg)
except ImportError:
log.info("Cython is enabled in options though it's not "
"present in the system path. Skipping Cython "
"modules.")
return getattr(mod, fun[fun.rindex('.') + 1:])(*arg)
示例2: scanfuncs
def scanfuncs(filename, prefixes, cython=False):
""" Return list of function names from ``filename`` that begin with prefix.
This *does not* import the Python file, so this is safe to use, but
functionality is limited to retrieving names of basic functions defined
within global scope of the file.
This *does*, however, import Cython files (if applicable).
"""
# Used by: findarenas, findbenchmarks
path, name = os.path.split(filename)
name, ext = os.path.splitext(name)
# Should `cython` be a keyword argument, or should we just infer it?
cython = cython or ext == '.pyx'
if not cython:
funcs = pyclbr.readmodule_ex(name, [path])
funcnames = []
for key, val in funcs.items():
if (any(key.startswith(prefix) for prefix in prefixes) and
val.file == filename):
funcnames.append(key)
return funcnames
# Scan Cython file. We need to import it.
import pyximport
pyximport.install()
sys.dont_write_bytecode = True
# Make sure local imports work for the given file
sys.path.insert(0, path)
pyximport.build_module(name, filename)
try:
mod = pyximport.load_module(name, filename)
except ImportError:
# There is most likely a '*.py' file that shares the same
# base name as the '*.pyx' file we are trying to import.
# Removing the directory from sys.path should fix this,
# but will disable local importing.
sys.path.pop(0)
mod = pyximport.load_module(name, filename)
# Undo making local imports work
if sys.path[0] == path:
sys.path.pop(0)
funcnames = []
for funcname in mod.__dict__:
if any(funcname.startswith(prefix) for prefix in prefixes):
funcnames.append(funcname)
return funcnames
示例3: gen_functions
def gen_functions(self):
'''
Return a dict of functions found in the defined module_dirs
'''
names = {}
modules = []
funcs = {}
for mod_dir in self.module_dirs:
if not mod_dir.startswith('/'):
continue
if not os.path.isdir(mod_dir):
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith('_'):
continue
if fn_.endswith('.py')\
or fn_.endswith('.pyc')\
or fn_.endswith('.pyo')\
or fn_.endswith('.so')\
or fn_.endswith('.pyx'):
names[fn_[:fn_.rindex('.')]] = os.path.join(mod_dir, fn_)
for name in names:
try:
if names[name].endswith('.pyx'):
mod = pyximport.load_module(name, names[name], '/tmp')
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(name, fn_, path, desc)
except ImportError:
continue
modules.append(mod)
for mod in modules:
virtual = ''
if hasattr(mod, '__opts__'):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
if hasattr(mod, '__virtual__'):
if callable(mod.__virtual__):
virtual = mod.__virtual__()
for attr in dir(mod):
if attr.startswith('_'):
continue
if callable(getattr(mod, attr)):
if virtual:
funcs[virtual + '.' + attr] = getattr(mod, attr)
elif virtual == False:
pass
else:
funcs[mod.__name__ + '.' + attr] = getattr(mod, attr)
return funcs
示例4: call
def call(self, fun, arg=[]):
'''
Call a function in the load path.
'''
name = fun[:fun.rindex('.')]
try:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(name, fn_, path, desc)
except ImportError:
# The module was not found, try to find a cython module
for mod_dir in self.module_dirs:
for fn_ in os.listdir(mod_dir):
if name == fn_[:fn_.rindex('.')]:
# Found it, load the mod and break the loop
mod = pyximport.load_module(name, os.path.join(mod_dir, fn_))
return getattr(mod, fun[fun.rindex('.') + 1:])(*arg)
return getattr(mod, fun[fun.rindex('.') + 1:])(*arg)
示例5: gen_functions
def gen_functions(self, pack=None, virtual_enable=True, whitelist=None):
'''
Return a dict of functions found in the defined module_dirs
'''
log.debug('loading {0} in {1}'.format(self.tag, self.module_dirs))
names = {}
modules = []
funcs = {}
disable = set(self.opts.get('disable_{0}s'.format(self.tag), []))
cython_enabled = False
if self.opts.get('cython_enable', True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info('Cython is enabled in the options but not present '
'in the system path. Skipping Cython modules.')
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
log.debug(
'Skipping {0}, it is not an absolute path'.format(
mod_dir
)
)
continue
if not os.path.isdir(mod_dir):
log.debug(
'Skipping {0}, it is not a directory'.format(
mod_dir
)
)
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith('_'):
# skip private modules
# log messages omitted for obviousness
continue
if fn_.split('.')[0] in disable:
log.debug(
'Skipping {0}, it is disabled by configuration'.format(
fn_
)
)
continue
if (fn_.endswith(('.py', '.pyc', '.pyo', '.so'))
or (cython_enabled and fn_.endswith('.pyx'))
or os.path.isdir(os.path.join(mod_dir, fn_))):
extpos = fn_.rfind('.')
if extpos > 0:
_name = fn_[:extpos]
else:
_name = fn_
names[_name] = os.path.join(mod_dir, fn_)
else:
log.debug(
'Skipping {0}, it does not end with an expected '
'extension'.format(
fn_
)
)
for name in names:
try:
if names[name].endswith('.pyx'):
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(
'{0}.{1}.{2}.{3}'.format(
self.loaded_base_name,
self.mod_type_check(names[name]),
self.tag,
name
), names[name], tempfile.gettempdir()
)
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(
'{0}.{1}.{2}.{3}'.format(
self.loaded_base_name,
self.mod_type_check(path),
self.tag,
name
), fn_, path, desc
)
# reload all submodules if necessary
submodules = [
getattr(mod, sname) for sname in dir(mod) if
isinstance(getattr(mod, sname), mod.__class__)
]
# reload only custom "sub"modules i.e is a submodule in
# parent module that are still available on disk (i.e. not
# removed during sync_modules)
for submodule in submodules:
try:
smname = '{0}.{1}.{2}'.format(
self.loaded_base_name,
self.tag,
name
#.........这里部分代码省略.........
示例6: gen_module
def gen_module(self, name, functions, pack=None):
'''
Load a single module and pack it with the functions passed
'''
full = ''
mod = None
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
continue
if not os.path.isdir(mod_dir):
continue
fn_ = os.path.join(mod_dir, name)
if os.path.isdir(fn_):
full = fn_
else:
for ext in ('.py', '.pyo', '.pyc', '.so'):
full_test = '{0}{1}'.format(fn_, ext)
if os.path.isfile(full_test):
full = full_test
if not full:
return None
cython_enabled = False
if self.opts.get('cython_enable', True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info('Cython is enabled in the options but not present '
'in the system path. Skipping Cython modules.')
try:
if full.endswith('.pyx') and cython_enabled:
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(name, full, tempfile.gettempdir())
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(
'{0}.{1}.{2}.{3}'.format(
self.loaded_base_name,
self.mod_type_check(path),
self.tag,
name
), fn_, path, desc
)
except ImportError:
log.debug(
'Failed to import {0} {1}:\n'.format(
self.tag, name
),
exc_info=True
)
return mod
except Exception:
log.warning(
'Failed to import {0} {1}, this is due most likely to a '
'syntax error:\n'.format(
self.tag, name
),
exc_info=True
)
return mod
if hasattr(mod, '__opts__'):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
if pack:
if isinstance(pack, list):
for chunk in pack:
try:
setattr(mod, chunk['name'], chunk['value'])
except KeyError:
pass
else:
setattr(mod, pack['name'], pack['value'])
# Call a module's initialization method if it exists
if hasattr(mod, '__init__'):
if callable(mod.__init__):
try:
mod.__init__(self.opts)
except TypeError:
pass
funcs = {}
module_name = mod.__name__[mod.__name__.rindex('.') + 1:]
if getattr(mod, '__load__', False) is not False:
log.info(
'The functions from module {0!r} are being loaded from the '
'provided __load__ attribute'.format(
module_name
)
)
for attr in getattr(mod, '__load__', dir(mod)):
if attr.startswith('_'):
# private functions are skipped
continue
#.........这里部分代码省略.........
示例7: gen_functions
def gen_functions(self, pack=None):
'''
Return a dict of functions found in the defined module_dirs
'''
names = {}
modules = []
funcs = {}
cython_enabled = False
if self.opts.get('cython_enable', True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info("Cython is enabled in options though it's not present "
"in the system path. Skipping Cython modules.")
for mod_dir in self.module_dirs:
if not mod_dir.startswith('/'):
continue
if not os.path.isdir(mod_dir):
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith('_'):
continue
if fn_.endswith('.py')\
or fn_.endswith('.pyc')\
or fn_.endswith('.pyo')\
or fn_.endswith('.so')\
or (cython_enabled and fn_.endswith('.pyx')):
names[fn_[:fn_.rindex('.')]] = os.path.join(mod_dir, fn_)
for name in names:
try:
if names[name].endswith('.pyx'):
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(name, names[name], '/tmp')
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(name, fn_, path, desc)
except ImportError:
continue
modules.append(mod)
for mod in modules:
virtual = ''
if hasattr(mod, '__opts__'):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
if pack:
if type(pack) == type(list()):
for chunk in pack:
setattr(mod, chunk['name'], chunk['value'])
else:
setattr(mod, pack['name'], pack['value'])
if hasattr(mod, '__virtual__'):
if callable(mod.__virtual__):
virtual = mod.__virtual__()
for attr in dir(mod):
if attr.startswith('_'):
continue
if callable(getattr(mod, attr)):
if virtual:
func = getattr(mod, attr)
funcs[virtual + '.' + attr] = func
self._apply_outputter(func, mod)
elif virtual == False:
pass
else:
func = getattr(mod, attr)
funcs[mod.__name__ + '.' + attr] = func
self._apply_outputter(func, mod)
for mod in modules:
if not hasattr(mod, '__salt__'):
mod.__salt__ = funcs
return funcs
示例8: gen_functions
def gen_functions(self, pack=None):
"""
Return a dict of functions found in the defined module_dirs
"""
names = {}
modules = []
funcs = {}
cython_enabled = False
if self.opts.get("cython_enable", True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info(
"Cython is enabled in options though it's not present "
"in the system path. Skipping Cython modules."
)
for mod_dir in self.module_dirs:
if not mod_dir.startswith("/"):
continue
if not os.path.isdir(mod_dir):
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith("_"):
continue
if (
fn_.endswith(".py")
or fn_.endswith(".pyc")
or fn_.endswith(".pyo")
or fn_.endswith(".so")
or (cython_enabled and fn_.endswith(".pyx"))
):
names[fn_[: fn_.rindex(".")]] = os.path.join(mod_dir, fn_)
for name in names:
try:
if names[name].endswith(".pyx"):
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(name, names[name], "/tmp")
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(name, fn_, path, desc)
except ImportError:
continue
modules.append(mod)
for mod in modules:
virtual = ""
if hasattr(mod, "__opts__"):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
if pack:
if type(pack) == type(list()):
for chunk in pack:
setattr(mod, chunk["name"], chunk["value"])
else:
setattr(mod, pack["name"], pack["value"])
if hasattr(mod, "__virtual__"):
if callable(mod.__virtual__):
virtual = mod.__virtual__()
for attr in dir(mod):
if attr.startswith("_"):
continue
if callable(getattr(mod, attr)):
if virtual:
funcs[virtual + "." + attr] = getattr(mod, attr)
elif virtual == False:
pass
else:
funcs[mod.__name__ + "." + attr] = getattr(mod, attr)
return funcs
示例9: gen_module
def gen_module(self, name, functions, pack=None):
'''
Load a single module and pack it with the functions passed
'''
full = ''
mod = None
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
continue
if not os.path.isdir(mod_dir):
continue
fn_ = os.path.join(mod_dir, name)
if os.path.isdir(fn_):
full = fn_
else:
for ext in ('.py', '.pyo', '.pyc', '.so'):
full_test = '{0}{1}'.format(fn_, ext)
if os.path.isfile(full_test):
full = full_test
if not full:
return None
cython_enabled = False
if self.opts.get('cython_enable', True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info('Cython is enabled in the options but not present '
'in the system path. Skipping Cython modules.')
try:
if full.endswith('.pyx') and cython_enabled:
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(name, full, tempfile.gettempdir())
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(
'{0}.{1}.{2}.{3}'.format(
loaded_base_name, _mod_type(path), self.tag, name
), fn_, path, desc
)
except ImportError as exc:
log.debug('Failed to import module {0}: {1}'.format(name, exc))
return mod
except Exception:
trb = traceback.format_exc()
log.warning('Failed to import module {0}, this is due most likely '
'to a syntax error: {1}'.format(name, trb))
return mod
if hasattr(mod, '__opts__'):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
if pack:
if isinstance(pack, list):
for chunk in pack:
try:
setattr(mod, chunk['name'], chunk['value'])
except KeyError:
pass
else:
setattr(mod, pack['name'], pack['value'])
# Call a module's initialization method if it exists
if hasattr(mod, '__init__'):
if callable(mod.__init__):
try:
mod.__init__(self.opts)
except TypeError:
pass
funcs = {}
for attr in dir(mod):
if attr.startswith('_'):
continue
if callable(getattr(mod, attr)):
func = getattr(mod, attr)
if hasattr(func, '__bases__'):
if 'BaseException' in func.__bases__:
# the callable object is an exception, don't load it
continue
funcs[
'{0}.{1}'.format(
mod.__name__[mod.__name__.rindex('.') + 1:], attr
)
] = func
self._apply_outputter(func, mod)
if not hasattr(mod, '__salt__'):
mod.__salt__ = functions
return funcs
示例10: gen_functions
def gen_functions(self, pack=None, virtual_enable=True):
'''
Return a dict of functions found in the defined module_dirs
'''
names = {}
modules = []
funcs = {}
disable = set(self.opts.get('disable_{0}s'.format(self.tag), []))
cython_enabled = False
if self.opts.get('cython_enable', True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info('Cython is enabled in the options but not present '
'in the system path. Skipping Cython modules.')
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
continue
if not os.path.isdir(mod_dir):
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith('_'):
continue
if fn_.split('.')[0] in disable:
continue
if (fn_.endswith(('.py', '.pyc', '.pyo', '.so'))
or (cython_enabled and fn_.endswith('.pyx'))
or os.path.isdir(os.path.join(mod_dir, fn_))):
extpos = fn_.rfind('.')
if extpos > 0:
_name = fn_[:extpos]
else:
_name = fn_
names[_name] = os.path.join(mod_dir, fn_)
for name in names:
try:
if names[name].endswith('.pyx'):
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(
'{0}.{1}.{2}.{3}'.format(
loaded_base_name,
_mod_type(names[name]),
self.tag,
name
), names[name], tempfile.gettempdir()
)
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(
'{0}.{1}.{2}.{3}'.format(
loaded_base_name, _mod_type(path), self.tag, name
), fn_, path, desc
)
# reload all submodules if necessary
submodules = [
getattr(mod, sname) for sname in dir(mod) if
type(getattr(mod, sname))==type(mod)
]
# reload only custom "sub"modules i.e is a submodule in
# parent module that are still available on disk (i.e. not
# removed during sync_modules)
for submodule in submodules:
try:
smname = '{0}.{1}.{2}'.format(loaded_base_name, self.tag, name)
smfile = os.path.splitext(submodule.__file__)[0] + ".py"
if submodule.__name__.startswith(smname) and os.path.isfile(smfile):
reload(submodule)
except AttributeError:
continue
except ImportError as exc:
log.debug('Failed to import module {0}, this is most likely '
'NOT a problem: {1}'.format(name, exc))
continue
except Exception as exc:
trb = traceback.format_exc()
log.warning('Failed to import module {0}, this is due most '
'likely to a syntax error: {1}'.format(name, trb))
continue
modules.append(mod)
for mod in modules:
virtual = ''
if hasattr(mod, '__opts__'):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
mod.__pillar__ = self.pillar
if pack:
if isinstance(pack, list):
for chunk in pack:
setattr(mod, chunk['name'], chunk['value'])
else:
setattr(mod, pack['name'], pack['value'])
#.........这里部分代码省略.........
示例11: load_modules
def load_modules(self):
'''
Loads all of the modules from module_dirs and returns a list of them
'''
self.modules = []
log.trace('loading {0} in {1}'.format(self.tag, self.module_dirs))
names = {}
disable = set(self.opts.get('disable_{0}s'.format(self.tag), []))
cython_enabled = False
if self.opts.get('cython_enable', True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info('Cython is enabled in the options but not present '
'in the system path. Skipping Cython modules.')
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
log.trace(
'Skipping {0}, it is not an absolute path'.format(
mod_dir
)
)
continue
if not os.path.isdir(mod_dir):
log.trace(
'Skipping {0}, it is not a directory'.format(
mod_dir
)
)
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith('_'):
# skip private modules
# log messages omitted for obviousness
continue
if fn_.split('.')[0] in disable:
log.trace(
'Skipping {0}, it is disabled by configuration'.format(
fn_
)
)
continue
if (fn_.endswith(('.py', '.pyc', '.pyo', '.so'))
or (cython_enabled and fn_.endswith('.pyx'))
or os.path.isdir(os.path.join(mod_dir, fn_))):
extpos = fn_.rfind('.')
if extpos > 0:
_name = fn_[:extpos]
else:
_name = fn_
names[_name] = os.path.join(mod_dir, fn_)
else:
log.trace(
'Skipping {0}, it does not end with an expected '
'extension'.format(
fn_
)
)
for name in names:
try:
if names[name].endswith('.pyx'):
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(
'{0}.{1}.{2}.{3}'.format(
self.loaded_base_name,
self.mod_type_check(names[name]),
self.tag,
name
), names[name], tempfile.gettempdir()
)
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(
'{0}.{1}.{2}.{3}'.format(
self.loaded_base_name,
self.mod_type_check(path),
self.tag,
name
), fn_, path, desc
)
# reload all submodules if necessary
submodules = [
getattr(mod, sname) for sname in dir(mod) if
isinstance(getattr(mod, sname), mod.__class__)
]
# reload only custom "sub"modules i.e is a submodule in
# parent module that are still available on disk (i.e. not
# removed during sync_modules)
for submodule in submodules:
try:
smname = '{0}.{1}.{2}'.format(
self.loaded_base_name,
self.tag,
name
#.........这里部分代码省略.........
示例12: print
tornado.web.URLSpec(r'/images/(?P<uid>[a-z0-9-]+)', ImageHandler, name='image'),
tornado.web.URLSpec(r'/algorithms/([^/]+)(.*)', AlgorithmHandler, name='algorithm'),
(r'/algorithms', AlgorithmHandler),
(r'/', IndexHandler),
])
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--logging', help='Set log level', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
default='INFO')
parser.add_argument('-c', '--cython', help='Compile algorithms with cython', action='store_true')
parser.add_argument('-p', '--port', help='Set port', type=int, default=8887)
args = parser.parse_args()
logging.basicConfig(level=args.logging)
if args.cython:
import pyximport
pyximport.install()
geometry.kcenter = pyximport.load_module('geometry.kcenter', 'geometry/kcenter.py')
graph.kcenter = pyximport.load_module('graph.kcenter', 'graph/kcenter.py')
application.listen(args.port)
print('Press strg-c to exit')
try:
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:
print('Shut down...')
示例13: gen_functions
def gen_functions(self, pack=None, virtual_enable=True):
"""
Return a dict of functions found in the defined module_dirs
"""
names = {}
modules = []
funcs = {}
cython_enabled = False
if self.opts.get("cython_enable", True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info(
"Cython is enabled in the options but not present " "in the system path. Skipping Cython modules."
)
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
continue
if not os.path.isdir(mod_dir):
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith("_"):
continue
if fn_.endswith((".py", ".pyc", ".pyo", ".so")) or (cython_enabled and fn_.endswith(".pyx")):
names[fn_[: fn_.rindex(".")]] = os.path.join(mod_dir, fn_)
for name in names:
try:
if names[name].endswith(".pyx"):
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module("{0}_{1}".format(name, self.tag), names[name], "/tmp")
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module("{0}_{1}".format(name, self.tag), fn_, path, desc)
except ImportError as exc:
log.debug(("Failed to import module {0}, this is most likely" " NOT a problem: {1}").format(name, exc))
continue
except Exception as exc:
log.warning(
("Failed to import module {0}, this is due most" " likely to a syntax error: {1}").format(name, exc)
)
continue
modules.append(mod)
for mod in modules:
virtual = ""
if hasattr(mod, "__opts__"):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
mod.__pillar__ = self.pillar
if pack:
if isinstance(pack, list):
for chunk in pack:
setattr(mod, chunk["name"], chunk["value"])
else:
setattr(mod, pack["name"], pack["value"])
# Call a module's initialization method if it exists
if hasattr(mod, "__init__"):
if callable(mod.__init__):
try:
mod.__init__()
except TypeError:
pass
if virtual_enable:
if hasattr(mod, "__virtual__"):
if callable(mod.__virtual__):
virtual = mod.__virtual__()
for attr in dir(mod):
if attr.startswith("_"):
continue
if callable(getattr(mod, attr)):
func = getattr(mod, attr)
if isinstance(func, type):
if any(["Error" in func.__name__, "Exception" in func.__name__]):
continue
if virtual:
funcs["{0}.{1}".format(virtual, attr)] = func
self._apply_outputter(func, mod)
elif virtual is False:
pass
else:
funcs["{0}.{1}".format(mod.__name__[: mod.__name__.rindex("_")], attr)] = func
self._apply_outputter(func, mod)
for mod in modules:
if not hasattr(mod, "__salt__"):
mod.__salt__ = funcs
return funcs
示例14: gen_module
def gen_module(self, name, functions, pack=None):
"""
Load a single module and pack it with the functions passed
"""
full = ""
mod = None
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
continue
if not os.path.isdir(mod_dir):
continue
fn_ = os.path.join(mod_dir, name)
for ext in (".py", ".pyo", ".pyc", ".so"):
full_test = "{0}{1}".format(fn_, ext)
if os.path.isfile(full_test):
full = full_test
if not full:
return None
try:
if full.endswith(".pyx") and self.opts["cython_enable"]:
mod = pyximport.load_module(name, full, "/tmp")
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module("{0}_{1}".format(name, self.tag), fn_, path, desc)
except ImportError as exc:
log.debug(("Failed to import module {0}: {1}").format(name, exc))
return mod
except Exception as exc:
log.warning(
("Failed to import module {0}, this is due most" " likely to a syntax error: {1}").format(name, exc)
)
return mod
if hasattr(mod, "__opts__"):
mod.__opts__.update(self.opts)
else:
mod.__opts__ = self.opts
mod.__grains__ = self.grains
if pack:
if isinstance(pack, list):
for chunk in pack:
setattr(mod, chunk["name"], chunk["value"])
else:
setattr(mod, pack["name"], pack["value"])
# Call a module's initialization method if it exists
if hasattr(mod, "__init__"):
if callable(mod.__init__):
try:
mod.__init__()
except TypeError:
pass
funcs = {}
for attr in dir(mod):
if attr.startswith("_"):
continue
if callable(getattr(mod, attr)):
func = getattr(mod, attr)
if hasattr(func, "__bases__"):
if "BaseException" in func.__bases__:
# the callable object is an exception, don't load it
continue
funcs["{0}.{1}".format(mod.__name__[: mod.__name__.rindex("_")], attr)] = func
self._apply_outputter(func, mod)
if not hasattr(mod, "__salt__"):
mod.__salt__ = functions
return funcs
示例15: gen_functions
def gen_functions(self, pack=None, virtual_enable=True):
'''
Return a dict of functions found in the defined module_dirs
'''
log.debug('loading {0} in {1}'.format(self.tag, self.module_dirs))
names = {}
modules = []
funcs = {}
disable = set(self.opts.get('disable_{0}s'.format(self.tag), []))
cython_enabled = False
if self.opts.get('cython_enable', True) is True:
try:
import pyximport
pyximport.install()
cython_enabled = True
except ImportError:
log.info('Cython is enabled in the options but not present '
'in the system path. Skipping Cython modules.')
for mod_dir in self.module_dirs:
if not os.path.isabs(mod_dir):
log.debug(('Skipping {0}, it is not an abosolute '
'path').format(mod_dir))
continue
if not os.path.isdir(mod_dir):
log.debug(('Skipping {0}, it is not a '
'directory').format(mod_dir))
continue
for fn_ in os.listdir(mod_dir):
if fn_.startswith('_'):
# skip private modules
# log messages omitted for obviousness
continue
if fn_.split('.')[0] in disable:
log.debug(('Skipping {0}, it is disabled by '
'configuration').format(fn_))
continue
if (fn_.endswith(('.py', '.pyc', '.pyo', '.so'))
or (cython_enabled and fn_.endswith('.pyx'))
or os.path.isdir(os.path.join(mod_dir, fn_))):
extpos = fn_.rfind('.')
if extpos > 0:
_name = fn_[:extpos]
else:
_name = fn_
names[_name] = os.path.join(mod_dir, fn_)
else:
log.debug(('Skipping {0}, it does not end with an '
'expected extension').format(fn_))
for name in names:
try:
if names[name].endswith('.pyx'):
# If there's a name which ends in .pyx it means the above
# cython_enabled is True. Continue...
mod = pyximport.load_module(
'{0}.{1}.{2}.{3}'.format(
loaded_base_name,
_mod_type(names[name]),
self.tag,
name
), names[name], tempfile.gettempdir()
)
else:
fn_, path, desc = imp.find_module(name, self.module_dirs)
mod = imp.load_module(
'{0}.{1}.{2}.{3}'.format(
loaded_base_name, _mod_type(path), self.tag, name
), fn_, path, desc
)
# reload all submodules if necessary
submodules = [
getattr(mod, sname) for sname in dir(mod) if
isinstance(getattr(mod, sname), mod.__class__)
]
# reload only custom "sub"modules i.e is a submodule in
# parent module that are still available on disk (i.e. not
# removed during sync_modules)
for submodule in submodules:
try:
smname = '{0}.{1}.{2}'.format(loaded_base_name, self.tag, name)
smfile = os.path.splitext(submodule.__file__)[0] + ".py"
if submodule.__name__.startswith(smname) and os.path.isfile(smfile):
reload(submodule)
except AttributeError:
continue
except ImportError as exc:
log.debug('Failed to import module {0}, this is most likely '
'NOT a problem: {1}'.format(name, exc))
continue
except Exception:
trb = traceback.format_exc()
log.warning('Failed to import module {0}, this is due most '
'likely to a syntax error: {1}'.format(name, trb))
continue
modules.append(mod)
for mod in modules:
virtual = ''
if hasattr(mod, '__opts__'):
mod.__opts__.update(self.opts)
#.........这里部分代码省略.........