Added from_struct to all objects; can now instatiate an object with JSON-like structs; added 10k tests that all pass

This commit is contained in:
Doug Blank 2013-11-09 10:43:30 -05:00
parent b3f6b8f27a
commit 564aaf88a7
45 changed files with 766 additions and 19 deletions

View File

@ -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):
"""

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -266,4 +266,4 @@ class PrimaryObject(BasicPrimaryObject):
Replace the handle reference with the new reference.
"""
pass

View File

@ -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.

View File

@ -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):
"""

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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')

View File

@ -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.

View File

@ -0,0 +1,102 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2013 Doug Blank <doug.blank@gmail.com>
#
# 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()

View File

@ -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)

View File

@ -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.