diff --git a/src/plugins/export/ExportSql.py b/src/plugins/export/ExportSql.py deleted file mode 100644 index 5dce0ca5c..000000000 --- a/src/plugins/export/ExportSql.py +++ /dev/null @@ -1,1074 +0,0 @@ -# Gramps - a GTK+/GNOME based genealogy program -# -# Copyright (C) 2008 Douglas S. Blank -# -# 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 -# -# $Id$ -# - -"Export to SQLite Database" - -#------------------------------------------------------------------------ -# -# Standard Python Modules -# -#------------------------------------------------------------------------ -from gettext import gettext as _ -from gettext import ngettext -import sqlite3 as sqlite -import time - -#------------------------------------------------------------------------ -# -# Set up logging -# -#------------------------------------------------------------------------ -import logging -log = logging.getLogger(".ExportSql") - -#------------------------------------------------------------------------ -# -# GRAMPS modules -# -#------------------------------------------------------------------------ -from ExportOptions import WriterOptionBox -from Utils import create_id - -#------------------------------------------------------------------------- -# -# Export functions -# -#------------------------------------------------------------------------- -def lookup(index, event_ref_list): - """ - Get the unserialized event_ref in an list of them and return it. - """ - if index < 0: - return None - else: - count = 0 - for event_ref in event_ref_list: - (private, note_list, attribute_list, ref, role) = event_ref - if index == count: - return ref - count += 1 - return None - -def makeDB(db): - db.query("""drop table note;""") - db.query("""drop table person;""") - db.query("""drop table event;""") - db.query("""drop table family;""") - db.query("""drop table repository;""") - db.query("""drop table repository_ref;""") - db.query("""drop table date;""") - db.query("""drop table place;""") - db.query("""drop table source;""") - db.query("""drop table media;""") - db.query("""drop table name;""") - db.query("""drop table link;""") - db.query("""drop table markup;""") - db.query("""drop table event_ref;""") - db.query("""drop table source_ref;""") - db.query("""drop table child_ref;""") - db.query("""drop table person_ref;""") - db.query("""drop table lds;""") - db.query("""drop table media_ref;""") - db.query("""drop table address;""") - db.query("""drop table location;""") - db.query("""drop table attribute;""") - db.query("""drop table url;""") - db.query("""drop table datamap;""") - - db.query("""CREATE TABLE note ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - text TEXT, - format INTEGER, - note_type1 INTEGER, - note_type2 TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE name ( - handle CHARACTER(25) PRIMARY KEY, - primary_name BOOLEAN, - private BOOLEAN, - first_name TEXT, - surname TEXT, - suffix TEXT, - title TEXT, - name_type0 INTEGER, - name_type1 TEXT, - prefix TEXT, - patronymic TEXT, - group_as TEXT, - sort_as INTEGER, - display_as INTEGER, - call TEXT);""") - - db.query("""CREATE TABLE date ( - handle CHARACTER(25) PRIMARY KEY, - calendar INTEGER, - modifier INTEGER, - quality INTEGER, - day1 INTEGER, - month1 INTEGER, - year1 INTEGER, - slash1 BOOLEAN, - day2 INTEGER, - month2 INTEGER, - year2 INTEGER, - slash2 BOOLEAN, - text TEXT, - sortval INTEGER, - newyear INTEGER);""") - - db.query("""CREATE TABLE person ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - gender INTEGER, - death_ref_handle TEXT, - birth_ref_handle TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE family ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - father_handle CHARACTER(25), - mother_handle CHARACTER(25), - the_type0 INTEGER, - the_type1 TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE place ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - title TEXT, - main_location CHARACTER(25), - long TEXT, - lat TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE event ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - the_type0 INTEGER, - the_type1 TEXT, - description TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE source ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - title TEXT, - author TEXT, - pubinfo TEXT, - abbrev TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE media ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - path TEXT, - mime TEXT, - desc TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE repository_ref ( - handle CHARACTER(25) PRIMARY KEY, - ref CHARACTER(25), - call_number TEXT, - source_media_type0 INTEGER, - source_media_type1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE repository ( - handle CHARACTER(25) PRIMARY KEY, - gid CHARACTER(25), - the_type0 INTEGER, - the_type1 TEXT, - name TEXT, - change INTEGER, - marker0 INTEGER, - marker1 TEXT, - private BOOLEAN);""") - - # One link to link them all - db.query("""CREATE TABLE link ( - from_type CHARACTER(25), - from_handle CHARACTER(25), - to_type CHARACTER(25), - to_handle CHARACTER(25));""") - - db.query("""CREATE INDEX idx_link_to ON - link(from_type, from_handle, to_type);""") - - db.query("""CREATE TABLE markup ( - handle CHARACTER(25) PRIMARY KEY, - markup0 INTEGER, - markup1 TEXT, - value TEXT, - start_stop_list TEXT);""") - - db.query("""CREATE TABLE event_ref ( - handle CHARACTER(25) PRIMARY KEY, - ref CHARACTER(25), - role0 INTEGER, - role1 TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE person_ref ( - handle CHARACTER(25) PRIMARY KEY, - description TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE source_ref ( - handle CHARACTER(25) PRIMARY KEY, - ref CHARACTER(25), - confidence INTEGER, - page CHARACTER(25), - private BOOLEAN);""") - - db.query("""CREATE TABLE child_ref ( - handle CHARACTER(25) PRIMARY KEY, - ref CHARACTER(25), - frel0 INTEGER, - frel1 CHARACTER(25), - mrel0 INTEGER, - mrel1 CHARACTER(25), - private BOOLEAN);""") - - db.query("""CREATE TABLE lds ( - handle CHARACTER(25) PRIMARY KEY, - type INTEGER, - place CHARACTER(25), - famc CHARACTER(25), - temple TEXT, - status INTEGER, - private BOOLEAN);""") - - db.query("""CREATE TABLE media_ref ( - handle CHARACTER(25) PRIMARY KEY, - ref CHARACTER(25), - role0 INTEGER, - role1 INTEGER, - role2 INTEGER, - role3 INTEGER, - private BOOLEAN);""") - - db.query("""CREATE TABLE address ( - handle CHARACTER(25) PRIMARY KEY, - private BOOLEAN);""") - - db.query("""CREATE TABLE location ( - handle CHARACTER(25) PRIMARY KEY, - street TEXT, - city TEXT, - county TEXT, - state TEXT, - country TEXT, - postal TEXT, - phone TEXT, - parish TEXT);""") - - db.query("""CREATE TABLE attribute ( - handle CHARACTER(25) PRIMARY KEY, - the_type0 INTEGER, - the_type1 TEXT, - value TEXT, - private BOOLEAN);""") - - db.query("""CREATE TABLE url ( - handle CHARACTER(25) PRIMARY KEY, - path TEXT, - desc TXT, - type0 INTEGER, - type1 TEXT, - private BOOLEAN); - """) - - db.query("""CREATE TABLE datamap ( - handle CHARACTER(25) PRIMARY KEY, - key_field TEXT, - value_field TXT); - """) - -class Database(object): - """ - The db connection. - """ - def __init__(self, database): - self.database = database - self.db = sqlite.connect(self.database) - self.cursor = self.db.cursor() - - def query(self, q, *args): - if q.strip().upper().startswith("DROP"): - try: - self.cursor.execute(q, args) - self.db.commit() - except: - "WARN: no such table to drop: '%s'" % q - else: - try: - self.cursor.execute(q, args) - self.db.commit() - except: - print "ERROR: query :", q - print "ERROR: values:", args - raise - return self.cursor.fetchall() - - def close(self): - """ Closes and writes out tables """ - self.cursor.close() - self.db.close() - -def export_location_list(db, from_type, from_handle, locations): - for location in locations: - export_location(db, from_type, from_handle, location) - -def export_url_list(db, from_type, from_handle, urls): - for url in urls: - # (False, u'http://www.gramps-project.org/', u'loleach', (0, u'kaabgo')) - (private, path, desc, type) = url - handle = create_id() - db.query("""insert INTO url ( - handle, - path, - desc, - type0, - type1, - private) VALUES (?, ?, ?, ?, ?, ?); - """, - handle, - path, - desc, - type[0], - type[1], - private) - # finally, link this to parent - export_link(db, from_type, from_handle, "url", handle) - -def export_person_ref_list(db, from_type, from_handle, person_ref_list): - for person_ref in person_ref_list: - (private, - source_list, - note_list, - handle, - desc) = person_ref - db.query("""INSERT INTO person_ref ( - handle, - description, - private) VALUES (?, ?, ?);""", - handle, - desc, - private - ) - export_list(db, "person_ref", handle, "note", note_list) - export_source_ref_list(db, "person_ref", handle, source_list) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "person_ref", handle) - -def export_lds(db, from_type, from_handle, data): - (lsource_list, lnote_list, date, type, place, - famc, temple, status, private) = data - lds_handle = create_id() - db.query("""INSERT into lds (handle, type, place, famc, temple, status, private) - VALUES (?,?,?,?,?,?,?);""", - lds_handle, type, place, famc, temple, status, private) - export_link(db, "lds", lds_handle, "place", place) - export_list(db, "lds", lds_handle, "note", lnote_list) - export_date(db, "lds", lds_handle, date) - export_source_ref_list(db, "lds", lds_handle, lsource_list) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "lds", lds_handle) - -def export_source_ref(db, from_type, from_handle, source): - (date, private, note_list, confidence, ref, page) = source - handle = create_id() - # handle is source_ref handle - # ref is source handle - db.query("""INSERT into source_ref ( - handle, - ref, - confidence, - page, - private - ) VALUES (?,?,?,?,?);""", - handle, - ref, - confidence, - page, - private) - export_date(db, "source_ref", handle, date) - export_list(db, "source_ref", handle, "note", note_list) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "source_ref", handle) - -def export_source(db, handle, gid, title, author, pubinfo, abbrev, change, - marker0, marker1, private): - db.query("""INSERT into source ( - handle, - gid, - title, - author, - pubinfo, - abbrev, - change, - marker0, - marker1, - private - ) VALUES (?,?,?,?,?,?,?,?,?,?);""", - handle, - gid, - title, - author, - pubinfo, - abbrev, - change, - marker0, - marker1, - private) - -def export_note(db, data): - (handle, gid, styled_text, format, note_type, - change, marker, private) = data - text, markup_list = styled_text - db.query("""INSERT into note ( - handle, - gid, - text, - format, - note_type1, - note_type2, - change, - marker0, - marker1, - private) values (?, ?, ?, ?, ?, - ?, ?, ?, ?, ?);""", - handle, gid, text, format, note_type[0], - note_type[1], change, marker[0], marker[1], private) - for markup in markup_list: - markup_code, value, start_stop_list = markup - export_markup(db, "note", handle, markup_code[0], markup_code[1], value, - str(start_stop_list)) # Not normal form; use eval - -def export_markup(db, from_type, from_handle, markup_code0, markup_code1, value, - start_stop_list): - markup_handle = create_id() - db.query("""INSERT INTO markup ( - handle, - markup0, - markup1, - value, - start_stop_list) VALUES (?,?,?,?,?);""", - markup_handle, markup_code0, markup_code1, value, - start_stop_list) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "markup", markup_handle) - -def export_event(db, data): - (handle, gid, the_type, date, description, place_handle, - source_list, note_list, media_list, attribute_list, - change, marker, private) = data - db.query("""INSERT INTO event ( - handle, - gid, - the_type0, - the_type1, - description, - change, - marker0, - marker1, - private) VALUES (?,?,?,?,?,?,?,?,?);""", - handle, - gid, - the_type[0], - the_type[1], - description, - change, - marker[0], - marker[1], - private) - export_date(db, "event", handle, date) - export_link(db, "event", handle, "place", place_handle) - export_list(db, "event", handle, "note", note_list) - export_attribute_list(db, "event", handle, attribute_list) - export_media_ref_list(db, "event", handle, media_list) - export_source_ref_list(db, "event", handle, source_list) - -def export_event_ref(db, from_type, from_handle, event_ref): - (private, note_list, attribute_list, ref, role) = event_ref - handle = create_id() - db.query("""insert INTO event_ref ( - handle, - ref, - role0, - role1, - private) VALUES (?,?,?,?,?);""", - handle, - ref, - role[0], - role[1], - private) - export_list(db, "event_ref", handle, "note", note_list) - export_attribute_list(db, "event_ref", handle, attribute_list) - # finally, link this to parent - export_link(db, from_type, from_handle, "event_ref", handle) - -def export_person(db, person): - (handle, # 0 - gid, # 1 - gender, # 2 - primary_name, # 3 - alternate_names, # 4 - death_ref_index, # 5 - birth_ref_index, # 6 - event_ref_list, # 7 - family_list, # 8 - parent_family_list, # 9 - media_list, # 10 - address_list, # 11 - attribute_list, # 12 - urls, # 13 - lds_ord_list, # 14 - psource_list, # 15 - pnote_list, # 16 - change, # 17 - marker, # 18 - private, # 19 - person_ref_list, # 20 - ) = person - db.query("""INSERT INTO person ( - handle, - gid, - gender, - death_ref_handle, - birth_ref_handle, - change, - marker0, - marker1, - private) values (?, ?, ?, ?, ?, ?, ?, ?, ?);""", - handle, - gid, - gender, - lookup(death_ref_index, event_ref_list), - lookup(birth_ref_index, event_ref_list), - change, - marker[0], - marker[1], - private) - - # Event Reference information - for event_ref in event_ref_list: - export_event_ref(db, "person", handle, event_ref) - export_list(db, "person", handle, "family", family_list) - export_list(db, "person", handle, "parent_family", parent_family_list) - export_media_ref_list(db, "person", handle, media_list) - export_list(db, "person", handle, "note", pnote_list) - export_attribute_list(db, "person", handle, attribute_list) - export_url_list(db, "person", handle, urls) - export_person_ref_list(db, "person", handle, person_ref_list) - export_source_ref_list(db, "person", handle, psource_list) - - # ------------------------------------- - # Address - # ------------------------------------- - for address in address_list: - export_address(db, "person", handle, address) - - # ------------------------------------- - # LDS ord - # ------------------------------------- - for ldsord in lds_ord_list: - export_lds(db, "person", handle, ldsord) - - # ------------------------------------- - # Names - # ------------------------------------- - export_name(db, "person", handle, True, primary_name) - map(lambda name: export_name(db, "person", handle, False, name), - alternate_names) - -def export_date(db, from_type, from_handle, data): - if data is None: return - (calendar, modifier, quality, dateval, text, sortval, newyear) = data - if len(dateval) == 4: - day1, month1, year1, slash1 = dateval - day2, month2, year2, slash2 = 0, 0, 0, 0 - elif len(dateval) == 8: - day1, month1, year1, slash1, day2, month2, year2, slash2 = dateval - else: - raise ("ERROR: date dateval format", dateval) - date_handle = create_id() - db.query("""INSERT INTO date ( - handle, - calendar, - modifier, - quality, - day1, - month1, - year1, - slash1, - day2, - month2, - year2, - slash2, - text, - sortval, - newyear) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, - ?, ?, ?, ?, ?, ?);""", - date_handle, calendar, modifier, quality, - day1, month1, year1, slash1, - day2, month2, year2, slash2, - text, sortval, newyear) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "date", date_handle) - -def export_name(db, from_type, from_handle, primary, data): - if data: - (private, source_list, note_list, date, - first_name, surname, suffix, title, - name_type, prefix, patronymic, - group_as, sort_as, display_as, call) = data - handle = create_id() - db.query("""INSERT into name ( - handle, - primary_name, - private, - first_name, - surname, - suffix, - title, - name_type0, - name_type1, - prefix, - patronymic, - group_as, - sort_as, - display_as, - call - ) values (?, ?, ?, ?, ?, ?, ?, ?, - ?, ?, ?, ?, ?, ?, ?);""", - handle, primary, private, first_name, surname, suffix, title, - name_type[0], name_type[1], prefix, patronymic, group_as, - sort_as, display_as, call) - export_date(db, "name", handle, date) - export_list(db, "name", handle, "note", note_list) - export_source_ref_list(db, "name", handle, source_list) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "name", handle) - -def export_attribute(db, from_type, from_handle, attribute): - (private, source_list, note_list, the_type, value) = attribute - handle = create_id() - db.query("""INSERT INTO attribute ( - handle, - the_type0, - the_type1, - value, - private) VALUES (?,?,?,?,?);""", - handle, the_type[0], the_type[1], value, private) - export_source_ref_list(db, "attribute", handle, source_list) - export_list(db, "attribute", handle, "note", note_list) - # finally, link the parent to the address - export_link(db, from_type, from_handle, "attribute", handle) - -def export_source_ref_list(db, from_type, from_handle, source_list): - for source in source_list: - export_source_ref(db, from_type, from_handle, source) - -def export_media_ref_list(db, from_type, from_handle, media_list): - for media in media_list: - export_media_ref(db, from_type, from_handle, media) - -def export_media_ref(db, from_type, from_handle, media): - (private, source_list, note_list, attribute_list, ref, role) = media - # handle is the media_ref handle - # ref is the media handle - handle = create_id() - if role is None: - role = (-1, -1, -1, -1) - db.query("""INSERT into media_ref ( - handle, - ref, - role0, - role1, - role2, - role3, - private) VALUES (?,?,?,?,?,?,?);""", - handle, ref, role[0], role[1], role[2], role[3], private) - export_list(db, "media_ref", handle, "note", note_list) - export_attribute_list(db, "media_ref", handle, attribute_list) - export_source_ref_list(db, "media_ref", handle, source_list) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "media_ref", handle) - -def export_attribute_list(db, from_type, from_handle, attr_list): - for attribute in attr_list: - export_attribute(db, from_type, from_handle, attribute) - -def export_child_ref_list(db, from_type, from_handle, to_type, ref_list): - for child_ref in ref_list: - # family -> child_ref - # (False, [], [], u'b305e96e39652d8f08c', (1, u''), (1, u'')) - (private, source_list, note_list, ref, frel, mrel) = child_ref - handle = create_id() - db.query("""INSERT INTO child_ref (handle, - ref, frel0, frel1, mrel0, mrel1, private) - VALUES (?, ?, ?, ?, ?, ?, ?);""", - handle, ref, frel[0], frel[1], - mrel[0], mrel[1], private) - export_source_ref_list(db, "child_ref", handle, source_list) - export_list(db, "child_ref", handle, "note", note_list) - # And finally, make a link from parent to new object - export_link(db, from_type, from_handle, "child_ref", handle) - -def export_list(db, from_type, from_handle, to_type, handle_list): - for to_handle in handle_list: - export_link(db, from_type, from_handle, to_type, to_handle) - -def export_link(db, from_type, from_handle, to_type, to_handle): - if to_handle: - db.query("""insert into link ( - from_type, - from_handle, - to_type, - to_handle) values (?, ?, ?, ?)""", - from_type, from_handle, to_type, to_handle) - -def export_datamap_dict(db, from_type, from_handle, datamap): - for key_field in datamap: - handle = create_id() - value_field = datamap[key_field] - db.query("""INSERT INTO datamap ( - handle, - key_field, - value_field) values (?, ?, ?)""", - handle, key_field, value_field) - export_link(db, from_type, from_handle, "datamap", handle) - -def export_address(db, from_type, from_handle, address): - (private, asource_list, anote_list, date, location) = address - addr_handle = create_id() - db.query("""INSERT INTO address ( - handle, - private) VALUES (?, ?);""", addr_handle, private) - export_location(db, "address", addr_handle, location) - export_date(db, "address", addr_handle, date) - export_list(db, "address", addr_handle, "note", anote_list) - export_source_ref_list(db, "address", addr_handle, asource_list) - # finally, link the parent to the address - export_link(db, from_type, from_handle, "address", addr_handle) - -def export_location(db, from_type, from_handle, location): - if location == None: return - if len(location) == 7: - (street, city, county, state, country, postal, phone) = location - parish = None - elif len(location) == 2: - ((street, city, county, state, country, postal, phone), parish) = location - else: - print "ERROR: what kind of location is this?", location - handle = create_id() - db.query("""INSERT INTO location ( - handle, - street, - city, - county, - state, - country, - postal, - phone, - parish) VALUES (?,?,?,?,?,?,?,?,?);""", - handle, street, city, county, state, country, postal, phone, parish) - # finally, link the parent to the address - export_link(db, from_type, from_handle, "location", handle) - -def export_repository_ref_list(db, from_type, from_handle, reporef_list): - for repo in reporef_list: - (note_list, - ref, - call_number, - source_media_type, - private) = repo - handle = create_id() - db.query("""insert INTO repository_ref ( - handle, - ref, - call_number, - source_media_type0, - source_media_type1, - private) VALUES (?,?,?,?,?,?);""", - handle, - ref, - call_number, - source_media_type[0], - source_media_type[1], - private) - export_list(db, "repository_ref", handle, "note", note_list) - # finally, link this to parent - export_link(db, from_type, from_handle, "repository_ref", handle) - -def exportData(database, filename, option_box=None, callback=None): - if not callable(callback): - callback = lambda (percent): None # dummy - - start = time.time() - total = (len(database.note_map) + - len(database.person_map) + - len(database.event_map) + - len(database.family_map) + - len(database.repository_map) + - len(database.place_map) + - len(database.media_map) + - len(database.source_map)) - count = 0.0 - - db = Database(filename) - makeDB(db) - - # --------------------------------- - # Notes - # --------------------------------- - for note_handle in database.note_map.keys(): - data = database.note_map[note_handle] - export_note(db, data) - count += 1 - callback(100 * count/total) - - # --------------------------------- - # Event - # --------------------------------- - for event_handle in database.event_map.keys(): - data = database.event_map[event_handle] - export_event(db, data) - count += 1 - callback(100 * count/total) - - # --------------------------------- - # Person - # --------------------------------- - for person_handle in database.person_map.keys(): - person = database.person_map[person_handle] - export_person(db, person) - count += 1 - callback(100 * count/total) - - # --------------------------------- - # Family - # --------------------------------- - for family_handle in database.family_map.keys(): - family = database.family_map[family_handle] - (handle, gid, father_handle, mother_handle, - child_ref_list, the_type, event_ref_list, media_list, - attribute_list, lds_seal_list, source_list, note_list, - change, marker, private) = family - # father_handle and/or mother_handle can be None - db.query("""INSERT INTO family ( - handle, - gid, - father_handle, - mother_handle, - the_type0, - the_type1, - change, - marker0, - marker1, - private) values (?,?,?,?,?,?,?,?,?,?);""", - handle, gid, father_handle, mother_handle, - the_type[0], the_type[1], change, marker[0], marker[1], - private) - - export_child_ref_list(db, "family", handle, "child_ref", child_ref_list) - export_list(db, "family", handle, "note", note_list) - export_attribute_list(db, "family", handle, attribute_list) - export_source_ref_list(db, "family", handle, source_list) - export_media_ref_list(db, "family", handle, media_list) - - # Event Reference information - for event_ref in event_ref_list: - export_event_ref(db, "family", handle, event_ref) - - # ------------------------------------- - # LDS - # ------------------------------------- - for ldsord in lds_seal_list: - export_lds(db, "family", handle, ldsord) - - count += 1 - callback(100 * count/total) - - # --------------------------------- - # Repository - # --------------------------------- - for repository_handle in database.repository_map.keys(): - repository = database.repository_map[repository_handle] - (handle, gid, the_type, name, note_list, - address_list, urls, change, marker, private) = repository - - db.query("""INSERT INTO repository ( - handle, - gid, - the_type0, - the_type1, - name, - change, - marker0, - marker1, - private) VALUES (?,?,?,?,?,?,?,?,?);""", - handle, gid, the_type[0], the_type[1], - name, change, marker[0], marker[1], private) - - export_list(db, "repository", handle, "note", note_list) - export_url_list(db, "repository", handle, urls) - - for address in address_list: - export_address(db, "repository", handle, address) - - count += 1 - callback(100 * count/total) - - # --------------------------------- - # Place - # --------------------------------- - for place_handle in database.place_map.keys(): - place = database.place_map[place_handle] - (handle, gid, title, long, lat, - main_loc, alt_location_list, - urls, - media_list, - source_list, - note_list, - change, marker, private) = place - - db.query("""INSERT INTO place ( - handle, - gid, - title, - long, - lat, - change, - marker0, - marker1, - private) values (?,?,?,?,?,?,?,?,?);""", - handle, gid, title, long, lat, - change, marker[0], marker[1], private) - - export_url_list(db, "place", handle, urls) - export_media_ref_list(db, "place", handle, media_list) - export_source_ref_list(db, "place", handle, source_list) - export_list(db, "place", handle, "note", note_list) - - # Main Location with parish: - # No need; we have the handle, but ok: - export_location(db, "place_main", handle, main_loc) - # But we need to link these: - export_location_list(db, "place_alt", handle, alt_location_list) - - count += 1 - callback(100 * count/total) - - # --------------------------------- - # Source - # --------------------------------- - for source_handle in database.source_map.keys(): - source = database.source_map[source_handle] - (handle, gid, title, - author, pubinfo, - note_list, - media_list, - abbrev, - change, datamap, - reporef_list, - marker, private) = source - - export_source(db, handle, gid, title, author, pubinfo, abbrev, change, - marker[0], marker[1], private) - export_list(db, "source", handle, "note", note_list) - export_media_ref_list(db, "source", handle, media_list) - export_datamap_dict(db, "source", handle, datamap) - export_repository_ref_list(db, "source", handle, reporef_list) - count += 1 - callback(100 * count/total) - - # --------------------------------- - # Media - # --------------------------------- - for media_handle in database.media_map.keys(): - media = database.media_map[media_handle] - (handle, gid, path, mime, desc, - attribute_list, - source_list, - note_list, - change, - date, - marker, - private) = media - - db.query("""INSERT INTO media ( - handle, - gid, - path, - mime, - desc, - change, - marker0, - marker1, - private) VALUES (?,?,?,?,?,?,?,?,?);""", - handle, gid, path, mime, desc, - change, marker[0], marker[1], private) - export_date(db, "media", handle, date) - export_list(db, "media", handle, "note", note_list) - export_source_ref_list(db, "media", handle, source_list) - export_attribute_list(db, "media", handle, attribute_list) - count += 1 - callback(100 * count/total) - - total_time = time.time() - start - msg = ngettext('Export Complete: %d second','Export Complete: %d seconds', total_time ) % total_time - print msg - return True - -# Future ideas -# Also include meta: -# Bookmarks -# Header - researcher info -# Name formats -# Namemaps? -# GRAMPS Version #, date, exporter diff --git a/src/plugins/import/ImportSql.py b/src/plugins/import/ImportSql.py deleted file mode 100644 index 599b4d495..000000000 --- a/src/plugins/import/ImportSql.py +++ /dev/null @@ -1,834 +0,0 @@ -# -# Gramps - a GTK+/GNOME based genealogy program -# -# Copyright (C) 2009 Douglas S. Blank -# -# 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 -# -# $Id$ - -"Import from SQLite Database" - -#------------------------------------------------------------------------- -# -# Standard Python Modules -# -#------------------------------------------------------------------------- -from gettext import gettext as _ -from gettext import ngettext -import sqlite3 as sqlite -import time - -#------------------------------------------------------------------------ -# -# Set up logging -# -#------------------------------------------------------------------------ -import logging -log = logging.getLogger(".ImportSql") - -#------------------------------------------------------------------------- -# -# GRAMPS modules -# -#------------------------------------------------------------------------- -import gen.lib -from QuestionDialog import ErrorDialog -from Utils import create_id - -#------------------------------------------------------------------------- -# -# Import functions -# -#------------------------------------------------------------------------- -def lookup(handle, event_ref_list): - """ - Find the handle in a unserialized event_ref_list and return code. - """ - if handle is None: - return -1 - else: - count = 0 - for event_ref in event_ref_list: - (private, note_list, attribute_list, ref, role) = event_ref - if handle == ref: - return count - count += 1 - return -1 - -#------------------------------------------------------------------------- -# -# SQLite DB Class -# -#------------------------------------------------------------------------- -class Database(object): - """ - The db connection. - """ - def __init__(self, database): - self.database = database - self.db = sqlite.connect(self.database) - self.cursor = self.db.cursor() - - def query(self, q, *args): - if q.strip().upper().startswith("DROP"): - try: - self.cursor.execute(q, args) - self.db.commit() - except: - "WARN: no such table to drop: '%s'" % q - else: - try: - self.cursor.execute(q, args) - self.db.commit() - except: - print "ERROR: query :", q - print "ERROR: values:", args - raise - return self.cursor.fetchall() - - def close(self): - """ Closes and writes out tables """ - self.cursor.close() - self.db.close() - -#------------------------------------------------------------------------- -# -# SQL Reader -# -#------------------------------------------------------------------------- -class SQLReader(object): - def __init__(self, db, filename, callback): - if not callable(callback): - callback = lambda (percent): None # dummy - self.db = db - self.filename = filename - self.callback = callback - self.debug = 0 - - def openSQL(self): - sql = None - try: - sql = Database(self.filename) - except IOError, msg: - errmsg = _("%s could not be opened\n") % self.filename - ErrorDialog(errmsg,str(msg)) - return None - return sql - - # ----------------------------------------------- - # Get methods to retrieve data from the tables - # ----------------------------------------------- - - def get_address_list(self, sql, from_type, from_handle, with_parish): - results = self.get_links(sql, from_type, from_handle, "address") - retval = [] - for handle in results: - result = sql.query("select * from address where handle = ?;", - handle) - retval.append(self.pack_address(sql, result[0], with_parish)) - return retval - - def get_attribute_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "attribute") - retval = [] - for handle in handles: - rows = sql.query("select * from attribute where handle = ?;", - handle) - for row in rows: - (handle, - the_type0, - the_type1, - value, - private) = row - source_list = self.get_source_ref_list(sql, "attribute", handle) - note_list = self.get_note_list(sql, "attribute", handle) - retval.append((private, source_list, note_list, - (the_type0, the_type1), value)) - return retval - - def get_child_ref_list(self, sql, from_type, from_handle): - results = self.get_links(sql, from_type, from_handle, "child_ref") - retval = [] - for handle in results: - rows = sql.query("select * from child_ref where handle = ?;", - handle) - for row in rows: - (handle, ref, frel0, frel1, mrel0, mrel1, private) = row - source_list = self.get_source_ref_list(sql, "child_ref", handle) - note_list = self.get_note_list(sql, "child_ref", handle) - retval.append((private, source_list, note_list, ref, - (frel0, frel1), (mrel0, mrel1))) - return retval - - def get_datamap(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "datamap") - datamap = {} - for handle in handles: - row = sql.query("select * from datamap where handle = ?;", - handle) - if len(row) == 1: - (handle, key_field, value_field) = row[0] - datamap[key_field] = value_field - else: - print "ERROR: invalid datamap item '%s'" % handle - return datamap - - def get_event_ref_list(self, sql, from_type, from_handle): - results = self.get_links(sql, from_type, from_handle, "event_ref") - retval = [] - for handle in results: - result = sql.query("select * from event_ref where handle = ?;", - handle) - retval.append(self.pack_event_ref(sql, result[0])) - return retval - - def get_family_list(self, sql, from_type, from_handle): - return self.get_links(sql, from_type, from_handle, "family") - - def get_parent_family_list(self, sql, from_type, from_handle): - return self.get_links(sql, from_type, from_handle, "parent_family") - - def get_person_ref_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "person_ref") - retval = [] - for ref_handle in handles: - rows = sql.query("select * from person_ref where handle = ?;", - ref_handle) - for row in rows: - (handle, - description, - private) = row - source_list = self.get_source_ref_list(sql, "person_ref", handle) - note_list = self.get_note_list(sql, "person_ref", handle) - retval.append((private, - source_list, - note_list, - handle, - description)) - return retval - - def get_location_list(self, sql, from_type, from_handle, with_parish): - handles = self.get_links(sql, from_type, from_handle, "location") - results = [] - for handle in handles: - results += sql.query("""select * from location where handle = ?;""", - handle) - return [self.pack_location(sql, result, with_parish) for result in results] - - def get_lds_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "lds") - results = [] - for handle in handles: - results += sql.query("""select * from lds where handle = ?;""", - handle) - return [self.pack_lds(sql, result) for result in results] - - def get_media_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "media_ref") - results = [] - for handle in handles: - results += sql.query("""select * from media_ref where handle = ?;""", - handle) - return [self.pack_media_ref(sql, result) for result in results] - - def get_note_list(self, sql, from_type, from_handle): - return self.get_links(sql, from_type, from_handle, "note") - - def get_repository_ref_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "repository_ref") - results = [] - for handle in handles: - results += sql.query("""select * from repository_ref where handle = ?;""", - handle) - return [self.pack_repository_ref(sql, result) for result in results] - - def get_source_ref_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "source_ref") - results = [] - for handle in handles: - results += sql.query("""select * from source_ref where handle = ?;""", - handle) - return [self.pack_source_ref(sql, result) for result in results] - - def get_url_list(self, sql, from_type, from_handle): - handles = self.get_links(sql, from_type, from_handle, "url") - results = [] - for handle in handles: - results += sql.query("""select * from url where handle = ?;""", - handle) - return [self.pack_url(sql, result) for result in results] - - # --------------------------------- - # Helpers - # --------------------------------- - - def pack_address(self, sql, data, with_parish): - (handle, private) = data - source_list = self.get_source_ref_list(sql, "address", handle) - date_handle = self.get_link(sql, "address", handle, "date") - date = self.get_date(sql, date_handle) - note_list = self.get_note_list(sql, "address", handle) - location = self.get_location(sql, "address", handle, with_parish) - return (private, source_list, note_list, date, location) - - def pack_lds(self, sql, data): - (handle, type, place, famc, temple, status, private) = data - source_list = self.get_source_ref_list(sql, "lds", handle) - note_list = self.get_note_list(sql, "lds", handle) - date_handle = self.get_link(sql, "lds", handle, "date") - date = self.get_date(sql, date_handle) - return (source_list, note_list, date, type, place, - famc, temple, status, private) - - def pack_media_ref(self, sql, data): - (handle, - ref, - role0, - role1, - role2, - role3, - private) = data - source_list = self.get_source_ref_list(sql, "media_ref", handle) - note_list = self.get_note_list(sql, "media_ref", handle) - attribute_list = self.get_attribute_list(sql, "media_ref", handle) - if role0 == role1 == role2 == role3 == -1: - role = None - else: - role = (role0, role1, role2, role3) - return (private, source_list, note_list, attribute_list, ref, role) - - def pack_repository_ref(self, sql, data): - (handle, - ref, - call_number, - source_media_type0, - source_media_type1, - private) = data - note_list = self.get_note_list(sql, "repository_ref", handle) - return (note_list, - ref, - call_number, - (source_media_type0, source_media_type1), - private) - - def pack_url(self, sql, data): - (handle, - path, - desc, - type0, - type1, - private) = data - return (private, path, desc, (type0, type1)) - - def pack_event_ref(self, sql, data): - (handle, - ref, - role0, - role1, - private) = data - note_list = self.get_note_list(sql, "event_ref", handle) - attribute_list = self.get_attribute_list(sql, "event_ref", handle) - role = (role0, role1) - return (private, note_list, attribute_list, ref, role) - - def pack_source_ref(self, sql, data): - (handle, - ref, - confidence, - page, - private) = data - date_handle = self.get_link(sql, "source_ref", handle, "date") - date = self.get_date(sql, date_handle) - note_list = self.get_note_list(sql, "source_ref", handle) - return (date, private, note_list, confidence, ref, page) - - def pack_source(self, sql, data): - (handle, - gid, - title, - author, - pubinfo, - abbrev, - change, - marker0, - marker1, - private) = data - note_list = self.get_note_list(sql, "source", handle) - media_list = self.get_media_list(sql, "source", handle) - reporef_list = self.get_repository_ref_list(sql, "source", handle) - datamap = {} - return (handle, gid, title, - author, pubinfo, - note_list, - media_list, - abbrev, - change, datamap, - reporef_list, - (marker0, marker1), private) - - def get_location(self, sql, from_type, from_handle, with_parish): - handle = self.get_link(sql, from_type, from_handle, "location") - if handle: - results = sql.query("""select * from location where handle = ?;""", - handle) - if len(results) == 1: - return self.pack_location(sql, results[0], with_parish) - - def get_names(self, sql, from_type, from_handle, primary): - handles = self.get_links(sql, from_type, from_handle, "name") - names = [] - for handle in handles: - results = sql.query("""select * from name where handle = ? and primary_name = ?;""", - handle, primary) - if len(results) > 0: - names += results - result = [self.pack_name(sql, name) for name in names] - if primary: - if len(result) == 1: - return result[0] - elif len(result) == 0: - return gen.lib.Name().serialize() - else: - raise Exception("too many primary names") - else: - return result - - def pack_name(self, sql, data): - # unpack name from SQL table: - (handle, - primary_name, - private, - first_name, - surname, - suffix, - title, - name_type0, - name_type1, - prefix, - patronymic, - group_as, - sort_as, - display_as, - call) = data - # build up a GRAMPS object: - source_list = self.get_source_ref_list(sql, "name", handle) - note_list = self.get_note_list(sql, "name", handle) - date_handle = self.get_link(sql, "name", handle, "date") - date = self.get_date(sql, date_handle) - return (private, source_list, note_list, date, - first_name, surname, suffix, title, - (name_type0, name_type1), prefix, patronymic, - group_as, sort_as, display_as, call) - - def pack_location(self, sql, data, with_parish): - (handle, - street, - city, - county, - state, - country, - postal, - phone, - parish) = data - if with_parish: - return ((street, city, county, state, country, postal, phone), parish) - else: - return (street, city, county, state, country, postal, phone) - - def get_place_from_handle(self, sql, ref_handle): - if ref_handle: - place_row = sql.query("select * from place where handle = ?;", - ref_handle) - if len(place_row) == 1: - # return just the handle here: - return place_row[0][0] - elif len(place_row) == 0: - print "ERROR: get_place_from_handle('%s'), no such handle." % (ref_handle, ) - else: - print "ERROR: get_place_from_handle('%s') should be unique; returned %d records." % (ref_handle, len(place_row)) - return '' - - def get_main_location(self, sql, from_handle, with_parish): - ref_handle = self.get_link(sql, "place_main", from_handle, "location") - if ref_handle: - place_row = sql.query("select * from location where handle = ?;", - ref_handle) - if len(place_row) == 1: - return self.pack_location(sql, place_row[0], with_parish) - elif len(place_row) == 0: - print "ERROR: get_main_location('%s'), no such handle." % (ref_handle, ) - else: - print "ERROR: get_main_location('%s') should be unique; returned %d records." % (ref_handle, len(place_row)) - return gen.lib.Location().serialize() - - def get_link(self, sql, from_type, from_handle, to_link): - """ - Return a link, and return handle. - """ - if from_handle is None: return - assert type(from_handle) in [unicode, str], "from_handle is wrong type: %s is %s" % (from_handle, type(from_handle)) - rows = self.get_links(sql, from_type, from_handle, to_link) - if len(rows) == 1: - return rows[0] - elif len(rows) > 1: - print "ERROR: too many links %s:%s -> %s (%d)" % (from_type, from_handle, to_link, len(rows)) - return None - - def get_links(self, sql, from_type, from_handle, to_link): - """ - Return a list of handles (possibly none). - """ - results = sql.query("""select to_handle from link where from_type = ? and from_handle = ? and to_type = ?;""", - from_type, from_handle, to_link) - return [result[0] for result in results] - - def get_date(self, sql, handle): - assert type(handle) in [unicode, str, type(None)], "handle is wrong type: %s" % handle - if handle: - rows = sql.query("select * from date where handle = ?;", handle) - if len(rows) == 1: - (handle, - calendar, - modifier, - quality, - day1, - month1, - year1, - slash1, - day2, - month2, - year2, - slash2, - text, - sortval, - newyear) = rows[0] - dateval = day1, month1, year1, slash1, day2, month2, year2, slash2 - if slash1 == day2 == month2 == year2 == slash2 == 0: - dateval = day1, month1, year1, slash1 - return (calendar, modifier, quality, dateval, text, sortval, newyear) - elif len(rows) == 0: - return None - else: - print Exception("ERROR, wrong number of dates: %s" % rows) - - def process(self): - sql = self.openSQL() - total = (sql.query("select count(*) from note;")[0][0] + - sql.query("select count(*) from person;")[0][0] + - sql.query("select count(*) from event;")[0][0] + - sql.query("select count(*) from family;")[0][0] + - sql.query("select count(*) from repository;")[0][0] + - sql.query("select count(*) from place;")[0][0] + - sql.query("select count(*) from media;")[0][0] + - sql.query("select count(*) from source;")[0][0]) - self.trans = self.db.transaction_begin("",batch=True) - self.db.disable_signals() - count = 0.0 - self.t = time.time() - # --------------------------------- - # Process note - # --------------------------------- - notes = sql.query("""select * from note;""") - for note in notes: - (handle, - gid, - text, - format, - note_type1, - note_type2, - change, - marker0, - marker1, - private) = note - styled_text = [text, []] - # direct connection with note handle - markups = sql.query("""select * from markup where handle = ?""", handle) - for markup in markups: - (mhandle, - markup0, - markup1, - value, - start_stop_list) = markup - ss_list = eval(start_stop_list) - styled_text[1] += [((markup0, markup1), value, ss_list)] - self.db.note_map[str(handle)] = (str(handle), gid, styled_text, - format, (note_type1, note_type2), change, - (marker0, marker1), private) - count += 1 - self.callback(100 * count/total) - - # --------------------------------- - # Process event - # --------------------------------- - events = sql.query("""select * from event;""") - for event in events: - (handle, - gid, - the_type0, - the_type1, - description, - change, - marker0, - marker1, - private) = event - - note_list = self.get_note_list(sql, "event", handle) - source_list = self.get_source_ref_list(sql, "event", handle) - media_list = self.get_media_list(sql, "event", handle) - attribute_list = self.get_attribute_list(sql, "event", handle) - - date_handle = self.get_link(sql, "event", handle, "date") - date = self.get_date(sql, date_handle) - - place_handle = self.get_link(sql, "event", handle, "place") - place = self.get_place_from_handle(sql, place_handle) - - data = (str(handle), gid, (the_type0, the_type1), date, description, place, - source_list, note_list, media_list, attribute_list, - change, (marker0, marker1), private) - - self.db.event_map[str(handle)] = data - - count += 1 - self.callback(100 * count/total) - - # --------------------------------- - # Process person - # --------------------------------- - people = sql.query("""select * from person;""") - for person in people: - if person is None: - continue - (handle, # 0 - gid, # 1 - gender, # 2 - death_ref_handle, # 5 - birth_ref_handle, # 6 - change, # 17 - marker0, # 18 - marker1, # 18 - private, # 19 - ) = person - primary_name = self.get_names(sql, "person", handle, True) # one - alternate_names = self.get_names(sql, "person", handle, False) # list - event_ref_list = self.get_event_ref_list(sql, "person", handle) - family_list = self.get_family_list(sql, "person", handle) - parent_family_list = self.get_parent_family_list(sql, "person", handle) - media_list = self.get_media_list(sql, "person", handle) - address_list = self.get_address_list(sql, "person", handle, with_parish=False) - attribute_list = self.get_attribute_list(sql, "person", handle) - urls = self.get_url_list(sql, "person", handle) - lds_ord_list = self.get_lds_list(sql, "person", handle) - psource_list = self.get_source_ref_list(sql, "person", handle) - pnote_list = self.get_note_list(sql, "person", handle) - person_ref_list = self.get_person_ref_list(sql, "person", handle) - death_ref_index = lookup(death_ref_handle, event_ref_list) - birth_ref_index = lookup(birth_ref_handle, event_ref_list) - self.db.person_map[str(handle)] = (str(handle), # 0 - gid, # 1 - gender, # 2 - primary_name, # 3 - alternate_names, # 4 - death_ref_index, # 5 - birth_ref_index, # 6 - event_ref_list, # 7 - family_list, # 8 - parent_family_list, # 9 - media_list, # 10 - address_list, # 11 - attribute_list, # 12 - urls, # 13 - lds_ord_list, # 14 - psource_list, # 15 - pnote_list, # 16 - change, # 17 - (marker0, marker1), # 18 - private, # 19 - person_ref_list, # 20 - ) - count += 1 - self.callback(100 * count/total) - # --------------------------------- - # Process family - # --------------------------------- - families = sql.query("""select * from family;""") - for family in families: - (handle, - gid, - father_handle, - mother_handle, - the_type0, - the_type1, - change, - marker0, - marker1, - private) = family - - child_ref_list = self.get_child_ref_list(sql, "family", handle) - event_ref_list = self.get_event_ref_list(sql, "family", handle) - media_list = self.get_media_list(sql, "family", handle) - attribute_list = self.get_attribute_list(sql, "family", handle) - lds_seal_list = self.get_lds_list(sql, "family", handle) - source_list = self.get_source_ref_list(sql, "family", handle) - note_list = self.get_note_list(sql, "family", handle) - - self.db.family_map[str(handle)] = (str(handle), gid, - father_handle, mother_handle, - child_ref_list, (the_type0, the_type1), - event_ref_list, media_list, - attribute_list, lds_seal_list, - source_list, note_list, - change, (marker0, marker1), private) - - count += 1 - self.callback(100 * count/total) - # --------------------------------- - # Process repository - # --------------------------------- - repositories = sql.query("""select * from repository;""") - for repo in repositories: - (handle, - gid, - the_type0, - the_type1, - name, - change, - marker0, - marker1, - private) = repo - - note_list = self.get_note_list(sql, "repository", handle) - address_list = self.get_address_list(sql, "repository", handle, with_parish=False) - urls = self.get_url_list(sql, "repository", handle) - - self.db.repository_map[str(handle)] = (str(handle), gid, - (the_type0, the_type1), - name, note_list, - address_list, urls, change, - (marker0, marker1), private) - count += 1 - self.callback(100 * count/total) - # --------------------------------- - # Process place - # --------------------------------- - places = sql.query("""select * from place;""") - for place in places: - count += 1 - (handle, - gid, - title, - main_loc, - long, - lat, - change, - marker0, - marker1, - private) = place - - # We could look this up by "place_main", but we have the handle: - main_loc = self.get_main_location(sql, handle, with_parish=True) - alt_location_list = self.get_location_list(sql, "place_alt", handle, - with_parish=True) - urls = self.get_url_list(sql, "place", handle) - media_list = self.get_media_list(sql, "place", handle) - source_list = self.get_source_ref_list(sql, "place", handle) - note_list = self.get_note_list(sql, "place", handle) - self.db.place_map[str(handle)] = (str(handle), gid, title, long, lat, - main_loc, alt_location_list, - urls, - media_list, - source_list, - note_list, - change, (marker0, marker1), - private) - self.callback(100 * count/total) - # --------------------------------- - # Process source - # --------------------------------- - sources = sql.query("""select * from source;""") - for source in sources: - (handle, - gid, - title, - author, - pubinfo, - abbrev, - change, - marker0, - marker1, - private) = source - note_list = self.get_note_list(sql, "source", handle) - media_list = self.get_media_list(sql, "source", handle) - datamap = self.get_datamap(sql, "source", handle) - reporef_list = self.get_repository_ref_list(sql, "source", handle) - - self.db.source_map[str(handle)] = (str(handle), gid, title, - author, pubinfo, - note_list, - media_list, - abbrev, - change, datamap, - reporef_list, - (marker0, marker1), private) - count += 1 - self.callback(100 * count/total) - # --------------------------------- - # Process media - # --------------------------------- - media = sql.query("""select * from media;""") - for med in media: - (handle, - gid, - path, - mime, - desc, - change, - marker0, - marker1, - private) = med - - attribute_list = self.get_attribute_list(sql, "media", handle) - source_list = self.get_source_ref_list(sql, "media", handle) - note_list = self.get_note_list(sql, "media", handle) - - date_handle = self.get_link(sql, "media", handle, "date") - date = self.get_date(sql, date_handle) - - self.db.media_map[str(handle)] = (str(handle), gid, path, mime, desc, - attribute_list, - source_list, - note_list, - change, - date, - (marker0, marker1), - private) - count += 1 - self.callback(100 * count/total) - return None - - def cleanup(self): - self.t = time.time() - self.t - msg = ngettext('Import Complete: %d second','Import Complete: %d seconds', self.t ) % self.t - self.db.transaction_commit(self.trans,_("SQL import")) - self.db.enable_signals() - self.db.request_rebuild() - print msg - - -def importData(db, filename, callback=None): - g = SQLReader(db, filename, callback) - g.process() - g.cleanup()