def __repair_db(self, obj): """ Start the repair process by calling the start_editing option on the line with the cursor. """ store, node = self.selection.get_selected() dirname = store[node][1] #First ask user if he is really sure :-) yes_no = QuestionDialog2( _("Repair family tree?"), _("If you click <b>Proceed</b>, Gramps will attempt to recover your family tree" " from the last good backup. There are several ways this can cause unwanted" " effects, so <b>backup</b> the family tree first.\n" "The Family tree you have selected is stored in %s.\n\n" "Before doing a repair, verify that the Family Tree can really no longer be " " opened, as the database back-end can recover from some errors automatically.\n\n" "<b>Details:</b> Repairing a Family Tree actually uses the last backup of" " the Family Tree, which Gramps stored on last use. If you have worked for" " several hours/days without closing Gramps, then all this information will" " be lost! If the repair fails, then the original family tree will be lost" " forever, hence a backup is needed. If the repair fails, or too much" " information is lost, you can fix the original family tree manually." " For details, see the webpage\n" "http://gramps-project.org/wiki/index.php?title=Recover_corrupted_family_tree\n" "Before doing a repair, try to open the family tree in the normal manner." " Several errors that trigger the repair button can be fixed automatically." " If this is the case, you can disable the repair button by removing the" " file <i>need_recover</i> in the family tree directory.") % dirname, _("Proceed, I have taken a backup"), _("Stop")) prompt = yes_no.run() if not prompt: return opened = store[node][OPEN_COL] if opened: self.dbstate.no_database() # delete files that are not backup files or the .txt file for filename in os.listdir(dirname): if os.path.splitext(filename)[1] not in (".gbkp", ".txt"): fname = os.path.join(dirname, filename) os.unlink(fname) newdb = DbBsddb() newdb.write_version(dirname) dbclass = DbBsddb dbase = dbclass() dbase.set_save_path(dirname) dbase.load(dirname, None) self.__start_cursor(_("Rebuilding database from backup files")) try: restore(dbase) except DbException, msg: DbManager.ERROR(_("Error restoring backup data"), msg)
def setUp(self): def dummy_callback(dummy): pass self._tmpdir = tempfile.mkdtemp() #self._filename = os.path.join(self._tmpdir,'test.grdb') self._db = DbBsddb() dbman = CLIDbManager(None) self._filename, title = dbman.create_new_db_cli(title="Test") self._db.load(self._filename, dummy_callback, "w")
class GrampsDbBaseTest(unittest.TestCase): """Base class for unittest that need to be able to create test databases.""" def setUp(self): def dummy_callback(dummy): pass self._tmpdir = tempfile.mkdtemp() #self._filename = os.path.join(self._tmpdir,'test.grdb') self._db = DbBsddb() dbman = CLIDbManager(None) self._filename, title = dbman.create_new_db_cli(title="Test") self._db.load(self._filename, dummy_callback, "w") def tearDown(self): self._db.close() shutil.rmtree(self._tmpdir) def _populate_database(self, num_sources=1, num_persons=0, num_families=0, num_events=0, num_places=0, num_media_objects=0, num_links=1): # start with sources sources = [] for i in xrange(0, num_sources): sources.append(self._add_source()) # now for each of the other tables. Give each entry a link # to num_link sources, sources are chosen on a round robin # basis for num, add_func in ((num_persons, self._add_person_with_sources), (num_families, self._add_family_with_sources), (num_events, self._add_event_with_sources), (num_places, self._add_place_with_sources), (num_media_objects, self._add_media_object_with_sources)): source_idx = 1 for person_idx in xrange(0, num): # Get the list of sources to link lnk_sources = set() for i in xrange(0, num_links): lnk_sources.add(sources[source_idx - 1]) source_idx = (source_idx + 1) % len(sources) try: add_func(lnk_sources) except: print "person_idx = ", person_idx print "lnk_sources = ", repr(lnk_sources) raise return def _add_source(self, repos=None): # Add a Source tran = self._db.transaction_begin() source = gen.lib.Source() if repos is not None: repo_ref = gen.lib.RepoRef() repo_ref.set_reference_handle(repos.get_handle()) source.add_repo_reference(repo_ref) self._db.add_source(source, tran) self._db.commit_source(source, tran) self._db.transaction_commit(tran, "Add Source") return source def _add_repository(self): # Add a Repository tran = self._db.transaction_begin() repos = gen.lib.Repository() self._db.add_repository(repos, tran) self._db.commit_repository(repos, tran) self._db.transaction_commit(tran, "Add Repository") return repos def _add_object_with_source(self, sources, object_class, add_method, commit_method): object = object_class() for source in sources: src_ref = gen.lib.SourceRef() src_ref.set_reference_handle(source.get_handle()) object.add_source_reference(src_ref) tran = self._db.transaction_begin() add_method(object, tran) commit_method(object, tran) self._db.transaction_commit(tran, "Add Object") return object def _add_person_with_sources(self, sources): return self._add_object_with_source(sources, gen.lib.Person, self._db.add_person, self._db.commit_person) def _add_family_with_sources(self, sources): return self._add_object_with_source(sources, gen.lib.Family, self._db.add_family, self._db.commit_family) def _add_event_with_sources(self, sources): return self._add_object_with_source(sources, gen.lib.Event, self._db.add_event, self._db.commit_event) def _add_place_with_sources(self, sources): return self._add_object_with_source(sources, gen.lib.Place, self._db.add_place, self._db.commit_place) def _add_media_object_with_sources(self, sources): return self._add_object_with_source(sources, gen.lib.MediaObject, self._db.add_object, self._db.commit_media_object)
class GrampsDbBaseTest(unittest.TestCase): """Base class for unittest that need to be able to create test databases.""" def setUp(self): def dummy_callback(dummy): pass self._tmpdir = tempfile.mkdtemp() #self._filename = os.path.join(self._tmpdir,'test.grdb') self._db = DbBsddb() dbman = CLIDbManager(None) self._filename, title = dbman.create_new_db_cli(title="Test") self._db.load(self._filename, dummy_callback, "w") def tearDown(self): self._db.close() shutil.rmtree(self._tmpdir) def _populate_database(self, num_sources = 1, num_persons = 0, num_families = 0, num_events = 0, num_places = 0, num_media_objects = 0, num_links = 1): # start with sources sources = [] for i in xrange(0, num_sources): sources.append(self._add_source()) # now for each of the other tables. Give each entry a link # to num_link sources, sources are chosen on a round robin # basis for num, add_func in ((num_persons, self._add_person_with_sources), (num_families, self._add_family_with_sources), (num_events, self._add_event_with_sources), (num_places, self._add_place_with_sources), (num_media_objects, self._add_media_object_with_sources)): source_idx = 1 for person_idx in xrange(0, num): # Get the list of sources to link lnk_sources = set() for i in xrange(0, num_links): lnk_sources.add(sources[source_idx-1]) source_idx = (source_idx+1) % len(sources) try: add_func(lnk_sources) except: print "person_idx = ", person_idx print "lnk_sources = ", repr(lnk_sources) raise return def _add_source(self,repos=None): # Add a Source tran = self._db.transaction_begin() source = gen.lib.Source() if repos is not None: repo_ref = gen.lib.RepoRef() repo_ref.set_reference_handle(repos.get_handle()) source.add_repo_reference(repo_ref) self._db.add_source(source,tran) self._db.commit_source(source,tran) self._db.transaction_commit(tran, "Add Source") return source def _add_repository(self): # Add a Repository tran = self._db.transaction_begin() repos = gen.lib.Repository() self._db.add_repository(repos,tran) self._db.commit_repository(repos,tran) self._db.transaction_commit(tran, "Add Repository") return repos def _add_object_with_source(self,sources, object_class,add_method,commit_method): object = object_class() for source in sources: src_ref = gen.lib.SourceRef() src_ref.set_reference_handle(source.get_handle()) object.add_source_reference(src_ref) tran = self._db.transaction_begin() add_method(object,tran) commit_method(object,tran) self._db.transaction_commit(tran, "Add Object") return object def _add_person_with_sources(self,sources): return self._add_object_with_source(sources, gen.lib.Person, self._db.add_person, self._db.commit_person) def _add_family_with_sources(self,sources): return self._add_object_with_source(sources, gen.lib.Family, self._db.add_family, self._db.commit_family) def _add_event_with_sources(self,sources): return self._add_object_with_source(sources, gen.lib.Event, self._db.add_event, self._db.commit_event) def _add_place_with_sources(self,sources): return self._add_object_with_source(sources, gen.lib.Place, self._db.add_place, self._db.commit_place) def _add_media_object_with_sources(self,sources): return self._add_object_with_source(sources, gen.lib.MediaObject, self._db.add_object, self._db.commit_media_object)