jpayne@68: """Python 'base64_codec' Codec - base64 content transfer encoding. jpayne@68: jpayne@68: This codec de/encodes from bytes to bytes. jpayne@68: jpayne@68: Written by Marc-Andre Lemburg (mal@lemburg.com). jpayne@68: """ jpayne@68: jpayne@68: import codecs jpayne@68: import base64 jpayne@68: jpayne@68: ### Codec APIs jpayne@68: jpayne@68: def base64_encode(input, errors='strict'): jpayne@68: assert errors == 'strict' jpayne@68: return (base64.encodebytes(input), len(input)) jpayne@68: jpayne@68: def base64_decode(input, errors='strict'): jpayne@68: assert errors == 'strict' jpayne@68: return (base64.decodebytes(input), len(input)) jpayne@68: jpayne@68: class Codec(codecs.Codec): jpayne@68: def encode(self, input, errors='strict'): jpayne@68: return base64_encode(input, errors) jpayne@68: def decode(self, input, errors='strict'): jpayne@68: return base64_decode(input, errors) jpayne@68: jpayne@68: class IncrementalEncoder(codecs.IncrementalEncoder): jpayne@68: def encode(self, input, final=False): jpayne@68: assert self.errors == 'strict' jpayne@68: return base64.encodebytes(input) jpayne@68: jpayne@68: class IncrementalDecoder(codecs.IncrementalDecoder): jpayne@68: def decode(self, input, final=False): jpayne@68: assert self.errors == 'strict' jpayne@68: return base64.decodebytes(input) jpayne@68: jpayne@68: class StreamWriter(Codec, codecs.StreamWriter): jpayne@68: charbuffertype = bytes jpayne@68: jpayne@68: class StreamReader(Codec, codecs.StreamReader): jpayne@68: charbuffertype = bytes jpayne@68: jpayne@68: ### encodings module API jpayne@68: jpayne@68: def getregentry(): jpayne@68: return codecs.CodecInfo( jpayne@68: name='base64', jpayne@68: encode=base64_encode, jpayne@68: decode=base64_decode, jpayne@68: incrementalencoder=IncrementalEncoder, jpayne@68: incrementaldecoder=IncrementalDecoder, jpayne@68: streamwriter=StreamWriter, jpayne@68: streamreader=StreamReader, jpayne@68: _is_text_encoding=False, jpayne@68: )