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