From 331d80a0605bf12543dd5b7df8e9cb2905ceadee Mon Sep 17 00:00:00 2001 From: "Christian T. Jacobs" Date: Sat, 15 Apr 2017 00:55:52 +0100 Subject: [PATCH] Create a new method called "rename" in the Log class. --- pyqso/log.py | 21 +++++++++++++++++++++ pyqso/logbook.py | 26 ++++++++------------------ pyqso/summary.py | 7 +++---- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/pyqso/log.py b/pyqso/log.py index 3af5ab2..004885f 100644 --- a/pyqso/log.py +++ b/pyqso/log.py @@ -241,6 +241,27 @@ class Log(Gtk.ListStore): assert(removed == len(duplicates)) return (len(duplicates), removed) + def rename(self, new_name): + """ Rename the log. + + :arg str new_name: The new name for the log. + :returns: True if the renaming process is successful. Otherwise returns False. + :rtype: bool + """ + try: + with self.connection: + # First try to alter the table name in the database. + c = self.connection.cursor() + query = "ALTER TABLE %s RENAME TO %s" % (self.name, new_name) + c.execute(query) + # If the table name change was successful, then change the name attribute of the Log object too. + self.name = new_name + success = True + except sqlite.Error as e: + logging.exception(e) + success = False + return success + def get_duplicates(self): """ Find the duplicates in the log, based on the CALL, QSO_DATE, TIME_ON, FREQ and MODE fields. diff --git a/pyqso/logbook.py b/pyqso/logbook.py index 8f88b2c..0ce52e3 100644 --- a/pyqso/logbook.py +++ b/pyqso/logbook.py @@ -493,33 +493,23 @@ class Logbook: log_index = self.get_log_index(name=old_log_name) - exists = True + success = False ln = LogNameDialog(self.application, title="Rename Log", name=old_log_name) - while(exists): + while(not success): response = ln.dialog.run() if(response == Gtk.ResponseType.OK): new_log_name = ln.name - try: - with self.connection: - c = self.connection.cursor() - query = "ALTER TABLE %s RENAME TO %s" % (old_log_name, new_log_name) - c.execute(query) - exists = False - except sqlite.Error as e: - logging.exception(e) - # Data is not valid - inform the user. + success = self.logs[log_index].rename(new_log_name) + if(success): + ln.dialog.destroy() + else: + # Unsuccessful rename attempt. Inform the user. error(parent=ln.dialog, message="Database error. Try another log name.") - exists = True else: ln.dialog.destroy() return - ln.dialog.destroy() - - # Remember to change the Log object's name... - self.logs[log_index].name = new_log_name - - # ...and the page's name + # Remember to change the page's name page.set_name(self.logs[log_index].name) # ...and update the tab's label diff --git a/pyqso/summary.py b/pyqso/summary.py index 6d54782..2b937c3 100644 --- a/pyqso/summary.py +++ b/pyqso/summary.py @@ -19,10 +19,9 @@ from gi.repository import Gtk import logging -from os.path import basename, getmtime, expanduser +from os import pardir +from os.path import basename, getmtime, expanduser, dirname, join, realpath from datetime import datetime, date -import os -import os.path try: import configparser except ImportError: @@ -52,7 +51,7 @@ class Summary(object): self.application = application self.logbook = self.application.logbook self.builder = self.application.builder - glade_file_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir, "res/pyqso.glade") + glade_file_path = join(realpath(dirname(__file__)), pardir, "res/pyqso.glade") self.builder.add_objects_from_file(glade_file_path, ("summary_page",)) self.summary_page = self.builder.get_object("summary_page")