From 8dddccf6b62bf22e2fc3f86bc55f3a3ca44be1bd Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Mon, 13 Aug 2012 23:45:19 +0000 Subject: [PATCH] Added to_struct methods to all gen.lib objects; 2623: Import Export Merge (GEPS 009) svn: r20205 --- src/gen/lib/address.py | 17 ++++++++ src/gen/lib/addressbase.py | 6 +++ src/gen/lib/attrbase.py | 6 +++ src/gen/lib/attribute.py | 10 +++++ src/gen/lib/baseobj.py | 6 +++ src/gen/lib/childref.py | 11 +++++ src/gen/lib/citation.py | 16 +++++++ src/gen/lib/citationbase.py | 6 +++ src/gen/lib/date.py | 12 ++++++ src/gen/lib/datebase.py | 10 +++++ src/gen/lib/event.py | 31 ++++++++++++++ src/gen/lib/eventref.py | 12 ++++++ src/gen/lib/family.py | 34 +++++++++++++++ src/gen/lib/grampstype.py | 5 +++ src/gen/lib/ldsord.py | 14 ++++++ src/gen/lib/ldsordbase.py | 6 +++ src/gen/lib/location.py | 14 ++++++ src/gen/lib/locationbase.py | 13 ++++++ src/gen/lib/mediabase.py | 6 +++ src/gen/lib/mediaobj.py | 31 ++++++++++++++ src/gen/lib/mediaref.py | 11 +++++ src/gen/lib/name.py | 20 +++++++++ src/gen/lib/note.py | 16 +++++++ src/gen/lib/notebase.py | 6 +++ src/gen/lib/person.py | 82 ++++++++++++++++++++++++++++++++++++ src/gen/lib/personref.py | 10 +++++ src/gen/lib/place.py | 38 +++++++++++++++++ src/gen/lib/primaryobj.py | 9 ++++ src/gen/lib/privacybase.py | 6 +++ src/gen/lib/refbase.py | 6 +++ src/gen/lib/repo.py | 14 ++++++ src/gen/lib/reporef.py | 12 ++++++ src/gen/lib/researcher.py | 9 ++++ src/gen/lib/src.py | 17 ++++++++ src/gen/lib/styledtext.py | 23 ++++++++++ src/gen/lib/styledtexttag.py | 11 +++++ src/gen/lib/surname.py | 10 +++++ src/gen/lib/surnamebase.py | 6 +++ src/gen/lib/tag.py | 28 +++++++++++- src/gen/lib/tagbase.py | 6 +++ src/gen/lib/url.py | 6 +++ src/gen/lib/urlbase.py | 6 +++ 42 files changed, 617 insertions(+), 1 deletion(-) diff --git a/src/gen/lib/address.py b/src/gen/lib/address.py index 38c526ac0..ab3d3037f 100644 --- a/src/gen/lib/address.py +++ b/src/gen/lib/address.py @@ -68,6 +68,23 @@ class Address(SecondaryObject, PrivacyBase, CitationBase, NoteBase, DateBase, DateBase.serialize(self), LocationBase.serialize(self)) + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + return {"private": PrivacyBase.serialize(self), + "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} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/addressbase.py b/src/gen/lib/addressbase.py index 5d5fd9088..72a9b2ca0 100644 --- a/src/gen/lib/addressbase.py +++ b/src/gen/lib/addressbase.py @@ -61,6 +61,12 @@ class AddressBase(object): """ return [addr.serialize() for addr in self.address_list] + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + return [addr.to_struct() for addr in self.address_list] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/attrbase.py b/src/gen/lib/attrbase.py index 67e29760f..779e2dfb1 100644 --- a/src/gen/lib/attrbase.py +++ b/src/gen/lib/attrbase.py @@ -65,6 +65,12 @@ class AttributeBase(object): """ return [attr.serialize() for attr in self.attribute_list] + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + return [attr.to_struct() for attr in self.attribute_list] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/attribute.py b/src/gen/lib/attribute.py index a8ed25bf6..d8d500085 100644 --- a/src/gen/lib/attribute.py +++ b/src/gen/lib/attribute.py @@ -83,6 +83,16 @@ class Attribute(SecondaryObject, PrivacyBase, CitationBase, NoteBase): NoteBase.serialize(self), self.type.serialize(), self.value) + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + return {"private": PrivacyBase.serialize(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "type": self.type.to_struct(), + "value": self.value} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/baseobj.py b/src/gen/lib/baseobj.py index fc37230b5..36799fc26 100644 --- a/src/gen/lib/baseobj.py +++ b/src/gen/lib/baseobj.py @@ -51,6 +51,12 @@ class BaseObject(object): """ assert False, "Needs to be overridden in the derived class" + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + assert False, "Needs to be overridden in the derived class" + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/childref.py b/src/gen/lib/childref.py index 4a6ac012c..47e587e1a 100644 --- a/src/gen/lib/childref.py +++ b/src/gen/lib/childref.py @@ -75,6 +75,17 @@ class ChildRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase): self.frel.serialize(), self.mrel.serialize()) + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + return {"private": PrivacyBase.to_struct(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "ref": RefBase.to_struct(self), + "frel": self.frel.to_struct(), + "mrel": self.mrel.to_struct()} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/citation.py b/src/gen/lib/citation.py index 73dd431c0..c26806e70 100644 --- a/src/gen/lib/citation.py +++ b/src/gen/lib/citation.py @@ -91,6 +91,22 @@ class Citation(MediaBase, NoteBase, PrimaryObject, DateBase): self.change, # 9 self.private) # 10 + def to_struct(self, no_text_date = False): + """ + Convert the object to a serialized struct of data. + """ + return {"handle": self.handle, # 0 + "gramps_id": self.gramps_id, # 1 + "date": DateBase.to_struct(self), # 2 + "page": unicode(self.page), # 3 + "confidence": self.confidence, # 4 + "source_handle": self.source_handle, # 5 + "note_list": NoteBase.to_struct(self), # 6 + "media_list": MediaBase.to_struct(self), # 7 + "datamap": self.datamap, # 8 + "change": self.change, # 9 + "private": self.private} # 10 + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/src/gen/lib/citationbase.py b/src/gen/lib/citationbase.py index 58332538d..952e675b8 100644 --- a/src/gen/lib/citationbase.py +++ b/src/gen/lib/citationbase.py @@ -69,6 +69,12 @@ class CitationBase(object): """ return self.citation_list + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return self.citation_list + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/date.py b/src/gen/lib/date.py index 8e43b3049..fef51dd6e 100644 --- a/src/gen/lib/date.py +++ b/src/gen/lib/date.py @@ -637,6 +637,18 @@ class Date(object): return (self.calendar, self.modifier, self.quality, self.dateval, text, self.sortval, self.newyear) + def to_struct(self): + """ + Convert to struct for data storage. + """ + return {"calendar": self.calendar, + "modifier": self.modifier, + "quality": self.quality, + "dateval": self.dateval, + "text": self.text, + "sortval": self.sortval, + "newyear": self.newyear} + def unserialize(self, data): """ Load from the format created by serialize. diff --git a/src/gen/lib/datebase.py b/src/gen/lib/datebase.py index c34f5aa4f..1213d1063 100644 --- a/src/gen/lib/datebase.py +++ b/src/gen/lib/datebase.py @@ -63,6 +63,16 @@ class DateBase(object): date = self.date.serialize(no_text_date) return date + def to_struct(self, no_text_date=False): + """ + Convert the object to a serialized tuple of data. + """ + if self.date is None: + date = Date().to_struct() + else: + date = self.date.to_struct() + return date + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/event.py b/src/gen/lib/event.py index 7897eb34e..c7e6bdf61 100644 --- a/src/gen/lib/event.py +++ b/src/gen/lib/event.py @@ -117,6 +117,37 @@ class Event(CitationBase, NoteBase, MediaBase, AttributeBase, AttributeBase.serialize(self), self.change, self.private) + def to_struct(self): + """ + Convert the data held in the event to a Python tuple that + represents all the data elements. + + This method is used to convert the object into a form that can easily + be saved to a database. + + These elements may be primitive Python types (string, integers), + complex Python types (lists or tuples, or Python objects. If the + target database cannot handle complex types (such as objects or + lists), the database is responsible for converting the data into + a form that it can use. + + :returns: Returns a python tuple containing the data that should + be considered persistent. + :rtype: tuple + """ + return {"handle": self.handle, + "gramps_id": self.gramps_id, + "type": self.__type.to_struct(), + "date": DateBase.to_struct(self), + "description": self.__description, + "place": self.place, + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "media_list": MediaBase.to_struct(self), + "attribute_list": AttributeBase.to_struct(self), + "change": self.change, + "private": self.private} + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/src/gen/lib/eventref.py b/src/gen/lib/eventref.py index e5f97b1b4..76e3a27c3 100644 --- a/src/gen/lib/eventref.py +++ b/src/gen/lib/eventref.py @@ -77,6 +77,18 @@ class EventRef(SecondaryObject, PrivacyBase, NoteBase, AttributeBase, RefBase): self.__role.serialize() ) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return { + "private": PrivacyBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "attribute_list": AttributeBase.to_struct(self), + "ref": RefBase.to_struct(self), + "role": self.__role.to_struct() + } + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/family.py b/src/gen/lib/family.py index cf0eb5a67..8bc35b78f 100644 --- a/src/gen/lib/family.py +++ b/src/gen/lib/family.py @@ -128,6 +128,40 @@ class Family(CitationBase, NoteBase, MediaBase, AttributeBase, LdsOrdBase, NoteBase.serialize(self), self.change, TagBase.serialize(self), self.private) + def to_struct(self): + """ + Convert the data held in the event to a Python tuple that + represents all the data elements. + + This method is used to convert the object into a form that can easily + be saved to a database. + + These elements may be primitive Python types (string, integers), + complex Python types (lists or tuples, or Python objects. If the + target database cannot handle complex types (such as objects or + lists), the database is responsible for converting the data into + a form that it can use. + + :returns: Returns a python tuple containing the data that should + be considered persistent. + :rtype: tuple + """ + return {"handle": self.handle, + "gramps_id": self.gramps_id, + "father_handle": self.father_handle, + "mother_handle": self.mother_handle, + "child_ref_list": [cr.to_struct() for cr in self.child_ref_list], + "type": self.type.to_struct(), + "event_ref_list": [er.to_struct() for er in self.event_ref_list], + "media_list": MediaBase.to_struct(self), + "attribute_list": AttributeBase.to_struct(self), + "lds_ord_list": LdsOrdBase.to_struct(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "change": self.change, + "tag_list": TagBase.to_struct(self), + "private": self.private} + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/src/gen/lib/grampstype.py b/src/gen/lib/grampstype.py index 4510cbc4e..17d52951b 100644 --- a/src/gen/lib/grampstype.py +++ b/src/gen/lib/grampstype.py @@ -198,6 +198,11 @@ class GrampsType(object): """Convert the object to a serialized tuple of data. """ return (self.__value, self.__string) + def to_struct(self): + """Convert the object to a serialized tuple of data. """ + return {"value": self.__value, + "string": str(self)} + def unserialize(self, data): """Convert a serialized tuple of data to an object.""" self.__value, self.__string = data diff --git a/src/gen/lib/ldsord.py b/src/gen/lib/ldsord.py index 85f102c47..c72238ff2 100644 --- a/src/gen/lib/ldsord.py +++ b/src/gen/lib/ldsord.py @@ -144,6 +144,20 @@ class LdsOrd(SecondaryObject, CitationBase, NoteBase, self.type, self.place, self.famc, self.temple, self.status, self.private) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "date": DateBase.to_struct(self), + "type": self.type, + "place": self.place, + "famc": self.famc, + "temple": self.temple, + "status": self.status, + "private": self.private} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/ldsordbase.py b/src/gen/lib/ldsordbase.py index bfeb2eff4..d70ccff77 100644 --- a/src/gen/lib/ldsordbase.py +++ b/src/gen/lib/ldsordbase.py @@ -66,6 +66,12 @@ class LdsOrdBase(object): """ return [lds_ord.serialize() for lds_ord in self.lds_ord_list] + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return [lds_ord.to_struct() for lds_ord in self.lds_ord_list] + def unserialize(self, data): """ Convert a serialized tuple of data to an object diff --git a/src/gen/lib/location.py b/src/gen/lib/location.py index d32d35c2c..8f0b5f10f 100644 --- a/src/gen/lib/location.py +++ b/src/gen/lib/location.py @@ -64,6 +64,20 @@ class Location(SecondaryObject, LocationBase): """ return (LocationBase.serialize(self), self.parish) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"street": self.street, + "locality": self.locality, + "city": self.city, + "country": self.county, + "state": self.state, + "country": self.country, + "postal": self.postal, + "phone": self.phone, + "parish": self.parish} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/locationbase.py b/src/gen/lib/locationbase.py index 62671f6e8..488bd54ba 100644 --- a/src/gen/lib/locationbase.py +++ b/src/gen/lib/locationbase.py @@ -66,6 +66,19 @@ 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 object to a serialized tuple of data. + """ + return {"street": self.street, + "locality": self.locality, + "city": self.city, + "country": self.county, + "state": self.state, + "country": self.country, + "postal": self.postal, + "phone": self.phone} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/mediabase.py b/src/gen/lib/mediabase.py index d04572f79..f139bd9f2 100644 --- a/src/gen/lib/mediabase.py +++ b/src/gen/lib/mediabase.py @@ -58,6 +58,12 @@ class MediaBase(object): """ return [mref.serialize() for mref in self.media_list] + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return [mref.to_struct() for mref in self.media_list] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/mediaobj.py b/src/gen/lib/mediaobj.py index daf6fcc6e..6e1022303 100644 --- a/src/gen/lib/mediaobj.py +++ b/src/gen/lib/mediaobj.py @@ -116,6 +116,37 @@ class MediaObject(CitationBase, NoteBase, DateBase, AttributeBase, TagBase.serialize(self), self.private) + def to_struct(self): + """ + Convert the data held in the event to a Python tuple that + represents all the data elements. + + This method is used to convert the object into a form that can easily + be saved to a database. + + These elements may be primitive Python types (string, integers), + complex Python types (lists or tuples, or Python objects. If the + target database cannot handle complex types (such as objects or + lists), the database is responsible for converting the data into + a form that it can use. + + :returns: Returns a python tuple containing the data that should + be considered persistent. + :rtype: tuple + """ + return {"handle": self.handle, + "gramps_id": self.gramps_id, + "path": self.path, + "mime": self.mime, + "desc": self.desc, + "attribute_list": AttributeBase.to_struct(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "change": self.change, + "date": DateBase.to_struct(self), + "tag_list": TagBase.to_struct(self), + "private": self.private} + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/src/gen/lib/mediaref.py b/src/gen/lib/mediaref.py index d7d6b6aae..ee8d604f6 100644 --- a/src/gen/lib/mediaref.py +++ b/src/gen/lib/mediaref.py @@ -70,6 +70,17 @@ class MediaRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase, RefBase.serialize(self), self.rect) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"private": PrivacyBase.serialize(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "attribute_list": AttributeBase.to_struct(self), + "ref": RefBase.to_struct(self), + "rect": self.rect} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/name.py b/src/gen/lib/name.py index 352b4e739..bcde754ce 100644 --- a/src/gen/lib/name.py +++ b/src/gen/lib/name.py @@ -126,6 +126,26 @@ class Name(SecondaryObject, PrivacyBase, SurnameBase, CitationBase, NoteBase, self.group_as, self.sort_as, self.display_as, self.call, self.nick, self.famnick) + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + return {"private": PrivacyBase.to_struct(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "date": DateBase.to_struct(self), + "first_name": self.first_name, + "surname_list": SurnameBase.to_struct(self), + "suffix": self.suffix, + "title": self.title, + "type": self.type.to_struct(), + "group_as": self.group_as, + "sort_as": self.sort_as, + "display_as": self.display_as, + "call": self.call, + "nick": self.nick, + "famnick": self.famnick} + def is_empty(self): """ Indicate if the name is empty. diff --git a/src/gen/lib/note.py b/src/gen/lib/note.py index 2fda4f4e0..3f7ad62d9 100644 --- a/src/gen/lib/note.py +++ b/src/gen/lib/note.py @@ -97,6 +97,22 @@ class Note(BasicPrimaryObject, TagBase): self.type.serialize(), self.change, TagBase.serialize(self), self.private) + def to_struct(self): + """Convert the object to a serialized tuple of data. + + :returns: The serialized format of the instance. + :rtype: tuple + + """ + return {"handle": self.handle, + "gramps_id": self.gramps_id, + "text": self.text.to_struct(), + "format": self.format, + "type": self.type.to_struct(), + "change": self.change, + "tag_list": TagBase.to_struct(self), + "private": self.private} + def unserialize(self, data): """Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/notebase.py b/src/gen/lib/notebase.py index 1e01763f9..4f70cc1e8 100644 --- a/src/gen/lib/notebase.py +++ b/src/gen/lib/notebase.py @@ -53,6 +53,12 @@ class NoteBase(object): """ return self.note_list + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return self.note_list + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/person.py b/src/gen/lib/person.py index 4ec55801b..9e5cedb2f 100644 --- a/src/gen/lib/person.py +++ b/src/gen/lib/person.py @@ -78,6 +78,28 @@ class Person(CitationBase, NoteBase, AttributeBase, MediaBase, MALE = 1 FEMALE = 0 + FIELDS = ["handle", # 0 + "gramps_id", # 1 + "gender", # 2 + "primary_name", # 3 + "alternate_names", # 4 + "death_ref_index", # 5 + "birth_ref_index", # 6 + "event_ref_list", # 7 + "family_list", # 8 + "parent_family_list", # 9 + "media_list", # 10 + "address_list", # 11 + "attribute_list", # 12 + "urls", # 13 + "lds_ord_list", # 14 + "citation_list", # 15 + "note_list", # 16 + "change", # 17 + "tag_list", # 18 + "private", # 19 + "person_ref_list"] # 20 + def __init__(self, data=None): """ Create a new Person instance. @@ -159,6 +181,65 @@ class Person(CitationBase, NoteBase, AttributeBase, MediaBase, [pr.serialize() for pr in self.person_ref_list] # 20 ) + def to_struct(self): + return { + "handle": self.handle, # 0 + "gramps_id": self.gramps_id, # 1 + "gender": self.gender, # 2 + "primary_name": self.primary_name.to_struct(), # 3 + "alternate_names": [name.to_struct() for name in self.alternate_names], # 4 + "death_ref_index": self.death_ref_index, # 5 + "birth_ref_index": self.birth_ref_index, # 6 + "event_ref_list": [er.to_struct() for er in self.event_ref_list], # 7 + "family_list": self.family_list, # 8 + "parent_family_list": self.parent_family_list, # 9 + "media_list": MediaBase.to_struct(self), # 10 + "address_list": AddressBase.to_struct(self), # 11 + "attribute_list": AttributeBase.to_struct(self), # 12 + "urls": UrlBase.to_struct(self), # 13 + "lds_ord_list": LdsOrdBase.to_struct(self), # 14 + "citation_list": CitationBase.to_struct(self), # 15 + "note_list": NoteBase.to_struct(self), # 16 + "change": self.change, # 17 + "tag_list": TagBase.to_struct(self), # 18 + "private": self.private, # 19 + "person_ref_list": [pr.to_struct() for pr in self.person_ref_list] # 20 + } + + def from_struct(self, to_struct): + self.handle = to_struct["handle"] # 0 + self.gramps_id = to_struct["gramps_id"] # 1 + self.gender = to_struct["gender"] # 2 + self.primary_name = Name.from_struct(to_struct["primary_name"]) # 3 + self.alternate_names = [Name.from_struct(name) + for name in to_struct["alternate_names"]] # 4 + self.death_ref_index = to_struct["death_ref_index"] # 5 + self.birth_ref_index = to_struct["birth_ref_index"] # 6 + self.event_ref_list = [EventRef.from_struct(er) + for er in to_struct["event_ref_list"]] # 7 + self.family_list = to_struct["family_list"] # 8 + self.parent_family_list = to_struct["parent_family_list"] # 9 + self.media_list = [MediaBase.from_struct(m) + for m in to_struct["media_list"]] # 10 + self.address_list = [AddressBase.from_struct(a) + for a in to_struct["address_list"]] # 11 + self.attribute_list = [AttributeBase.from_struct(attr) + for attr in to_struct["attribute_list"]] # 12 + self.urls = [UrlBase.from_struct(url) + for url in to_struct["urls"]] # 13 + self.lds_ord_list = [LdsOrdBase.from_struct(lref) + for lref in to_struct["lds_ord_list"]] # 14 + self.citation_list = [CitationBase.from_struct(cref) + for cref in to_struct["citation_list"]] # 15 + self.note_list = [NoteBase.from_struct(noteref) + for noteref in to_struct["note_list"]] # 16 + self.change = to_struct["change"] # 17 + self.tag_list = [TagBase.from_struct(tag) + for tag in to_struct["tag_list"]] # 18 + self.private = to_struct["private"] # 19 + self.person_ref_list = [PersonRef.from_struct(pr) + for pr in to_struct["person_ref_list"]] # 20 + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method @@ -979,3 +1060,4 @@ class Person(CitationBase, NoteBase, AttributeBase, MediaBase, break else: self.person_ref_list.append(addendum) + diff --git a/src/gen/lib/personref.py b/src/gen/lib/personref.py index de8d4b50a..5a1c9ae5f 100644 --- a/src/gen/lib/personref.py +++ b/src/gen/lib/personref.py @@ -72,6 +72,16 @@ class PersonRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase): RefBase.serialize(self), self.rel) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"private": PrivacyBase.to_struct(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "ref": RefBase.to_struct(self), + "rel": self.rel} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/place.py b/src/gen/lib/place.py index 375ceb88d..85dd20e16 100644 --- a/src/gen/lib/place.py +++ b/src/gen/lib/place.py @@ -109,6 +109,44 @@ class Place(CitationBase, NoteBase, MediaBase, UrlBase, PrimaryObject): NoteBase.serialize(self), self.change, self.private) + def to_struct(self): + """ + Convert the data held in the Place to a Python tuple that + represents all the data elements. + + This method is used to convert the object into a form that can easily + be saved to a database. + + These elements may be primitive Python types (string, integers), + complex Python types (lists or tuples, or Python objects. If the + target database cannot handle complex types (such as objects or + lists), the database is responsible for converting the data into + a form that it can use. + + :returns: Returns a python tuple containing the data that should + be considered persistent. + :rtype: tuple + """ + + if self.main_loc is None or self.main_loc.serialize() == _EMPTY_LOC: + main_loc = None + else: + main_loc = self.main_loc.to_struct() + + return {"handle": self.handle, + "gramps_id": self.gramps_id, + "title": self.title, + "long": self.long, + "lat": self.lat, + "main_loc": main_loc, + "alt_loc": [al.to_struct() for al in self.alt_loc], + "urls": UrlBase.to_struct(self), + "media_list": MediaBase.to_struct(self), + "citation_list": CitationBase.to_struct(self), + "note_list": NoteBase.to_struct(self), + "change": self.change, + "private": self.private} + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/src/gen/lib/primaryobj.py b/src/gen/lib/primaryobj.py index 25e0a3793..7247f214d 100644 --- a/src/gen/lib/primaryobj.py +++ b/src/gen/lib/primaryobj.py @@ -164,6 +164,14 @@ class BasicPrimaryObject(TableObject, PrivacyBase): def replace_media_references(self, old_handle, new_handle): pass + def get_fields(self): + """ + Return a list of fields of the struct. + """ + return self.to_struct().keys() + + FIELDS = property(get_fields) + #------------------------------------------------------------------------- # # Primary Object class @@ -264,3 +272,4 @@ class PrimaryObject(BasicPrimaryObject): Replace the handle reference with the new reference. """ pass + diff --git a/src/gen/lib/privacybase.py b/src/gen/lib/privacybase.py index 3e0b0446d..1c89be4b9 100644 --- a/src/gen/lib/privacybase.py +++ b/src/gen/lib/privacybase.py @@ -57,6 +57,12 @@ class PrivacyBase(object): """ return self.private + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return self.private + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/refbase.py b/src/gen/lib/refbase.py index 28d0251bf..9dca8585b 100644 --- a/src/gen/lib/refbase.py +++ b/src/gen/lib/refbase.py @@ -48,6 +48,12 @@ class RefBase(object): """ return self.ref + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return self.ref + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/repo.py b/src/gen/lib/repo.py index a684d2dbe..2f8e22f44 100644 --- a/src/gen/lib/repo.py +++ b/src/gen/lib/repo.py @@ -67,6 +67,20 @@ class Repository(NoteBase, AddressBase, UrlBase, PrimaryObject): UrlBase.serialize(self), self.change, self.private) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"handle": self.handle, + "gramps_id": self.gramps_id, + "type": self.type.to_struct(), + "name": unicode(self.name), + "note_list": NoteBase.to_struct(self), + "address_list": AddressBase.to_struct(self), + "urls": UrlBase.to_struct(self), + "change": self.change, + "private": self.private} + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/src/gen/lib/reporef.py b/src/gen/lib/reporef.py index dcfa7243c..e5f9b8463 100644 --- a/src/gen/lib/reporef.py +++ b/src/gen/lib/reporef.py @@ -69,6 +69,18 @@ class RepoRef(SecondaryObject, PrivacyBase, NoteBase, RefBase): PrivacyBase.serialize(self), ) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return { + "note_list": NoteBase.to_struct(self), + "ref": RefBase.to_struct(self), + "call_number": self.call_number, + "media_type": self.media_type.to_struct(), + "private": PrivacyBase.serialize(self), + } + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/researcher.py b/src/gen/lib/researcher.py index 243ce99c2..3278e79d0 100644 --- a/src/gen/lib/researcher.py +++ b/src/gen/lib/researcher.py @@ -61,6 +61,15 @@ class Researcher(LocationBase): return (LocationBase.serialize(self), self.name, self.addr, self.email) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"location": LocationBase.to_struct(self), + "name": self.name, + "address": self.addr, + "email": self.email} + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/src.py b/src/gen/lib/src.py index 957149e54..e79ead768 100644 --- a/src/gen/lib/src.py +++ b/src/gen/lib/src.py @@ -69,6 +69,23 @@ class Source(MediaBase, NoteBase, PrimaryObject): [rr.serialize() for rr in self.reporef_list], self.private) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"handle": self.handle, + "gramps_id": self.gramps_id, + "title": unicode(self.title), + "author": unicode(self.author), + "pubinfo": unicode(self.pubinfo), + "note_list": NoteBase.to_struct(self), + "media_list": MediaBase.to_struct(self), + "abbrev": unicode(self.abbrev), + "change": self.change, + "datamap": {"dict": self.datamap}, + "reporef_list": [rr.to_struct() for rr in self.reporef_list], + "private": self.private} + def unserialize(self, data): """ Convert the data held in a tuple created by the serialize method diff --git a/src/gen/lib/styledtext.py b/src/gen/lib/styledtext.py index 35a42d956..62eb2f2b8 100644 --- a/src/gen/lib/styledtext.py +++ b/src/gen/lib/styledtext.py @@ -272,6 +272,21 @@ class StyledText(object): return (self._string, the_tags) + def to_struct(self): + """Convert the object to a serialized tuple of data. + + :returns: Serialized format of the instance. + :returnstype: tuple + + """ + if self._tags: + the_tags = [tag.to_struct() for tag in self._tags] + else: + the_tags = [] + + return {"string": self._string, + "tags": the_tags} + def unserialize(self, data): """Convert a serialized tuple of data to an object. @@ -298,6 +313,14 @@ class StyledText(object): """ return self._tags + def get_string(self): + """ + Accessor for the associated string. + """ + return self._string + + tags = property(get_tags) + string = property(get_string) if __name__ == '__main__': from styledtexttagtype import StyledTextTagType diff --git a/src/gen/lib/styledtexttag.py b/src/gen/lib/styledtexttag.py index 85e2ae979..003fa0b56 100644 --- a/src/gen/lib/styledtexttag.py +++ b/src/gen/lib/styledtexttag.py @@ -73,6 +73,17 @@ class StyledTextTag(object): """ return (self.name.serialize(), self.value, self.ranges) + def to_struct(self): + """Convert the object to a serialized tuple of data. + + :returns: Serialized format of the instance. + :returnstype: tuple + + """ + return {"name": self.name.to_struct(), + "value": self.value, + "ranges": self.ranges} + def unserialize(self, data): """Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/surname.py b/src/gen/lib/surname.py index 19b2c7a13..6c7151aea 100644 --- a/src/gen/lib/surname.py +++ b/src/gen/lib/surname.py @@ -71,6 +71,16 @@ class Surname(SecondaryObject): return (self.surname, self.prefix, self.primary, self.origintype.serialize(), self.connector) + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return {"surname": self.surname, + "prefix": self.prefix, + "primary": self.primary, + "origintype": self.origintype.to_struct(), + "connector": self.connector} + def is_empty(self): """ Indicate if the surname is empty. diff --git a/src/gen/lib/surnamebase.py b/src/gen/lib/surnamebase.py index 81ef1a62d..54a8bc696 100644 --- a/src/gen/lib/surnamebase.py +++ b/src/gen/lib/surnamebase.py @@ -62,6 +62,12 @@ class SurnameBase(object): """ return [surname.serialize() for surname in self.surname_list] + def to_struct(self): + """ + Convert the object to a serialized struct of data. + """ + return [surname.to_struct() for surname in self.surname_list] + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/tag.py b/src/gen/lib/tag.py index 88780c8af..758a8ff5a 100644 --- a/src/gen/lib/tag.py +++ b/src/gen/lib/tag.py @@ -198,6 +198,32 @@ class Tag(TableObject): :rtype: int """ return self.__priority + + def to_struct(self): + """ + Convert the data held in the event to a Python tuple that + represents all the data elements. + + This method is used to convert the object into a form that can easily + be saved to a database. + + These elements may be primitive Python types (string, integers), + complex Python types (lists or tuples, or Python objects. If the + target database cannot handle complex types (such as objects or + lists), the database is responsible for converting the data into + a form that it can use. + + :returns: Returns a python tuple containing the data that should + be considered persistent. + :rtype: tuple + """ + return {"handle": self.handle, + "name": self.__name, + "color": self.__color, + "priority": self.__priority, + "change": self.change} + + FIELDS = ["handle", "name", "color", "priority", "change"] + priority = property(get_priority, set_priority, None, 'Returns or sets priority of the tag') - diff --git a/src/gen/lib/tagbase.py b/src/gen/lib/tagbase.py index 29c2c7913..7d75a45ac 100644 --- a/src/gen/lib/tagbase.py +++ b/src/gen/lib/tagbase.py @@ -54,6 +54,12 @@ class TagBase(object): """ return self.tag_list + def to_struct(self): + """ + Convert the object to a serialized tuple of data. + """ + return self.tag_list + def unserialize(self, data): """ Convert a serialized tuple of data to an object. diff --git a/src/gen/lib/url.py b/src/gen/lib/url.py index 08e335536..3cbbce714 100644 --- a/src/gen/lib/url.py +++ b/src/gen/lib/url.py @@ -69,6 +69,12 @@ class Url(SecondaryObject, PrivacyBase): def serialize(self): return (self.private, self.path, self.desc, self.type.serialize()) + def to_struct(self): + return {"private": self.private, + "path": self.path, + "desc": self.desc, + "type": self.type.to_struct()} + def unserialize(self, data): (self.private, self.path, self.desc, type_value) = data self.type.unserialize(type_value) diff --git a/src/gen/lib/urlbase.py b/src/gen/lib/urlbase.py index f7201f1c8..0e61944cb 100644 --- a/src/gen/lib/urlbase.py +++ b/src/gen/lib/urlbase.py @@ -60,6 +60,12 @@ class UrlBase(object): """ return [url.serialize() for url in self.urls] + def to_struct(self): + """ + Convert the object to a serialized tuple of data + """ + return [url.to_struct() for url in self.urls] + def unserialize(self, data): """ Convert a serialized tuple of data to an object.