From 789158aca5a0b3cab5817386e1f0bf32d06d9731 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Wed, 13 May 2015 21:29:07 -0400 Subject: [PATCH] DictionaryDb: now reads/writes on open/close --- gramps/gen/db/dbconst.py | 58 +++++-- .../plugins/database/bsddb_support/write.py | 33 ---- gramps/plugins/database/dictionarydb.py | 142 +++++++++++++++--- gramps/plugins/database/djangodb.py | 6 - gramps/plugins/importer/importxml.py | 2 +- 5 files changed, 170 insertions(+), 71 deletions(-) diff --git a/gramps/gen/db/dbconst.py b/gramps/gen/db/dbconst.py index e53535248..21a17db80 100644 --- a/gramps/gen/db/dbconst.py +++ b/gramps/gen/db/dbconst.py @@ -28,20 +28,16 @@ Declare constants used by database modules # constants # #------------------------------------------------------------------------- -__all__ = ( - ('DBPAGE', 'DBMODE', 'DBCACHE', 'DBLOCKS', 'DBOBJECTS', 'DBUNDO', - 'DBEXT', 'DBMODE_R', 'DBMODE_W', 'DBUNDOFN', 'DBLOCKFN', - 'DBRECOVFN','BDBVERSFN', 'DBLOGNAME', 'SCHVERSFN', 'PCKVERSFN', - 'DBBACKEND' - ) + - - ('PERSON_KEY', 'FAMILY_KEY', 'SOURCE_KEY', 'CITATION_KEY', - 'EVENT_KEY', 'MEDIA_KEY', 'PLACE_KEY', 'REPOSITORY_KEY', - 'NOTE_KEY', 'REFERENCE_KEY', 'TAG_KEY' - ) + - - ('TXNADD', 'TXNUPD', 'TXNDEL') - ) +__all__ = ( 'DBPAGE', 'DBMODE', 'DBCACHE', 'DBLOCKS', 'DBOBJECTS', 'DBUNDO', + 'DBEXT', 'DBMODE_R', 'DBMODE_W', 'DBUNDOFN', 'DBLOCKFN', + 'DBRECOVFN','BDBVERSFN', 'DBLOGNAME', 'SCHVERSFN', 'PCKVERSFN', + 'DBBACKEND', + 'PERSON_KEY', 'FAMILY_KEY', 'SOURCE_KEY', 'CITATION_KEY', + 'EVENT_KEY', 'MEDIA_KEY', 'PLACE_KEY', 'REPOSITORY_KEY', + 'NOTE_KEY', 'REFERENCE_KEY', 'TAG_KEY', + 'TXNADD', 'TXNUPD', 'TXNDEL', + "CLASS_TO_KEY_MAP", "KEY_TO_CLASS_MAP", "KEY_TO_NAME_MAP" + ) DBEXT = ".db" # File extension to be used for database files DBUNDOFN = "undo.db" # File name of 'undo' database @@ -74,3 +70,37 @@ TAG_KEY = 9 CITATION_KEY = 10 TXNADD, TXNUPD, TXNDEL = 0, 1, 2 + +CLASS_TO_KEY_MAP = {"Person": PERSON_KEY, + "Family": FAMILY_KEY, + "Source": SOURCE_KEY, + "Citation": CITATION_KEY, + "Event": EVENT_KEY, + "MediaObject": MEDIA_KEY, + "Place": PLACE_KEY, + "Repository": REPOSITORY_KEY, + "Note" : NOTE_KEY, + "Tag": TAG_KEY} + +KEY_TO_CLASS_MAP = {PERSON_KEY: "Person", + FAMILY_KEY: "Family", + SOURCE_KEY: "Source", + CITATION_KEY: "Citation", + EVENT_KEY: "Event", + MEDIA_KEY: "MediaObject", + PLACE_KEY: "Place", + REPOSITORY_KEY: "Repository", + NOTE_KEY: "Note", + TAG_KEY: "Tag"} + +KEY_TO_NAME_MAP = {PERSON_KEY: 'person', + FAMILY_KEY: 'family', + EVENT_KEY: 'event', + SOURCE_KEY: 'source', + CITATION_KEY: 'citation', + PLACE_KEY: 'place', + MEDIA_KEY: 'media', + REPOSITORY_KEY: 'repository', + #REFERENCE_KEY: 'reference', + NOTE_KEY: 'note', + TAG_KEY: 'tag'} diff --git a/gramps/plugins/database/bsddb_support/write.py b/gramps/plugins/database/bsddb_support/write.py index 606f9484a..66d4e4a53 100644 --- a/gramps/plugins/database/bsddb_support/write.py +++ b/gramps/plugins/database/bsddb_support/write.py @@ -130,39 +130,6 @@ DBERRS = (db.DBRunRecoveryError, db.DBAccessError, # these maps or modifying the values of the keys will break # existing databases. -CLASS_TO_KEY_MAP = {Person.__name__: PERSON_KEY, - Family.__name__: FAMILY_KEY, - Source.__name__: SOURCE_KEY, - Citation.__name__: CITATION_KEY, - Event.__name__: EVENT_KEY, - MediaObject.__name__: MEDIA_KEY, - Place.__name__: PLACE_KEY, - Repository.__name__:REPOSITORY_KEY, - Note.__name__: NOTE_KEY, - Tag.__name__: TAG_KEY} - -KEY_TO_CLASS_MAP = {PERSON_KEY: Person.__name__, - FAMILY_KEY: Family.__name__, - SOURCE_KEY: Source.__name__, - CITATION_KEY: Citation.__name__, - EVENT_KEY: Event.__name__, - MEDIA_KEY: MediaObject.__name__, - PLACE_KEY: Place.__name__, - REPOSITORY_KEY: Repository.__name__, - NOTE_KEY: Note.__name__, - TAG_KEY: Tag.__name__} - -KEY_TO_NAME_MAP = {PERSON_KEY: 'person', - FAMILY_KEY: 'family', - EVENT_KEY: 'event', - SOURCE_KEY: 'source', - CITATION_KEY: 'citation', - PLACE_KEY: 'place', - MEDIA_KEY: 'media', - REPOSITORY_KEY: 'repository', - #REFERENCE_KEY: 'reference', - NOTE_KEY: 'note', - TAG_KEY: 'tag'} #------------------------------------------------------------------------- # # Helper functions diff --git a/gramps/plugins/database/dictionarydb.py b/gramps/plugins/database/dictionarydb.py index 621425e2d..09e2451b6 100644 --- a/gramps/plugins/database/dictionarydb.py +++ b/gramps/plugins/database/dictionarydb.py @@ -28,6 +28,7 @@ import pickle import base64 import time import re +import os from gramps.gen.db import DbReadBase, DbWriteBase, DbTxn from gramps.gen.utils.callback import Callback from gramps.gen.updatecallback import UpdateCallback @@ -40,7 +41,8 @@ from gramps.gen.db import (PERSON_KEY, PLACE_KEY, REPOSITORY_KEY, NOTE_KEY, - TAG_KEY) + TAG_KEY, + KEY_TO_NAME_MAP) from gramps.gen.utils.id import create_id from gramps.gen.lib.researcher import Researcher @@ -197,7 +199,7 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): __callback_map = {} - def __init__(self, *args, **kwargs): + def __init__(self, directory=None): DbReadBase.__init__(self) DbWriteBase.__init__(self) Callback.__init__(self) @@ -323,7 +325,7 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): self.place_bookmarks = Bookmarks() self.citation_bookmarks = Bookmarks() self.source_bookmarks = Bookmarks() - self.repository_bookmarks = Bookmarks() + self.repo_bookmarks = Bookmarks() self.media_bookmarks = Bookmarks() self.note_bookmarks = Bookmarks() self.set_person_id_prefix('I%04d') @@ -373,6 +375,9 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): self.modified = 0 self.txn = DictionaryTxn("DbDictionary Transaction", self) self.transaction = None + self._directory = directory + if directory: + self.load(directory) def version_supported(self): """Return True when the file has a supported version.""" @@ -768,24 +773,66 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): return tag return None - def get_family_from_gramps_id(self, gramps_id): - for family in self.family_map.values(): - if family.gramps_id == gramps_id: - return family - return None - def get_person_from_gramps_id(self, gramps_id): for person in self.person_map.values(): if person.gramps_id == gramps_id: return person return None + def get_family_from_gramps_id(self, gramps_id): + for family in self.family_map.values(): + if family.gramps_id == gramps_id: + return family + return None + + def get_citation_from_gramps_id(self, gramps_id): + for citation in self.citation_map.values(): + if citation.gramps_id == gramps_id: + return citation + return None + + def get_source_from_gramps_id(self, gramps_id): + for source in self.source_map.values(): + if source.gramps_id == gramps_id: + return source + return None + + def get_event_from_gramps_id(self, gramps_id): + for event in self.event_map.values(): + if event.gramps_id == gramps_id: + return event + return None + + def get_media_from_gramps_id(self, gramps_id): + for media in self.media_map.values(): + if media.gramps_id == gramps_id: + return media + return None + def get_place_from_gramps_id(self, gramps_id): for place in self.place_map.values(): if place.gramps_id == gramps_id: return place return None + def get_repository_from_gramps_id(self, gramps_id): + for repository in self.repository_map.values(): + if repository.gramps_id == gramps_id: + return repository + return None + + def get_note_from_gramps_id(self, gramps_id): + for note in self.note_map.values(): + if note.gramps_id == gramps_id: + return note + return None + + def get_tag_from_gramps_id(self, gramps_id): + for tag in self.tag_map.values(): + if tag.gramps_id == gramps_id: + return tag + return None + def get_number_of_people(self): return len(self.person_map) @@ -1038,33 +1085,73 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): return obj.handle def commit_person(self, person, trans, change_time=None): + if person.handle in self.person_map: + self.emit("person-update", ([person.handle],)) + else: + self.emit("person-add", ([person.handle],)) self.person_map[person.handle] = person def commit_family(self, family, trans, change_time=None): + if family.handle in self.family_map: + self.emit("family-update", ([family.handle],)) + else: + self.emit("family-add", ([family.handle],)) self.family_map[family.handle] = family def commit_citation(self, citation, trans, change_time=None): + if citation.handle in self.citation_map: + self.emit("citation-update", ([citation.handle],)) + else: + self.emit("citation-add", ([citation.handle],)) self.citation_map[citation.handle] = citation def commit_source(self, source, trans, change_time=None): + if source.handle in self.source_map: + self.emit("source-update", ([source.handle],)) + else: + self.emit("source-add", ([source.handle],)) self.source_map[source.handle] = source def commit_repository(self, repository, trans, change_time=None): + if repository.handle in self.repository_map: + self.emit("repository-update", ([repository.handle],)) + else: + self.emit("repository-add", ([repository.handle],)) self.repository_map[repository.handle] = repository def commit_note(self, note, trans, change_time=None): + if note.handle in self.note_map: + self.emit("note-update", ([note.handle],)) + else: + self.emit("note-add", ([note.handle],)) self.note_map[note.handle] = note def commit_place(self, place, trans, change_time=None): + if place.handle in self.place_map: + self.emit("place-update", ([place.handle],)) + else: + self.emit("place-add", ([place.handle],)) self.place_map[place.handle] = place def commit_event(self, event, trans, change_time=None): + if event.handle in self.event_map: + self.emit("event-update", ([event.handle],)) + else: + self.emit("event-add", ([event.handle],)) self.event_map[event.handle] = event def commit_tag(self, tag, trans, change_time=None): + if tag.handle in self.tag_map: + self.emit("tag-update", ([tag.handle],)) + else: + self.emit("tag-add", ([tag.handle],)) self.tag_map[tag.handle] = tag def commit_media_object(self, obj, transaction, change_time=None): + if commit.handle in self.commit_map: + self.emit("commit-update", ([commit.handle],)) + else: + self.emit("commit-add", ([commit.handle],)) self.media_map[obj.handle] = obj def get_gramps_ids(self, obj_key): @@ -1092,7 +1179,16 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): pass def request_rebuild(self): - pass + self.emit('person-rebuild') + self.emit('family-rebuild') + self.emit('place-rebuild') + self.emit('source-rebuild') + self.emit('citation-rebuild') + self.emit('media-rebuild') + self.emit('event-rebuild') + self.emit('repository-rebuild') + self.emit('note-rebuild') + self.emit('tag-rebuild') def copy_from_db(self, db): """ @@ -1163,6 +1259,7 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): self.delete_primary_from_reference_map(handle, transaction, txn=self.txn) self.person_map.delete(handle, txn=self.txn) + self.emit("person-delete", ([handle],)) transaction.add(PERSON_KEY, TXNDEL, handle, person.serialize(), None) def remove_source(self, handle, transaction): @@ -1262,6 +1359,7 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): txn=self.txn) old_data = data_map.get(handle, txn=self.txn) data_map.delete(handle, txn=self.txn) + self.emit(KEY_TO_NAME_MAP[key] + "-delete", ([handle],)) transaction.add(key, TXNDEL, handle, old_data, None) def delete_primary_from_reference_map(self, handle, transaction, txn=None): @@ -1328,7 +1426,12 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): pass def close(self): - pass + if self._directory: + from gramps.plugins.export.exportxml import XmlWriter + from gramps.cli.user import User + writer = XmlWriter(self, User(), strip_photos=0, compress=1) + filename = os.path.join(self._directory, "data.gramps") + writer.write(filename) def find_backlink_handles(self, handle, include_classes=None): return [] @@ -1424,7 +1527,7 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): return [] def get_repo_bookmarks(self): - return self.repository_bookmarks + return self.repo_bookmarks def get_repository_types(self): return [] @@ -1498,12 +1601,17 @@ class DictionaryDb(DbWriteBase, DbReadBase, UpdateCallback, Callback): def iter_tags(self): return (key for key in self.tag_map.values()) - def load(self, directory, pulse_progress, mode, - force_schema_upgrade, - force_bsddb_upgrade, - force_bsddb_downgrade, - force_python_upgrade): + def load(self, directory, pulse_progress=None, mode=None, + force_schema_upgrade=False, + force_bsddb_upgrade=False, + force_bsddb_downgrade=False, + force_python_upgrade=False): + from gramps.plugins.importer.importxml import importData + from gramps.cli.user import User self._directory = directory + filename = os.path.join(directory, "data.gramps") + if os.path.isfile(filename): + importData(self, filename, User()) def prepare_import(self): pass diff --git a/gramps/plugins/database/djangodb.py b/gramps/plugins/database/djangodb.py index ee6d4162d..56f40f5ac 100644 --- a/gramps/plugins/database/djangodb.py +++ b/gramps/plugins/database/djangodb.py @@ -1402,7 +1402,6 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback): if not person.gramps_id and set_gid: person.gramps_id = self.find_next_person_gramps_id() self.commit_person(person, trans) - self.emit("person-add", ([person.handle],)) return person.handle def add_family(self, family, trans, set_gid=True): @@ -1411,7 +1410,6 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback): if not family.gramps_id and set_gid: family.gramps_id = self.find_next_family_gramps_id() self.commit_family(family, trans) - self.emit("family-add", ([family.handle],)) return family.handle def add_citation(self, citation, trans, set_gid=True): @@ -1420,7 +1418,6 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback): if not citation.gramps_id and set_gid: citation.gramps_id = self.find_next_citation_gramps_id() self.commit_citation(citation, trans) - self.emit("citation-add", ([citation.handle],)) return citation.handle def add_source(self, source, trans, set_gid=True): @@ -1429,7 +1426,6 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback): if not source.gramps_id and set_gid: source.gramps_id = self.find_next_source_gramps_id() self.commit_source(source, trans) - self.emit("source-add", ([source.handle],)) return source.handle def add_repository(self, repository, trans, set_gid=True): @@ -1438,7 +1434,6 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback): if not repository.gramps_id and set_gid: repository.gramps_id = self.find_next_repository_gramps_id() self.commit_repository(repository, trans) - self.emit("repository-add", ([repository.handle],)) return repository.handle def add_note(self, note, trans, set_gid=True): @@ -1447,7 +1442,6 @@ class DbDjango(DbWriteBase, DbReadBase, UpdateCallback, Callback): if not note.gramps_id and set_gid: note.gramps_id = self.find_next_note_gramps_id() self.commit_note(note, trans) - self.emit("note-add", ([note.handle],)) return note.handle def add_place(self, place, trans, set_gid=True): diff --git a/gramps/plugins/importer/importxml.py b/gramps/plugins/importer/importxml.py index 00aa28420..39a3f04d4 100644 --- a/gramps/plugins/importer/importxml.py +++ b/gramps/plugins/importer/importxml.py @@ -57,7 +57,7 @@ from gramps.gen.lib import (Address, Attribute, AttributeType, ChildRef, SrcAttribute, SrcAttributeType, StyledText, StyledTextTag, StyledTextTagType, Surname, Tag, Url) from gramps.gen.db import DbTxn -from gramps.gen.db.write import CLASS_TO_KEY_MAP +#from gramps.gen.db.write import CLASS_TO_KEY_MAP from gramps.gen.errors import GrampsImportError from gramps.gen.utils.id import create_id from gramps.gen.utils.db import family_name