Skip to content
Snippets Groups Projects
Commit fbfab27b authored by Jan Rinze Peterzon's avatar Jan Rinze Peterzon
Browse files

Task #8531: refactor argument parsing for RPC.

parent 180e4b2e
No related branches found
No related tags found
No related merge requests found
......@@ -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())
......
......@@ -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):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment