jpayne@68: """ Generic Python Character Mapping Codec. jpayne@68: jpayne@68: Use this codec directly rather than through the automatic jpayne@68: conversion mechanisms supplied by unicode() and .encode(). jpayne@68: jpayne@68: jpayne@68: Written by Marc-Andre Lemburg (mal@lemburg.com). jpayne@68: jpayne@68: (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. jpayne@68: jpayne@68: """#" jpayne@68: jpayne@68: import codecs jpayne@68: jpayne@68: ### Codec APIs jpayne@68: jpayne@68: class Codec(codecs.Codec): jpayne@68: jpayne@68: # Note: Binding these as C functions will result in the class not jpayne@68: # converting them to methods. This is intended. jpayne@68: encode = codecs.charmap_encode jpayne@68: decode = codecs.charmap_decode jpayne@68: jpayne@68: class IncrementalEncoder(codecs.IncrementalEncoder): jpayne@68: def __init__(self, errors='strict', mapping=None): jpayne@68: codecs.IncrementalEncoder.__init__(self, errors) jpayne@68: self.mapping = mapping jpayne@68: jpayne@68: def encode(self, input, final=False): jpayne@68: return codecs.charmap_encode(input, self.errors, self.mapping)[0] jpayne@68: jpayne@68: class IncrementalDecoder(codecs.IncrementalDecoder): jpayne@68: def __init__(self, errors='strict', mapping=None): jpayne@68: codecs.IncrementalDecoder.__init__(self, errors) jpayne@68: self.mapping = mapping jpayne@68: jpayne@68: def decode(self, input, final=False): jpayne@68: return codecs.charmap_decode(input, self.errors, self.mapping)[0] jpayne@68: jpayne@68: class StreamWriter(Codec,codecs.StreamWriter): jpayne@68: jpayne@68: def __init__(self,stream,errors='strict',mapping=None): jpayne@68: codecs.StreamWriter.__init__(self,stream,errors) jpayne@68: self.mapping = mapping jpayne@68: jpayne@68: def encode(self,input,errors='strict'): jpayne@68: return Codec.encode(input,errors,self.mapping) jpayne@68: jpayne@68: class StreamReader(Codec,codecs.StreamReader): jpayne@68: jpayne@68: def __init__(self,stream,errors='strict',mapping=None): jpayne@68: codecs.StreamReader.__init__(self,stream,errors) jpayne@68: self.mapping = mapping jpayne@68: jpayne@68: def decode(self,input,errors='strict'): jpayne@68: return Codec.decode(input,errors,self.mapping) jpayne@68: jpayne@68: ### encodings module API jpayne@68: jpayne@68: def getregentry(): jpayne@68: return codecs.CodecInfo( jpayne@68: name='charmap', jpayne@68: encode=Codec.encode, jpayne@68: decode=Codec.decode, jpayne@68: incrementalencoder=IncrementalEncoder, jpayne@68: incrementaldecoder=IncrementalDecoder, jpayne@68: streamwriter=StreamWriter, jpayne@68: streamreader=StreamReader, jpayne@68: )