diff --git a/pyqso/adif.py b/pyqso/adif.py index d242f0e..0edcdbf 100644 --- a/pyqso/adif.py +++ b/pyqso/adif.py @@ -202,7 +202,7 @@ class ADIF: """ Read an ADIF file and parse it. :arg str path: The path to the ADIF file to read. - :returns: A list of dictionaries (one dictionary per QSO), with each dictionary containing field-value pairs, e.g. {FREQ:145.500, BAND:2M, MODE:FM}. + :returns: A list of dictionaries (one dictionary per QSO), with each dictionary containing field-value pairs, e.g. {FREQ:145.500, BAND:2M, MODE:FM}. If the file cannot be read, the method returns None. :rtype: list :raises IOError: if the ADIF file does not exist or cannot be read (e.g. due to lack of read permissions). """ @@ -215,9 +215,11 @@ class ADIF: f.close() # Close the file, otherwise "bad things" might happen! except IOError as e: logging.error("I/O error %d: %s" % (e.errno, e.strerror)) + return None except Exception as e: logging.error("An error occurred when reading the ADIF file.") logging.exception(e) + return None records = self.parse_adi(text) @@ -231,7 +233,7 @@ class ADIF: """ Parse some raw text (defined in the 'text' argument) for ADIF field data. :arg str text: The raw text from the ADIF file to parse. - :returns: A list of dictionaries (one dictionary per QSO). Each dictionary contains the field-value pairs, e.g. {FREQ:145.500, BAND:2M, MODE:FM}. + :returns: A list of dictionaries (one dictionary per QSO). Each dictionary contains the field-value pairs, e.g. {"FREQ": "145.500", "BAND": "2M", "MODE": "FM"}. :rtype: list """ @@ -331,11 +333,14 @@ class ADIF: :arg list records: The list of QSO records to write. :arg str path: The desired path of the ADIF file to write to. - :returns: None + :returns: True if the write process was successful, otherwise False. + :rtype: bool :raises IOError: if the ADIF file cannot be written (e.g. due to lack of write permissions). """ logging.debug("Writing records to an ADIF file...") + + success = False try: f = open(path, mode='w', errors="replace") # Open file for writing @@ -364,13 +369,14 @@ class ADIF: logging.debug("Finished writing records to the ADIF file.") f.close() logging.info("Wrote %d QSOs to %s in ADIF format." % (len(records), path)) + success = True except IOError as e: logging.error("I/O error %d: %s" % (e.errno, e.strerror)) except Exception as e: # All other exceptions. logging.error("An error occurred when writing the ADIF file.") logging.exception(e) - return + return success def is_valid(self, field_name, data, data_type): """ Validate the data in a field with respect to the ADIF specification. diff --git a/pyqso/cabrillo.py b/pyqso/cabrillo.py index 150f540..1ddad7c 100644 --- a/pyqso/cabrillo.py +++ b/pyqso/cabrillo.py @@ -40,10 +40,13 @@ class Cabrillo: :arg str path: The desired path of the Cabrillo file to write to. :arg str contest: The name of the contest. :arg str mycall: The callsign used during the contest. - :returns: None + :returns: True if the write process was successful, otherwise False. + :rtype: bool :raises IOError: if the Cabrillo file cannot be written (e.g. due to lack of write permissions).""" logging.debug("Writing records to a Cabrillo file...") + + success = False try: f = open(path, mode='w', errors="replace") # Open file for writing @@ -102,11 +105,13 @@ class Cabrillo: f.close() + logging.info("Wrote %d QSOs to %s in Cabrillo format." % (len(records), path)) + success = True + except IOError as e: logging.error("I/O error %d: %s" % (e.errno, e.strerror)) except Exception as e: # All other exceptions. logging.error("An error occurred when writing the Cabrillo file.") logging.exception(e) - logging.info("Wrote %d QSOs to %s in Cabrillo format." % (len(records), path)) - return + return success diff --git a/pyqso/logbook.py b/pyqso/logbook.py index 399295c..4192e87 100644 --- a/pyqso/logbook.py +++ b/pyqso/logbook.py @@ -599,19 +599,24 @@ class Logbook: ln.dialog.destroy() adif = ADIF() + records = adif.read(path) - l.add_record(records) - l.populate() - if(not exists): - self.logs.append(l) - self.render_log(self.log_count-1) + if(records is None): + error(parent=self.application.window, message="Could not import the log.") + else: + l.add_record(records) + l.populate() - # Update statistics, etc. - self.summary.update() - self.application.toolbox.awards.count(self) + if(not exists): + self.logs.append(l) + self.render_log(self.log_count-1) - info(parent=self.application.window, message="Imported %d QSOs into log '%s'." % (len(records), l.name)) + # Update statistics, etc. + self.summary.update() + self.application.toolbox.awards.count(self) + + info(parent=self.application.window, message="Imported %d QSOs into log '%s'." % (len(records), l.name)) return @@ -656,10 +661,10 @@ class Logbook: adif = ADIF() records = log.records if(records is not None): - try: - adif.write(records, path) + success = adif.write(records, path) + if(success): info(parent=self.application.window, message="Exported %d QSOs to %s in ADIF format." % (len(records), path)) - except: + else: error(parent=self.application.window, message="Could not export the records.") else: error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been exported.") @@ -717,10 +722,10 @@ class Logbook: cabrillo = Cabrillo() records = log.records if(records is not None): - try: - cabrillo.write(records, path, contest=contest, mycall=mycall) + success = cabrillo.write(records, path, contest=contest, mycall=mycall) + if(success): info(parent=self.application.window, message="Exported %d QSOs to %s in Cabrillo format." % (len(records), path)) - except: + else: error(parent=self.application.window, message="Could not export the records.") else: error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been exported.")