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