def write_tasks(self, outfname=None, outftype=None): """ Writes out the current list of tasks and task groupings from memory to a file. TODO: Update docstring. """ if DEBUG: print("Tasks:") print("------") for task in self.tasks: pprint(task) print("") if outfname is None: outfname = self.config['TASKS_FNAME_OUT'] outftype = self.config['TASKS_FTYPE_OUT'] if outftype == FTYPE_CSV: # FIXME: May have been broken when groups were added. import csv with open(outfname, newline='') as outfile: taskwriter = csv.writer(outfile) for task in self.tasks: taskwriter.writerow(task) for group in self.groups: taskwriter.writerow(group) elif outftype == FTYPE_XML: from backend.xml import XmlBackend mode = 'r+b' if self._xml_header_written else 'wb' with open_backed_up(outfname, mode, suffix=self.config['BACKUP_SUFFIX']) \ as outfile: if self._xml_header_written: # Skip before the last line (assumed to read # "</wyrdinData>"). outfile.seek(-len(b'</wyrdinData>\n'), 2) else: outfile.seek(0, 2) XmlBackend.write_tasks(self.tasks, self.groups, outfile=outfile, standalone=not self._xml_header_written) if self._xml_header_written: outfile.write(b'</wyrdinData>\n') self._xml_header_written = True elif outftype == FTYPE_PICKLE: import pickle with open_backed_up(outfname, 'wb', suffix=self.config['BACKUP_SUFFIX']) \ as outfile: for task in self.tasks: pickle.dump(task, outfile) for group in self.groups: pickle.dump(group, outfile) else: raise NotImplementedError("Session.write_tasks() is not " "implemented for this type of files.")