Changeset 67


Ignore:
Timestamp:
11/06/13 12:17:09 (11 years ago)
Author:
tmorgan
Message:

added documentation to a few functions

allowing strings as argument to decode

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/bletchley/blobtools.py

    r66 r67  
    417417    encodings["%s/%s" % (enc.name, d)] = e
    418418
     419
    419420def supportedEncodings():
    420421    e = list(encodings.keys())
     
    456457
    457458def 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')
    458475    return encodings[encoding].decode(blob)
    459476
    460477def 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."""
    461489    return encodings[encoding].encode(blob)
     490
    462491
    463492def decodeAll(encoding, blobs):
    464493    return [encodings[encoding].decode(b) for b in blobs]
    465494
     495
    466496def encodeAll(encoding, blobs):
    467497    return [encodings[encoding].encode(b) for b in blobs]
    468498
     499
    469500def 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    """
    470521    for decoding in decoding_chain:
    471522        blob = decode(decoding, blob)
    472523    return blob
    473524
     525
    474526def 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    """   
    475545    for encoding in encoding_chain:
    476546        blob = encode(encoding, blob)
    477547    return blob
     548
    478549
    479550def getLengths(s):
Note: See TracChangeset for help on using the changeset viewer.