Add convenience call function to loader

This commit is contained in:
Thomas S Hatch 2011-04-22 22:56:59 -06:00
parent 1f499bd475
commit 03eb195d13

View File

@ -34,6 +34,16 @@ def returners(opts):
load = Loader(module_dirs, opts) load = Loader(module_dirs, opts)
return load.filter_func('returner') return load.filter_func('returner')
def call(fun, arg=[], dirs=[]):
'''
Directly call a function inside a loader directory
'''
module_dirs = [
os.path.join(distutils.sysconfig.get_python_lib(), 'salt/modules'),
] + dirs
load = Loader(module_dirs)
return load.call(fun, args)
class Loader(object): class Loader(object):
''' '''
@ -66,18 +76,17 @@ class Loader(object):
docs[fun] = funcs[fun].__doc__ docs[fun] = funcs[fun].__doc__
return docs return docs
def call(self, fun, arg=[], dirs=[]): def call(self, fun, arg=[]):
''' '''
Call a function in the load path. Call a function in the load path.
''' '''
dirs += self.module_dirs
name = fun[:fun.rindex('.')] name = fun[:fun.rindex('.')]
try: try:
fn_, path, desc = imp.find_module(name, dirs) fn_, path, desc = imp.find_module(name, module_dirs)
mod = imp.load_module(name, fn_, path, desc) mod = imp.load_module(name, fn_, path, desc)
except ImportError: except ImportError:
# The module was not found, try to find a cython module # The module was not found, try to find a cython module
for mod_dir in dirs: for mod_dir in module_dirs:
for fn_ in os.listdir(mod_dir): for fn_ in os.listdir(mod_dir):
if name == fn_[:fn_.rindex('.')]: if name == fn_[:fn_.rindex('.')]:
# Found it, load the mod and break the loop # Found it, load the mod and break the loop