def write_header_row(self, count_col_headers): self.csv_writer.writerow( ['Date: %s' % UIUtils.get_cur_timestamp_str()]) self.csv_writer.writerow(['File: %s' % self.trs_filename]) self.csv_writer.writerow(['']) headers = [ 'Time', 'Phrase', 'Speakers', 'Target Listeners', 'WHQ Count' ] headers.extend(count_col_headers) self.csv_writer.writerow(headers)
def export(self, progress_update_fcn=None, progress_next_phase_fcn=None): #create csv file export_file = open(self.export_filename, 'wb') #write header info csv_writer = csv.writer(export_file, quoting=csv.QUOTE_ALL) #use Python csv library csv_writer.writerow( ['Export Date: %s' % (UIUtils.get_cur_timestamp_str())]) csv_writer.writerow( ['Configuration Creation Date: %s' % (self.config.created)]) csv_writer.writerow(['TRS Filename: %s' % (self.trs_filename)]) csv_writer.writerow(['Output Configuration:']) csv_writer.writerow(['Name: %s' % (self.config.name)]) csv_writer.writerow(['Description: %s' % (self.config.desc)]) csv_writer.writerow(['']) csv_writer.writerow(['Outputs:']) csv_writer.writerow(['']) #parse the trs file trs_parser = TRSParser(self.trs_filename) segs = trs_parser.parse(progress_update_fcn, progress_next_phase_fcn, validate=False) chains = None #this is populated on demand, then cached summary_row = [os.path.basename(self.trs_filename)[:-4]] summary_head = ["TRS file"] #iterate through all outputs in the configuration, adding segments/chains to each one, then writing the output to the spreadsheet file i = 0 while i < len(self.config.outputs): #update progress bar text if progress_next_phase_fcn: progress_next_phase_fcn() cur_output = self.config.outputs[i] cur_output.reset() #clear any cached utterances from previous runs #if we need chains, parse them from the segment list if cur_output.chained and not chains: chains = FilterManager.get_chains(segs) #add chains/segments to the current output items = chains if cur_output.chained else segs j = 0 while j < len(items): cur_output.add_item( items[j], filter_utters=True ) #note: filter_utters only affects segs (not chains) j += 1 #note: updating progress individually within the above loop (for every iteration of j) slows down the processing considerably (by a factor of ~4) - a compromise is to just set each phase to 100% after it completes. if progress_update_fcn: progress_update_fcn(1) #grab the output's results and write them to the file cur_output.write_csv_rows(csv_writer) # get summary from output summary_head += [cur_output.name] summary_row += [cur_output.get_summary()] csv_writer.writerow(['']) i += 1 export_file.close() if len(self.summary_filename) > 0: need_head = False # check the existence of file, decide the header if not os.path.isfile(self.summary_filename): need_head = True with open(self.summary_filename, 'at') as fp: summary_writer = csv.writer(fp, quoting=csv.QUOTE_ALL) if need_head: summary_writer.writerow(summary_head) summary_writer.writerow(summary_row)