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

Source Code for Module tdi.markup.soup.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   Soup Input Decoders 
 20  ===================== 
 21   
 22  Soup Input Decoders. 
 23  """ 
 24  __author__ = u"Andr\xe9 Malo" 
 25  __docformat__ = "restructuredtext en" 
 26   
 27  from tdi import _htmldecode as _htmldecode 
 28  from tdi import interfaces as _interfaces 
 29   
 30   
31 -class HTMLDecoder(object):
32 """ 33 Decoder for (X)HTML input 34 35 :IVariables: 36 `encoding` : ``str`` 37 Character encoding 38 """ 39 __implements__ = [_interfaces.DecoderInterface] 40
41 - def __init__(self, encoding):
42 """ 43 Initialization 44 45 :Parameters: 46 `encoding` : ``str`` 47 Encoding 48 """ 49 self.encoding = encoding
50
51 - def normalize(self, name):
52 """ :See: `DecoderInterface` """ 53 return name.lower()
54
55 - def decode(self, value, errors='strict'):
56 """ :See: `DecoderInterface` """ 57 return _htmldecode.decode(value, self.encoding, errors=errors)
58
59 - def attribute(self, value, errors='strict'):
60 """ :See: `DecoderInterface` """ 61 if value.startswith('"') or value.startswith("'"): 62 value = value[1:-1] 63 return _htmldecode.decode(value, self.encoding, errors=errors)
64 65
66 -class XMLDecoder(object):
67 """ 68 Decoder for XML input 69 70 :IVariables: 71 `encoding` : ``str`` 72 Character encoding 73 """ 74 __implements__ = [_interfaces.DecoderInterface] 75
76 - def __init__(self, encoding):
77 """ 78 Initialization 79 80 :Parameters: 81 `encoding` : ``str`` 82 Character encoding 83 """ 84 self.encoding = encoding
85
86 - def normalize(self, name):
87 """ :See: `DecoderInterface` """ 88 return name
89
90 - def decode(self, value, errors='strict'):
91 """ :See: `DecoderInterface` """ 92 return _htmldecode.decode(value, self.encoding, errors=errors)
93
94 - def attribute(self, value, errors='strict'):
95 """ :See: `DecoderInterface` """ 96 if value.startswith('"') or value.startswith("'"): 97 value = value[1:-1] 98 return _htmldecode.decode(value, self.encoding, errors=errors)
99 100 101 from tdi import c 102 c = c.load('impl') 103 if c is not None: 104 HTMLDecoder, XMLDecoder = c.HTMLDecoder, c.XMLDecoder 105 del c 106