More error handling.

pull/17/head
Christian Jacobs 2013-09-14 21:33:01 +01:00
rodzic f3232ee5b2
commit 45aee6bf74
3 zmienionych plików z 30 dodań i 14 usunięć

Wyświetl plik

@ -31,7 +31,7 @@ class DXCluster(Gtk.VBox):
""" A tool for connecting to a DX cluster (specifically Telnet-based DX clusters). """
def __init__(self, parent):
""" Also sets up a timer so that PyQSO can retrieve new data from the Telnet server every few seconds. """
""" Set up the DX cluster's Gtk.VBox, and set up a timer so that PyQSO can retrieve new data from the Telnet server every few seconds. """
Gtk.VBox.__init__(self, spacing=2)
@ -91,7 +91,7 @@ class DXCluster(Gtk.VBox):
return
def telnet_connect(self, widget=None):
""" Connects to a user-specified Telnet server, with the host and login details specified in the Gtk.Entry boxes in the TelnetConnectionDialog. """
""" Connect to a user-specified Telnet server, with the host and login details specified in the Gtk.Entry boxes in the TelnetConnectionDialog. """
dialog = TelnetConnectionDialog(self.parent)
response = dialog.run()
if(response == Gtk.ResponseType.OK):
@ -132,7 +132,7 @@ class DXCluster(Gtk.VBox):
return
def telnet_disconnect(self, widget=None):
""" Disconnects from a Telnet server. """
""" Disconnect from a Telnet server. """
if(self.connection):
self.connection.close()
self.buffer.set_text("")
@ -141,14 +141,14 @@ class DXCluster(Gtk.VBox):
return
def telnet_send_command(self, widget=None):
""" Sends the user-specified command in the Gtk.Entry box to the Telnet server (if PyQSO is connected to one). """
""" Send the user-specified command in the Gtk.Entry box to the Telnet server (if PyQSO is connected to one). """
if(self.connection):
self.connection.write(self.command.get_text() + "\n")
self.command.set_text("")
return
def on_telnet_io(self):
""" Retrieves any new data from the Telnet server and prints it out in the Gtk.TextView widget. Always returns True to satisfy the GObject timer. """
""" Retrieve any new data from the Telnet server and print it out in the Gtk.TextView widget. Always returns True to satisfy the GObject timer. """
if(self.connection):
text = self.connection.read_very_eager()
#text = text.replace(u"\u0007", "") # Remove the BEL Unicode character from the end of the line
@ -169,12 +169,12 @@ class DXCluster(Gtk.VBox):
return True
def on_delete(self, widget, event):
""" Removes the I/O timer and ends the connection with the Telnet server. """
""" Remove the I/O timer and end the connection with the Telnet server. """
self.telnet_disconnect()
GObject.source_remove(self.check_io_event)
def set_connect_button_sensitive(self, sensitive):
""" Enables/disables the relevant buttons for connecting/disconnecting from a DX cluster, so that users cannot click the connect button if PyQSO is already connected. """
""" Enable/disable the relevant buttons for connecting/disconnecting from a DX cluster, so that users cannot click the connect button if PyQSO is already connected. """
self.buttons["CONNECT"].set_sensitive(sensitive)
self.buttons["DISCONNECT"].set_sensitive(not sensitive)
self.send.set_sensitive(not sensitive)

Wyświetl plik

@ -76,5 +76,5 @@ class GreyLine(Gtk.VBox):
return True
else:
return True # Don't try to re-draw the canvas if the necessary modules to do so could not be imported.
return False # Don't try to re-draw the canvas if the necessary modules to do so could not be imported.

Wyświetl plik

@ -333,9 +333,13 @@ class Logbook(Gtk.Notebook):
response = question(parent=self.parent, message="Are you sure you want to delete log %s?" % log.name)
if(response == Gtk.ResponseType.YES):
with self.connection:
try:
c = self.connection.cursor()
c.execute("DROP TABLE %s" % log.name)
except sqlite.Error as e:
logging.exception(e)
error(parent=self.parent, message="Database error. Could not delete the log.")
return
self.logs.pop(log_index)
# Remove the log from the renderers too
@ -558,6 +562,10 @@ class Logbook(Gtk.Notebook):
response = question(parent=self.parent, message="Are you sure you want to import into an existing log?")
if(response == Gtk.ResponseType.YES):
break
elif(self.log_name_exists(log_name) is None):
# Could not determine if the log name exists. It's safer to stop here than to try to add a new log.
dialog.destroy()
return
else:
# Create a new log with the name the user supplies
exists = False
@ -583,9 +591,8 @@ class Logbook(Gtk.Notebook):
adif = ADIF()
records = adif.read(path)
print "Importing records..."
logging.debug("Importing records from the ADIF file with path: %s" % path)
for record in records:
print record
l.add_record(record)
l.populate()
@ -826,7 +833,7 @@ class Logbook(Gtk.Notebook):
duplicates = []
# Find the duplicates in the log, based on the CALL, QSO_DATE, TIME_ON, FREQ and MODE fields.
with self.connection:
try:
c = self.connection.cursor()
c.execute(
"""SELECT rowid FROM repeater_contacts WHERE rowid NOT IN
@ -836,6 +843,10 @@ SELECT MIN(rowid) FROM repeater_contacts GROUP BY call, qso_date, time_on, freq,
result = c.fetchall()
for rowid in result:
duplicates.append(rowid[0]) # Get the integers from inside the tuples.
except sqlite.Error as e:
logging.exception(e)
error(parent=self.parent, message="Database error.")
return
removed = 0 # Count the number of records that are removed. Hopefully this will be the same as len(duplicates).
path = Gtk.TreePath(0) # Start with the first row in the log.
@ -857,13 +868,18 @@ SELECT MIN(rowid) FROM repeater_contacts GROUP BY call, qso_date, time_on, freq,
def log_name_exists(self, table_name):
""" Return True if the log name already exists in the logbook, and False otherwise. """
with self.connection:
try:
c = self.connection.cursor()
c.execute("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name=?)", [table_name])
exists = c.fetchone()
if(exists[0] == 1):
return True
return False
else:
return False
except sqlite.Error as e:
logging.exception(e)
error(parent=self.parent, message="Database error. Could not check if the log name exists.")
return None
def _get_log_index(self, name=None):
""" Given the name of a log, return its index in the self.log list. """