10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi""" Generic Python Character Mapping Codec. 20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi Use this codec directly rather than through the automatic 40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi conversion mechanisms supplied by unicode() and .encode(). 50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill YiWritten by Marc-Andre Lemburg (mal@lemburg.com). 80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi(c) Copyright CNRI, All Rights Reserved. NO WARRANTY. 100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi"""#" 120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport codecs 140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi### Codec APIs 160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass Codec(codecs.Codec): 180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # Note: Binding these as C functions will result in the class not 200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi # converting them to methods. This is intended. 210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi encode = codecs.charmap_encode 220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi decode = codecs.charmap_decode 230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass IncrementalEncoder(codecs.IncrementalEncoder): 250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __init__(self, errors='strict', mapping=None): 260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi codecs.IncrementalEncoder.__init__(self, errors) 270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.mapping = mapping 280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def encode(self, input, final=False): 300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return codecs.charmap_encode(input, self.errors, self.mapping)[0] 310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass IncrementalDecoder(codecs.IncrementalDecoder): 330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __init__(self, errors='strict', mapping=None): 340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi codecs.IncrementalDecoder.__init__(self, errors) 350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.mapping = mapping 360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def decode(self, input, final=False): 380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return codecs.charmap_decode(input, self.errors, self.mapping)[0] 390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass StreamWriter(Codec,codecs.StreamWriter): 410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __init__(self,stream,errors='strict',mapping=None): 430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi codecs.StreamWriter.__init__(self,stream,errors) 440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.mapping = mapping 450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def encode(self,input,errors='strict'): 470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return Codec.encode(input,errors,self.mapping) 480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass StreamReader(Codec,codecs.StreamReader): 500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def __init__(self,stream,errors='strict',mapping=None): 520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi codecs.StreamReader.__init__(self,stream,errors) 530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi self.mapping = mapping 540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi def decode(self,input,errors='strict'): 560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return Codec.decode(input,errors,self.mapping) 570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi### encodings module API 590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi 600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef getregentry(): 610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi return codecs.CodecInfo( 620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi name='charmap', 630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi encode=Codec.encode, 640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi decode=Codec.decode, 650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi incrementalencoder=IncrementalEncoder, 660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi incrementaldecoder=IncrementalDecoder, 670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi streamwriter=StreamWriter, 680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi streamreader=StreamReader, 690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi ) 70