[PATCH] [WoK 0/2] eliminate code repetition when fetching model instances

From: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> To avoid code duplication across WoK plug-ins, this patch encapsulates the core logic in all 'model.py' files in two utility functions in wok/utils.py to be used by all plug-ins. An example of usage is given in model.py from WoK. Daniel Henrique Barboza (2): wok/utils.py: adding utility 'get model instances' methods model.py: use the new 'get_all_model_instances' utils function src/wok/model/model.py | 22 ++---------------- src/wok/utils.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 20 deletions(-) -- 2.5.5

From: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> To avoid the shameless code repetition we have today widespread across all plug-ins and WoK itself, this patch consolidates the process of retrieving all the model instances from the current dir of a given base (root) model file. 'get_model_instances' is a helper function to retrieve any Model instances from a given module. 'get_all_model_instances' is a function that consolidates the logic all plug-ins up to this date were using to load all the 'Model' instances from the model dir. Refer to the function doc string for more info. Signed-off-by: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> --- src/wok/utils.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/wok/utils.py b/src/wok/utils.py index d6bdf0a..f5f864a 100644 --- a/src/wok/utils.py +++ b/src/wok/utils.py @@ -23,6 +23,7 @@ import cherrypy import glob import grp +import inspect import os import psutil import pwd @@ -311,6 +312,65 @@ def listPathModules(path): return sorted(modules) +def get_model_instances(module_name): + instances = [] + module = import_module(module_name) + members = inspect.getmembers(module, inspect.isclass) + for cls_name, instance in members: + if inspect.getmodule(instance) == module and \ + cls_name.endswith('Model'): + instances.append(instance) + return instances + + +def get_all_model_instances(root_model_name, root_model_file, + kwargs): + """Function that returns all model instances from all modules + found on the same dir as root_model_name module. + + The intended use of this function is to be called from a root + model class that subclasses BaseModel to get all model instances + contained in its dir. This module array would then be used in + the super init call of BaseModel. + + The root model itself is ignored in the return array. + + Args: + root_model_name (str): the python name of the root module. For + example, in WoK case it would be 'wok.model.model'. This + value can be retrieved by calling '__name__' inside the root + model file. + + root_model_file (str): the absolute file name of the root model. + This can be retrived by calling '__file__' in the root model + file. + + kwargs (dict): keyword arguments to be used to initiate the + models found. For example, {'objstore': ...} + + Returns: + array: an array with all module instances found, excluding the + root module itself. + + """ + models = [] + + root_model = os.path.basename(root_model_file) + ignore_mod = os.path.splitext(root_model)[0] + + package_namespace = root_model_name.rsplit('.', 1)[0] + + for mod_name in listPathModules(os.path.dirname(root_model_file)): + if mod_name.startswith("_") or mod_name == ignore_mod: + continue + + instances = get_model_instances(package_namespace + '.' + mod_name) + for instance in instances: + models.append(instance(**kwargs)) + + return models + + def run_setfacl_set_attr(path, attr="r", user=""): set_user = ["setfacl", "--modify", "user:%s:%s" % (user, attr), path] out, error, ret = run_command(set_user) -- 2.5.5

From: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> This patch uses the new get_all_model_instances function to retrieve all WoK model instances. Signed-off-by: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> --- src/wok/model/model.py | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/wok/model/model.py b/src/wok/model/model.py index c5e7630..90202b2 100644 --- a/src/wok/model/model.py +++ b/src/wok/model/model.py @@ -19,12 +19,9 @@ # 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 wok.basemodel import BaseModel from wok.objectstore import ObjectStore -from wok.utils import import_module, listPathModules +from wok.utils import get_all_model_instances class Model(BaseModel): @@ -32,20 +29,5 @@ class Model(BaseModel): self.objstore = ObjectStore(objstore_loc) kargs = {'objstore': self.objstore} - - 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('wok.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)) - + models = get_all_model_instances(__name__, __file__, kargs) return super(Model, self).__init__(models) -- 2.5.5

This patch set is deprecated. Please check the v2. On 08/11/2016 05:48 PM, dhbarboza82@gmail.com wrote:
From: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com>
To avoid code duplication across WoK plug-ins, this patch encapsulates the core logic in all 'model.py' files in two utility functions in wok/utils.py to be used by all plug-ins.
An example of usage is given in model.py from WoK.
Daniel Henrique Barboza (2): wok/utils.py: adding utility 'get model instances' methods model.py: use the new 'get_all_model_instances' utils function
src/wok/model/model.py | 22 ++---------------- src/wok/utils.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 20 deletions(-)
participants (2)
-
Daniel Henrique Barboza
-
dhbarboza82@gmail.com