1#
2# Gramps - a GTK+/GNOME based genealogy program
3#
4# Copyright (C) 2013       Nick Hall
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20
21"""
22Helper class for importing places.
23"""
24from collections import OrderedDict
25
26#-------------------------------------------------------------------------
27#
28# Gramps modules
29#
30#-------------------------------------------------------------------------
31from gramps.gen.lib import Place, PlaceName, PlaceType, PlaceRef
32
33#-------------------------------------------------------------------------
34#
35# PlaceImport class
36#
37#-------------------------------------------------------------------------
38class PlaceImport:
39    """
40    Helper class for importing places.
41    """
42    def __init__(self, db):
43        self.db = db
44        self.loc2handle = {}
45        self.handle2loc = OrderedDict()
46
47    def store_location(self, location, handle):
48        """
49        Store the location of a place already in the database.
50        """
51        self.loc2handle[location] = handle
52        self.handle2loc[handle] = location
53
54    def remove_location(self, handle):
55        """
56        Remove the location of a place already in the database.
57        """
58        if handle in self.handle2loc:
59            loc = self.handle2loc[handle]
60            del(self.loc2handle[loc])
61            del(self.handle2loc[handle])
62
63    def generate_hierarchy(self, trans):
64        """
65        Generate missing places in the place hierarchy.
66        """
67        for handle, location in self.handle2loc.items():
68
69            # find title and type
70            for type_num, name in enumerate(location):
71                if name:
72                    break
73
74            loc = list(location)
75            loc[type_num] = ''
76
77            # find top parent
78            parent = None
79            for n in range(7):
80                if loc[n]:
81                    tup = tuple([''] * n + loc[n:])
82                    parent = self.loc2handle.get(tup)
83                    if parent:
84                        break
85
86            # create missing parent places
87            if parent:
88                n -= 1
89            while n > type_num:
90                if loc[n]:
91                    # TODO for Arabic, should the next comma be translated?
92                    title = ', '.join([item for item in loc[n:] if item])
93                    parent = self.__add_place(loc[n], n, parent, title, trans)
94                    self.loc2handle[tuple([''] * n + loc[n:])] = parent
95                n -= 1
96
97            # link to existing place
98            if parent:
99                place = self.db.get_place_from_handle(handle)
100                placeref = PlaceRef()
101                placeref.ref = parent
102                place.set_placeref_list([placeref])
103                self.db.commit_place(place, trans, place.get_change_time())
104
105    def __add_place(self, name, type_num, parent, title, trans):
106        """
107        Add a missing place to the database.
108        """
109        place = Place()
110        place_name = PlaceName()
111        place_name.set_value(name)
112        place.name = place_name
113        place.title = title
114        place.place_type = PlaceType(7-type_num)
115        if parent is not None:
116            placeref = PlaceRef()
117            placeref.ref = parent
118            place.set_placeref_list([placeref])
119        handle = self.db.add_place(place, trans)
120        self.db.commit_place(place, trans)
121        return handle
122