1#
2# Gramps - a GTK+/GNOME based genealogy program
3#
4# Copyright (C) 2000-2006  Donald N. Allingham
5# Copyright (C) 2010       Michiel D. Nauta
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21
22"""
23LdsOrdBase class for Gramps.
24"""
25
26#-------------------------------------------------------------------------
27#
28# Gramps modules
29#
30#-------------------------------------------------------------------------
31from .ldsord import LdsOrd
32from .const import IDENTICAL, EQUAL
33
34#-------------------------------------------------------------------------
35#
36# LdsOrdBase classes
37#
38#-------------------------------------------------------------------------
39class LdsOrdBase:
40    """
41    Base class for lds_ord-aware objects.
42    """
43
44    def __init__(self, source=None):
45        """
46        Initialize a LdsOrdBase.
47
48        If the source is not None, then object is initialized from values of
49        the source object.
50
51        :param source: Object used to initialize the new object
52        :type source: LdsOrdBase
53        """
54
55        if source:
56            self.lds_ord_list = [LdsOrd(lds_ord)
57                                 for lds_ord in source.lds_ord_list]
58        else:
59            self.lds_ord_list = []
60
61    def serialize(self):
62        """
63        Convert the object to a serialized tuple of data.
64        """
65        return [lds_ord.serialize() for lds_ord in self.lds_ord_list]
66
67    def unserialize(self, data):
68        """
69        Convert a serialized tuple of data to an object
70        """
71        self.lds_ord_list = [LdsOrd().unserialize(item) for item in data]
72
73    def add_lds_ord(self, lds_ord):
74        """
75        Add the :class:`~.ldsord.LdsOrd` instance to the object's list of
76        lds_ords.
77
78        :param lds_ord: :class:`~.ldsord.LdsOrd` instance to add to the object's
79                        lds_ord list
80        :type lds_ord: list
81        """
82        self.lds_ord_list.append(lds_ord)
83
84    def remove_lds_ord(self, lds_ord):
85        """
86        Remove the specified :class:`~.ldsord.LdsOrd` instance from the lds_ord
87        list.
88
89        If the instance does not exist in the list, the operation has no effect.
90
91        :param lds_ord: :class:`~.ldsord.LdsOrd` instance to remove from the
92                        list
93        :type lds_ord: :class:`~.ldsord.LdsOrd`
94
95        :returns: True if the lds_ord was removed, False if it was not in the
96                  list.
97        :rtype: bool
98        """
99        if lds_ord in self.lds_ord_list:
100            self.lds_ord_list.remove(lds_ord)
101            return True
102        else:
103            return False
104
105    def get_lds_ord_list(self):
106        """
107        Return the list of :class:`~.ldsord.LdsOrd` instances associated with
108        the object.
109
110        :returns: Returns the list of :class:`~.ldsord.LdsOrd` instances
111        :rtype: list
112        """
113        return self.lds_ord_list
114
115    def set_lds_ord_list(self, lds_ord_list):
116        """
117        Assign the passed list to the object's list of :class:`~.ldsord.LdsOrd`
118        instances.
119
120        :param lds_ord_list: List of :class:`~.ldsord.LdsOrd` instances to be
121                             associated with the object
122        :type lds_ord_list: list
123        """
124        self.lds_ord_list = lds_ord_list
125
126    def _merge_lds_ord_list(self, acquisition):
127        """
128        Merge the list of ldsord from acquisition with our own.
129
130        :param acquisition: the ldsord list of this object will be merged with
131                            the current ldsord list.
132        :type acquisition: LdsOrdBase
133        """
134        ldsord_list = self.lds_ord_list[:]
135        for addendum in acquisition.get_lds_ord_list():
136            for ldsord in ldsord_list:
137                equi = ldsord.is_equivalent(addendum)
138                if equi == IDENTICAL:
139                    break
140                elif equi == EQUAL:
141                    ldsord.merge(addendum)
142                    break
143            else:
144                self.lds_ord_list.append(addendum)
145