def process_value(self, value, property, prefix=""): unique = property.get('unique', True) expected_type = property.expected_type.key at = {"key": self.key, "property": prefix + property.name} if isinstance(value, list): if unique is True: raise common.BadData(message='expected atom, found list', at=at, value=value) p = web.storage(property.copy()) p.unique = True return [self.process_value(v, p) for v in value] if unique is False: raise common.BadData(message='expected list, found atom', at=at, value=value) type_found = common.find_type(value) if expected_type in common.primitive_types: # string can be converted to any type and int can be converted to float try: if type_found == '/type/string' and expected_type != '/type/string': value = common.primitive_types[expected_type](value) elif type_found == '/type/int' and expected_type == '/type/float': value = float(value) except ValueError, e: raise common.BadData(message=str(e), at=at, value=value)
def process_value(self, value, property, prefix=""): unique = property.get("unique", True) expected_type = property.expected_type.key at = {"key": self.key, "property": prefix + property.name} if isinstance(value, list): if unique is True: raise common.BadData(message="expected atom, found list", at=at, value=value) p = web.storage(property.copy()) p.unique = True return [self.process_value(v, p) for v in value] if unique is False: raise common.BadData(message="expected list, found atom", at=at, value=value) type_found = common.find_type(value) if expected_type in common.primitive_types: # string can be converted to any type and int can be converted to float try: if type_found == "/type/string" and expected_type != "/type/string": value = common.primitive_types[expected_type](value) elif type_found == "/type/int" and expected_type == "/type/float": value = float(value) except ValueError, e: raise common.BadData(message=str(e), at=at, value=value)
def process_value(self, value, property): """ >>> def property(expected_type, unique=True): ... return web.storage(expected_type=web.storage(key=expected_type, kind='regular'), unique=unique) ... >>> p = SaveProcessor(common.create_test_store(), None) >>> p.process_value(1, property('/type/int')) 1 >>> p.process_value('1', property('/type/int')) 1 >>> p.process_value(['1', '2'], property('/type/int', unique=False)) [1, 2] >>> p.process_value('x', property('/type/int')) Traceback (most recent call last): ... BadData: {"message": "invalid literal for int() with base 10: 'x'", "error": "bad_data"} >>> p.process_value('1', property('/type/int', unique=False)) Traceback (most recent call last): ... BadData: {"message": "expected list, found atom", "error": "bad_data"} >>> p.process_value(['1'], property('/type/int')) Traceback (most recent call last): ... BadData: {"message": "expected atom, found list", "error": "bad_data"} >>> p.process_value('/type/string', property('/type/type')) <ref: u'/type/string'> """ unique = property.get('unique', True) expected_type = property.expected_type.key if isinstance(value, list): if unique is True: raise common.BadData(message='expected atom, found list', property=property.name) p = web.storage(property.copy()) p.unique = True return [self.process_value(v, p) for v in value] if unique is False: raise common.BadData(message='expected list, found atom', property=property.name) type_found = common.find_type(value) if expected_type in common.primitive_types: # string can be converted to any type and int can be converted to float try: if type_found == '/type/string' and expected_type != '/type/string': value = common.primitive_types[expected_type](value) elif type_found == '/type/int' and expected_type == '/type/float': value = float(value) except ValueError, e: raise common.BadData(message=str(e))
if type_found == '/type/string' and expected_type != '/type/string': value = common.primitive_types[expected_type](value) elif type_found == '/type/int' and expected_type == '/type/float': value = float(value) except ValueError, e: raise common.BadData(message=str(e)) elif property.expected_type.kind == 'embeddable': if isinstance(value, dict): return self.process_data(value, property.expected_type) else: raise common.TypeMismatch(expected_type, type_found) else: if type_found == '/type/string': value = common.Reference(value) type_found = common.find_type(value) if type_found == '/type/object': thing = get_thing(self.store, value) if thing is None: raise common.NotFound(key=value) type_found = thing.type.key if expected_type != type_found: raise common.BadData(message='expected %s, found %s' % (repr(property.expected_type.key), repr(type_found))) return value class WriteQueryProcessor: def __init__(self, store, author): self.store = store self.author = author
if type_found == '/type/string' and expected_type != '/type/string': value = common.primitive_types[expected_type](value) elif type_found == '/type/int' and expected_type == '/type/float': value = float(value) except ValueError, e: raise common.BadData(message=str(e), at=at, value=value) elif property.expected_type.kind == 'embeddable': if isinstance(value, dict): return self.process_data(value, property.expected_type, prefix=at['property'] + ".") else: raise common.TypeMismatch(expected_type, type_found, at=at, value=value) else: if type_found == '/type/string': value = common.Reference(value) type_found = common.find_type(value) if type_found == '/type/object': type_found = self.get_type(value) # type is not found only when the thing id not found. if type_found is None: raise common.NotFound(key=unicode(value), at=at) if expected_type != type_found: raise common.BadData(message='expected %s, found %s' % (property.expected_type.key, type_found), at=at, value=value) return value class WriteQueryProcessor: def __init__(self, store, author):