Juan Hernandez has uploaded a new change for review.

Change subject: sdk: Replace decorators before marshal
......................................................................

sdk: Replace decorators before marshal

The code generated by the generateDS.py tool expects that all the
objects implement the "export" method that generates the XML text. The
entity and collection decorators don't implement this methods, but the
user may use them in places where simple parameter objects are expected.
To avoid issues with this we had an implementation of the "__setattr__"
method in the base class of parameter objects that replaces references
to decorators with the corresponding parameter objects. But this doesn't
work when the references are set without calling the __setattr__ method,
for example when adding an element to a list. To avoid this issue this
patch removes the __setattr__ method and instead of that the replacement
is executed recusively right before doing the conversion to XML.

Change-Id: I7843dea85c9f09de5fb3439f57dba71ce6a28898
Bug-Url: https://bugzilla.redhat.com/1024696
Signed-off-by: Juan Hernandez <[email protected]>
(cherry picked from commit 06862abc036073620ae1c724b758309e8ac98c54)
---
M src/codegen/xsd/paramsconf.py
M src/ovirtsdk/utils/parsehelper.py
2 files changed, 43 insertions(+), 52 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk refs/changes/66/28366/1

diff --git a/src/codegen/xsd/paramsconf.py b/src/codegen/xsd/paramsconf.py
index ee1bd9c..8e12f16 100644
--- a/src/codegen/xsd/paramsconf.py
+++ b/src/codegen/xsd/paramsconf.py
@@ -31,54 +31,6 @@
 # attributes of certain class, will be append to this calls in params.py
 _generatedsSuperAttribute = """
 # Begin NOT_GENERATED
-def __setattr__(self, item, value):
-    if (value is not None and
-        not isinstance(value, list) and
-        ReflectionHelper.isModuleMember(
-            sys.modules['ovirtsdk.infrastructure.brokers'],
-            type(value)) and
-        not ReflectionHelper.isModuleMember(sys.modules[__name__],
-            type(value)) and
-        value.__dict__.has_key('superclass') and
-        value.superclass is not None and
-        value.superclass != BaseResource):
-        if (ReflectionHelper.isModuleMember(
-                sys.modules['ovirtsdk.infrastructure.brokers'],
-                type(self)) and
-           self.__dict__.has_key('superclass') and
-           self.superclass is not None):
-            object.__setattr__(self.superclass, item, value.superclass)
-        else:
-            object.__setattr__(self, item, value.superclass)
-    elif (not isinstance(value, list) and
-         ReflectionHelper.isModuleMember(
-                 sys.modules['ovirtsdk.infrastructure.brokers'],
-                 type(self)) and
-         self.__dict__.has_key('superclass') and
-         self.superclass is not None and
-         not ReflectionHelper.isModuleMember(
-                 sys.modules['ovirtsdk.infrastructure.brokers'],
-                 type(value)) and
-         item is not 'superclass' and
-         item is not 'parentclass'):
-        object.__setattr__(self.superclass, item, value)
-    elif isinstance(value, list):
-        parsed_list = []
-        for obj in value:
-            if (ReflectionHelper.isModuleMember(
-                    sys.modules['ovirtsdk.infrastructure.brokers'],
-                    type(obj)) and
-               obj.__dict__.has_key('superclass') and
-               obj.superclass is not None and
-               item is not 'superclass' and
-               item is not 'parentclass'):
-                parsed_list.append(obj.superclass)
-            else:
-                parsed_list.append(obj)
-        object.__setattr__(self, item, parsed_list)
-    else:
-        object.__setattr__(self, item, value)
-
 def __eq__(self, other):
     return Comparator.compare(self, other)
 
diff --git a/src/ovirtsdk/utils/parsehelper.py 
b/src/ovirtsdk/utils/parsehelper.py
index 122df67..6f70486 100644
--- a/src/ovirtsdk/utils/parsehelper.py
+++ b/src/ovirtsdk/utils/parsehelper.py
@@ -15,20 +15,59 @@
 #
 
 import StringIO
-from ovirtsdk.xml import params
+from ovirtsdk.infrastructure import common
 from ovirtsdk.utils.reflectionhelper import ReflectionHelper
+from ovirtsdk.xml import params
 import sys
 
 class ParseHelper():
     '''Provides parsing capabilities'''
 
     @staticmethod
+    def replaceDecorators(obj):
+        """
+        Returns the object that replaces the given one. If the object is a
+        resource decorator then it will be replaced by the corresponding
+        parameters object. Collection decorators will be replaced by None, as
+        then can't be serialized.
+        """
+
+        # Parameter objects don't need to be replaced themselves, but its
+        # attributes do:
+        if isinstance(obj, params.GeneratedsSuper):
+            for attr_name, attr_value in obj.__dict__.iteritems():
+                obj.__dict__[attr_name] = 
ParseHelper.replaceDecorators(attr_value)
+            return obj
+
+        # Resource decorators are replaced by the reference to the parameters
+        # object that they store in the "superclass" attribute, collections 
don't
+        # have this attribute, so they are replaced by None:
+        if isinstance(obj, common.Base):
+            try:
+               replacement = obj.superclass
+            except AttributeError:
+               replacement = None
+            return replacement
+
+        # Lists have to be iterated and the elements replaced:
+        if isinstance(obj, list):
+            replacement = []
+            for element in obj:
+                replacement.append(ParseHelper.replaceDecorators(element))
+            return replacement
+
+        # Other types of object don't need any replacement:
+        return obj
+
+    @staticmethod
     def toXml(entity):
         '''Parse entity to corresponding XML representation'''
 
-        if 
ReflectionHelper.isModuleMember(sys.modules['ovirtsdk.infrastructure.brokers'],
-                                           type(entity)) and hasattr(entity, 
'superclass'):
-            entity = entity.superclass
+        # The entity given may be a decorator, or it may contain a decorator
+        # deeply nested. Those decorators can't be serialized to XML, so we
+        # need to replace all references to decorators with the corresponding
+        # parameter objects:
+        entity = ParseHelper.replaceDecorators(entity)
 
         type_name = type(entity).__name__.lower()
         output = StringIO.StringIO()


-- 
To view, visit http://gerrit.ovirt.org/28366
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7843dea85c9f09de5fb3439f57dba71ce6a28898
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-sdk
Gerrit-Branch: sdk_3.5
Gerrit-Owner: Juan Hernandez <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to