ZIO
Python and C++ interface to ZeroMQ and Zyre
util.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import json
4 
5 def objectify(morl):
6  '''
7  Return a flow object.
8 
9  The morl may be a zio.Message or a zio.Message.label
10 
11  See also zio.Message.label_object
12  '''
13  if not morl:
14  return dict()
15  if type(morl) is bytes:
16  morl = morl.decode('utf-8')
17  if type(morl) is str:
18  return json.loads(morl)
19  return objectify(morl.prefix.label)
20 
21 def stringify(flowtype, **params):
22  '''
23  Return a flow label string of given flow type and any extra
24  parameters.
25  '''
26  params = params or dict()
27  params['flow'] = flowtype
28  return json.dumps(params)
29 
30 
31 def switch_direction(fobj):
32  if isinstance(fobj, str):
33  fobj = json.loads(fobj)
34  else:
35  fobj = dict(fobj)
36  if fobj["direction"] == 'inject':
37  fobj["direction"] = 'extract'
38  elif fobj["direction"] == 'extract':
39  fobj["direction"] = 'inject'
40  else:
41  raise KeyError('direction')
42  return fobj
43 
44 def message_to_dict(msg):
45  '''
46  Return a simple dictionary of message header info.
47  '''
48  d = objectify(msg)
49  d['origin'] = msg.origin
50  d['granule'] = msg.granule
51  d['seqno'] = msg.seqno
52  return d
53 
def stringify(flowtype, params)
Definition: util.py:21
def switch_direction(fobj)
Definition: util.py:31
def objectify(morl)
Definition: util.py:5
def message_to_dict(msg)
Definition: util.py:44