Package tdi :: Package tools :: Package htmlform :: Module _adapters
[frames] | no frames]

Source Code for Module tdi.tools.htmlform._adapters

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2007 - 2012 
  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   HTML forms reloaded 
 20  ===================== 
 21   
 22  Form helper classes. 
 23  """ 
 24  __author__ = u"Andr\xe9 Malo" 
 25  __docformat__ = "restructuredtext en" 
 26  __all__ = [ 
 27      'DictParameterAdapter', 'ListDictParameterAdapter', 
 28      'MultiDictParameterAdapter', 'NullParameterAdapter', 
 29  ] 
 30   
 31  from tdi.tools.htmlform._interfaces import ParameterAdapterInterface 
 32   
 33   
34 -class DictParameterAdapter(object):
35 """ 36 HTMLForm parameter adapter from a simple dict 37 38 :IVariables: 39 `param` : ``dict`` 40 Parameters 41 """ 42 __implements__ = [ParameterAdapterInterface] 43
44 - def __init__(self, param):
45 """ 46 Initialization 47 48 :Parameters: 49 `param` : ``dict`` 50 Parameters 51 """ 52 self.param = param
53
54 - def getfirst(self, name, default=None):
55 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """ 56 return self.param.get(name, default)
57
58 - def getlist(self, name): # pylint: disable = E0202
59 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """ 60 if name in self.param: 61 return [self.param[name]] 62 return []
63 64
65 -class ListDictParameterAdapter(object):
66 """ 67 HTMLForm parameter adapter from a dict of sequences 68 69 :IVariables: 70 `param` : dict of sequences 71 Parameters 72 """ 73 __implements__ = [ParameterAdapterInterface] 74
75 - def __init__(self, param):
76 """ 77 Initialization 78 79 :Parameters: 80 `param` : dict of sequences 81 Parameters. Empty sequences act as if the key was not present. 82 Otherwise ``getfirst`` will return the first element and 83 ``getlist`` will return a shallow copy of the sequence as a 84 ``list``. 85 """ 86 self.param = param
87
88 - def getfirst(self, name, default=None):
89 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """ 90 try: 91 result = self.param[name] 92 except KeyError: 93 pass 94 else: 95 if result: 96 return result[0] 97 return default
98
99 - def getlist(self, name):
100 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """ 101 try: 102 result = self.param[name] 103 except KeyError: 104 pass 105 else: 106 return list(result) 107 return []
108 109
110 -class MultiDictParameterAdapter(object):
111 """ 112 HTMLForm parameter adapter from a multidict (like paste provides) 113 114 :IVariables: 115 `param` : multidict 116 Parameters 117 """ 118 __implements__ = [ParameterAdapterInterface] 119
120 - def __init__(self, param):
121 """ 122 Initialization 123 124 :Parameters: 125 `param` : multidict 126 Parameters. The object is expected to provide a getall() method 127 """ 128 self.param = param
129
130 - def getfirst(self, name, default=None):
131 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """ 132 try: 133 return self.param.getall(name)[0] 134 except IndexError: 135 return default
136
137 - def getlist(self, name): # pylint: disable = E0202
138 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """ 139 return self.param.getall(name)
140 141
142 -class NullParameterAdapter(object):
143 """ This adapter just returns nothing """ 144 __implements__ = [ParameterAdapterInterface] 145
146 - def getlist(self, name):
147 """ :See: `ParameterAdapterInterface.getlist` """ 148 # pylint: disable = W0613 149 return []
150
151 - def getfirst(self, name, default=None):
152 """ :See: `ParameterAdapterInterface.getfirst` """ 153 # pylint: disable = W0613 154 return default
155