[Kimchi-devel] [PATCH 1/4] improve controller: add a method to load root sub collections/resouces automatically

Zhou Zheng Sheng zhshzhou at linux.vnet.ibm.com
Mon Jan 20 08:30:13 UTC 2014


on 2014/01/20 14:00, shaohef at linux.vnet.ibm.com wrote:
> From: ShaoHe Feng <shaohef at 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 at 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..6d3f2ad 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_sub_node_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, except_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, except_attr):
> +                continue
> +            name = getattr(node, except_attr)["name"]
> +            sub_nodes.update({name: node})
> +
> +    return sub_nodes
> 

It seems except_attr should be renamed to expect_attr.

-- 
Thanks and best regards!

Zhou Zheng Sheng / 周征晟
E-mail: zhshzhou at linux.vnet.ibm.com
Telephone: 86-10-82454397




More information about the Kimchi-devel mailing list