Added metadata to struct to allow object creation

This commit is contained in:
Doug Blank 2013-11-09 21:28:40 -05:00
parent 1137274354
commit 4405e32cb9
31 changed files with 93 additions and 27 deletions

View File

@ -89,7 +89,8 @@ class Address(SecondaryObject, PrivacyBase, CitationBase, NoteBase, DateBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": PrivacyBase.serialize(self),
return {"_class": "Address",
"private": PrivacyBase.serialize(self),
"citation_list": CitationBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"date": DateBase.to_struct(self),

View File

@ -96,7 +96,8 @@ class AttributeRoot(SecondaryObject, PrivacyBase):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": PrivacyBase.serialize(self),
return {"_class": self.__class__.__name__,
"private": PrivacyBase.serialize(self),
"type": self.type.to_struct(),
"value": self.value}
@ -264,7 +265,8 @@ class Attribute(AttributeRoot, CitationBase, NoteBase):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": PrivacyBase.serialize(self),
return {"_class": "Attribute",
"private": PrivacyBase.serialize(self),
"citation_list": CitationBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"type": self.type.to_struct(),

View File

@ -96,7 +96,8 @@ class ChildRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": PrivacyBase.to_struct(self),
return {"_class": "ChildRef",
"private": PrivacyBase.to_struct(self),
"citation_list": CitationBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"ref": RefBase.to_struct(self),

View File

@ -118,7 +118,8 @@ class Citation(MediaBase, NoteBase, SrcAttributeBase, IndirectCitationBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Citation", self.handle), # 0
return {"_class": "Citation",
"handle": Handle("Citation", self.handle), # 0
"gramps_id": self.gramps_id, # 1
"date": DateBase.to_struct(self), # 2
"page": cuni(self.page), # 3

View File

@ -671,7 +671,8 @@ class Date(object):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"calendar": self.calendar,
return {"_class": "Date",
"calendar": self.calendar,
"modifier": self.modifier,
"quality": self.quality,
"dateval": self.dateval,

View File

@ -139,7 +139,8 @@ class Event(CitationBase, NoteBase, MediaBase, AttributeBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Event", self.handle),
return {"_class": "Event",
"handle": Handle("Event", self.handle),
"gramps_id": self.gramps_id,
"type": self.__type.to_struct(),
"date": DateBase.to_struct(self),

View File

@ -101,6 +101,7 @@ class EventRef(PrivacyBase, NoteBase, AttributeBase, RefBase,
:rtype: dict
"""
return {
"_class": "EventRef",
"private": PrivacyBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"attribute_list": AttributeBase.to_struct(self),

View File

@ -148,7 +148,8 @@ class Family(CitationBase, NoteBase, MediaBase, AttributeBase, LdsOrdBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Family", self.handle),
return {"_class": "Family",
"handle": Handle("Family", self.handle),
"gramps_id": self.gramps_id,
"father_handle": Handle("Person", self.father_handle),
"mother_handle": Handle("Person", self.mother_handle),

View File

@ -231,7 +231,8 @@ class GrampsType(GrampsTypeC):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"value": self.__value,
return {"_class": self.__class__.__name__,
"value": self.__value,
"string": str(self)}
@classmethod

View File

@ -166,7 +166,8 @@ class LdsOrd(SecondaryObject, CitationBase, NoteBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"citation_list": CitationBase.to_struct(self),
return {"_class": "LdsOrd",
"citation_list": CitationBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"date": DateBase.to_struct(self),
"type": self.type,

View File

@ -85,7 +85,8 @@ class Location(SecondaryObject, LocationBase):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"street": self.street,
return {"_class": "Location",
"street": self.street,
"locality": self.locality,
"city": self.city,
"country": self.county,

View File

@ -87,6 +87,7 @@ class LocationBase(object):
:rtype: dict
"""
return {
"_class": "LocationBase",
"street": self.street,
"locality": self.locality,
"city": self.city,

View File

@ -143,7 +143,8 @@ class MediaObject(CitationBase, NoteBase, DateBase, AttributeBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Media", self.handle),
return {"_class": "MediaObject",
"handle": Handle("Media", self.handle),
"gramps_id": self.gramps_id,
"path": self.path,
"mime": self.mime,

View File

@ -91,7 +91,8 @@ class MediaRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": PrivacyBase.serialize(self),
return {"_class": "MediaRef",
"private": PrivacyBase.serialize(self),
"citation_list": CitationBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"attribute_list": AttributeBase.to_struct(self),

View File

@ -147,7 +147,8 @@ class Name(SecondaryObject, PrivacyBase, SurnameBase, CitationBase, NoteBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": PrivacyBase.to_struct(self),
return {"_class": "Name",
"private": PrivacyBase.to_struct(self),
"citation_list": CitationBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"date": DateBase.to_struct(self),

View File

@ -119,7 +119,8 @@ class Note(BasicPrimaryObject):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Note", self.handle),
return {"_class": "Note",
"handle": Handle("Note", self.handle),
"gramps_id": self.gramps_id,
"text": self.text.to_struct(),
"format": self.format,

View File

@ -183,6 +183,7 @@ class Person(CitationBase, NoteBase, AttributeBase, MediaBase,
:rtype: dict
"""
return {
"_class": "Person",
"handle": Handle("Person", self.handle), # 0
"gramps_id": self.gramps_id, # 1
"gender": self.gender, # 2

View File

@ -93,7 +93,8 @@ class PersonRef(SecondaryObject, PrivacyBase, CitationBase, NoteBase, RefBase):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": PrivacyBase.to_struct(self),
return {"_class": "PersonRef",
"private": PrivacyBase.to_struct(self),
"citation_list": CitationBase.to_struct(self),
"note_list": NoteBase.to_struct(self),
"ref": RefBase.to_struct(self),

View File

@ -135,7 +135,8 @@ class Place(CitationBase, NoteBase, MediaBase, UrlBase, PrimaryObject):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Place", self.handle),
return {"_class": "Place",
"handle": Handle("Place", self.handle),
"gramps_id": self.gramps_id,
"title": self.title,
"long": self.long,

View File

@ -85,6 +85,7 @@ class PlaceRef(RefBase, DateBase, SecondaryObject):
:rtype: dict
"""
return {
"_class": "PlaceRef",
"ref": RefBase.to_struct(self),
"date": DateBase.to_struct(self)
}

View File

@ -92,7 +92,8 @@ class Repository(NoteBase, AddressBase, UrlBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Repository", self.handle),
return {"_class": "Repository",
"handle": Handle("Repository", self.handle),
"gramps_id": self.gramps_id,
"type": self.type.to_struct(),
"name": cuni(self.name),

View File

@ -91,6 +91,7 @@ class RepoRef(SecondaryObject, PrivacyBase, NoteBase, RefBase):
:rtype: dict
"""
return {
"_class": "RepositoryRef",
"note_list": NoteBase.to_struct(self),
"ref": RefBase.to_struct(self),
"call_number": self.call_number,

View File

@ -82,7 +82,8 @@ class Researcher(LocationBase):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"street": self.street,
return {"_class": "Researcher",
"street": self.street,
"locality": self.locality,
"city": self.city,
"country": self.county,

View File

@ -102,7 +102,8 @@ class Source(MediaBase, NoteBase, SrcAttributeBase, IndirectCitationBase,
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Source", self.handle),
return {"_class": "Source",
"handle": Handle("Source", self.handle),
"gramps_id": self.gramps_id,
"title": cuni(self.title),
"author": cuni(self.author),

View File

@ -313,7 +313,8 @@ class StyledText(object):
else:
the_tags = []
return {"string": self._string,
return {"_class": "StyledText",
"string": self._string,
"tags": the_tags}
@classmethod

View File

@ -94,7 +94,8 @@ class StyledTextTag(object):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"name": self.name.to_struct(),
return {"_class": "StyledTextTag",
"name": self.name.to_struct(),
"value": self.value,
"ranges": self.ranges}

View File

@ -92,7 +92,8 @@ class Surname(SecondaryObject):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"surname": self.surname,
return {"_class": "Surname",
"surname": self.surname,
"prefix": self.prefix,
"primary": self.primary,
"origintype": self.origintype.to_struct(),

View File

@ -221,7 +221,8 @@ class Tag(TableObject):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"handle": Handle("Tag", self.handle),
return {"_class": "Tag",
"handle": Handle("Tag", self.handle),
"name": self.__name,
"color": self.__color,
"priority": self.__priority,

View File

@ -23,8 +23,8 @@
import unittest
from .. import (Person, Family, Event, Source, Place, Citation,
Repository, MediaObject, Note)
from gramps.gen.merge.diff import import_as_dict
Repository, MediaObject, Note, Tag)
from gramps.gen.merge.diff import import_as_dict, from_struct
from gramps.cli.user import User
class BaseCheck:
@ -82,6 +82,11 @@ class NoteCheck(unittest.TestCase, BaseCheck):
self.cls = Note
self.object = self.cls()
class TagCheck(unittest.TestCase, BaseCheck):
def setUp(self):
self.cls = Tag
self.object = self.cls()
class DatabaseCheck(unittest.TestCase):
maxDiff = None
@ -95,6 +100,11 @@ def generate_test(obj):
self.assertEqual(obj.serialize(), serialized)
name = "test_%s_%s" % (obj.__class__.__name__, obj.handle)
setattr(DatabaseCheck, name, test)
####
def test2(self):
self.assertEqual(obj.serialize(), from_struct(struct).serialize())
name = "test_create_%s_%s" % (obj.__class__.__name__, obj.handle)
setattr(DatabaseCheck, name, test2)
db = import_as_dict("example/gramps/example.gramps", User())
for table in db._tables.keys():

View File

@ -93,7 +93,8 @@ class Url(SecondaryObject, PrivacyBase):
:returns: Returns a struct containing the data of the object.
:rtype: dict
"""
return {"private": self.private,
return {"_class": "Url",
"private": self.private,
"path": self.path,
"desc": self.desc,
"type": self.type.to_struct()}

View File

@ -184,3 +184,29 @@ def diff_db_to_file(old_db, filename, user=None):
diffs, m_old, m_new = diff_dbs(old_db, new_db, user)
return diffs, m_old, m_new
def from_struct(struct):
from gramps.gen.lib import (Person, Family, Event, Source, Place, Citation,
Repository, MediaObject, Note, Tag)
if isinstance(struct, dict):
if "_class" in struct.keys():
if struct["_class"] == "Person":
return Person.create(Person.from_struct(struct))
elif struct["_class"] == "Family":
return Family.create(Family.from_struct(struct))
elif struct["_class"] == "Event":
return Event.create(Event.from_struct(struct))
elif struct["_class"] == "Source":
return Source.create(Source.from_struct(struct))
elif struct["_class"] == "Place":
return Place.create(Place.from_struct(struct))
elif struct["_class"] == "Citation":
return Citation.create(Citation.from_struct(struct))
elif struct["_class"] == "Repository":
return Repository.create(Repository.from_struct(struct))
elif struct["_class"] == "MediaObject":
return MediaObject.create(MediaObject.from_struct(struct))
elif struct["_class"] == "Note":
return Note.create(Note.from_struct(struct))
elif struct["_class"] == "Tag":
return Tag.create(Tag.from_struct(struct))
raise AttributeError("invalid struct")