Home | History | Annotate | Download | only in encodings
      1 """ Generic Python Character Mapping Codec.
      2 
      3     Use this codec directly rather than through the automatic
      4     conversion mechanisms supplied by unicode() and .encode().
      5 
      6 
      7 Written by Marc-Andre Lemburg (mal (at] lemburg.com).
      8 
      9 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
     10 
     11 """#"
     12 
     13 import codecs
     14 
     15 ### Codec APIs
     16 
     17 class Codec(codecs.Codec):
     18 
     19     # Note: Binding these as C functions will result in the class not
     20     # converting them to methods. This is intended.
     21     encode = codecs.charmap_encode
     22     decode = codecs.charmap_decode
     23 
     24 class IncrementalEncoder(codecs.IncrementalEncoder):
     25     def __init__(self, errors='strict', mapping=None):
     26         codecs.IncrementalEncoder.__init__(self, errors)
     27         self.mapping = mapping
     28 
     29     def encode(self, input, final=False):
     30         return codecs.charmap_encode(input, self.errors, self.mapping)[0]
     31 
     32 class IncrementalDecoder(codecs.IncrementalDecoder):
     33     def __init__(self, errors='strict', mapping=None):
     34         codecs.IncrementalDecoder.__init__(self, errors)
     35         self.mapping = mapping
     36 
     37     def decode(self, input, final=False):
     38         return codecs.charmap_decode(input, self.errors, self.mapping)[0]
     39 
     40 class StreamWriter(Codec,codecs.StreamWriter):
     41 
     42     def __init__(self,stream,errors='strict',mapping=None):
     43         codecs.StreamWriter.__init__(self,stream,errors)
     44         self.mapping = mapping
     45 
     46     def encode(self,input,errors='strict'):
     47         return Codec.encode(input,errors,self.mapping)
     48 
     49 class StreamReader(Codec,codecs.StreamReader):
     50 
     51     def __init__(self,stream,errors='strict',mapping=None):
     52         codecs.StreamReader.__init__(self,stream,errors)
     53         self.mapping = mapping
     54 
     55     def decode(self,input,errors='strict'):
     56         return Codec.decode(input,errors,self.mapping)
     57 
     58 ### encodings module API
     59 
     60 def getregentry():
     61     return codecs.CodecInfo(
     62         name='charmap',
     63         encode=Codec.encode,
     64         decode=Codec.decode,
     65         incrementalencoder=IncrementalEncoder,
     66         incrementaldecoder=IncrementalDecoder,
     67         streamwriter=StreamWriter,
     68         streamreader=StreamReader,
     69     )
     70