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.")
def write_all(self, tasks_ftype=None, tasks_fname=None, log_ftype=None, log_fname=None): """Writes out projects, tasks, task groupings, and working slots to files as dictated by configuration settings. """ self.write_projects() if tasks_ftype is None: tasks_ftype = self.config['TASKS_FTYPE_OUT'] if log_ftype is None: log_ftype = self.config['LOG_FTYPE_OUT'] if tasks_fname is None: tasks_fname = self.config['TASKS_FNAME_OUT'] if log_fname is None: log_fname = self.config['LOG_FNAME_OUT'] # The only special case so far. if (tasks_ftype == FTYPE_XML and log_ftype == FTYPE_XML and tasks_fname == log_fname): from backend.xml import XmlBackend # TODO: Use the context manager at other places too. with open_backed_up(tasks_fname, 'wb', suffix=self.config['BACKUP_SUFFIX']) \ as outfile: XmlBackend.write_all(self.tasks, self.groups, self.wslots, outfile) else: # FIXME: The type of file is not looked at, unless the file name is # supplied too. Provide some default filename for the supported # file types. self.write_log(outftype=log_ftype, outfname=log_fname) self.write_tasks(outftype=tasks_ftype, outfname=tasks_fname)
def write_log(self, outfname=None, outftype=None): """TODO: Update docstring.""" if outfname is None: outfname = self.config['LOG_FNAME_OUT'] outftype = self.config['LOG_FTYPE_OUT'] if outftype == FTYPE_PICKLE: import pickle with open(outfname, 'wb') as outfile: for wtime in self.wslots: pickle.dump(wtime, outfile) elif outftype == FTYPE_XML: from backend.xml import XmlBackend # XXX This assumes that `write_log' was called soon after # `write_tasks'. 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) XmlBackend.write_workslots(self.wslots, outfile, not self._xml_header_written) if self._xml_header_written: outfile.write(b'</wyrdinData>\n') self._xml_header_written = True else: raise NotImplementedError("Session.write_log() is not " "implemented for this type of files.")
def write_projects(self, outfname=None): """Writes the list of projects into a file. In the current implementation, simply writes out an alphabetically sorted list of all known projects to the file. """ if DEBUG: print("Projects:") print("---------") for project in self.projects: pprint(project) print("") if outfname is None: outfname = self.config['PROJECTS_FNAME'] with open_backed_up(outfname, 'w', suffix=self.config['BACKUP_SUFFIX']) as outfile: for project in self.projects: outfile.write(project + '\n')