[Kimchi-devel] [PATCH 03/23] refactor model: Create a common model builder
Aline Manera
alinefm at linux.vnet.ibm.com
Wed Jan 29 23:34:44 UTC 2014
From: Aline Manera <alinefm at br.ibm.com>
It will be useful while splitting model and mockmodel into smaller
modules.
It will automatically map all sub-model instance methods to a BaseModel
instance.
Signed-off-by: Aline Manera <alinefm at br.ibm.com>
Signed-off-by: Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>
---
src/kimchi/basemodel.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
create mode 100644 src/kimchi/basemodel.py
diff --git a/src/kimchi/basemodel.py b/src/kimchi/basemodel.py
new file mode 100644
index 0000000..285e86d
--- /dev/null
+++ b/src/kimchi/basemodel.py
@@ -0,0 +1,47 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2014
+#
+# Authors:
+# Zhou Zheng Sheng <zhshzhou 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
+
+
+class BaseModel(object):
+ '''
+ This model squashes all sub-model's public callable methods to itself.
+
+ Model methods are not limited to get_list, create, lookup, update, delete.
+ Controller can call generate_action_handler to generate new actions, which
+ call the related model methods. So all public callable methods of a
+ sub-model should be mapped to this model.
+ '''
+ def __init__(self, model_instances):
+ for model_instance in model_instances:
+ cls_name = model_instance.__class__.__name__
+ if cls_name.endswith('Model'):
+ method_prefix = cls_name[:-len('Model')].lower()
+ else:
+ method_prefix = cls_name.lower()
+
+ callables = [m for m in dir(model_instance)
+ if not m.startswith('_') and
+ callable(getattr(model_instance, m))]
+
+ for member_name in callables:
+ m = getattr(model_instance, member_name, None)
+ setattr(self, '%s_%s' % (method_prefix, member_name), m)
--
1.7.10.4
More information about the Kimchi-devel
mailing list