Decode a transmitted package. The return value indicates how many binary attachment packets are necessary to fully decode the packet.
(self, encoded_packet)
| 67 | return encoded_packet |
| 68 | |
| 69 | def decode(self, encoded_packet): |
| 70 | """Decode a transmitted package. |
| 71 | |
| 72 | The return value indicates how many binary attachment packets are |
| 73 | necessary to fully decode the packet. |
| 74 | """ |
| 75 | ep = encoded_packet |
| 76 | try: |
| 77 | self.packet_type = int(ep[0:1]) |
| 78 | except TypeError: |
| 79 | self.packet_type = ep |
| 80 | ep = '' |
| 81 | self.namespace = None |
| 82 | self.data = None |
| 83 | ep = ep[1:] |
| 84 | dash = ep.find('-') |
| 85 | attachment_count = 0 |
| 86 | if dash > 0 and ep[0:dash].isdigit(): |
| 87 | if dash > 10: |
| 88 | raise ValueError('too many attachments') |
| 89 | attachment_count = int(ep[0:dash]) |
| 90 | ep = ep[dash + 1:] |
| 91 | if ep and ep[0:1] == '/': |
| 92 | sep = ep.find(',') |
| 93 | if sep == -1: |
| 94 | self.namespace = ep |
| 95 | ep = '' |
| 96 | else: |
| 97 | self.namespace = ep[0:sep] |
| 98 | ep = ep[sep + 1:] |
| 99 | q = self.namespace.find('?') |
| 100 | if q != -1: |
| 101 | self.namespace = self.namespace[0:q] |
| 102 | if ep and ep[0].isdigit(): |
| 103 | i = 1 |
| 104 | end = len(ep) |
| 105 | while i < end: |
| 106 | if not ep[i].isdigit() or i >= 100: |
| 107 | break |
| 108 | i += 1 |
| 109 | self.id = int(ep[:i]) |
| 110 | ep = ep[i:] |
| 111 | if len(ep) > 0 and ep[0].isdigit(): |
| 112 | raise ValueError('id field is too long') |
| 113 | if ep: |
| 114 | self.data = self.json.loads(ep) |
| 115 | return attachment_count |
| 116 | |
| 117 | def add_attachment(self, attachment): |
| 118 | if self.attachment_count <= len(self.attachments): |