Exemplo n.º 1
0
    def read_tasks(self, infname=None, inftype=None):
        """
        Reads in tasks from files listing the user's tasks. Which files these
        are, can be found in `self.config'.

        TODO: Update docstring.

        """
        if infname is None:
            infname = self.config['TASKS_FNAME_IN']
            inftype = self.config['TASKS_FTYPE_IN']
        # If no tasks have been written yet, don't load any.
        if not os.path.exists(infname):
            return
        # This is a primitive implementation for the backend as a CSV.
        if inftype == FTYPE_CSV:
            import csv
            # Read the tasks from the file to the memory.
            with open(infname, newline='') as infile:
                taskreader = csv.reader(infile)
                self.tasks = [task for task in taskreader]
        elif inftype == FTYPE_XML:
            from backend.xml import XmlBackend
            with open(infname, 'rb') as infile:
                self.tasks = XmlBackend.read_tasks(infile)
        elif inftype == FTYPE_PICKLE:
            import pickle
            if not os.path.exists(infname):
                open(infname, 'wb').close()
            with open(infname, 'rb') as infile:
                self.tasks = []
                while True:
                    try:
                        task = pickle.load(infile)
                        self.tasks.append(task)
                    except EOFError:
                        break
        else:
            raise NotImplementedError("Session.read_tasks() is not "
                                      "implemented for this type of files.")