def _finish(self): if self._validate_cur_test(): self.save_input(mark_last_run=True, mark_as_completed=True) filename, check_results = UIUtils.save_file( filters=[UIUtils.CSV_FILE_FILTER], open_now_opt=True, save_last_location=True) if filename: exporter = ReliabilityExporter(self.check, filename) if exporter.export(): if check_results: subprocess.Popen([ '%s' % DBConstants.SETTINGS.SPREADSHEET_PATH, filename ]) else: UIUtils.show_message_dialog( 'Results exported successfully.') self._exit(False) #we have already saved above else: UIUtils.show_message_dialog( 'An error occurred while exporting the results. These results are still saved in the database, and can be exported at a later time, pending the correction of this problem. Please bother the programmer until this happens.' )
def _export(self, treeview, col_headers, db): write_filename, open_now = UIUtils.save_file(filters=[UIUtils.CSV_FILE_FILTER, UIUtils.ALL_FILE_FILTER], open_now_opt=True) if write_filename: #if they didn't click cancel #lag_time_cutoff = float( UIUtils.show_entry_dialog(None, 'Lag time cutoff for counts: ', default_text='2', validate_regex=r'^-?\d+(\.\d+)?$', invalid_msg='Please enter a number.') ) lag_time_cutoff = 2.0 if write_filename and lag_time_cutoff != None: #if user did not click cancel (note: lag time cutoff could be 0) if not write_filename.lower().endswith('.csv'): write_filename += '.csv' try: csv_file = open(write_filename, 'wb') writer = csv.writer(csv_file, quoting=csv.QUOTE_ALL) cols = treeview.get_columns() visible_col_indices = filter(lambda i: cols[i].get_visible(), range(len(cols))) filtered_headers = [col_headers[i] for i in visible_col_indices] writer.writerow(filtered_headers) progress_dialog = ProgressDialog(title='Exporting to file', phases=['Exporting...']) progress_dialog.show() num_rows = len(treeview.get_model()) row_index = 1 #this is awkward, but there is no way to pull all the rows out of the model in one shot, so we have to use a non-indexed for loop and manually track the index for row in treeview.get_model(): filtered_row = [row[i] for i in visible_col_indices] writer.writerow(filtered_row) progress_dialog.set_fraction(float(row_index) / float(num_rows)) row_index += 1 # export_stats = self._get_export_stats(lag_time_cutoff, db, col_headers) # if export_stats: # for row in export_stats: # writer.writerow(row) progress_dialog.ensure_finish() csv_file.close() if open_now: subprocess.Popen(['%s' % DBConstants.SETTINGS.SPREADSHEET_PATH, write_filename]) else: UIUtils.show_message_dialog('Data exported successfully.') #UIUtils.show_message_dialog('Data export completed.') except Exception as err: UIUtils.show_message_dialog('Unable to export data - please make sure the destination file is not already open in another program.') raise err