[cura-tools] master: introduce lmi_associators() (5cd5cb2)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 5cd5cb2cce891892fb04a5599387a3928787f954
Author: Peter Hatina <phatina(a)redhat.com>
Date: Mon Sep 30 07:52:00 2013 +0200
introduce lmi_associators()
This function should speed up associations traversal by pulling all the instances referenced by association class and joining them by hand.
>---------------------------------------------------------------
cli/lmi/shell/LMIConsole.py | 2 +
cli/lmi/shell/LMIUtil.py | 59 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 0 deletions(-)
diff --git a/cli/lmi/shell/LMIConsole.py b/cli/lmi/shell/LMIConsole.py
index bebcce3..13e74e4 100644
--- a/cli/lmi/shell/LMIConsole.py
+++ b/cli/lmi/shell/LMIConsole.py
@@ -37,6 +37,7 @@ from LMIConnection import connect as connect_internal
from LMIUtil import lmi_set_use_exceptions
from LMIUtil import lmi_get_use_exceptions
+from LMIUtil import lmi_associators
from LMIUtil import lmi_isinstance
from LMIHelper import LMIHelper
@@ -66,6 +67,7 @@ class LMIConsole(code.InteractiveConsole):
"LMIInstanceName" : LMIObjectFactory().LMIInstanceName,
"LMIMethod" : LMIObjectFactory().LMIMethod,
"use_exceptions" : lmi_set_use_exceptions,
+ "lmi_associators" : lmi_associators,
"lmi_isinstance" : lmi_isinstance,
"help" : LMIHelper()
}
diff --git a/cli/lmi/shell/LMIUtil.py b/cli/lmi/shell/LMIUtil.py
index 94f38b4..4f85db4 100644
--- a/cli/lmi/shell/LMIUtil.py
+++ b/cli/lmi/shell/LMIUtil.py
@@ -15,6 +15,7 @@
import os
import sys
+import hashlib
import pywbem
from LMIObjectFactory import LMIObjectFactory
@@ -301,3 +302,61 @@ def lmi_script_name():
Returns a string of executed script.
"""
return os.path.basename(sys.argv[0])
+
+def lmi_associators(assoc_classes):
+ """
+ Helper function to speed up associator traversal. Returns a list of tuples,
+ where each tuple contains LMIInstance objects, which are in association.
+
+ Arguments:
+ assoc_classes -- list of LMIClass objects, for which the associations
+ will be returned
+ """
+ def make_key(path):
+ path.host = None
+ return hashlib.md5(path.classname + path.namespace + str(path.keybindings)).hexdigest()
+
+ result = []
+
+ instances = {}
+ for assoc_class in assoc_classes:
+ conn = assoc_class._conn
+ assoc_class.fetch()
+
+ # Reference properties
+ ref_props = [
+ ref_prop_name \
+ for (ref_prop_name, ref_prop) in assoc_class._cim_class.properties.iteritems() \
+ if ref_prop.type == "reference"
+ ]
+
+ # Reference class names
+ ref_class_names = [
+ assoc_class._cim_class.properties[ref_prop].reference_class for ref_prop in ref_props
+ ]
+
+ # Get instances, which will be joined as associators
+ for ref_class_name in ref_class_names:
+ if not ref_class_name in instances:
+ (inst_list, out, err) = conn._client._get_instances(ref_class_name)
+ instances.update({ make_key(inst.path) : inst for inst in inst_list })
+
+ # Join associated objects
+ (assoc_instance_names, out, err) = conn._client._get_instance_names(assoc_class.classname)
+ for assoc in assoc_instance_names:
+ ref_list = []
+ for ref_prop in ref_props:
+ path = assoc[ref_prop]
+ # XXX: Some association classes report in key property a class, which its
+ # instances do not refer to.
+ inst = instances.get(make_key(path), None)
+ if inst:
+ ref_list.append(
+ lmi_wrap_cim_instance(conn, inst, inst.classname,
+ inst.path.namespace)
+ )
+
+ if len(ref_list) == len(ref_class_names):
+ result.append(tuple(ref_list))
+
+ return result