Wrapper for binary data.
| 365 | # @param data An 8-bit string containing arbitrary data. |
| 366 | |
| 367 | class Binary: |
| 368 | """Wrapper for binary data.""" |
| 369 | |
| 370 | def __init__(self, data=None): |
| 371 | if data is None: |
| 372 | data = b"" |
| 373 | else: |
| 374 | if not isinstance(data, (bytes, bytearray)): |
| 375 | raise TypeError("expected bytes or bytearray, not %s" % |
| 376 | data.__class__.__name__) |
| 377 | data = bytes(data) # Make a copy of the bytes! |
| 378 | self.data = data |
| 379 | |
| 380 | ## |
| 381 | # Get buffer contents. |
| 382 | # |
| 383 | # @return Buffer contents, as an 8-bit string. |
| 384 | |
| 385 | def __str__(self): |
| 386 | return str(self.data, "latin-1") # XXX encoding?! |
| 387 | |
| 388 | def __eq__(self, other): |
| 389 | if isinstance(other, Binary): |
| 390 | other = other.data |
| 391 | return self.data == other |
| 392 | |
| 393 | def decode(self, data): |
| 394 | self.data = base64.decodebytes(data) |
| 395 | |
| 396 | def encode(self, out): |
| 397 | out.write("<value><base64>\n") |
| 398 | encoded = base64.encodebytes(self.data) |
| 399 | out.write(encoded.decode('ascii')) |
| 400 | out.write("</base64></value>\n") |
| 401 | |
| 402 | def _binary(data): |
| 403 | # decode xml element contents into a Binary structure |
no outgoing calls
no test coverage detected
searching dependent graphs…