Changeset 67
- Timestamp:
- 11/06/13 12:17:09 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/bletchley/blobtools.py
r66 r67 417 417 encodings["%s/%s" % (enc.name, d)] = e 418 418 419 419 420 def supportedEncodings(): 420 421 e = list(encodings.keys()) … … 456 457 457 458 def decode(encoding, blob): 459 """Given an encoding name and a blob, decodes the blob and returns it. 460 461 encoding -- A string representation of the encoding and dialect. 462 For a list of valid encoding names, run: 463 bletchley-analyze -e ? 464 465 blob -- A bytes or bytearray object to be decoded. If a string 466 is provided instead, it will be converted to a bytes 467 object using 'utf-8'. 468 469 Returns a bytes object containing the decoded representation of 470 blob. Will throw various types of exceptions if a problem is 471 encountered. 472 """ 473 if isinstance(blob, str): 474 blob = blob.encode('utf-8') 458 475 return encodings[encoding].decode(blob) 459 476 460 477 def encode(encoding, blob): 478 """Given an encoding name and a blob, encodes the blob and returns it. 479 480 encoding -- A string representation of the encoding and dialect. 481 For a list of valid encoding names, run: 482 bletchley-analyze -e ? 483 484 blob -- A bytes or bytearray object to be encoded. 485 486 Returns a bytes object containing the encoded representation of 487 blob. Will throw various types of exceptions if a problem is 488 encountered.""" 461 489 return encodings[encoding].encode(blob) 490 462 491 463 492 def decodeAll(encoding, blobs): 464 493 return [encodings[encoding].decode(b) for b in blobs] 465 494 495 466 496 def encodeAll(encoding, blobs): 467 497 return [encodings[encoding].encode(b) for b in blobs] 468 498 499 469 500 def decodeChain(decoding_chain, blob): 501 """Given a sequence of encoding names (decoding_chain) and a blob, 502 decodes the blob once for each element of the decoding_chain. For 503 instance, if the decoding_chain were 504 ['percent/lower', 'base64/rfc3548'] 505 then blob would first be decoded as 'percent/lower', followed by 506 'base64/rfc3548'. 507 508 decoding_chain -- A sequence (list,tuple,...) of string 509 representations of the encoding and dialect. For a 510 list of valid encoding names, run: 511 bletchley-analyze -e ? 512 513 blob -- A bytes or bytearray object to be decoded. If a string 514 is provided instead, it will be converted to a bytes 515 object using 'utf-8'. 516 517 Returns a bytes object containing the decoded representation of 518 blob. Will throw various types of exceptions if a problem is 519 encountered. 520 """ 470 521 for decoding in decoding_chain: 471 522 blob = decode(decoding, blob) 472 523 return blob 473 524 525 474 526 def encodeChain(encoding_chain, blob): 527 """Given a sequence of encoding names (encoding_chain) and a blob, 528 encodes the blob once for each element of the encoding_chain. For 529 instance, if the encoding_chain were 530 ['base64/rfc3548', 'percent/lower',] 531 then blob would first be encoded as 'base64/rfc3548', followed by 532 'percent/lower'. 533 534 encoding_chain -- A sequence (list,tuple,...) of string 535 representations of the encoding and dialect. For a 536 list of valid encoding names, run: 537 bletchley-analyze -e ? 538 539 blob -- A bytes or bytearray object to be encoded. 540 541 Returns a bytes object containing the encoded representation of 542 blob. Will throw various types of exceptions if a problem is 543 encountered. 544 """ 475 545 for encoding in encoding_chain: 476 546 blob = encode(encoding, blob) 477 547 return blob 548 478 549 479 550 def getLengths(s):
Note: See TracChangeset
for help on using the changeset viewer.