diff --git a/gramps/gen/lib/address.py b/gramps/gen/lib/address.py index 352e4a5de..a8a4185af 100644 --- a/gramps/gen/lib/address.py +++ b/gramps/gen/lib/address.py @@ -89,7 +89,8 @@ class Address(SecondaryObject, PrivacyBase, CitationBase, NoteBase, DateBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": PrivacyBase.serialize(self), + return {"_class": "Address", + "private": PrivacyBase.serialize(self), "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "date": DateBase.to_struct(self), diff --git a/gramps/gen/lib/attribute.py b/gramps/gen/lib/attribute.py index c6c16139b..f21c5fd87 100644 --- a/gramps/gen/lib/attribute.py +++ b/gramps/gen/lib/attribute.py @@ -96,7 +96,8 @@ class AttributeRoot(SecondaryObject, PrivacyBase): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": PrivacyBase.serialize(self), + return {"_class": self.__class__.__name__, + "private": PrivacyBase.serialize(self), "type": self.type.to_struct(), "value": self.value} @@ -264,7 +265,8 @@ class Attribute(AttributeRoot, CitationBase, NoteBase): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": PrivacyBase.serialize(self), + return {"_class": "Attribute", + "private": PrivacyBase.serialize(self), "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "type": self.type.to_struct(), diff --git a/gramps/gen/lib/childref.py b/gramps/gen/lib/childref.py index 7fa8ed36f..6c7736dc3 100644 --- a/gramps/gen/lib/childref.py +++ b/gramps/gen/lib/childref.py @@ -96,7 +96,8 @@ class ChildRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": PrivacyBase.to_struct(self), + return {"_class": "ChildRef", + "private": PrivacyBase.to_struct(self), "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "ref": RefBase.to_struct(self), diff --git a/gramps/gen/lib/citation.py b/gramps/gen/lib/citation.py index 10afd66b1..94130eda5 100644 --- a/gramps/gen/lib/citation.py +++ b/gramps/gen/lib/citation.py @@ -118,7 +118,8 @@ class Citation(MediaBase, NoteBase, SrcAttributeBase, IndirectCitationBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Citation", self.handle), # 0 + return {"_class": "Citation", + "handle": Handle("Citation", self.handle), # 0 "gramps_id": self.gramps_id, # 1 "date": DateBase.to_struct(self), # 2 "page": cuni(self.page), # 3 diff --git a/gramps/gen/lib/date.py b/gramps/gen/lib/date.py index 9cf39b63e..03f53dff0 100644 --- a/gramps/gen/lib/date.py +++ b/gramps/gen/lib/date.py @@ -671,7 +671,8 @@ class Date(object): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"calendar": self.calendar, + return {"_class": "Date", + "calendar": self.calendar, "modifier": self.modifier, "quality": self.quality, "dateval": self.dateval, diff --git a/gramps/gen/lib/event.py b/gramps/gen/lib/event.py index 42dded428..d5410680f 100644 --- a/gramps/gen/lib/event.py +++ b/gramps/gen/lib/event.py @@ -139,7 +139,8 @@ class Event(CitationBase, NoteBase, MediaBase, AttributeBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Event", self.handle), + return {"_class": "Event", + "handle": Handle("Event", self.handle), "gramps_id": self.gramps_id, "type": self.__type.to_struct(), "date": DateBase.to_struct(self), diff --git a/gramps/gen/lib/eventref.py b/gramps/gen/lib/eventref.py index 4cb71e8df..63d66e456 100644 --- a/gramps/gen/lib/eventref.py +++ b/gramps/gen/lib/eventref.py @@ -101,6 +101,7 @@ class EventRef(PrivacyBase, NoteBase, AttributeBase, RefBase, :rtype: dict """ return { + "_class": "EventRef", "private": PrivacyBase.to_struct(self), "note_list": NoteBase.to_struct(self), "attribute_list": AttributeBase.to_struct(self), diff --git a/gramps/gen/lib/family.py b/gramps/gen/lib/family.py index 4fd3e46d4..be0e60344 100644 --- a/gramps/gen/lib/family.py +++ b/gramps/gen/lib/family.py @@ -148,7 +148,8 @@ class Family(CitationBase, NoteBase, MediaBase, AttributeBase, LdsOrdBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Family", self.handle), + return {"_class": "Family", + "handle": Handle("Family", self.handle), "gramps_id": self.gramps_id, "father_handle": Handle("Person", self.father_handle), "mother_handle": Handle("Person", self.mother_handle), diff --git a/gramps/gen/lib/grampstype.py b/gramps/gen/lib/grampstype.py index c911deb4d..ddd701fb3 100644 --- a/gramps/gen/lib/grampstype.py +++ b/gramps/gen/lib/grampstype.py @@ -231,7 +231,8 @@ class GrampsType(GrampsTypeC): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"value": self.__value, + return {"_class": self.__class__.__name__, + "value": self.__value, "string": str(self)} @classmethod diff --git a/gramps/gen/lib/ldsord.py b/gramps/gen/lib/ldsord.py index 1edfba9ae..cd9d943bf 100644 --- a/gramps/gen/lib/ldsord.py +++ b/gramps/gen/lib/ldsord.py @@ -166,7 +166,8 @@ class LdsOrd(SecondaryObject, CitationBase, NoteBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"citation_list": CitationBase.to_struct(self), + return {"_class": "LdsOrd", + "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "date": DateBase.to_struct(self), "type": self.type, diff --git a/gramps/gen/lib/location.py b/gramps/gen/lib/location.py index 1aca25e7f..627abec5b 100644 --- a/gramps/gen/lib/location.py +++ b/gramps/gen/lib/location.py @@ -85,7 +85,8 @@ class Location(SecondaryObject, LocationBase): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"street": self.street, + return {"_class": "Location", + "street": self.street, "locality": self.locality, "city": self.city, "country": self.county, diff --git a/gramps/gen/lib/locationbase.py b/gramps/gen/lib/locationbase.py index 8b61adbb7..dcdef4ac8 100644 --- a/gramps/gen/lib/locationbase.py +++ b/gramps/gen/lib/locationbase.py @@ -87,6 +87,7 @@ class LocationBase(object): :rtype: dict """ return { + "_class": "LocationBase", "street": self.street, "locality": self.locality, "city": self.city, diff --git a/gramps/gen/lib/mediaobj.py b/gramps/gen/lib/mediaobj.py index c3179f373..66291fa8d 100644 --- a/gramps/gen/lib/mediaobj.py +++ b/gramps/gen/lib/mediaobj.py @@ -143,7 +143,8 @@ class MediaObject(CitationBase, NoteBase, DateBase, AttributeBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Media", self.handle), + return {"_class": "MediaObject", + "handle": Handle("Media", self.handle), "gramps_id": self.gramps_id, "path": self.path, "mime": self.mime, diff --git a/gramps/gen/lib/mediaref.py b/gramps/gen/lib/mediaref.py index a448b7baf..af806d32f 100644 --- a/gramps/gen/lib/mediaref.py +++ b/gramps/gen/lib/mediaref.py @@ -91,7 +91,8 @@ class MediaRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": PrivacyBase.serialize(self), + return {"_class": "MediaRef", + "private": PrivacyBase.serialize(self), "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "attribute_list": AttributeBase.to_struct(self), diff --git a/gramps/gen/lib/name.py b/gramps/gen/lib/name.py index 47af5fa2d..2a842e191 100644 --- a/gramps/gen/lib/name.py +++ b/gramps/gen/lib/name.py @@ -147,7 +147,8 @@ class Name(SecondaryObject, PrivacyBase, SurnameBase, CitationBase, NoteBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": PrivacyBase.to_struct(self), + return {"_class": "Name", + "private": PrivacyBase.to_struct(self), "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "date": DateBase.to_struct(self), diff --git a/gramps/gen/lib/note.py b/gramps/gen/lib/note.py index 053dce846..bfc75fe7d 100644 --- a/gramps/gen/lib/note.py +++ b/gramps/gen/lib/note.py @@ -119,7 +119,8 @@ class Note(BasicPrimaryObject): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Note", self.handle), + return {"_class": "Note", + "handle": Handle("Note", self.handle), "gramps_id": self.gramps_id, "text": self.text.to_struct(), "format": self.format, diff --git a/gramps/gen/lib/person.py b/gramps/gen/lib/person.py index 883aec285..6c14860aa 100644 --- a/gramps/gen/lib/person.py +++ b/gramps/gen/lib/person.py @@ -183,6 +183,7 @@ class Person(CitationBase, NoteBase, AttributeBase, MediaBase, :rtype: dict """ return { + "_class": "Person", "handle": Handle("Person", self.handle), # 0 "gramps_id": self.gramps_id, # 1 "gender": self.gender, # 2 diff --git a/gramps/gen/lib/personref.py b/gramps/gen/lib/personref.py index f600ba249..ab64c8cb8 100644 --- a/gramps/gen/lib/personref.py +++ b/gramps/gen/lib/personref.py @@ -93,7 +93,8 @@ class PersonRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": PrivacyBase.to_struct(self), + return {"_class": "PersonRef", + "private": PrivacyBase.to_struct(self), "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "ref": RefBase.to_struct(self), diff --git a/gramps/gen/lib/place.py b/gramps/gen/lib/place.py index c51580d90..ff1a94f96 100644 --- a/gramps/gen/lib/place.py +++ b/gramps/gen/lib/place.py @@ -135,7 +135,8 @@ class Place(CitationBase, NoteBase, MediaBase, UrlBase, PrimaryObject): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Place", self.handle), + return {"_class": "Place", + "handle": Handle("Place", self.handle), "gramps_id": self.gramps_id, "title": self.title, "long": self.long, diff --git a/gramps/gen/lib/placeref.py b/gramps/gen/lib/placeref.py index a9ec9cd97..9482ae4cb 100644 --- a/gramps/gen/lib/placeref.py +++ b/gramps/gen/lib/placeref.py @@ -85,6 +85,7 @@ class PlaceRef(RefBase, DateBase, SecondaryObject): :rtype: dict """ return { + "_class": "PlaceRef", "ref": RefBase.to_struct(self), "date": DateBase.to_struct(self) } diff --git a/gramps/gen/lib/repo.py b/gramps/gen/lib/repo.py index 89f830ee0..9c54a154b 100644 --- a/gramps/gen/lib/repo.py +++ b/gramps/gen/lib/repo.py @@ -92,7 +92,8 @@ class Repository(NoteBase, AddressBase, UrlBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Repository", self.handle), + return {"_class": "Repository", + "handle": Handle("Repository", self.handle), "gramps_id": self.gramps_id, "type": self.type.to_struct(), "name": cuni(self.name), diff --git a/gramps/gen/lib/reporef.py b/gramps/gen/lib/reporef.py index 035ed07e6..deb23dd70 100644 --- a/gramps/gen/lib/reporef.py +++ b/gramps/gen/lib/reporef.py @@ -91,6 +91,7 @@ class RepoRef(SecondaryObject, PrivacyBase, NoteBase, RefBase): :rtype: dict """ return { + "_class": "RepositoryRef", "note_list": NoteBase.to_struct(self), "ref": RefBase.to_struct(self), "call_number": self.call_number, diff --git a/gramps/gen/lib/researcher.py b/gramps/gen/lib/researcher.py index 720d541fb..9a3ec3513 100644 --- a/gramps/gen/lib/researcher.py +++ b/gramps/gen/lib/researcher.py @@ -82,7 +82,8 @@ class Researcher(LocationBase): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"street": self.street, + return {"_class": "Researcher", + "street": self.street, "locality": self.locality, "city": self.city, "country": self.county, diff --git a/gramps/gen/lib/src.py b/gramps/gen/lib/src.py index 1c02b28f3..16e2254d1 100644 --- a/gramps/gen/lib/src.py +++ b/gramps/gen/lib/src.py @@ -102,7 +102,8 @@ class Source(MediaBase, NoteBase, SrcAttributeBase, IndirectCitationBase, :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Source", self.handle), + return {"_class": "Source", + "handle": Handle("Source", self.handle), "gramps_id": self.gramps_id, "title": cuni(self.title), "author": cuni(self.author), diff --git a/gramps/gen/lib/styledtext.py b/gramps/gen/lib/styledtext.py index 12293aee8..827ba2fd4 100644 --- a/gramps/gen/lib/styledtext.py +++ b/gramps/gen/lib/styledtext.py @@ -313,7 +313,8 @@ class StyledText(object): else: the_tags = [] - return {"string": self._string, + return {"_class": "StyledText", + "string": self._string, "tags": the_tags} @classmethod diff --git a/gramps/gen/lib/styledtexttag.py b/gramps/gen/lib/styledtexttag.py index 5d81263cc..66baaf5d4 100644 --- a/gramps/gen/lib/styledtexttag.py +++ b/gramps/gen/lib/styledtexttag.py @@ -94,7 +94,8 @@ class StyledTextTag(object): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"name": self.name.to_struct(), + return {"_class": "StyledTextTag", + "name": self.name.to_struct(), "value": self.value, "ranges": self.ranges} diff --git a/gramps/gen/lib/surname.py b/gramps/gen/lib/surname.py index 529a79a64..3be7333af 100644 --- a/gramps/gen/lib/surname.py +++ b/gramps/gen/lib/surname.py @@ -92,7 +92,8 @@ class Surname(SecondaryObject): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"surname": self.surname, + return {"_class": "Surname", + "surname": self.surname, "prefix": self.prefix, "primary": self.primary, "origintype": self.origintype.to_struct(), diff --git a/gramps/gen/lib/tag.py b/gramps/gen/lib/tag.py index 0bf7a1450..890c9dfd2 100644 --- a/gramps/gen/lib/tag.py +++ b/gramps/gen/lib/tag.py @@ -221,7 +221,8 @@ class Tag(TableObject): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"handle": Handle("Tag", self.handle), + return {"_class": "Tag", + "handle": Handle("Tag", self.handle), "name": self.__name, "color": self.__color, "priority": self.__priority, diff --git a/gramps/gen/lib/test/struct_test.py b/gramps/gen/lib/test/struct_test.py index fbe726d83..bfb837f21 100644 --- a/gramps/gen/lib/test/struct_test.py +++ b/gramps/gen/lib/test/struct_test.py @@ -23,8 +23,8 @@ import unittest from .. import (Person, Family, Event, Source, Place, Citation, - Repository, MediaObject, Note) -from gramps.gen.merge.diff import import_as_dict + Repository, MediaObject, Note, Tag) +from gramps.gen.merge.diff import import_as_dict, from_struct from gramps.cli.user import User class BaseCheck: @@ -82,6 +82,11 @@ class NoteCheck(unittest.TestCase, BaseCheck): self.cls = Note self.object = self.cls() +class TagCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Tag + self.object = self.cls() + class DatabaseCheck(unittest.TestCase): maxDiff = None @@ -95,6 +100,11 @@ def generate_test(obj): self.assertEqual(obj.serialize(), serialized) name = "test_%s_%s" % (obj.__class__.__name__, obj.handle) setattr(DatabaseCheck, name, test) + #### + def test2(self): + self.assertEqual(obj.serialize(), from_struct(struct).serialize()) + name = "test_create_%s_%s" % (obj.__class__.__name__, obj.handle) + setattr(DatabaseCheck, name, test2) db = import_as_dict("example/gramps/example.gramps", User()) for table in db._tables.keys(): diff --git a/gramps/gen/lib/url.py b/gramps/gen/lib/url.py index 44bb7a20c..ebce90c02 100644 --- a/gramps/gen/lib/url.py +++ b/gramps/gen/lib/url.py @@ -93,7 +93,8 @@ class Url(SecondaryObject, PrivacyBase): :returns: Returns a struct containing the data of the object. :rtype: dict """ - return {"private": self.private, + return {"_class": "Url", + "private": self.private, "path": self.path, "desc": self.desc, "type": self.type.to_struct()} diff --git a/gramps/gen/merge/diff.py b/gramps/gen/merge/diff.py index dd8fe3cec..147a8f78f 100644 --- a/gramps/gen/merge/diff.py +++ b/gramps/gen/merge/diff.py @@ -184,3 +184,29 @@ def diff_db_to_file(old_db, filename, user=None): diffs, m_old, m_new = diff_dbs(old_db, new_db, user) return diffs, m_old, m_new +def from_struct(struct): + from gramps.gen.lib import (Person, Family, Event, Source, Place, Citation, + Repository, MediaObject, Note, Tag) + if isinstance(struct, dict): + if "_class" in struct.keys(): + if struct["_class"] == "Person": + return Person.create(Person.from_struct(struct)) + elif struct["_class"] == "Family": + return Family.create(Family.from_struct(struct)) + elif struct["_class"] == "Event": + return Event.create(Event.from_struct(struct)) + elif struct["_class"] == "Source": + return Source.create(Source.from_struct(struct)) + elif struct["_class"] == "Place": + return Place.create(Place.from_struct(struct)) + elif struct["_class"] == "Citation": + return Citation.create(Citation.from_struct(struct)) + elif struct["_class"] == "Repository": + return Repository.create(Repository.from_struct(struct)) + elif struct["_class"] == "MediaObject": + return MediaObject.create(MediaObject.from_struct(struct)) + elif struct["_class"] == "Note": + return Note.create(Note.from_struct(struct)) + elif struct["_class"] == "Tag": + return Tag.create(Tag.from_struct(struct)) + raise AttributeError("invalid struct")