From fbfab27b9452b3f0d72f8feeb41d68795cd33dcd Mon Sep 17 00:00:00 2001 From: Jan Rinze Peterzon <peterzon@astron.nl> Date: Fri, 6 Nov 2015 12:35:07 +0000 Subject: [PATCH] Task #8531: refactor argument parsing for RPC. --- LCS/Messaging/python/messaging/RPC.py | 24 ++++--------------- LCS/Messaging/python/messaging/messages.py | 27 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/LCS/Messaging/python/messaging/RPC.py b/LCS/Messaging/python/messaging/RPC.py index 72d73d6d271..8e29424b407 100644 --- a/LCS/Messaging/python/messaging/RPC.py +++ b/LCS/Messaging/python/messaging/RPC.py @@ -21,7 +21,7 @@ # RPC invocation with possible timeout from lofar.messaging.messagebus import ToBus, FromBus -from lofar.messaging.messages import ServiceMessage, ReplyMessage +from lofar.messaging.messages import ServiceMessage, ReplyMessage, analyze_args, args_as_content import uuid class RPCException(Exception): @@ -77,7 +77,7 @@ class RPC(): """ self.Request.close() - def __call__(self, *msg, **kwargs): + def __call__(self, *args, **kwargs): """ Enable the use of the object to directly invoke the RPC. @@ -90,24 +90,8 @@ class RPC(): timeout= kwargs.pop("timeout",self.timeout) - Content=list(msg) - HasKwArgs=(len(kwargs)>0) - # more than one argument given? - HasArgs=(len(msg)> 1 ) or (( len(kwargs)>0 ) and (len(msg)>0)) - if HasArgs: - # convert arguments to list - Content = list(msg) - if HasKwArgs: - # if both positional and named arguments then - # we add the kwargs dictionary as the last item in the list - Content.append(kwargs) - else: - if HasKwArgs: - # we have only one named argument - Content=kwargs - else: - # we have only one positional argument - Content=Content[0] + Content=args_as_content(*args,**kwargs) + HasArgs,HasKwArgs = analyze_args(args,kwargs) # create unique reply address for this rpc call options={'create':'always','delete':'receiver'} ReplyAddress= "reply." + str(uuid.uuid4()) diff --git a/LCS/Messaging/python/messaging/messages.py b/LCS/Messaging/python/messaging/messages.py index fab2e94e212..4d0abfcebac 100644 --- a/LCS/Messaging/python/messaging/messages.py +++ b/LCS/Messaging/python/messaging/messages.py @@ -96,6 +96,33 @@ def to_qpid_message(msg): return msg.qpid_msg raise InvalidMessage("Invalid message type: %r" % type(msg)) +def analyze_args(args,kwargs): + HasKwArgs=(len(kwargs)>0) + # more than one argument given? + HasMultipleArgs=(len(args)> 1 ) or (( len(kwargs)>0 ) and (len(args)>0)) + return (HasMultipleArgs,HasKwArgs) + +def args_as_content(*args,**kwargs): + """ + Convert positional args and named args into a message body. + :param msg: Message to be converted into a Qpid message. + :return: Qpid message + :raise InvalidMessage if `msg` cannot be converted into a Qpid message. + """ + HasMultipleArgs,HasKwArgs = analyze_args(args, kwargs) + if HasMultipleArgs: + # convert arguments to list + Content = list(args) + if HasKwArgs: + # if both positional and named arguments then + # we add the kwargs dictionary as the last item in the list + Content.append(kwargs) + return Content + if HasKwArgs: + # we have only one named argument + return kwargs + # we have only one positional argument + return list(args)[0] class MessageFactory(Factory): """ -- GitLab