
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> load_url_sub_node can load collections/resouces automatically when they are tagged with @urlSubNode Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/utils.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py index 331da91..89eaba2 100644 --- a/src/kimchi/utils.py +++ b/src/kimchi/utils.py @@ -22,6 +22,7 @@ # import cherrypy +import imp import os import subprocess import urllib2 @@ -154,3 +155,41 @@ def run_command(cmd, timeout=None): finally: if timer and not timeout_flag[0]: timer.cancel() + + +class urlSubNode(object): + def __init__(self, name, auth=False): + self.name = name + self.auth = auth + + def __call__(self, fun): + fun._url_sub_node_name = {"name": self.name} + fun.url_auth = self.auth + return fun + + +def listPathModules(path): + modules = set() + for f in os.listdir(path): + base, ext = os.path.splitext(f) + if ext in ('.py', '.pyc', '.pyo'): + modules.add(base) + return sorted(modules) + + +def load_url_sub_node(path, expect_attr="_url_sub_node_name"): + sub_nodes = {} + + for mod_name in listPathModules(path): + if mod_name.startswith("_"): + continue + mod_fobj, mod_absp, mod_desc = imp.find_module(mod_name, [path]) + module = imp.load_module(mod_name, mod_fobj, + mod_absp, mod_desc) + for node in [getattr(module, x) for x in dir(module)]: + if not hasattr(node, expect_attr): + continue + name = getattr(node, expect_attr)["name"] + sub_nodes.update({name: node}) + + return sub_nodes -- 1.8.4.2