From d2f5667cb74792f1e83f5c2d8bf5a8f89b76e349 Mon Sep 17 00:00:00 2001 From: stonebig Date: Sun, 15 Aug 2021 11:38:27 +0200 Subject: [PATCH] .backup and .restore menu --- HISTORY.rst | 8 ++++++ sqlite_bro/sqlite_bro.py | 54 ++++++++++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index fa05924..53a94c4 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,14 @@ Changelog ========= +2021-08-15a : v0.12.0 'Script me more!' +--------------------------------------- + +* '.backup' and '.restore' functions are accessible via menu + +* can run in an enviromment without DISPLAY + + 2021-08-09b : v0.11.1 'Script me more!' --------------------------------------- diff --git a/sqlite_bro/sqlite_bro.py b/sqlite_bro/sqlite_bro.py index 33ab0cf..8204e5e 100644 --- a/sqlite_bro/sqlite_bro.py +++ b/sqlite_bro/sqlite_bro.py @@ -137,6 +137,10 @@ def create_menu(self): self.menu.add_command(label="Close Database", command=self.close_db) self.menu.add_separator() self.menu.add_command(label="Attach Database", command=self.attach_db) + if sys.version_info[:2] >= (3 , 7): + self.menu.add_separator() + self.menu.add_command(label="Backup current Database", command=self.backup_db) + self.menu.add_command(label="Restore into current Database", command=self.restore_db) self.menu.add_separator() self.menu.add_command(label="Quit", command=self.quit_db) @@ -234,6 +238,43 @@ def open_db(self, filename="", isolation_level=None): self.conn = Baresql(self.database_file) self.actualize_db() + def backup_db(self, filename="", isolation_level=None): + """Backup the current database""" + if filename == "": + filename = filedialog.asksaveasfilename( + initialdir=self.initialdir, + defaultextension=".db", + title="Define a new database name and location", + filetypes=[("default", "*.db"), ("other", "*.db*"), ("all", "*.*")], + ) + if filename != "": + if os.path.isfile(filename): + self.set_initialdir(filename) + if messagebox.askyesno( + message="Confirm Destruction of previous Datas ?", + icon="question", + title="Destroying", + ): + os.remove(filename) + db_to = sqlite.connect(filename) + self.conn.conn.backup(db_to) + db_to.close() + self.actualize_db() + + def restore_db(self, filename="", isolation_level=None): + """Restore an existing database into current one""" + if filename == "": + filename = filedialog.askopenfilename( + initialdir=self.initialdir, + defaultextension=".db", + filetypes=[("default", "*.db"), ("other", "*.db*"), ("all", "*.*")], + ) + if filename != "": + db_from = sqlite.connect(filename) + db_from.backup(self.conn.conn) + db_from.close + self.actualize_db() + def load_script(self): """load a script file, ask validation of detected Python code""" filename = filedialog.askopenfilename( @@ -1013,8 +1054,7 @@ def bip(c): if (filename + "z")[0] == "~": filename = os.path.join(self.home, filename[1:]) db_from = sqlite.connect(filename) - with db_from: - db_from.backup(self.conn.conn) + db_from.backup(self.conn.conn) db_from.close self.actualize_db() if shell_list[0] == ".backup" and len(shell_list) >= 2: @@ -1022,8 +1062,7 @@ def bip(c): if (filename + "z")[0] == "~": filename = os.path.join(self.home, filename[1:]) db_to = sqlite.connect(filename) - with db_to: - self.conn.conn.backup(db_to) + self.conn.conn.backup(db_to) db_to.close() if shell_list[0] == ".shell" and len(shell_list) >= 2: os.system(instru[len(".print") + 1 :] + "\n") @@ -1983,6 +2022,7 @@ def export_writer( [i if isinstance(i, str) else i[0] for i in cursor.description] ) # PyPy as a strange list of list writer.writerows(cursor.fetchall()) + fout.close # PyPy3-7.3.5 needs that close else: # python2.7 (minimal) write_mode = "wb" if initialize else "ab" # Write or Append with io.open(csv_file, write_mode) as fout: @@ -1997,7 +2037,7 @@ def export_writer( [i if isinstance(i, str) else i[0] for i in cursor.description] ) # heading row with anti-PyPy bug writer.writerows(cursor.fetchall()) - + fout.close # PyPy3-7.3.5 needs that close def _main(): welcome_text = """-- SQLite Memo (Demo = click on green "->" and "@" icons) @@ -2056,7 +2096,7 @@ def _main(): RELEASE SAVEPOINT remember_Neo; -- free memory \n\n-- '.' commands understood: --- .backup FILE Backup DB (default "main") to FILE +-- .backup FILE Backup DB (default "main") to FILE (if Python>=3.7) -- .cd DIRECTORY Change the working directory to DIRECTORY -- .dump ?FILE? Render database content as SQL (to FILE if specified) -- .excel Display the output of next command in spreadsheet @@ -2068,7 +2108,7 @@ def _main(): -- .output ?FILE? Send output to FILE or stdout if FILE is omitted -- .print STRING... Print literal STRING -- .read FILE Read input from FILE --- .restore FILE Restore content of DB (default "main") from FILE +-- .restore FILE Restore DB (default "main") from FILE (if Python>=3.7) -- .separator COL Set column separator in next .once exports (default ,) -- .shell CMD ARGS... Run CMD ARGS... in a system shell