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

Source Code for Module tdi.markup.text.decoder

 1  # -*- coding: ascii -*- 
 2  u""" 
 3  :Copyright: 
 4   
 5   Copyright 2013 
 6   Andr\xe9 Malo or his licensors, as applicable 
 7   
 8  :License: 
 9   
10   Licensed under the Apache License, Version 2.0 (the "License"); 
11   you may not use this file except in compliance with the License. 
12   You may obtain a copy of the License at 
13   
14       http://www.apache.org/licenses/LICENSE-2.0 
15   
16   Unless required by applicable law or agreed to in writing, software 
17   distributed under the License is distributed on an "AS IS" BASIS, 
18   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
19   See the License for the specific language governing permissions and 
20   limitations under the License. 
21   
22  ==================== 
23   Text Input Decoder 
24  ==================== 
25   
26  Text Input Decoder. 
27  """ 
28  __author__ = u"Andr\xe9 Malo" 
29  __docformat__ = "restructuredtext en" 
30   
31  import re as _re 
32   
33  from tdi import interfaces as _interfaces 
34   
35   
36  #: Backslash-escape Substituter 
37  #: 
38  #: :Type: ``callable`` 
39  _SLASHSUB = _re.compile(ur'\\(.)', _re.S).sub 
40   
41   
42 -class TextDecoder(object):
43 """ 44 Decoder for text input 45 46 :IVariables: 47 `encoding` : ``str`` 48 Character encoding 49 """ 50 __implements__ = [_interfaces.DecoderInterface] 51
52 - def __init__(self, encoding):
53 """ 54 Initialization 55 56 :Parameters: 57 `encoding` : ``str`` 58 Character encoding 59 """ 60 self.encoding = encoding
61
62 - def normalize(self, name):
63 """ :See: `DecoderInterface` """ 64 return name
65
66 - def decode(self, value, errors='strict'):
67 """ :See: `DecoderInterface` """ 68 return value.decode(self.encoding, errors)
69
70 - def attribute(self, value, errors='strict'):
71 """ :See: `DecoderInterface` """ 72 if value.startswith('"') or value.startswith("'"): 73 value = value[1:-1] 74 return _SLASHSUB(ur'\1', value.decode(self.encoding, errors))
75 76 77 from tdi import c 78 c = c.load('impl') 79 if c is not None: 80 TextDecoder = c.TextDecoder 81 del c 82