##// END OF EJS Templates
Make top-level metadata dictionary not optional.
Jason Grout -
Show More
@@ -351,7 +351,7 b' var IPython = (function (IPython) {'
351 var reply = $.parseJSON(e.data);
351 var reply = $.parseJSON(e.data);
352 var content = reply.content;
352 var content = reply.content;
353 var msg_type = reply.header.msg_type;
353 var msg_type = reply.header.msg_type;
354 var metadata = reply.metadata || {};
354 var metadata = reply.metadata;
355 var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id);
355 var callbacks = this.get_callbacks_for_msg(reply.parent_header.msg_id);
356 if (msg_type !== 'status' && callbacks === undefined) {
356 if (msg_type !== 'status' && callbacks === undefined) {
357 // Message not from one of this notebook's cells and there are no
357 // Message not from one of this notebook's cells and there are no
@@ -421,11 +421,9 b' class Session(Configurable):'
421 msg['msg_type'] = header['msg_type']
421 msg['msg_type'] = header['msg_type']
422 msg['parent_header'] = {} if parent is None else extract_header(parent)
422 msg['parent_header'] = {} if parent is None else extract_header(parent)
423 msg['content'] = {} if content is None else content
423 msg['content'] = {} if content is None else content
424 metadata_dict = self.metadata.copy()
424 msg['metadata'] = self.metadata.copy()
425 if metadata is not None:
425 if metadata is not None:
426 metadata_dict.update(metadata)
426 msg['metadata'].update(metadata)
427 if metadata_dict:
428 msg['metadata'] = metadata_dict
429 return msg
427 return msg
430
428
431 def sign(self, msg_list):
429 def sign(self, msg_list):
@@ -459,7 +457,7 b' class Session(Configurable):'
459 -------
457 -------
460 msg_list : list
458 msg_list : list
461 The list of bytes objects to be sent with the format:
459 The list of bytes objects to be sent with the format:
462 [ident1,ident2,...,DELIM,HMAC,p_header,p_parent,p_content,
460 [ident1,ident2,...,DELIM,HMAC,p_header,p_parent,p_metadata,p_content,
463 buffer1,buffer2,...]. In this list, the p_* entities are
461 buffer1,buffer2,...]. In this list, the p_* entities are
464 the packed or serialized versions, so if JSON is used, these
462 the packed or serialized versions, so if JSON is used, these
465 are utf8 encoded JSON strings.
463 are utf8 encoded JSON strings.
@@ -480,7 +478,8 b' class Session(Configurable):'
480
478
481 real_message = [self.pack(msg['header']),
479 real_message = [self.pack(msg['header']),
482 self.pack(msg['parent_header']),
480 self.pack(msg['parent_header']),
483 content
481 self.pack(msg['metadata']),
482 content,
484 ]
483 ]
485
484
486 to_send = []
485 to_send = []
@@ -607,7 +606,7 b' class Session(Configurable):'
607 The ZMQ stream or socket to use for sending the message.
606 The ZMQ stream or socket to use for sending the message.
608 msg_list : list
607 msg_list : list
609 The serialized list of messages to send. This only includes the
608 The serialized list of messages to send. This only includes the
610 [p_header,p_parent,p_content,buffer1,buffer2,...] portion of
609 [p_header,p_parent,p_metadata,p_content,buffer1,buffer2,...] portion of
611 the message.
610 the message.
612 ident : ident or list
611 ident : ident or list
613 A single ident or a list of idents to use in sending.
612 A single ident or a list of idents to use in sending.
@@ -705,7 +704,7 b' class Session(Configurable):'
705 -----------
704 -----------
706 msg_list : list of bytes or Message objects
705 msg_list : list of bytes or Message objects
707 The list of message parts of the form [HMAC,p_header,p_parent,
706 The list of message parts of the form [HMAC,p_header,p_parent,
708 p_content,buffer1,buffer2,...].
707 p_metadata,p_content,buffer1,buffer2,...].
709 content : bool (True)
708 content : bool (True)
710 Whether to unpack the content dict (True), or leave it packed
709 Whether to unpack the content dict (True), or leave it packed
711 (False).
710 (False).
@@ -719,7 +718,7 b' class Session(Configurable):'
719 The nested message dict with top-level keys [header, parent_header,
718 The nested message dict with top-level keys [header, parent_header,
720 content, buffers].
719 content, buffers].
721 """
720 """
722 minlen = 4
721 minlen = 5
723 message = {}
722 message = {}
724 if not copy:
723 if not copy:
725 for i in range(minlen):
724 for i in range(minlen):
@@ -741,12 +740,13 b' class Session(Configurable):'
741 message['msg_id'] = header['msg_id']
740 message['msg_id'] = header['msg_id']
742 message['msg_type'] = header['msg_type']
741 message['msg_type'] = header['msg_type']
743 message['parent_header'] = self.unpack(msg_list[2])
742 message['parent_header'] = self.unpack(msg_list[2])
743 message['metadata'] = self.unpack(msg_list[3])
744 if content:
744 if content:
745 message['content'] = self.unpack(msg_list[3])
745 message['content'] = self.unpack(msg_list[4])
746 else:
746 else:
747 message['content'] = msg_list[3]
747 message['content'] = msg_list[4]
748
748
749 message['buffers'] = msg_list[4:]
749 message['buffers'] = msg_list[5:]
750 return message
750 return message
751
751
752 def test_msg2obj():
752 def test_msg2obj():
@@ -47,10 +47,11 b' class TestSession(SessionTestCase):'
47 def test_msg(self):
47 def test_msg(self):
48 """message format"""
48 """message format"""
49 msg = self.session.msg('execute')
49 msg = self.session.msg('execute')
50 thekeys = set('header parent_header content msg_type msg_id'.split())
50 thekeys = set('header parent_header metadata content msg_type msg_id'.split())
51 s = set(msg.keys())
51 s = set(msg.keys())
52 self.assertEqual(s, thekeys)
52 self.assertEqual(s, thekeys)
53 self.assertTrue(isinstance(msg['content'],dict))
53 self.assertTrue(isinstance(msg['content'],dict))
54 self.assertTrue(isinstance(msg['metadata'],dict))
54 self.assertTrue(isinstance(msg['header'],dict))
55 self.assertTrue(isinstance(msg['header'],dict))
55 self.assertTrue(isinstance(msg['parent_header'],dict))
56 self.assertTrue(isinstance(msg['parent_header'],dict))
56 self.assertTrue(isinstance(msg['msg_id'],str))
57 self.assertTrue(isinstance(msg['msg_id'],str))
@@ -69,6 +70,7 b' class TestSession(SessionTestCase):'
69 self.assertEqual(new_msg['header'],msg['header'])
70 self.assertEqual(new_msg['header'],msg['header'])
70 self.assertEqual(new_msg['content'],msg['content'])
71 self.assertEqual(new_msg['content'],msg['content'])
71 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
72 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
73 self.assertEqual(new_msg['metadata'],msg['metadata'])
72 # ensure floats don't come out as Decimal:
74 # ensure floats don't come out as Decimal:
73 self.assertEqual(type(new_msg['content']['b']),type(new_msg['content']['b']))
75 self.assertEqual(type(new_msg['content']['b']),type(new_msg['content']['b']))
74
76
@@ -85,6 +87,7 b' class TestSession(SessionTestCase):'
85 self.assertEqual(new_msg['header'],msg['header'])
87 self.assertEqual(new_msg['header'],msg['header'])
86 self.assertEqual(new_msg['content'],msg['content'])
88 self.assertEqual(new_msg['content'],msg['content'])
87 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
89 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
90 self.assertEqual(new_msg['metadata'],msg['metadata'])
88 self.assertEqual(new_msg['buffers'],[b'bar'])
91 self.assertEqual(new_msg['buffers'],[b'bar'])
89
92
90 socket.data = []
93 socket.data = []
@@ -92,9 +95,10 b' class TestSession(SessionTestCase):'
92 content = msg['content']
95 content = msg['content']
93 header = msg['header']
96 header = msg['header']
94 parent = msg['parent_header']
97 parent = msg['parent_header']
98 metadata = msg['metadata']
95 msg_type = header['msg_type']
99 msg_type = header['msg_type']
96 self.session.send(socket, None, content=content, parent=parent,
100 self.session.send(socket, None, content=content, parent=parent,
97 header=header, ident=b'foo', buffers=[b'bar'])
101 header=header, metadata=metadata, ident=b'foo', buffers=[b'bar'])
98 ident, msg_list = self.session.feed_identities(socket.data)
102 ident, msg_list = self.session.feed_identities(socket.data)
99 new_msg = self.session.unserialize(msg_list)
103 new_msg = self.session.unserialize(msg_list)
100 self.assertEqual(ident[0], b'foo')
104 self.assertEqual(ident[0], b'foo')
@@ -102,6 +106,7 b' class TestSession(SessionTestCase):'
102 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
106 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
103 self.assertEqual(new_msg['header'],msg['header'])
107 self.assertEqual(new_msg['header'],msg['header'])
104 self.assertEqual(new_msg['content'],msg['content'])
108 self.assertEqual(new_msg['content'],msg['content'])
109 self.assertEqual(new_msg['metadata'],msg['metadata'])
105 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
110 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
106 self.assertEqual(new_msg['buffers'],[b'bar'])
111 self.assertEqual(new_msg['buffers'],[b'bar'])
107
112
@@ -114,6 +119,7 b' class TestSession(SessionTestCase):'
114 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
119 self.assertEqual(new_msg['msg_type'],msg['msg_type'])
115 self.assertEqual(new_msg['header'],msg['header'])
120 self.assertEqual(new_msg['header'],msg['header'])
116 self.assertEqual(new_msg['content'],msg['content'])
121 self.assertEqual(new_msg['content'],msg['content'])
122 self.assertEqual(new_msg['metadata'],msg['metadata'])
117 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
123 self.assertEqual(new_msg['parent_header'],msg['parent_header'])
118 self.assertEqual(new_msg['buffers'],[b'bar'])
124 self.assertEqual(new_msg['buffers'],[b'bar'])
119
125
@@ -106,7 +106,7 b' A message is defined by the following four-dictionary structure::'
106 # depends on the message type.
106 # depends on the message type.
107 'content' : dict,
107 'content' : dict,
108
108
109 # Any metadata associated with the message; this dictionary is optional.
109 # Any metadata associated with the message.
110 'metadata' : dict,
110 'metadata' : dict,
111 }
111 }
112
112
@@ -130,7 +130,7 b' messages upon deserialization to the following form for convenience::'
130 'msg_type' : str,
130 'msg_type' : str,
131 'parent_header' : dict,
131 'parent_header' : dict,
132 'content' : dict,
132 'content' : dict,
133 'metadata' : dict, # optional
133 'metadata' : dict,
134 }
134 }
135
135
136 All messages sent to or received by any IPython process should have this
136 All messages sent to or received by any IPython process should have this
General Comments 0
You need to be logged in to leave comments. Login now