diff --git a/gramps/gen/lib/address.py b/gramps/gen/lib/address.py index 5fd0d6383..0b21a0a05 100644 --- a/gramps/gen/lib/address.py +++ b/gramps/gen/lib/address.py @@ -92,14 +92,22 @@ class Address(SecondaryObject, PrivacyBase, CitationBase, NoteBase, DateBase, "citation_list": CitationBase.to_struct(self), "note_list": NoteBase.to_struct(self), "date": DateBase.to_struct(self), - "street": self.street, - "locality": self.locality, - "city": self.city, - "country": self.county, - "state": self.state, - "country": self.country, - "postal": self.postal, - "phone": self.phone} + "location": LocationBase.to_struct(self) + } + + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (PrivacyBase.from_struct(struct["private"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + DateBase.from_struct(struct["date"]), + LocationBase.from_struct(struct["location"]) + ) def unserialize(self, data): """ diff --git a/gramps/gen/lib/addressbase.py b/gramps/gen/lib/addressbase.py index c1cf220a8..ba8a73c83 100644 --- a/gramps/gen/lib/addressbase.py +++ b/gramps/gen/lib/addressbase.py @@ -83,6 +83,15 @@ class AddressBase(object): """ return [addr.to_struct() for addr in self.address_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [Address.from_struct(addr) for addr in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/attrbase.py b/gramps/gen/lib/attrbase.py index 21e9c6817..56b8b7cf0 100644 --- a/gramps/gen/lib/attrbase.py +++ b/gramps/gen/lib/attrbase.py @@ -90,6 +90,15 @@ class AttributeRootBase(object): """ return [attr.to_struct() for attr in self.attribute_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [cls._CLASS.from_struct(attr) for attr in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/attribute.py b/gramps/gen/lib/attribute.py index fa43d39ef..ad1ba05ca 100644 --- a/gramps/gen/lib/attribute.py +++ b/gramps/gen/lib/attribute.py @@ -100,6 +100,17 @@ class AttributeRoot(SecondaryObject, PrivacyBase): "type": self.type.to_struct(), "value": self.value} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (PrivacyBase.from_struct(struct["private"]), + AttributeType.from_struct(struct["type"]), + struct["value"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. @@ -258,6 +269,19 @@ class Attribute(AttributeRoot, CitationBase, NoteBase): "type": self.type.to_struct(), "value": self.value} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (PrivacyBase.from_struct(struct["private"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + AttributeType.from_struct(struct["type"]), + struct["value"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/baseobj.py b/gramps/gen/lib/baseobj.py index 27ca380d2..b73512fa3 100644 --- a/gramps/gen/lib/baseobj.py +++ b/gramps/gen/lib/baseobj.py @@ -58,6 +58,13 @@ class BaseObject(object): """ assert False, "Needs to be overridden in the derived class" + def unserialize(self, data): + """ + Convert a serialized tuple of data to an object. + """ + assert False, "Needs to be overridden in the derived class" + return self + def to_struct(self): """ Convert the data held in this object to a structure (eg, @@ -79,13 +86,22 @@ class BaseObject(object): """ assert False, "Needs to be overridden in the derived class" - def unserialize(self, data): + def from_struct(self, struct): """ - Convert a serialized tuple of data to an object. + Given a struct data representation, return an object of this type. + + These structures may be primitive Python types (string, + integer, boolean, etc.) or complex Python types (lists, + tuples, or dicts). If the return type is a dict, then the keys + of the dict match the fieldname of the object. If the return + struct (or value of a dict key) is a list, then it is a list + of structs. Otherwise, the struct is just the value of the + attribute. + + :returns: Returns an object of this type. """ assert False, "Needs to be overridden in the derived class" - return self - + def matches_string(self, pattern, case_sensitive=False): """ Return True if any text data in the object or any of it's child diff --git a/gramps/gen/lib/childref.py b/gramps/gen/lib/childref.py index 68c84de1a..4c1e0d880 100644 --- a/gramps/gen/lib/childref.py +++ b/gramps/gen/lib/childref.py @@ -102,6 +102,20 @@ class ChildRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase): "frel": self.frel.to_struct(), "mrel": self.mrel.to_struct()} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (PrivacyBase.from_struct(struct["private"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + RefBase.from_struct(struct["ref"]), + ChildRefType.from_struct(struct["frel"]), + ChildRefType.from_struct(struct["mrel"])) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/citation.py b/gramps/gen/lib/citation.py index 56d366a6f..b7b9a4433 100644 --- a/gramps/gen/lib/citation.py +++ b/gramps/gen/lib/citation.py @@ -131,6 +131,26 @@ class Citation(MediaBase, NoteBase, SrcAttributeBase, IndirectCitationBase, "tag_list": TagBase.to_struct(self), # 10 "private": self.private} # 11 + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["gramps_id"], + DateBase.from_struct(struct["date"]), + struct["page"], + struct["confidence"], + struct["source_handle"].handle, + NoteBase.from_struct(struct["note_list"]), + MediaBase.from_struct(struct["media_list"]), + SrcAttributeBase.from_struct(struct["srcattr_list"]), + struct["change"], + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/citationbase.py b/gramps/gen/lib/citationbase.py index f568a423d..ea997f3a7 100644 --- a/gramps/gen/lib/citationbase.py +++ b/gramps/gen/lib/citationbase.py @@ -98,6 +98,15 @@ class CitationBase(object): """ return [Handle("Citation", c) for c in self.citation_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [handle.handle for handle in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/date.py b/gramps/gen/lib/date.py index 7fff65444..f9097ff86 100644 --- a/gramps/gen/lib/date.py +++ b/gramps/gen/lib/date.py @@ -679,6 +679,25 @@ class Date(object): "sortval": self.sortval, "newyear": self.newyear} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + retval = (struct["calendar"], + struct["modifier"], + struct["quality"], + struct["dateval"], + struct["text"], + struct["sortval"], + struct["newyear"]) + if retval == (0, 0, 0, (0, 0, 0, False), '', 0, 0): + return None + else: + return retval + def unserialize(self, data): """ Load from the format created by serialize. diff --git a/gramps/gen/lib/datebase.py b/gramps/gen/lib/datebase.py index 4b23b2c2b..524f91040 100644 --- a/gramps/gen/lib/datebase.py +++ b/gramps/gen/lib/datebase.py @@ -89,6 +89,15 @@ class DateBase(object): date = self.date.to_struct() return date + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return Date.from_struct(struct) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/event.py b/gramps/gen/lib/event.py index 653324ca3..74b521d2f 100644 --- a/gramps/gen/lib/event.py +++ b/gramps/gen/lib/event.py @@ -153,6 +153,28 @@ class Event(CitationBase, NoteBase, MediaBase, AttributeBase, "tag_list": TagBase.to_struct(self), "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + from .grampstype import GrampsType + return (struct["handle"].handle, + struct["gramps_id"], + EventType.from_struct(struct["type"]), + DateBase.from_struct(struct["date"]), + struct["description"], + struct["place"].handle, + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + MediaBase.from_struct(struct["media_list"]), + AttributeBase.from_struct(struct["attribute_list"]), + struct["change"], + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/eventref.py b/gramps/gen/lib/eventref.py index 872b97800..004590728 100644 --- a/gramps/gen/lib/eventref.py +++ b/gramps/gen/lib/eventref.py @@ -107,6 +107,21 @@ class EventRef(PrivacyBase, NoteBase, AttributeBase, RefBase, "role": self.__role.to_struct() } + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return ( + PrivacyBase.from_struct(struct["private"]), + NoteBase.from_struct(struct["note_list"]), + AttributeBase.from_struct(struct["attribute_list"]), + RefBase.from_struct(struct["ref"]), + EventRoleType.from_struct(struct["role"]) + ) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/family.py b/gramps/gen/lib/family.py index 78b31db28..1dc7af866 100644 --- a/gramps/gen/lib/family.py +++ b/gramps/gen/lib/family.py @@ -164,6 +164,29 @@ class Family(CitationBase, NoteBase, MediaBase, AttributeBase, LdsOrdBase, "tag_list": TagBase.to_struct(self), "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["gramps_id"], + struct["father_handle"].handle, + struct["mother_handle"].handle, + [ChildRef.from_struct(cr) for cr in struct["child_ref_list"]], + FamilyRelType.from_struct(struct["type"]), + [EventRef.from_struct(er) for er in struct["event_ref_list"]], + MediaBase.from_struct(struct["media_list"]), + AttributeBase.from_struct(struct["attribute_list"]), + LdsOrdBase.from_struct(struct["lds_ord_list"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + struct["change"], + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/grampstype.py b/gramps/gen/lib/grampstype.py index 8c82a423a..f5e8d33a4 100644 --- a/gramps/gen/lib/grampstype.py +++ b/gramps/gen/lib/grampstype.py @@ -234,6 +234,18 @@ class GrampsType(GrampsTypeC): return {"value": self.__value, "string": str(self)} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + if struct["value"] == cls._CUSTOM: + return (struct["value"], struct["string"]) + else: + return (struct["value"], '') + def unserialize(self, data): """Convert a serialized tuple of data to an object.""" self.__value, self.__string = data diff --git a/gramps/gen/lib/handle.py b/gramps/gen/lib/handle.py index b1550f695..455471323 100644 --- a/gramps/gen/lib/handle.py +++ b/gramps/gen/lib/handle.py @@ -28,13 +28,10 @@ class Handle: self.classname = classname if handle and not isinstance(handle, UNITYPE): handle = handle.decode('utf-8') - if handle: - self.handle = handle - else: - self.handle = None + self.handle = handle def __repr__(self): - return "Handle(%s, %s)" % (self.classname, self.handle) + return "Handle(%s, %s)" % (repr(self.classname), repr(self.handle)) def __str__(self): if self.handle: diff --git a/gramps/gen/lib/ldsord.py b/gramps/gen/lib/ldsord.py index 43c5c312d..76989836a 100644 --- a/gramps/gen/lib/ldsord.py +++ b/gramps/gen/lib/ldsord.py @@ -175,6 +175,23 @@ class LdsOrd(SecondaryObject, CitationBase, NoteBase, "status": self.status, "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + DateBase.from_struct(struct["date"]), + struct["type"], + struct["place"], + struct["famc"], + struct["temple"], + struct["status"], + struct["private"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/ldsordbase.py b/gramps/gen/lib/ldsordbase.py index 1927e9e70..90f13c0d9 100644 --- a/gramps/gen/lib/ldsordbase.py +++ b/gramps/gen/lib/ldsordbase.py @@ -88,6 +88,15 @@ class LdsOrdBase(object): """ return [lds_ord.to_struct() for lds_ord in self.lds_ord_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [LdsOrd.from_struct(lds_ord) for lds_ord in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object diff --git a/gramps/gen/lib/location.py b/gramps/gen/lib/location.py index 40ca8a009..236c7ac2d 100644 --- a/gramps/gen/lib/location.py +++ b/gramps/gen/lib/location.py @@ -94,6 +94,23 @@ class Location(SecondaryObject, LocationBase): "phone": self.phone, "parish": self.parish} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return ((struct["street"], + struct["locality"], + struct["city"], + struct["country"], + struct["state"], + struct["country"], + struct["postal"], + struct["phone"]), + struct["parish"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/locationbase.py b/gramps/gen/lib/locationbase.py index 62671f6e8..2836d5afb 100644 --- a/gramps/gen/lib/locationbase.py +++ b/gramps/gen/lib/locationbase.py @@ -66,6 +66,53 @@ class LocationBase(object): return (self.street, self.locality, self.city, self.county, self.state, self.country, self.postal, self.phone) + def to_struct(self): + """ + Convert the data held in this object to a structure (eg, + struct) that represents all the data elements. + + This method is used to recursively convert the object into a + self-documenting form that can easily be used for various + purposes, including diffs and queries. + + These structures may be primitive Python types (string, + integer, boolean, etc.) or complex Python types (lists, + tuples, or dicts). If the return type is a dict, then the keys + of the dict match the fieldname of the object. If the return + struct (or value of a dict key) is a list, then it is a list + of structs. Otherwise, the struct is just the value of the + attribute. + + :returns: Returns a struct containing the data of the object. + :rtype: dict + """ + return { + "street": self.street, + "locality": self.locality, + "city": self.city, + "county": self.county, + "state": self.state, + "country": self.country, + "postal": self.postal, + "phone": self.phone + } + + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["street"], + struct["locality"], + struct["city"], + struct["county"], + struct["state"], + struct["country"], + struct["postal"], + struct["phone"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/mediabase.py b/gramps/gen/lib/mediabase.py index 580ea4d22..cb70adcdf 100644 --- a/gramps/gen/lib/mediabase.py +++ b/gramps/gen/lib/mediabase.py @@ -80,6 +80,15 @@ class MediaBase(object): """ return [mref.to_struct() for mref in self.media_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [MediaRef.from_struct(mref) for mref in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/mediaobj.py b/gramps/gen/lib/mediaobj.py index f369ba247..c3832b2c7 100644 --- a/gramps/gen/lib/mediaobj.py +++ b/gramps/gen/lib/mediaobj.py @@ -157,6 +157,27 @@ class MediaObject(CitationBase, NoteBase, DateBase, AttributeBase, "tag_list": TagBase.to_struct(self), "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["gramps_id"], + struct["path"], + struct["mime"], + struct["desc"], + struct["checksum"], + AttributeBase.from_struct(struct["attribute_list"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + struct["change"], + DateBase.from_struct(struct["date"]), + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/mediaref.py b/gramps/gen/lib/mediaref.py index 068f4e682..6e13e39bd 100644 --- a/gramps/gen/lib/mediaref.py +++ b/gramps/gen/lib/mediaref.py @@ -97,6 +97,20 @@ class MediaRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase, "ref": RefBase.to_struct(self), "rect": self.rect if self.rect != (0,0,0,0) else None} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (PrivacyBase.from_struct(struct["private"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + AttributeBase.from_struct(struct["attribute_list"]), + RefBase.from_struct(struct["ref"]), + struct["rect"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/name.py b/gramps/gen/lib/name.py index 7076889f8..9004e4cfd 100644 --- a/gramps/gen/lib/name.py +++ b/gramps/gen/lib/name.py @@ -162,6 +162,29 @@ class Name(SecondaryObject, PrivacyBase, SurnameBase, CitationBase, NoteBase, "nick": self.nick, "famnick": self.famnick} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (PrivacyBase.from_struct(struct["private"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + DateBase.from_struct(struct["date"]), + struct["first_name"], + SurnameBase.from_struct(struct["surname_list"]), + struct["suffix"], + struct["title"], + NameType.from_struct(struct["type"]), + struct["group_as"], + struct["sort_as"], + struct["display_as"], + struct["call"], + struct["nick"], + struct["famnick"]) + def is_empty(self): """ Indicate if the name is empty. diff --git a/gramps/gen/lib/note.py b/gramps/gen/lib/note.py index 4f58acfef..b27a7707b 100644 --- a/gramps/gen/lib/note.py +++ b/gramps/gen/lib/note.py @@ -128,6 +128,22 @@ class Note(BasicPrimaryObject): "tag_list": TagBase.to_struct(self), "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["gramps_id"], + StyledText.from_struct(struct["text"]), + struct["format"], + NoteType.from_struct(struct["type"]), + struct["change"], + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/notebase.py b/gramps/gen/lib/notebase.py index efaee269d..92a0fd1d4 100644 --- a/gramps/gen/lib/notebase.py +++ b/gramps/gen/lib/notebase.py @@ -77,6 +77,15 @@ class NoteBase(object): """ return [Handle("Note", n) for n in self.note_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [n.handle for n in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/person.py b/gramps/gen/lib/person.py index 92d0cd6d1..85f588ed8 100644 --- a/gramps/gen/lib/person.py +++ b/gramps/gen/lib/person.py @@ -211,6 +211,37 @@ class Person(CitationBase, NoteBase, AttributeBase, MediaBase, for pr in self.person_ref_list] # 20 } + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return ( + struct["handle"].handle, + struct["gramps_id"], + struct["gender"], + Name.from_struct(struct["primary_name"]), + [Name.from_struct(name) for name in struct["alternate_names"]], + struct["death_ref_index"], + struct["birth_ref_index"], + [EventRef.from_struct(er) for er in struct["event_ref_list"]], + [handle.handle for handle in struct["family_list"]], + [handle.handle for handle in struct["parent_family_list"]], + MediaBase.from_struct(struct["media_list"]), + AddressBase.from_struct(struct["address_list"]), + AttributeBase.from_struct(struct["attribute_list"]), + UrlBase.from_struct(struct["urls"]), + LdsOrdBase.from_struct(struct["lds_ord_list"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + struct["change"], + TagBase.from_struct(struct["tag_list"]), + struct["private"], + [PersonRef.from_struct(p) for p in struct["person_ref_list"]] + ) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/personref.py b/gramps/gen/lib/personref.py index a386e44dd..cccb7fe8a 100644 --- a/gramps/gen/lib/personref.py +++ b/gramps/gen/lib/personref.py @@ -98,6 +98,19 @@ class PersonRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase): "ref": RefBase.to_struct(self), "rel": self.rel} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (PrivacyBase.from_struct(struct["private"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + RefBase.from_struct(struct["ref"]), + struct["rel"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/place.py b/gramps/gen/lib/place.py index 82b4821a9..24a6d96ca 100644 --- a/gramps/gen/lib/place.py +++ b/gramps/gen/lib/place.py @@ -152,6 +152,31 @@ class Place(CitationBase, NoteBase, MediaBase, UrlBase, PrimaryObject): "tag_list": TagBase.to_struct(self), "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["gramps_id"], + struct["title"], + struct["long"], + struct["lat"], + [PlaceRef.from_struct(pr) for pr in struct["placeref_list"]], + struct["name"], + PlaceType.from_struct(struct["place_type"]), + struct["code"], + [Location.from_struct(al) for al in struct["alt_loc"]], + UrlBase.from_struct(struct["urls"]), + MediaBase.from_struct(struct["media_list"]), + CitationBase.from_struct(struct["citation_list"]), + NoteBase.from_struct(struct["note_list"]), + struct["change"], + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/placeref.py b/gramps/gen/lib/placeref.py index 394ca5e95..f1d1848b1 100644 --- a/gramps/gen/lib/placeref.py +++ b/gramps/gen/lib/placeref.py @@ -89,6 +89,18 @@ class PlaceRef(RefBase, DateBase, SecondaryObject): "date": DateBase.to_struct(self) } + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return ( + RefBase.from_struct(struct["ref"]), + DateBase.from_struct(struct["date"]) + ) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/primaryobj.py b/gramps/gen/lib/primaryobj.py index e21387ad3..2487932de 100644 --- a/gramps/gen/lib/primaryobj.py +++ b/gramps/gen/lib/primaryobj.py @@ -266,4 +266,4 @@ class PrimaryObject(BasicPrimaryObject): Replace the handle reference with the new reference. """ pass - + diff --git a/gramps/gen/lib/privacybase.py b/gramps/gen/lib/privacybase.py index de7c5c13e..fea50c205 100644 --- a/gramps/gen/lib/privacybase.py +++ b/gramps/gen/lib/privacybase.py @@ -79,6 +79,15 @@ class PrivacyBase(object): """ return self.private + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return struct + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/refbase.py b/gramps/gen/lib/refbase.py index d1042e280..7e6d4c759 100644 --- a/gramps/gen/lib/refbase.py +++ b/gramps/gen/lib/refbase.py @@ -70,7 +70,16 @@ class RefBase(object): :returns: Returns a struct containing the data of the object. :rtype: str """ - return [Handle(*t) for t in self.get_referenced_handles()] + return self.ref + + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return struct def unserialize(self, data): """ diff --git a/gramps/gen/lib/repo.py b/gramps/gen/lib/repo.py index 3695d2e9f..d20900839 100644 --- a/gramps/gen/lib/repo.py +++ b/gramps/gen/lib/repo.py @@ -103,6 +103,24 @@ class Repository(NoteBase, AddressBase, UrlBase, "tag_list": TagBase.to_struct(self), "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["gramps_id"], + RepositoryType.from_struct(struct["type"]), + struct["name"], + NoteBase.from_struct(struct["note_list"]), + AddressBase.from_struct(struct["address_list"]), + UrlBase.from_struct(struct["urls"]), + struct["change"], + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/reporef.py b/gramps/gen/lib/reporef.py index 45a80aab5..2a9a91eb2 100644 --- a/gramps/gen/lib/reporef.py +++ b/gramps/gen/lib/reporef.py @@ -97,6 +97,21 @@ class RepoRef(SecondaryObject, PrivacyBase, NoteBase, RefBase): "private": PrivacyBase.serialize(self), } + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return ( + NoteBase.from_struct(struct["note_list"]), + RefBase.from_struct(struct["ref"]), + struct["call_number"], + SourceMediaType.from_struct(struct["media_type"]), + struct["private"], + ) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/researcher.py b/gramps/gen/lib/researcher.py index cc0594d8c..948f5e759 100644 --- a/gramps/gen/lib/researcher.py +++ b/gramps/gen/lib/researcher.py @@ -93,6 +93,25 @@ class Researcher(LocationBase): "address": self.addr, "email": self.email} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["street"], + struct["locality"], + struct["city"], + struct["country"], + struct["state"], + struct["country"], + struct["postal"], + struct["phone"], + struct["name"], + struct["address"], + struct["email"]) + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/src.py b/gramps/gen/lib/src.py index 37561ce2d..8e17e2318 100644 --- a/gramps/gen/lib/src.py +++ b/gramps/gen/lib/src.py @@ -115,6 +115,27 @@ class Source(MediaBase, NoteBase, SrcAttributeBase, IndirectCitationBase, "tag_list": TagBase.to_struct(self), "private": self.private} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["gramps_id"], + struct["title"], + struct["author"], + struct["pubinfo"], + NoteBase.from_struct(struct["note_list"]), + MediaBase.from_struct(struct["media_list"]), + struct["abbrev"], + struct["change"], + SrcAttributeBase.from_struct(struct["srcattr_list"]), + [RepoRef.from_struct(rr) for rr in struct["reporef_list"]], + TagBase.from_struct(struct["tag_list"]), + struct["private"]) + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/gramps/gen/lib/styledtext.py b/gramps/gen/lib/styledtext.py index a71fd5f99..a40ca335f 100644 --- a/gramps/gen/lib/styledtext.py +++ b/gramps/gen/lib/styledtext.py @@ -315,6 +315,15 @@ class StyledText(object): return {"string": self._string, "tags": the_tags} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["string"], [StyledTextTag.from_struct(t) for t in struct["tags"]]) + def unserialize(self, data): """Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/styledtexttag.py b/gramps/gen/lib/styledtexttag.py index 1255c6728..df157e5b6 100644 --- a/gramps/gen/lib/styledtexttag.py +++ b/gramps/gen/lib/styledtexttag.py @@ -97,6 +97,17 @@ class StyledTextTag(object): "value": self.value, "ranges": self.ranges} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (StyledTextTagType.from_struct(struct["name"]), + struct["value"], + struct["ranges"]) + def unserialize(self, data): """Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/surname.py b/gramps/gen/lib/surname.py index c38fa34cf..9119dff89 100644 --- a/gramps/gen/lib/surname.py +++ b/gramps/gen/lib/surname.py @@ -97,6 +97,19 @@ class Surname(SecondaryObject): "origintype": self.origintype.to_struct(), "connector": self.connector} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["surname"], + struct["prefix"], + struct["primary"], + NameOriginType.from_struct(struct["origintype"]), + struct["connector"]) + def is_empty(self): """ Indicate if the surname is empty. diff --git a/gramps/gen/lib/surnamebase.py b/gramps/gen/lib/surnamebase.py index 5bea47ed4..a128b3eea 100644 --- a/gramps/gen/lib/surnamebase.py +++ b/gramps/gen/lib/surnamebase.py @@ -85,6 +85,15 @@ class SurnameBase(object): """ return [surname.to_struct() for surname in self.surname_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [Surname.from_struct(surname) for surname in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/tag.py b/gramps/gen/lib/tag.py index 1054117be..4d9be74d8 100644 --- a/gramps/gen/lib/tag.py +++ b/gramps/gen/lib/tag.py @@ -226,5 +226,18 @@ class Tag(TableObject): "priority": self.__priority, "change": self.change} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["handle"].handle, + struct["name"], + struct["color"], + struct["priority"], + struct["change"]) + priority = property(get_priority, set_priority, None, 'Returns or sets priority of the tag') diff --git a/gramps/gen/lib/tagbase.py b/gramps/gen/lib/tagbase.py index f4aef89ad..3ac62d6c7 100644 --- a/gramps/gen/lib/tagbase.py +++ b/gramps/gen/lib/tagbase.py @@ -79,6 +79,15 @@ class TagBase(object): """ return [Handle('Tag', t) for t in self.tag_list] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [t.handle for t in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/gramps/gen/lib/test/struct_test.py b/gramps/gen/lib/test/struct_test.py new file mode 100644 index 000000000..de60a2521 --- /dev/null +++ b/gramps/gen/lib/test/struct_test.py @@ -0,0 +1,102 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2013 Doug Blank +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" Unittest for to_struct, from_struct """ + +import unittest + +from .. import (Person, Family, Event, Source, Place, Citation, + Repository, MediaObject, Note) +from gramps.gen.merge.diff import import_as_dict +from gramps.cli.user import User + +class BaseCheck: + def test_from_struct(self): + struct = self.object.to_struct() + serialized = self.cls.from_struct(struct) + self.assertEqual(self.object.serialize(), serialized) + +class PersonCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Person + self.object = self.cls() + +class FamilyCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Family + self.object = self.cls() + +class EventCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Event + self.object = self.cls() + +class SourceCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Source + self.object = self.cls() + +class PlaceCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Place + self.object = self.cls() + +class CitationCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Citation + self.object = self.cls() + +class RepositoryCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Repository + self.object = self.cls() + +class MediaObjectCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = MediaObject + self.object = self.cls() + +class NoteCheck(unittest.TestCase, BaseCheck): + def setUp(self): + self.cls = Note + self.object = self.cls() + +class DatabaseCheck(unittest.TestCase): + maxDiff = None + +def generate_test(obj): + """ + Dynamically generate tests and attach to DatabaseCheck. + """ + struct = obj.to_struct() + serialized = obj.__class__.from_struct(struct) + def test(self): + self.assertEqual(obj.serialize(), serialized) + name = "test_%s_%s" % (obj.__class__.__name__, obj.handle) + setattr(DatabaseCheck, name, test) + +db = import_as_dict("example/gramps/example.gramps", User()) +for table in db._tables.keys(): + for handle in db._tables[table]["handles_func"](): + obj = db._tables[table]["handle_func"](handle) + generate_test(obj) + +if __name__ == "__main__": + unittest.main() diff --git a/gramps/gen/lib/url.py b/gramps/gen/lib/url.py index fb16095cb..dffe1a523 100644 --- a/gramps/gen/lib/url.py +++ b/gramps/gen/lib/url.py @@ -98,6 +98,18 @@ class Url(SecondaryObject, PrivacyBase): "desc": self.desc, "type": self.type.to_struct()} + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return (struct["private"], + struct["path"], + struct["desc"], + UrlType.from_struct(struct["type"])) + def unserialize(self, data): (self.private, self.path, self.desc, type_value) = data self.type.unserialize(type_value) diff --git a/gramps/gen/lib/urlbase.py b/gramps/gen/lib/urlbase.py index a927e290c..2b1533a49 100644 --- a/gramps/gen/lib/urlbase.py +++ b/gramps/gen/lib/urlbase.py @@ -82,6 +82,15 @@ class UrlBase(object): """ return [url.to_struct() for url in self.urls] + @classmethod + def from_struct(cls, struct): + """ + Given a struct data representation, return a serialized object. + + :returns: Returns a serialized object + """ + return [Url.from_struct(url) for url in struct] + def unserialize(self, data): """ Convert a serialized tuple of data to an object.