Convert gen.lib to use abstract base classes

This commit is contained in:
Nick Hall 2016-06-23 18:58:32 +01:00
parent bb009aa276
commit fe9af29bf5
5 changed files with 48 additions and 25 deletions

View File

@ -24,9 +24,10 @@ Base Object class for Gramps
#-------------------------------------------------------------------------
#
# standard python modules
# Standard Python modules
#
#-------------------------------------------------------------------------
from abc import ABCMeta, abstractmethod
import re
#-------------------------------------------------------------------------
@ -34,7 +35,7 @@ import re
# Base Object
#
#-------------------------------------------------------------------------
class BaseObject:
class BaseObject(metaclass=ABCMeta):
"""
The BaseObject is the base class for all data objects in Gramps,
whether primary or not.
@ -43,18 +44,19 @@ class BaseObject:
searching through all available information.
"""
@abstractmethod
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
@abstractmethod
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
@abstractmethod
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
@ -74,8 +76,8 @@ class BaseObject:
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
@abstractmethod
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
@ -90,7 +92,6 @@ class BaseObject:
:returns: Returns an object of this type.
"""
raise NotImplementedError
def matches_string(self, pattern, case_sensitive=False):
"""

View File

@ -23,6 +23,13 @@
Basic Primary Object class for Gramps.
"""
#-------------------------------------------------------------------------
#
# Standard Python modules
#
#-------------------------------------------------------------------------
from abc import abstractmethod
#-------------------------------------------------------------------------
#
# Gramps modules
@ -71,18 +78,19 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
else:
self.gramps_id = None
@abstractmethod
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
@abstractmethod
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
@abstractmethod
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
@ -102,8 +110,8 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
@abstractmethod
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
@ -118,7 +126,6 @@ class BasicPrimaryObject(TableObject, PrivacyBase, TagBase):
:returns: Returns an object of this type.
"""
raise NotImplementedError
def set_gramps_id(self, gramps_id):
"""
@ -259,18 +266,19 @@ class PrimaryObject(BasicPrimaryObject):
"""
BasicPrimaryObject.__init__(self, source)
@abstractmethod
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
@abstractmethod
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
@abstractmethod
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
@ -290,8 +298,8 @@ class PrimaryObject(BasicPrimaryObject):
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
@abstractmethod
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
@ -306,7 +314,6 @@ class PrimaryObject(BasicPrimaryObject):
:returns: Returns an object of this type.
"""
raise NotImplementedError
def has_handle_reference(self, classname, handle):
"""

View File

@ -22,12 +22,19 @@
Base Reference class for Gramps.
"""
#-------------------------------------------------------------------------
#
# Standard Python modules
#
#-------------------------------------------------------------------------
from abc import ABCMeta, abstractmethod
#-------------------------------------------------------------------------
#
# RefBase class
#
#-------------------------------------------------------------------------
class RefBase:
class RefBase(metaclass=ABCMeta):
"""
Base reference class to manage references to other objects.
@ -62,6 +69,7 @@ class RefBase:
self.ref = data
return self
@abstractmethod
def get_referenced_handles(self):
"""
Returns the list of (classname, handle) tuples for all directly
@ -71,7 +79,6 @@ class RefBase:
objects.
:rtype: list
"""
raise NotImplementedError
def set_reference_handle(self, handle):
"""

View File

@ -22,6 +22,13 @@
Secondary Object class for Gramps.
"""
#-------------------------------------------------------------------------
#
# Standard Python modules
#
#-------------------------------------------------------------------------
from abc import abstractmethod
#-------------------------------------------------------------------------
#
# Gramps modules
@ -40,18 +47,19 @@ class SecondaryObject(BaseObject):
database.
"""
@abstractmethod
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
@abstractmethod
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
@abstractmethod
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
@ -71,8 +79,8 @@ class SecondaryObject(BaseObject):
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
@abstractmethod
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
@ -87,7 +95,6 @@ class SecondaryObject(BaseObject):
:returns: Returns an object of this type.
"""
raise NotImplementedError
def is_equal(self, source):
return self.serialize() == source.serialize()

View File

@ -25,9 +25,10 @@ Table Object class for Gramps.
#-------------------------------------------------------------------------
#
# standard python modules
# Standard Python modules
#
#-------------------------------------------------------------------------
from abc import abstractmethod
import time
#-------------------------------------------------------------------------
@ -78,18 +79,19 @@ class TableObject(BaseObject):
self.handle = None
self.change = 0
@abstractmethod
def serialize(self):
"""
Convert the object to a serialized tuple of data.
"""
raise NotImplementedError
@abstractmethod
def unserialize(self, data):
"""
Convert a serialized tuple of data to an object.
"""
raise NotImplementedError
@abstractmethod
def to_struct(self):
"""
Convert the data held in this object to a structure (eg,
@ -109,8 +111,8 @@ class TableObject(BaseObject):
:returns: Returns a struct containing the data of the object.
"""
raise NotImplementedError
@abstractmethod
def from_struct(self, struct):
"""
Given a struct data representation, return an object of this type.
@ -125,7 +127,6 @@ class TableObject(BaseObject):
:returns: Returns an object of this type.
"""
raise NotImplementedError
def get_change_time(self):
"""