[cura-tools] master: convert strings to unicode when working with LMIInstance(Name)s (84ee574)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 84ee574ca4c6f7d7ba9dda0a74561a01afa7a01a
Author: Peter Hatina <phatina(a)redhat.com>
Date: Mon Feb 3 11:54:53 2014 +0100
convert strings to unicode when working with LMIInstance(Name)s
>---------------------------------------------------------------
cli/lmi/shell/LMIClass.py | 3 +++
cli/lmi/shell/LMIInstanceName.py | 28 ++++++++++++++++++++++------
cli/lmi/shell/LMIUtil.py | 1 +
3 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/cli/lmi/shell/LMIClass.py b/cli/lmi/shell/LMIClass.py
index 67c21c1..c6265e7 100644
--- a/cli/lmi/shell/LMIClass.py
+++ b/cli/lmi/shell/LMIClass.py
@@ -175,6 +175,9 @@ class LMIClass(LMIWrapperBaseObject):
for key, value in keybindings.iteritems():
if isinstance(value, LMIObjectFactory().LMIInstanceName):
value = value.wrapped_object
+ elif isinstance(value, str):
+ # Convert strings to unicode
+ value = unicode(value, "utf-8")
kbs[key] = value
cim_inst_name = pywbem.CIMInstanceName(self.classname, kbs,
namespace=self.namespace)
diff --git a/cli/lmi/shell/LMIInstanceName.py b/cli/lmi/shell/LMIInstanceName.py
index 01b0e33..ff63837 100644
--- a/cli/lmi/shell/LMIInstanceName.py
+++ b/cli/lmi/shell/LMIInstanceName.py
@@ -28,10 +28,12 @@ class LMIInstanceName(LMIWrapperBaseObject):
:param CIMInstanceName cim_instance_name: wrapped object
"""
def __init__(self, conn, cim_instance_name):
- super(LMIInstanceName, self).__init__(conn)
if isinstance(cim_instance_name, LMIInstanceName):
cim_instance_name = cim_instance_name.wrapped_object
- self._cim_instance_name = cim_instance_name
+ # We use __dict__ to avoid recursion potentially caused by
+ # combo __setattr__ and __getattr__
+ self.__dict__["_cim_instance_name"] = cim_instance_name
+ super(LMIInstanceName, self).__init__(conn)
def __cmp__(self, other):
"""
@@ -53,22 +55,36 @@ class LMIInstanceName(LMIWrapperBaseObject):
def __getattr__(self, name):
"""
- Simplifies the code. Key properties values can be retrieved by
- :samp:`object.key_property`.
+ Returns class member or key property.
:param string name: class member or key property name
:returns: class member or key property
+ :raises: :py:exc:`AttributeError`
"""
if name in self.__dict__:
return self.__dict__[name]
- key_props = self.key_properties()
- if key_props and name in key_props:
+ if name in self._cim_instance_name.keys():
member = self._cim_instance_name[name]
if isinstance(member, pywbem.CIMInstanceName):
member = lmi_wrap_cim_instance_name(self._conn, member)
return member
raise AttributeError(name)
+ def __setattr__(self, name, value):
+ """
+ Assigns a new value to class member or key property.
+
+ :param string name: class member or key property name
+ :param value: new value to assign
+ """
+ if name in self._cim_instance_name.keys():
+ # Convert string value into unicode
+ if isinstance(value, str):
+ value = unicode(value, "utf-8")
+ self._cim_instance_name[name] = value
+ else:
+ self.__dict__[name] = value
+
def __str__(self):
"""
:returns: string containing object path
diff --git a/cli/lmi/shell/LMIUtil.py b/cli/lmi/shell/LMIUtil.py
index 69cbf75..2122c78 100644
--- a/cli/lmi/shell/LMIUtil.py
+++ b/cli/lmi/shell/LMIUtil.py
@@ -157,6 +157,7 @@ def lmi_cast_to_cim(t, value):
"uint32" : lambda x: pywbem.Uint32(x),
"sint64" : lambda x: pywbem.Sint64(x),
"uint64" : lambda x: pywbem.Uint64(x),
+ "string" : lambda x: unicode(x, "utf-8") if isinstance(x, str) else x,
"reference": lambda x: x.path if isinstance(x, LMIObjectFactory().LMIInstance) else x
}
return _lmi_do_cast(t, value, cast)
10 years, 3 months