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