Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
commit 46c6d40785292a39b6d0acaf590cd0fdf66c469f Author: Peter Hatina phatina@redhat.com Date: Fri Aug 9 10:16:21 2013 +0200
cast constant values to native python types
cli/lmi/shell/LMIConstantValues.py | 14 +++++----- cli/lmi/shell/LMIUtil.py | 48 +++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 13 deletions(-)
diff --git a/cli/lmi/shell/LMIConstantValues.py b/cli/lmi/shell/LMIConstantValues.py index 6e564ac..002c594 100644 --- a/cli/lmi/shell/LMIConstantValues.py +++ b/cli/lmi/shell/LMIConstantValues.py @@ -17,7 +17,7 @@ import abc import sys
from LMIBaseObject import LMIBaseObject -from LMIUtil import lmi_cast_to_cim +from LMIUtil import lmi_cast_to_lmi
class LMIConstantValues(LMIBaseObject): """ @@ -25,7 +25,7 @@ class LMIConstantValues(LMIBaseObject): """ __metaclass__ = abc.ABCMeta
- def __init__(self, cim_obj): + def __init__(self, cim_obj, cast_type): """ Constructs a object.
@@ -34,6 +34,7 @@ class LMIConstantValues(LMIBaseObject): CIMMethod. Construction of this object requires to have a member "_cast_type" to properly cast CIM object. When constructing defived objects, make sure, that the mentioned member is present before calling this constructor. + cast_type -- parameter/property cast type """ # Keys can contain various undesirable characters, such as python # operators, etc. So we drop them. @@ -42,11 +43,12 @@ class LMIConstantValues(LMIBaseObject): values = cim_obj.qualifiers["ValueMap"].value self._value_map = {} self._value_map_inv = {} + self._cast_type = cast_type # Fill two dictionaries for bidirectional access to constant values. for i in range(0, len(keys)): try: key = keys[i] - val = lmi_cast_to_cim(self._cast_type, values[i]) + val = lmi_cast_to_lmi(self._cast_type, values[i]) self._value_map[key] = val self._value_map_inv[val] = key except ValueError, e: @@ -128,8 +130,7 @@ class LMIConstantValuesParamProp(LMIConstantValues): cim_property -- CIMProperty or CIMParameter object. Both objects have necessary member "type" which is needed for proper casting. """ - self._cast_type = cim_property.type - super(LMIConstantValuesParamProp, self).__init__(cim_property) + super(LMIConstantValuesParamProp, self).__init__(cim_property, cim_property.type)
class LMIConstantValuesMethodReturnType(LMIConstantValues): """ @@ -142,5 +143,4 @@ class LMIConstantValuesMethodReturnType(LMIConstantValues): Arguments: cim_method -- CIMMethod object """ - self._cast_type = cim_method.return_type - super(LMIConstantValuesMethodReturnType, self).__init__(cim_method) + super(LMIConstantValuesMethodReturnType, self).__init__(cim_method, cim_method.return_type) diff --git a/cli/lmi/shell/LMIUtil.py b/cli/lmi/shell/LMIUtil.py index 5954b96..990ac8d 100644 --- a/cli/lmi/shell/LMIUtil.py +++ b/cli/lmi/shell/LMIUtil.py @@ -124,6 +124,27 @@ def lmi_raise_or_dump_exception(e=None): else: raise type(e), e, tb
+def _lmi_do_cast(t, value, cast): + """ + Helper function, which preforms the actual cast. + + Arguments: + t -- string of CIM type + value -- variable to cast + cast -- dictionary with type:cast_func + """ + cast_func = cast[t.lower()] if t.lower() in cast else lambda x: x + if isinstance(value, dict): + return dict( + zip(value.keys()), + map(lambda val: _lmi_do_cast(t, val, cast), value.values()) + ) + elif isinstance(value, list): + return map(lambda val: _lmi_do_cast(t, val, cast), value) + elif isinstance(value, tuple): + return tuple(map(lambda val: _lmi_do_cast(t, val, cast), value)) + return cast_func(value) + def lmi_cast_to_cim(t, value): """ Casts the value to CIM type. @@ -143,12 +164,27 @@ def lmi_cast_to_cim(t, value): "uint64" : lambda x: pywbem.Uint64(x), "reference": lambda x: x.path if isinstance(x, LMIObjectFactory().LMIInstance) else x } - cast_func = cast[t] if t.lower() in cast else lambda x: x - if isinstance(value, list): - return map(cast_func, value) - elif isinstance(value, tuple): - return tuple(map(cast_func, value)) - return cast_func(value) + return _lmi_do_cast(t, value, cast) + +def lmi_cast_to_lmi(t, value): + """ + Casts the value to LMI (python) type. + + Arguments: + t -- string of CIM type + value -- variable to cast + """ + cast = { + "sint8" : lambda x: int(x), + "uint8" : lambda x: int(x), + "sint16" : lambda x: int(x), + "uint16" : lambda x: int(x), + "sint32" : lambda x: int(x), + "uint32" : lambda x: int(x), + "sint64" : lambda x: int(x), + "uint64" : lambda x: int(x), + } + return _lmi_do_cast(t, value, cast)
def lmi_wrap_cim_namespace(conn, cim_namespace_name): """
cura-tools-devel@lists.fedorahosted.org