From ff025a0ce09e29f4e54ac5c2d6f92ed0327784f7 Mon Sep 17 00:00:00 2001 From: Christian Jacobs Date: Sat, 30 Mar 2013 02:03:30 +0000 Subject: [PATCH] Pass the data type as an argument, rather than passing in the whole log to the validation method. --- src/logbook.py | 4 ++-- src/record_dialog.py | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/logbook.py b/src/logbook.py index 68ad686..1792394 100644 --- a/src/logbook.py +++ b/src/logbook.py @@ -183,7 +183,7 @@ class Logbook(Gtk.Notebook): for i in range(0, len(field_names)): #TODO: Validate user input! fields_and_data[field_names[i]] = dialog.get_data(field_names[i]) - if(not(dialog.is_valid(log, field_names[i], fields_and_data[field_names[i]]))): + if(not(dialog.is_valid(field_names[i], fields_and_data[field_names[i]], log.SELECTED_FIELD_NAMES_TYPES[field_names[i]]))): # Data is not valid - inform the user. message = Gtk.MessageDialog(parent, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, @@ -264,7 +264,7 @@ class Logbook(Gtk.Notebook): for i in range(0, len(field_names)): #TODO: Validate user input! fields_and_data[field_names[i]] = dialog.get_data(field_names[i]) - if(not(dialog.is_valid(self.logs[current], field_names[i], fields_and_data[field_names[i]]))): + if(not(dialog.is_valid(field_names[i], fields_and_data[field_names[i]], self.logs[current].SELECTED_FIELD_NAMES_TYPES[field_names[i]]))): # Data is not valid - inform the user. message = Gtk.MessageDialog(parent, Gtk.DialogFlags.DESTROY_WITH_PARENT, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, diff --git a/src/record_dialog.py b/src/record_dialog.py index eec08b0..a928dcb 100644 --- a/src/record_dialog.py +++ b/src/record_dialog.py @@ -146,15 +146,15 @@ class RecordDialog(Gtk.Dialog): else: return self.sources[field_name].get_text() - def is_valid(self, log, field_name, data): + def is_valid(self, field_name, data, data_type): + ''' Validate the fields with respect to the ADIF specification ''' # Allow an empty string, in case the user doesn't want # to fill in this field. if(data == ""): return True - # Validate the fields with respect to the ADIF specification. - if(log.SELECTED_FIELD_NAMES_TYPES[field_name] == "N"): + if(data_type == "N"): # Allow a decimal point before and/or after any numbers, # but don't allow a decimal point on its own. m = re.match("-?(([0-9]+\.?[0-9]*)|([0-9]*\.?[0-9]+))", data) @@ -166,7 +166,7 @@ class RecordDialog(Gtk.Dialog): # otherwise there may be an invalid character after the match. return (m.group(0) == data) - elif(log.SELECTED_FIELD_NAMES_TYPES[field_name] == "B"): + elif(data_type == "B"): # Boolean m = re.match("(Y|N)", data) if(m is None): @@ -174,7 +174,7 @@ class RecordDialog(Gtk.Dialog): else: return (m.group(0) == data) - elif(log.SELECTED_FIELD_NAMES_TYPES[field_name] == "D"): + elif(data_type == "D"): # Date pattern = re.compile("([0-9]){4}") m_year = pattern.match(data, 0) @@ -199,7 +199,7 @@ class RecordDialog(Gtk.Dialog): # otherwise there may be an invalid character after the match. return (len(data) == 8) - elif(log.SELECTED_FIELD_NAMES_TYPES[field_name] == "T"): + elif(data_type == "T"): # Time pattern = re.compile("([0-9]){2}") m_hour = pattern.match(data, 0) @@ -226,7 +226,7 @@ class RecordDialog(Gtk.Dialog): # otherwise there may be an invalid character after the match. return (len(data) == 6) # HHMMSS format - elif(log.SELECTED_FIELD_NAMES_TYPES[field_name] == "E"): + elif(data_type == "E"): # Enumeration. # We'll assume that this data is valid already, # since the user can only select from a pre-defined (valid) list.