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

Source Code for Module tdi.tools.htmlform._adapters

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