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

Frediano Ziglio fziglio at redhat.com
Mon May 14 22:18:52 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 | 66 +++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 38 deletions(-)

diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
index 0f6d8d6..ef580aa 100644
--- a/python_modules/ptypes.py
+++ b/python_modules/ptypes.py
@@ -1024,53 +1024,43 @@ class ChannelType(Type):
         if self.base != None:
             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()
-
             # 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
+        class MessagesInfo:
+            def __init__(self, base, is_server):
+                self.is_server = is_server
+                if base != None:
+                    kind = 'server' if is_server else 'client'
+                    self.messages = base.__dict__[kind + '_messages'][:]
+                    self.messages_byname = base.__dict__[kind + '_messages_byname'].copy()
+                else:
+                    self.messages = []
+                    self.messages_byname = {}
+                self.count = 1
+
+        server_info = MessagesInfo(self.base, True)
+        client_info = MessagesInfo(self.base, False)
 
-        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