218def map_coercion(class_name, schema):
219 """Given a schema of a class, maps coercion functions from libpyconffwk."""
220
221 cls = ConfigObject.ConfigObject
222
223 schema['mapping'] = {}
224
225 for k, v in list(schema['attribute'].items()):
226 typename = v['type']
227 getname = v['type']
228 if getname in oks_types['string']:
229 getname = 'string'
230 if v['multivalue']:
231 typename += '_vec'
232 getname += '_vec'
233 v['co_get_method'] = getattr(cls, 'get_' + getname)
234 v['co_set_method'] = getattr(cls, 'set_' + typename)
235
236 else:
237 v['co_get_method'] = getattr(cls, 'get_' + getname)
238 v['co_set_method'] = getattr(cls, 'set_' + typename)
239
240 if v['type'] in oks_types['string']:
241 v['python_class'] = str
242 elif v['type'] in oks_types['bool']:
243 v['python_class'] = bool
244 elif v['type'] in oks_types['integer']:
245 v['python_class'] = to_int
246 elif v['type'] in oks_types['long']:
247 v['python_class'] = to_long
248 elif v['type'] in oks_types['float']:
249 v['python_class'] = float
250
251
252 v['range_re'] = None
253 if v['range']:
254 if v['type'] == 'string':
255 v['range_re'] = re.compile(v['range'])
256 else:
257 v['range'] = [decode_range(j) for j in v['range'].split(',')]
258 for j in range(len(v['range'])):
259 if isinstance(v['range'][j], str):
260 v['range'][j] = v['python_class'](v['range'][j])
261 elif len(v['range'][j]) == 1:
262 v['range'][j] = v['python_class'](v['range'][j][0])
263 else:
264 v['range'][j] = (v['python_class'](v['range'][j][0]),
265 v['python_class'](v['range'][j][1]))
266
267
268 elif v['type'] in oks_types['int-number']:
269 if v['type'] == 's8':
270 v['range'] = [(-2**7, (2**7)-1)]
271 if v['type'] == 'u8':
272 v['range'] = [(0, (2**8)-1)]
273 if v['type'] == 's16':
274 v['range'] = [(-2**15, (2**15)-1)]
275 if v['type'] == 'u16':
276 v['range'] = [(0, (2**16)-1)]
277 if v['type'] == 's32':
278 v['range'] = [(-2**31, (2**31)-1)]
279 if v['type'] == 'u32':
280 v['range'] = [(0, (2**32)-1)]
281 if v['type'] == 's64':
282 v['range'] = [(-2**63, (2**63)-1)]
283 if v['type'] == 'u64':
284 v['range'] = [(0, (2**64)-1)]
285
286
287 if v['init-value']:
288 try:
289 if v['type'] in oks_types['string']:
290 pass
291 elif v['multivalue']:
292 v['init-value'] = [coerce(j, v)
293 for j in v['init-value'].split(',')]
294 else:
295 v['init-value'] = coerce(v['init-value'], v)
296 except ValueError as e:
297 logging.warning('Initial value of "%s.%s" could not be '
298 'coerced: %s' %
299 (class_name, k, e))
300
301
302
303 if not v['init-value'] and v['type'] == 'date':
304 import datetime
305 v['init-value'] == datetime.date.today().isoformat()
306
307
308
309 if not v['init-value'] and v['type'] == 'time':
310 import datetime
311 now = datetime.datetime.today()
312 now.replace(microsecond=0)
313 v['init-value'] == now.isoformat(sep=' ')
314
315 for v in list(schema['relation'].values()):
316 if v['multivalue']:
317 v['co_get_method'] = getattr(cls, 'get_objs')
318 v['co_set_method'] = getattr(cls, 'set_objs')
319
320 else:
321 v['co_get_method'] = getattr(cls, 'get_obj')
322 v['co_set_method'] = getattr(cls, 'set_obj')
323
324 return schema
325
326