+def verifyDBrecord(constraints, record):
+ """
+ constraints is a list of key, value pairs.
+ # [ {... : ...}==AND , ... , ... , ] == OR
+ """
+ def has_key(obj, key):
+ try:
+ x = obj.__getattribute__(key)
+ return True
+ except:
+ return False
+
+ def get_val(obj, key):
+ try:
+ return obj.__getattribute__(key)
+ except:
+ return None
+
+ def get(obj, path):
+ indexes = path.split("/")
+ value = get_val(obj,indexes[0])
+ if value is not None and len(indexes) > 1:
+ for key in indexes[1:]:
+ if key in value:
+ value = value[key]
+ else:
+ raise NoKeyException(key)
+ return value
+
+ #print constraints, record
+
+ con_or_true = False
+ for con in constraints:
+ #print "con: %s" % con
+ if len(con.keys()) == 0:
+ con_and_true = False
+ else:
+ con_and_true = True
+
+ for key in con.keys():
+ #print "looking at key: %s" % key
+ if has_key(record, key):
+ value_re = re.compile(con[key])
+ if type([]) == type(get(record,key)):
+ local_or_true = False
+ for val in get(record,key):
+ local_or_true = local_or_true | (value_re.search(val) is not None)
+ con_and_true = con_and_true & local_or_true
+ else:
+ if get(record,key) is not None:
+ con_and_true = con_and_true & (value_re.search(get(record,key)) is not None)
+ else:
+ print "missing key %s" % key,
+ pass
+
+ con_or_true = con_or_true | con_and_true
+
+ return con_or_true
+
+def verify(constraints, data):
+ """
+ constraints is a list of key, value pairs.
+ # [ {... : ...}==AND , ... , ... , ] == OR
+ """
+ con_or_true = False
+ for con in constraints:
+ #print "con: %s" % con
+ if len(con.keys()) == 0:
+ con_and_true = False
+ else:
+ con_and_true = True
+
+ for key in con.keys():
+ #print "looking at key: %s" % key
+ if first(key) in data:
+ value_re = re.compile(con[key])
+ if type([]) == type(get(data,key)):
+ local_or_true = False
+ for val in get(data,key):
+ local_or_true = local_or_true | (value_re.search(val) is not None)
+ con_and_true = con_and_true & local_or_true
+ else:
+ if get(data,key) is not None:
+ con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
+ elif first(key) not in data:
+ print "missing key %s" % first(key)
+
+ con_or_true = con_or_true | con_and_true
+
+ return con_or_true
+