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

Source Code for Module tdi.markup.text.decoder

 1  # -*- coding: ascii -*- 
 2  r""" 
 3  :Copyright: 
 4   
 5   Copyright 2013 - 2015 
 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  if __doc__: 
29      # pylint: disable = redefined-builtin 
30      __doc__ = __doc__.encode('ascii').decode('unicode_escape') 
31  __author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape') 
32  __docformat__ = "restructuredtext en" 
33   
34  import re as _re 
35   
36  from ... import interfaces as _interfaces 
37   
38   
39  #: Backslash-escape Substituter 
40  #: 
41  #: :Type: ``callable`` 
42  _SLASHSUB = _re.compile(ur'\\(.)', _re.S).sub 
43   
44   
45 -class TextDecoder(object):
46 """ 47 Decoder for text input 48 49 :IVariables: 50 `encoding` : ``str`` 51 Character encoding 52 """ 53 __implements__ = [_interfaces.DecoderInterface] 54
55 - def __init__(self, encoding):
56 """ 57 Initialization 58 59 :Parameters: 60 `encoding` : ``str`` 61 Character encoding 62 """ 63 self.encoding = encoding
64
65 - def normalize(self, name):
66 """ :See: `DecoderInterface` """ 67 return name
68
69 - def decode(self, value, errors='strict'):
70 """ :See: `DecoderInterface` """ 71 return value.decode(self.encoding, errors)
72
73 - def attribute(self, value, errors='strict'):
74 """ :See: `DecoderInterface` """ 75 if value.startswith('"') or value.startswith("'"): 76 value = value[1:-1] 77 return _SLASHSUB(ur'\1', value.decode(self.encoding, errors))
78 79 80 from ... import c 81 c = c.load('impl') 82 if c is not None: 83 TextDecoder = c.TextDecoder # noqa 84 del c 85