def CisRpc(outname, outfmt, inname, infmt, matlab=False): r"""Get class for sending a message and then receiving a response. Args: outname (str): The name of the output message queue. outfmt (str): Format string used to format variables in a message sent to the output message queue. inname (str): The name of the input message queue. infmt (str): Format string used to recover variables from messages received from the input message queue. Returns: DefaultComm: Communication object. """ from cis_interface.communication import RPCComm if matlab: # pragma: matlab infmt = backwards.decode_escape(infmt) outfmt = backwards.decode_escape(outfmt) icomm_kwargs = dict(format_str=infmt) ocomm_kwargs = dict(format_str=outfmt) if inname == outname: name = inname else: name = '%s_%s' % (inname, outname) icomm_kwargs['name'] = inname ocomm_kwargs['name'] = outname out = RPCComm.RPCComm(name, icomm_kwargs=icomm_kwargs, ocomm_kwargs=ocomm_kwargs, is_interface=True, recv_timeout=False, matlab=matlab) return out
def CisRpcClient(name, outfmt='%s', infmt='%s', matlab=False): r"""Get class for handling requests and response to an RPC Server from a client. Args: name (str): The name of the server queues. outfmt (str, optional): Format string used to format variables in a message sent to the request queue. Defautls to '%s'. infmt (str, optional): Format string used to recover variables from messages received from the response queue. Defautls to '%s'. Returns: ClientComm: Communication object. """ from cis_interface.communication import ClientComm if matlab: # pragma: matlab infmt = backwards.decode_escape(infmt) outfmt = backwards.decode_escape(outfmt) icomm_kwargs = dict(format_str=infmt) ocomm_kwargs = dict(format_str=outfmt) out = ClientComm.ClientComm(name, response_kwargs=icomm_kwargs, is_interface=True, recv_timeout=False, matlab=matlab, **ocomm_kwargs) return out
def CisOutput(name, format_str=None, matlab=False): r"""Get class for handling output to a message queue. Args: name (str): The name of the message queue. Combined with the suffix '_OUT', it should match an environment variable containing a message queue key. format_str (str, optional): C style format string that should be used to create a message from a list of python ojbects. Defaults to None and raw string messages are sent. Returns: DefaultComm: Communication object. """ if matlab and format_str is not None: # pragma: matlab format_str = backwards.decode_escape(format_str) return DefaultComm(name, direction='send', format_str=format_str, is_interface=True, recv_timeout=False, matlab=matlab)
def CisAsciiTableOutput(name, fmt, as_array=False, dst_type=1, matlab=False, **kwargs): r"""Get class for handling table-like formatted output. Args: name (str): The path to the local file where output should be saved (if dst_type == 0) or the name of the message queue where the output should be sent. fmt (str): A C style format string specifying how each 'row' of output should be formated. This should include the newline character. as_array (bool, optional): If True, send expects and entire array. If False, send expects the entries for one table row. Defaults to False. dst_type (int, optional): If 0, output is sent to a local file. Otherwise, the output is sent to a message queue. Defaults to 1. **kwargs: Additional keyword arguments are passed to the base comm. Returns: DefaultComm: Communication object. """ if matlab: # pragma: matlab fmt = backwards.decode_escape(fmt) if dst_type == 0: from cis_interface.communication import AsciiTableComm base = AsciiTableComm.AsciiTableComm kwargs.setdefault('address', name) kwargs.update(as_array=as_array, format_str=fmt) else: base = DefaultComm kwargs['serializer_kwargs'] = dict(as_array=as_array, format_str=fmt) kwargs.setdefault('direction', 'send') out = base(name, is_interface=True, recv_timeout=False, matlab=matlab, **kwargs) return out
def test_decode_escape(): r"""Test esscape decoding.""" s = 'hello\\nhello' ans = 'hello\nhello' nt.assert_equal(backwards.decode_escape(s), ans)