[Spice-devel] [PATCH spice-common v2] codegen: Remove duplicate client and server code from ChannelType::resolve

Frediano Ziglio fziglio at redhat.com
Wed May 16 15:35:56 UTC 2018


Code that handled client and server messages check was the same, just
changed some variable names.
Instead use a class to store same information and reuse the code.
This allows easier extension of the 2 path of code.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 python_modules/ptypes.py | 68 +++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 39 deletions(-)

Changes since v1:
- avoid __dict__ usage.

diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
index 0f6d8d6..fd9b1d0 100644
--- a/python_modules/ptypes.py
+++ b/python_modules/ptypes.py
@@ -1021,56 +1021,46 @@ class ChannelType(Type):
         return self.server_messages_byname[name]
 
     def resolve(self):
-        if self.base != None:
+        class MessagesInfo:
+            def __init__(self, is_server, messages = [], messages_byname = {}):
+                self.is_server = is_server
+                self.messages = messages[:]
+                self.messages_byname = messages_byname.copy()
+                self.count = 1
+
+        if self.base == None:
+            server_info = MessagesInfo(True)
+            client_info = MessagesInfo(False)
+        else:
             self.base = self.base.resolve()
 
-            server_messages = self.base.server_messages[:]
-            server_messages_byname = self.base.server_messages_byname.copy()
-            client_messages = self.base.client_messages[:]
-            client_messages_byname = self.base.client_messages_byname.copy()
+            server_info = MessagesInfo(True, self.base.server_messages,
+                                       self.base.server_messages_byname)
+            client_info = MessagesInfo(False, self.base.client_messages,
+                                       self.base.client_messages_byname)
 
             # Set default member_name, FooChannel -> foo
             self.member_name = self.name[:-7].lower()
-        else:
-            server_messages = []
-            server_messages_byname = {}
-            client_messages = []
-            client_messages_byname = {}
 
-        server_count = 1
-        client_count = 1
-
-        server = True
+        info = server_info
         for m in self.members:
             if m == "server":
-                server = True
+                info = server_info
             elif m == "client":
-                server = False
-            elif server:
-                m.is_server = True
-                m = m.resolve(self)
-                if m.value:
-                    server_count = m.value + 1
-                else:
-                    m.value = server_count
-                    server_count = server_count + 1
-                server_messages.append(m)
-                server_messages_byname[m.name] = m
+                info = client_info
             else:
-                m.is_server = False
+                m.is_server = info.is_server
                 m = m.resolve(self)
-                if m.value:
-                    client_count = m.value + 1
-                else:
-                    m.value = client_count
-                    client_count = client_count + 1
-                client_messages.append(m)
-                client_messages_byname[m.name] = m
-
-        self.server_messages = server_messages
-        self.server_messages_byname = server_messages_byname
-        self.client_messages = client_messages
-        self.client_messages_byname = client_messages_byname
+                if not m.value:
+                    m.value = info.count
+                info.count = m.value + 1
+                info.messages.append(m)
+                info.messages_byname[m.name] = m
+
+        self.server_messages = server_info.messages
+        self.server_messages_byname = server_info.messages_byname
+        self.client_messages = client_info.messages
+        self.client_messages_byname = client_info.messages_byname
 
         return self
 
-- 
2.17.0



More information about the Spice-devel mailing list