[Kimchi-devel] [PATCH 04/22] Create a model to join all model resources implementation

Aline Manera alinefm at linux.vnet.ibm.com
Tue Jan 28 20:21:18 UTC 2014


From: Aline Manera <alinefm at br.ibm.com>

That way we don't need to change tests and controller in order to use
the separated model implementations
All models will be automatically loaded into Model() which will contain
all methods needed by controller.

Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 src/kimchi/control/utils.py |   12 +---------
 src/kimchi/model_/model.py  |   53 +++++++++++++++++++++++++++++++++++++++++++
 src/kimchi/utils.py         |    9 ++++++++
 3 files changed, 63 insertions(+), 11 deletions(-)
 create mode 100644 src/kimchi/model_/model.py

diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py
index 2dd351c..9c6878b 100644
--- a/src/kimchi/control/utils.py
+++ b/src/kimchi/control/utils.py
@@ -25,14 +25,13 @@
 
 import cherrypy
 import json
-import os
 
 
 from jsonschema import Draft3Validator, ValidationError, FormatChecker
 
 
 from kimchi.exception import InvalidParameter
-from kimchi.utils import import_module
+from kimchi.utils import import_module, listPathModules
 
 
 def get_class_name(cls):
@@ -118,15 +117,6 @@ class UrlSubNode(object):
         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, package_name, expect_attr="_url_sub_node_name"):
     sub_nodes = {}
     for mod_name in listPathModules(path):
diff --git a/src/kimchi/model_/model.py b/src/kimchi/model_/model.py
new file mode 100644
index 0000000..709e0bb
--- /dev/null
+++ b/src/kimchi/model_/model.py
@@ -0,0 +1,53 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2013
+#
+# Authors:
+#  Aline Manera <alinefm at linux.vnet.ibm.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import inspect
+import os
+
+from kimchi.basemodel import BaseModel
+from kimchi.model_.libvirtconnection import LibvirtConnection
+from kimchi.objectstore import ObjectStore
+from kimchi.utils import import_module, listPathModules
+
+
+class Model(BaseModel):
+    def __init__(self, libvirt_uri='qemu:///system', objstore_loc=None):
+        self.objstore = ObjectStore(objstore_loc)
+        self.conn = LibvirtConnection(libvirt_uri)
+        kargs = {'objstore': self.objstore, 'conn': self.conn}
+
+        this = os.path.basename(__file__)
+        this_mod = os.path.splitext(this)[0]
+
+        models = []
+        for mod_name in listPathModules(os.path.dirname(__file__)):
+            if mod_name.startswith("_") or mod_name == this_mod:
+                continue
+
+            module = import_module('model_.' + mod_name)
+            members = inspect.getmembers(module, inspect.isclass)
+            for cls_name, instance in members:
+                if inspect.getmodule(instance) == module:
+                    if cls_name.endswith('Model'):
+                        models.append(instance(**kargs))
+
+        return super(Model, self).__init__(models)
diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
index 0e66214..59500dd 100644
--- a/src/kimchi/utils.py
+++ b/src/kimchi/utils.py
@@ -179,3 +179,12 @@ def patch_find_nfs_target(nfs_server):
         target['type'] = 'nfs'
         target['host_name'] = nfs_server
     return targets
+
+
+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)
-- 
1.7.10.4




More information about the Kimchi-devel mailing list