~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/encodings/charmap.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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@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
    )