def obj_to_doc(obj): """ Convert a Gramps object into a MongoDB document. """ doc = json.loads(to_json(obj)) doc['_id'] = doc['handle'] del doc['handle'] return doc
def _set_metadata(self, key, value): """ key: string value: item, will be serialized here """ type_name = type(value).__name__ if type_name in ('set', 'tuple'): value = list(value) elif type_name not in ('int', 'str', 'list'): value = json.loads(to_json(value)) self.db.metadata.update_one( {"setting": key}, {"$set": {"value": value, "type": type_name}}, upsert=True)
def old_unchanged(self, db: DbReadBase, class_name: str, handle: str, old_data: Dict) -> bool: """Check if the "old" object is still unchanged.""" handle_func = db.method("get_%s_from_handle", class_name) try: obj = handle_func(handle) except HandleError: if old_data is None: return True return False obj_dict = json.loads(to_json(obj)) if diff_items(class_name, old_data, obj_dict): return False return True
def transaction_to_json(transaction: DbTxn) -> List[Dict[str, Any]]: """Return a JSON representation of a database transaction.""" out = [] for recno in transaction.get_recnos(reverse=False): key, action, handle, old_data, new_data = transaction.get_record(recno) try: obj_cls_name = KEY_TO_CLASS_MAP[key] except KeyError: continue # this happens for references trans_dict = {TXNUPD: "update", TXNDEL: "delete", TXNADD: "add"} obj_cls = getattr(gramps.gen.lib, obj_cls_name) if old_data: old_data = obj_cls().unserialize(old_data) if new_data: new_data = obj_cls().unserialize(new_data) item = { "type": trans_dict[action], "handle": handle, "_class": obj_cls_name, "old": json.loads(to_json(old_data)), "new": json.loads(to_json(new_data)), } out.append(item) return out
def _set_metadata(self, key, value): """ key: string value: item, will be serialized here """ type_name = type(value).__name__ if type_name in ('set', 'tuple'): value = list(value) elif type_name not in ('int', 'str', 'list'): value = json.loads(to_json(value)) self.db.metadata.update_one( {"setting": key}, {"$set": { "value": value, "type": type_name }}, upsert=True)
def write_line(fp, obj): """ Write a single object to the file. """ fp.write(to_json(obj) + "\n")
def hash_object(obj: GrampsObject) -> str: """Generate a SHA256 hash for a Gramps object's data.""" data = to_json(obj).encode() return sha256(data).hexdigest()