Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit 14ead24f27eac8ca7ea543d32100570fd8ff5e00
Author: Peter Hatina <phatina(a)redhat.com>
Date: Mon Feb 3 15:02:07 2014 +0100
allow CIMInstance(Name) or LMIInstance(Name) objects to be passed into CIM methods
---------------------------------------------------------------
cli/lmi/shell/LMIBaseClient.py | 154 +++++++++++++++++++++++++---------------
cli/lmi/shell/LMIUtil.py | 25 +++++++
2 files changed, 121 insertions(+), 58 deletions(-)
diff --git a/cli/lmi/shell/LMIBaseClient.py b/cli/lmi/shell/LMIBaseClient.py
index 6a0f677..a16f9f1 100644
--- a/cli/lmi/shell/LMIBaseClient.py
+++ b/cli/lmi/shell/LMIBaseClient.py
@@ -17,9 +17,9 @@ import sys
import pywbem
import urlparse
-from LMIObjectFactory import LMIObjectFactory
from LMIReturnValue import LMIReturnValue
from LMIUtil import lmi_raise_or_dump_exception
+from LMIUtil import lmi_instance_to_path
from LMIExceptions import LMIFilterError
@@ -142,7 +142,15 @@ class LMIBaseClient(object):
"""
Returns a :py:class:`CIMInstance` object.
- :param CIMInstanceName path: path of the object, which is about to be retrieved
+ :param path: path of the object, which is about to be retrieved. The object
needs
+ to be instance of following classes:
+
+ * :py:class:`CIMInstanceName`
+ * :py:class:`CIMInstance`
+ * :py:class:`LMIInstanceName`
+ * :py:class:`LMIInstance`
+
+
:param bool LocalOnly: indicates if to include the only elements (properties,
methods, references) overridden or defined in the class
:param bool IncludeQualifiers: indicates, if all Qualifiers for the class and
its
@@ -162,11 +170,11 @@ class LMIBaseClient(object):
:returns: :py:class:`LMIReturnValue` object, where ``rval`` is set to
:pyclass:`CIMInstance` object, if no error occurs; otherwise ``errorstr`` is
set
to corresponding error string
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
+ :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`,
+ :py:exc:`TypeError`
"""
- if isinstance(path, LMIObjectFactory().LMIInstanceName):
- path = path.wrapped_object
try:
+ path = lmi_instance_to_path(path)
inst = self._cliconn.GetInstance(path, **kwargs)
except pywbem.cim_operations.CIMError, e:
lmi_raise_or_dump_exception(e)
@@ -358,21 +366,28 @@ class LMIBaseClient(object):
def _call_method_raw(self, instance, method, **params):
"""
- Executes a method within a given :py:class:`CIMInstance`.
+ Executes a method within a given instance.
+
+ :param instance: object, on which the method will be executed. The object needs
to
+ be instance of following classes:
+
+ * :py:class:`CIMInstance`
+ * :py:class:`CIMInstanceName`
+ * :py:class:`LMIInstance`
+ * :py:class:`LMIInstanceName`
- :param CIMInstance instance: object, on which the method will be issued
:param string method: string containing a method name
:param dictionary params: parameters passed to the method call
:returns: :py:class:`LMIReturnValue` object with rval set to return value of the
method call, rparams set to returned parameters from the method call, if no
error occurs; otherwise rval is set to -1 and errorstr to appropriate error
string
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
+ :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`,
+ :py:exc:`TypeError`
"""
- if isinstance(instance, LMIObjectFactory().LMIInstance):
- instance = instance.wrapped_object
try:
- (rval, rparams) = self._cliconn.InvokeMethod(method, instance.path,
**params)
+ path = lmi_instance_to_path(instance)
+ (rval, rparams) = self._cliconn.InvokeMethod(method, path, **params)
except pywbem.cim_operations.CIMError, e:
lmi_raise_or_dump_exception(e)
errorstr = e.args[1] + ": '" + method + "'"
@@ -383,29 +398,20 @@ class LMIBaseClient(object):
return LMIReturnValue(rval=-1, errorstr=errorstr)
return LMIReturnValue(rval=rval, rparams=rparams)
- def _call_method(self, instance, method, **params):
- """
- Executes a method within a given :py:class:`CIMInstance`.
-
- :param CIMInstance instance: object, on which the method will be issued
- :param string method: a method name
- :param dictionary params: parameters passed to the method call
- :returns: :py:class:`LMIReturnValue` object with ``rval`` set to True,
- rparams set to returned parameters from the method call, if no
- error occurs; otherwise ``rval`` is set to False and ``errorstr``
- to appropriate error string
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
- """
- (rval, rparams, errorstr) = self._call_method_raw(instance, method, **params)
- return LMIReturnValue(rval=(rval == 0), rparams=rparams, errorstr=errorstr)
-
def _get_associator_names(self, instance, **kwargs):
"""
Returns a list of associated :py:class:`CIMInstanceName` objects with an input
instance.
- :param CIMInstance instance: for this object the list of associated
- :py:class:`CIMInstanceName` will be returned
+ :param instance: for this object the list of associated
+ :py:class:`CIMInstanceName` will be returned. The object needs to be instance
of
+ following classes:
+
+ * :py:class:`CIMInstance`
+ * :py:class:`CIMInstanceName`
+ * :py:class:`LMIInstance`
+ * :py:class:`LMIInstanceName`
+
:param string AssocClass: valid CIM association class name. It acts as a filter
on
the returned set of names by mandating that each returned name identify an
object that shall be associated to the source object through an instance of
@@ -429,23 +435,30 @@ class LMIBaseClient(object):
:returns: list of associated :py:class:`CIMInstanceName` objects with
an input instance, if no error occurs; otherwise en empty list is
returned
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
+ :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`,
+ :py:exc:`TypeError`
"""
- if isinstance(instance, LMIObjectFactory().LMIInstance):
- instance = instance.wrapped_object
try:
- return self._cliconn.AssociatorNames(instance.path, **kwargs)
+ path = lmi_instance_to_path(instance)
+ return self._cliconn.AssociatorNames(path, **kwargs)
except (pywbem.cim_operations.CIMError, pywbem.cim_http.AuthError), e:
lmi_raise_or_dump_exception(e)
return []
def _get_associators(self, instance, **kwargs):
"""
- Returns a list of associated :py:class:`CIMInstance` objects with an
- input instance.
+ Returns a list of associated :py:class:`CIMInstance` objects with an input
+ instance.
+
+ :param instance: for this object the list of associated :py:class:`CIMInstance`
+ objects will be returned. The object needs to be instance of following
+ classes:
+
+ * :py:class:`CIMInstance`
+ * :py:class:`CIMInstanceName`
+ * :py:class:`LMIInstance`
+ * :py:class:`LMIInstanceName`
- :param CIMInstance instance: for this object the list of associated
- :py:class:`CIMInstance` objects will be returned
:param string AssocClass: valid CIM association class name. It acts as a filter
on
the returned set of objects by mandating that each returned object shall be
associated to the source object through an instance of this class or one of
@@ -479,22 +492,30 @@ class LMIBaseClient(object):
filtering is defined. Default value is None.
:returns: list of associated :py:class:`CIMInstance` objects with an input
instance, if no error occurs; otherwise an empty list is returned
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
+ :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`,
+ :py:exc:`TypeError`
"""
- if isinstance(instance, LMIObjectFactory().LMIInstance):
- instance = instance.wrapped_object
try:
- return self._cliconn.Associators(instance.path, **kwargs)
+ path = lmi_instance_to_path(instance)
+ return self._cliconn.Associators(path, **kwargs)
except (pywbem.cim_operations.CIMError, pywbem.cim_http.AuthError), e:
lmi_raise_or_dump_exception(e)
return []
def _get_reference_names(self, instance, **kwargs):
"""
- Returns a list of association :py:class:`CIMInstanceName` objects.
+ Returns a list of association :py:class:`CIMInstanceName` objects with an input
+ instance.
+
+ :param instance: for this object the association :py:class:`CIMInstanceName`
+ objects will be returned. The object needs to be instance of following
+ classes:
+
+ * :py:class:`CIMInstance`
+ * :py:class:`CIMInstanceName`
+ * :py:class:`LMIInstance`
+ * :py:class:`LMIInstanceName`
- :param CIMInstance instance: for this object the association
- :py:class:`CIMInstanceName` objects will be returned
:param string ResultClass: valid CIM class name. It acts as a filter on the
returned set of object names by mandating that each returned Object Name
identify
an instance of this class (or one of its subclasses) or this class (or one of
its
@@ -505,22 +526,30 @@ class LMIBaseClient(object):
matches the value of this parameter.
:returns: list of association :py:class:`CIMInstanceName` objects with an input
instance, if no error occurs; otherwise an empty list is returned
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
+ :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`,
+ :py:exc:`TypeError`
"""
- if isinstance(instance, LMIObjectFactory().LMIInstance):
- instance = instance.wrapped_object
try:
- return self._cliconn.ReferenceNames(instance.path, **kwargs)
+ path = lmi_instance_to_path(instance)
+ return self._cliconn.ReferenceNames(path, **kwargs)
except (pywbem.cim_operations.CIMError, pywbem.cim_http.AuthError), e:
lmi_raise_or_dump_exception(e)
return []
def _get_references(self, instance, **kwargs):
"""
- Returns a list of association :py:class:`CIMInstance` objects.
+ Returns a list of association :py:class:`CIMInstance` objects with an input
+ instance.
+
+ :param instance: for this object the list of association
+ :py:class:`CIMInstance` objects will be returned. The object needs to be
+ instance of following classes:
+
+ * :py:class:`CIMInstance`
+ * :py:class:`CIMInstanceName`
+ * :py:class:`LMIInstance`
+ * :py:class:`LMIInstanceName`
- :param CIMInstance instance: for this object the list of association
- :py:class:`CIMInstance` objects will be returned
:param string ResultClass: valid CIM class name. It acts as a filter on the
returned set of objects by mandating that each returned object shall be an
instance of this class (or one of its subclasses) or this class (or one of
its
@@ -543,12 +572,12 @@ class LMIBaseClient(object):
additional filtering is defined. Default value is None.
:returns: list of association :py:class:`CIMInstance` objects with an input
instance, if no error occurs; otherwise an empty list is returned
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
+ :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`,
+ :py:exc:`TypeError`
"""
- if isinstance(instance, LMIObjectFactory().LMIInstance):
- instance = instance.wrapped_object
try:
- return self._cliconn.References(instance.path, **kwargs)
+ path = lmi_instance_to_path(instance)
+ return self._cliconn.References(path, **kwargs)
except (pywbem.cim_operations.CIMError, pywbem.cim_http.AuthError), e:
lmi_raise_or_dump_exception(e)
return []
@@ -615,14 +644,23 @@ class LMIBaseClient(object):
"""
Deletes a :py:class:`CIMInstance` from the CIMOM side.
- :param CIMInstance instance: object to be deleted
+ :param instance: object to be deleted. The object needs to be instance of
+ following classes:
+
+ * :py:class:`CIMInstance`
+ * :py:class:`CIMInstanceName`
+ * :py:class:`LMIInstance`
+ * :py:class:`LMIInstanceName`
+
:returns: :py:class:`LMIReturnValue` object with ``rval`` set to 0, if no error
occurs; otherwise ``rval`` is set to -1 and ``errorstr`` is set to
corresponding error string
- :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`
+ :raises: :py:exc:`pywbem.CIMError`, :py:exc:`pywbem.AuthError`,
+ :py:exc:`TypeError`
"""
try:
- self._cliconn.DeleteInstance(instance)
+ path = lmi_instance_to_path(instance)
+ self._cliconn.DeleteInstance(path)
except pywbem.cim_operations.CIMError, e:
lmi_raise_or_dump_exception(e)
return LMIReturnValue(rval=e.args[0], errorstr=e.args[1])
diff --git a/cli/lmi/shell/LMIUtil.py b/cli/lmi/shell/LMIUtil.py
index 2122c78..c615302 100644
--- a/cli/lmi/shell/LMIUtil.py
+++ b/cli/lmi/shell/LMIUtil.py
@@ -400,3 +400,28 @@ def lmi_associators(assoc_classes):
result.append(tuple(ref_list))
return result
+
+def lmi_instance_to_path(instance):
+ """
+ Helper function, which returns :py:class:`CIMInstanceName` extracted out of
+ input instance.
+
+ :param instance: object, which can be instance of following classes:
+
+ * :py:class:`CIMInstance`
+ * :py:class:`CIMInstanceName`
+ * :py:class:`LMIInstance`
+ * :py:class:`LMIInstanceName`
+
+ :returns: extracteed :py:class:`CIMInstanceName` object
+ :raises: :py:exc:`TypeError`
+ """
+ if isinstance(instance, pywbem.CIMInstance):
+ return instance.path
+ elif isinstance(instance, pywbem.CIMInstanceName):
+ return instance
+ elif isinstance(instance, LMIObjectFactory().LMIInstance):
+ return instance.wrapped_object.path
+ elif isinstance(instance, LMIObjectFactory().LMIInstanceName):
+ return instance.wrapped_object
+ raise TypeError("instance has to be pywbem.CIMInstance(Name) or
LMIInstance(Name)")