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