1 """
2 RESTx: Sane, simple and effective data publishing and integration.
3
4 Copyright (C) 2010 MuleSoft Inc. http://www.mulesoft.com
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 """
20
21 """
22 Definition of the L{RestxParameter} class.
23
24 This object describes the properties of a paremeter.
25 Parameters are used in different contexts.
26
27 - A resource creation time parameter of a component.
28 - A parameter for a component service
29 - A parameter for a resource service (sub-resource)
30
31 """
32
33 from restxclient.restx_client_exception import RestxClientException
34
36 """
37 Translate a string to a numeric value.
38
39 This sort of translation function is necessary to be more
40 flexible in handling the type of input for parameter values.
41
42 @param x: String representing a number or a number
43 type.
44 @type x: string or number type
45
46 @return: Numeric value
47 @rtype: Integer or Float
48
49 """
50 if type(x) in [ int, float ]:
51 return x
52 try:
53 return int(x)
54 except:
55 return float(x)
56
58 """
59 Translate a string to a boolean value.
60
61 This sort of translation function is necessary to be more
62 flexible in handling the type of input for parameter values.
63
64 Strings like 'y', 'yes', 'true', 't', '1' are interpreted
65 as True, all other strings are considered to mean False.
66
67 It works for upper and lower-case strings.
68
69 @param x: String representation of a boolean value
70 or a boolean.
71 @type x: string or boolean
72
73 @return: Boolean value.
74 @rtype: boolean
75
76 """
77 if type(x) is bool:
78 return x
79 if x.lower() in [ "y", "yes", "true", "t", "1" ]:
80 return True
81 else:
82 return False
83
84
86 """
87 Represents a parameter for a RESTx resource or service.
88
89 This is a representation of the parameter's definition, not of the
90 parameter's value.
91
92 """
93
94 __DESC_KEY = "desc"
95 __REQUIRED_KEY = "required"
96 __TYPE_KEY = "type"
97 __DEFAULT_KEY = "default"
98
99 __name = None
100 __desc = None
101 __required = None
102 __default_val = None
103 __type_str = None
104
105
106 __PARAM_STRING = "string"
107 __PARAM_PASSWORD = "password"
108 __PARAM_BOOL = "boolean"
109 __PARAM_DATE = "date"
110 __PARAM_TIME = "time"
111 __PARAM_NUMBER = "number"
112 __PARAM_URI = "uri"
113
114
115
116
117
118
119 __TYPE_CONVERT = {
120 __PARAM_STRING : ([str, unicode], None),
121 __PARAM_PASSWORD : ([str, unicode], None),
122 __PARAM_BOOL : ([str, unicode, bool], _bool_convert),
123 __PARAM_DATE : ([None], lambda x : date(*[ int(elem) for elem in x.split("-")])),
124 __PARAM_TIME : ([None], lambda x : time_class(*[ int(elem) for elem in x.split(":")])),
125 __PARAM_NUMBER : ([str, unicode, int, float], _numstr_to_num),
126 __PARAM_URI : ([str, unicode], None),
127 }
128
130 """
131 Return a string representation of this paramater.
132
133 """
134 return \
135 """RestxParameter '%s':
136 Description: %s
137 Type: %s
138 Required: %s
139 Default val: %s""" % (self.__name, self.__desc, self.__type_str, self.__required, self.__default_val)
140
179
181 """
182 Check whether this is a valid value for this parameter.
183
184 Raises an exception if there's a problem.
185
186 @param value: Some value object.
187 @type value: object
188
189 """
190 suitable_types, conversion_function = self.__TYPE_CONVERT[self.__type_str]
191 if type(value) not in suitable_types:
192 raise RestxClientException("Type '%s' is not suitable for parameter '%s'. Has to be one of '%s'." % (type(value), self.__name, suitable_types))
193
195 """
196 Return the name of the parameter.
197
198 @return: Name of parameter.
199 @rtype: string
200
201 """
202 return self.__name
203
205 """
206 Return the description of the parameter.
207
208 @return: Description of parameter.
209 @rtype: string
210
211 """
212 return self.__desc
213
215 """
216 Return the string representation of the type of this parameter.
217
218 @return: String representation of parameter type.
219 @rtype: string
220
221 """
222 return self.__type_str
223
225 """
226 Return the default value, if one was set.
227
228 @return: The default value, or None.
229 @rtype: Whichever type the default value has.
230
231 """
232 return self.__default_val
233
235 """
236 Indicate whether this is a required parameter.
237
238 @return: Flag, which is True if required, False otherwise.
239 @rtype: boolean
240
241 """
242 return self.__required
243
244
245
246
247 name = property(get_name, None)
248 description = property(get_description, None)
249 required = property(is_required, None)
250 type_str = property(get_parameter_type_str, None)
251