charmap.py 2.0 KB

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