Package tdi :: Package markup :: Package text :: Module decoder
[frames] | no frames]

Source Code for Module tdi.markup.text.decoder

 1  # -*- coding: ascii -*- 
 2  # 
 3  # Copyright 2013 
 4  # Andr\xe9 Malo or his licensors, as applicable 
 5  # 
 6  # Licensed under the Apache License, Version 2.0 (the "License"); 
 7  # you may not use this file except in compliance with the License. 
 8  # You may obtain a copy of the License at 
 9  # 
10  #     http://www.apache.org/licenses/LICENSE-2.0 
11  # 
12  # Unless required by applicable law or agreed to in writing, software 
13  # distributed under the License is distributed on an "AS IS" BASIS, 
14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15  # See the License for the specific language governing permissions and 
16  # limitations under the License. 
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  #: Backslash-escape Substituter 
33  #: 
34  #: :Type: ``callable`` 
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