1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 ====================
19 Text Input Decoder
20 ====================
21
22 Text Input Decoder.
23 """
24 __author__ = u"Andr\xe9 Malo"
25 __docformat__ = "restructuredtext en"
26
27 import re as _re
28
29 from tdi import interfaces as _interfaces
30
31
32
33
34
35 _SLASHSUB = _re.compile(ur'\\(.)', _re.S).sub
36
37
38 -class TextDecoder(object):
39 """
40 Decoder for text input
41
42 :IVariables:
43 `encoding` : ``str``
44 Character encoding
45 """
46 __implements__ = [_interfaces.DecoderInterface]
47
48 - def __init__(self, encoding):
49 """
50 Initialization
51
52 :Parameters:
53 `encoding` : ``str``
54 Character encoding
55 """
56 self.encoding = encoding
57
58 - def normalize(self, name):
59 """ :See: `DecoderInterface` """
60 return name
61
62 - def decode(self, value, errors='strict'):
63 """ :See: `DecoderInterface` """
64 return value.decode(self.encoding, errors)
65
66 - def attribute(self, value, errors='strict'):
67 """ :See: `DecoderInterface` """
68 if value.startswith('"') or value.startswith("'"):
69 value = value[1:-1]
70 return _SLASHSUB(ur'\1', value.decode(self.encoding, errors))
71
72
73 from tdi import c
74 c = c.load('impl')
75 if c is not None:
76 TextDecoder = c.TextDecoder
77 del c
78