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

Lukáš Hrázký lhrazky at redhat.com
Wed May 23 09:13:51 UTC 2018


On Thu, 2018-05-17 at 13:49 +0100, Frediano Ziglio wrote:
> 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.
> 
> Changes since v2:
> - minor syntax updates.
> 
> diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py
> index 0f6d8d6..631739e 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 is 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

Assuming it works :)

Acked-by: Lukáš Hrázký <lhrazky at redhat.com>


More information about the Spice-devel mailing list