Beispiel #1
0
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.intro = """
  yaGTD is a free software available under the terms of the GNU GPL.
  Refer to the file COPYING (which should be included in this distribution)
  for the specific terms of this licence.
  """
        self.prompt = "GTD> "
        self.todo = ToDo()  # the to-do list is here!

        self.todotxt = TODO_TXT

        self.colorize = False
Beispiel #2
0
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.intro= """
  yaGTD is a free software available under the terms of the GNU GPL.
  Refer to the file COPYING (which should be included in this distribution)
  for the specific terms of this licence.
  """
        self.prompt = "GTD> "
        self.todo = ToDo()  # the to-do list is here!

        self.todotxt = TODO_TXT
        self.donetxt = DONE_TXT

        self.colorize = False
        self.tasks_selected = []
Beispiel #3
0
class GTD(cmd.Cmd):
    """GTD(cmd.Cmd) class."""

    def __init__(self):
        cmd.Cmd.__init__(self)
        self.intro= """
  yaGTD is a free software available under the terms of the GNU GPL.
  Refer to the file COPYING (which should be included in this distribution)
  for the specific terms of this licence.
  """
        self.prompt = "GTD> "
        self.todo = ToDo()  # the to-do list is here!

        self.todotxt = TODO_TXT
        self.donetxt = DONE_TXT

        self.colorize = False
        self.tasks_selected = []

    #
    # Private functions.
    #

    def _parse_args(self, args):
        """Parse command arguments= num + str."""

        num = string = None

        # Parse command args
        n = re.search(r"^(?x)(\d+)", args)  # num
        s = re.search(r" +([@!:\+\-\w ]+)$", args)  # string

        if n: num = int(n.group(1))
        if s: string = s.group(1)

        #print "parsed", num, string
        return (num, string)

    def _parse_batch(self, args):  # BB
        """Return search criterion and elemenst to add/change."""

        regexp = action = ''
        try:
            regexp, action = args.split(' ', 1)
        except:
            pass
        return regexp, action

    def _colorize(self, string):
        """Colorize the given 'string'."""

        s = string

        if self.colorize:
            # Scan to colorize attributes (accordingly to global settings) ...
            for attr in Task.attributes_list[3:]:
                color_fct = lambda m: eval(attr.upper() + '_COLOR') + m.group(0) + DEFAULT_COLOR
                s = eval(attr.upper() + '_REGEXP').sub(color_fct, s)

        return s

    def _parse_line(self, line):
        """Return a dictionary (task mapping) from 'line' parsing."""

        t = {}  # Task mapping
        title = line  # the 'title' extracted from line

        # Parse for GTD attributes
        for attr in ['context', 'project', 'status', 'reference']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                t[attr] = matches

        # Parse additional properties
        for attr in ['urgency', 'importance', 'complete']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                t[attr] = int(matches[-1])  # keep only last!

        # Parse timedelta
        for attr in ['time', 'recurrence']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                match = matches[-1]  # keep only last!
                hours = minutes = 0  # compute hours

                if attr == 'time':  # compute time requiered (in working hours)
                    if match[1].upper() == 'W':  # weeks
                        hours = int(match[0]) * gtd.WEEK_IN_HOURS
                    elif match[1].upper() == 'D':  # days
                        hours = int(match[0]) * gtd.DAY_IN_HOURS
                    elif match[1].upper() == 'H':  # hours
                        hours = int(match[0])
                    elif match[1].upper() == 'M':  # minutes
                        minutes = int(match[0])
                    else:
                        pass  # invalid time range indicator

                elif attr == 'recurrence':  # compute full hours
                    if match[1].upper() == 'W':  # weeks
                        hours = int(match[0]) * 7 * 24
                    elif match[1].upper() == 'D':  # days
                        hours = int(match[0]) * 24
                    elif match[1].upper() == 'H':  # hours
                        hours = int(match[0])
                    elif match[1].upper() == 'M':  # minutes
                        minutes = int(match[0])
                    else:
                        pass  # invalid time range indicator

                t[attr] = datetime.timedelta(hours= hours, minutes=minutes)

        # Parse dates
        for attr in ['start', 'due', 'end']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                year, month, day = matches[-1].split('-')  # keep only last!
                t[attr] = datetime.datetime(int(year), int(month), int(day))

            else:  # check if it is in format of special day
                title = eval(attr.upper() + '_DOW_REGEXP').sub('', title)

                matches = eval(attr.upper() + '_DOW_REGEXP').findall(line)
                if matches:
                    today     = datetime.datetime.today()
                    today_dow = today.weekday()  # monday is first
                    given_dow = DOW.index(matches[-1])

                    day_diff = given_dow - today_dow
                    if day_diff < 0:
                        day_diff += 7;

                    day = today + datetime.timedelta (int (day_diff))
                    t[attr] = day

        # Post-processing
        if t.has_key('end') or t.has_key('reference'):  # ignore completed and archived tasks
            t['complete'] = 100

        # if due < start then correct due by setting it to start
        if t.has_key('due') and t.has_key('start'):
            if t['due'] < t['start']:
                t['due'] = t['start']

        # Set the title
        t['title'] = " ".join(title.split())  # remove useless blank chars too

        return t

    def _dump_line(self, task):
        """Return a formatted line from the given 'task'."""

        s = task['title']  # init the line with the title

        # Dump GTD attributes
        for attr in ['context', 'project', 'status', 'reference']:
            if task.has_key(attr):
                for value in task[attr]:
                    s += " " + eval(attr.upper() + '_CHAR') + value

        # Dump additional properties
        for attr in ['urgency', 'importance', 'complete']:
            if task.has_key(attr) and task[attr]:
                s += " " + eval(attr.upper() + '_CHAR') + str(task[attr])

        # Parse timedelta
        for attr in ['time', 'recurrence']:
            if task.has_key(attr) and task[attr]:
                hours = task[attr].days * 24 + task[attr].seconds / 3600
                if hours > 0:
                    s += " " + eval(attr.upper() + '_CHAR') + str(hours) + "H"
                else:  # less than 1 hour!
                    minutes = task[attr].seconds / 60
                    s += " " + eval(attr.upper() + '_CHAR') + str(minutes) + "M"

        # Parse dates
        for attr in ['start', 'due', 'end']:
            if task.has_key(attr) and task[attr]:
                s += " " + eval(attr.upper() + '_CHAR') + task[attr].strftime("%Y-%m-%d")

        return s

    def _disp(self, task):
        """Display the 'id' and a summary of the 'task'."""

        if FORMATTED_DISPLAY:
            task_line = self._dump_line(task)
        else:
            task_line = task['title']

        id_str = "%3d:(%f)" % (task['id'], task.priority())
        if self.colorize:  # colorize #id
            id_str = COLOR_CODES['cyan'] + id_str + DEFAULT_COLOR

        return "%s %s" % (id_str, self._colorize(task_line))

    def _show(self, task):
        """Display details of the given 'task'."""

        s = ""
        for attrib in Task.attributes_list[1:]:
            if task.has_key(attrib) and task[attrib]:
                if s: s = s + "\n"
                s = s + attrib.capitalize() + ": "
                if attrib == 'urgency':
                    s = s + str(gtd.URGENCY[task[attrib]])
                elif attrib == 'importance':
                    s = s + str(gtd.IMPORTANCE[task[attrib]])
                elif attrib == 'complete':
                    s = s + str(task[attrib]) + '%'
                else:
                    s = s + str(task[attrib])
        return s

    def _add(self, line):
        """Adds task from the given line and returns the new task_id."""

        if line:
            # Create the new task
            task = Task()

            # Parse input line for GTD and additional attributes
            task.add(**self._parse_line(line))

            # And, add it to the to-do list
            task_id = self.todo.add(task)

            # And, set the start date if none
            self.do_append("%d S:%s" % (task_id, datetime.datetime.now().strftime("%Y-%m-%d")))

            return task_id

    def _replace(self, idx, line):
        """Replace the task given by its idx by the new line."""

        # Frist, we need to find the task
        task = self.todo.find('id', idx)
        if task:
            i = self.todo.index(task)

            task = Task()  # create a new task

            # Parse input line for GTD and additional attributes
            task.add(**self._parse_line(line))
            task.add(id=idx)

            # And, replace it into the to-do list
            self.todo[i] = task

            # And, set the start date if none
            self.do_append("%d S:%s" % (idx, datetime.datetime.now().strftime("%Y-%m-%d")))

    def _search(self, regexp, completed=False, quiet=False):
        """Retrieve tasks matching given regexp.
        If completed True, also look for completed tasks."""

        # Rk: improved search function that retrieve regexp
        #     over all tasks attributes

        if completed:
            todos = [ t for t in self.todo.sort() ]  # first sort by priority
        else:
            todos = [ t for t in self.todo.sort() if t['complete'] < 100 ]

        # Then, retrieve the pattern (into full desc tasks)
        expr = re.compile(regexp, re.IGNORECASE)
        tasks = [ t for t in todos if expr.search(self._dump_line(t)) ]

        if not quiet:
            for t in tasks:
                print self._disp(t)

            # Show number of tasks as result of search if more than 10
            if len(tasks) > 10:
                print "%d tasks found" % len(tasks)

        # BB: This tiny line will be damn useful for further use...
        return tasks

    def _searchplus(self, regexp):  # BB
        """It searches, and returns the IDs."""

        lines = self._search(regexp, quiet=True)
        return [ line['id'] for line in lines ]

    def _get_deadlines_ordered(self):
        """Get tasks ordered by deadline, oldest first."""

        tasks = {}  # due -> task
        for task in self.todo:
            if not task['due'] or task['complete'] == 100:
                continue
            if not task['due'] in tasks:
                tasks[task['due']] = []
            tasks[task['due']].append(task)

        dues = tasks.keys()
        dues.sort()

        ordered = []

        for due in dues:
            ordered.extend(tasks[due])

        return ordered

    def _get_deadline_string(self, task):
        """Return formatted string of a deadline task."""

        now = datetime.datetime.now()
        daystart = datetime.datetime(year=now.year,
                                     month=now.month,
                                     day=now.day)
        dayend = daystart + datetime.timedelta(days=1)

        s = ""
        due = task.get('due', None)
        if due:
            left = due - daystart
            if due < daystart:
                days = -left.days
                if days == 1:
                    s = " [overdue one day]"
                else:
                    s = " [overdue %s days]" % days
            elif due < dayend:
                s = " [due today]"
            else:
                if left.days == 1:
                    s = " [one day left]"
                else:
                    s = " [%s days left]" % left.days

        return s

        s = ""
        due = task.get('due', None)
        if due:
            left = due - now
            if left < datetime.timedelta(days=1):
                s = " [due today]"
            elif left > datetime.timedelta(0):
                s = " [%s days left]" % left.days

    #
    # Edition.
    #

    def do_add(self, line):
        """Add a new task (from the input line):
        GTD> add My new task. @context p:Project"""

        task_id = self._add(line)
        if task_id:
            print "Task #%d added" % task_id

    do_a = do_add

    def do_del(self, id):
        """Delete given task:
        GTD> del #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.todo.supp(idx)
            print "Task #%d deleted" % idx

    do_rm = do_del

    def do_edit(self, id):
        """Interactively edit task given by #id:
        GTD> edit #id
        GTD edit> My task to edit. @context p:Project"""

        try:
            import readline
        except ImportError:
            print "Cannot edit without the 'readline' module!"
            return

        # Parse command line
        idx = self._parse_args(id)[0]

        if not idx:
            return

        task = self.todo.find('id', idx)
        if not task:
            return

        def pre_input_hook():
            readline.insert_text(self._dump_line(task))
            readline.redisplay()

            # Unset the hook again
            readline.set_pre_input_hook(None)

        readline.set_pre_input_hook(pre_input_hook)

        line = raw_input("GTD edit> ")
        # Remove edited line from history:
        #   oddly, get_history_item is 1-based,
        #   but remove_history_item is 0-based
        readline.remove_history_item(readline.get_current_history_length() - 1)
        self._replace(idx, line)
        print "Task #%d updated" % idx

    def do_close(self, id):
        """Close the given task:
        GTD> close #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            # First, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                if task['recurrence']:
                    if not task['due']:
                        task['due'] = datetime.datetime.now()
                    task['start'] = task['start'] + task['recurrence']  # reinit start date
                    task['due'] = task['due'] + task['recurrence']
                    print "Task #%d (%s) rescheduled (%s)." % (idx, task['title'], task['due'])
                else:
                    task['end'] = datetime.datetime.now()
                    task['complete'] = 100
                    print "Task #%d (%s) completed." % (idx, task['title'])

    do_done = do_close
    do_do = do_close

    def do_replace(self, id_line):
        """Replace the entire task by a new one:
        GTD> replace #id My new task. @computer p:Project"""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            self._replace(idx, line)
            print "Task #%d replaced" % idx

    do_sub = do_replace

    def do_extend(self, id_desc):
        """Add more text (description) to task:
        GTD> extend #id Additional description."""

        # Parse command line
        idx, desc = self._parse_args(id_desc)

        if idx and desc:
            task = self.todo.find('id', idx)
            if task:
                task['title'] += " " + desc  # TODO: allow to attach additional notes
                print "Task #%d extended" % idx

    do_notes = do_extend

    def do_append(self, id_line):
        """Add new elements to task but leave existing elements unchanged:
        GTD> append #id @newcontext p:NewProject"""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            # Frist, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                # Parse additional input line
                attrs = self._parse_line(line)

                # Then, modify the task
                task_modified = False
                for attr, value in attrs.items():
                    if value and not task[attr]:  # not override!
                        task[attr] = value
                        task_modified = True
                if task_modified:
                    print "Task #%d appended" % idx

    do_addto = do_append

    def do_appendall(self, args):  # BB
        """Add new elements to several tasks but leaves other elements unchanged:
        GTD> appendall regexp @newcontext p:NewProject"""

        regexp, action = self._parse_batch(args)

        tasks = self._searchplus(regexp)
        for taskid in tasks:
            self.do_append('%d %s' % (taskid, action))

    def do_modify(self, id_line):
        """Add/change elements of task but leave each other unchanged:
        GTD> modify #id @othercontext p:UpdtProject"""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            # First, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                # Parse additional input line
                attrs = self._parse_line(line)

                # Then, modify the task
                task_modified = False
                for attr, value in attrs.items():
                    if value:  # override!
                        task[attr] = value
                        task_modified = True
                if task_modified:
                    print "Task #%d modified" % idx
            else:
                print "Please check the id of the task to modify."
        else:
            print "Please specify the id of the task to modify first."

    do_mod = do_modify

    def do_modifyall(self, args):  # BB
        """Add/change elements of several tasks but leave each other unchanged:
        GTD> modifyall regexp @othercontext p:UpdtProject"""

        regexp, action = self._parse_batch(args)

        tasks = self._searchplus(regexp)
        for taskid in tasks:
            self.do_modify('%d %s' % (taskid, action))

    #
    # Specific GTD commands.
    #

    def do_someday(self, id):
        """Mark the given task as !someday:
        GTD> someday #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.do_modify("%d %s" % (idx, "!someday"))

    do_maybe = do_someday

    def do_waitingfor(self, id):
        """Mark the given task as !waitingfor:
        GTD> waitingfor #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.do_modify("%d %s" % (idx, "!waitingfor"))

    def do_ref(self, id_ref):
        """Archive the given task as Reference:
        GTD> ref #id refname
        """

        # Parse command line
        idx, reference = self._parse_args(id_ref)

        if idx and reference:
            self.do_append("%d ref:%s" % (idx, reference))

    #
    # Additional options from Stephen Covey.
    #

    def do_urgency(self, id_level):
        """Set the Urgency (5=today, 4=this_week, 3=this_month, 2=this_quarter, 1=this_year):
        GTD> urgency #id lvl"""

        # Parse command line
        idx, level = self._parse_args(id_level)

        if idx and level and re.match(DIGIT_MATCH, level):
            self.do_modify("%d U:%d" % (idx, int(level)))

    do_u = do_urgency

    def do_importance(self, id_level):
        """Set the Importance (5=crucial,4=high,3=normal,2=low,1=someday):
        GTD> importance #id lvl"""

        # Parse command line
        idx, level = self._parse_args(id_level)

        if idx and level and re.match(DIGIT_MATCH, level):
            self.do_modify("%d I:%d" % (idx, int(level)))

    do_i = do_importance

    def do_complete(self, id_percent):
        """Set the percent complete:
        GTD> complete #id percentage"""

        # Parse command line
        idx, percent = self._parse_args(id_percent)

        if idx and percent and re.match(NUMBER_MATCH, percent):
            self.do_modify("%d C:%d" % (idx, int(percent)))

    do_c = do_complete

    def do_time(self, id_time):
        """Set the Time requiered to accomplish the task (in Weeks, Days, Hours or Minutes):
        GTD> time #id n[WDHM]"""

        # Parse command line
        idx, time = self._parse_args(id_time)

        if idx and time and re.match(TIMEDELTA_MATCH, time):
            self.do_modify("%d T:%s" % (idx, time))

    def do_recurrence(self, id_rec):
        """Set the Recurrence of the task (in Weeks, Days or Hours):
        GTD> recurrence #id n[WDH]"""

        # Parse command line
        idx, recurrence = self._parse_args(id_rec)

        if idx and recurrence and re.match(TIMEDELTA_MATCH, recurrence):
            self.do_modify("%d R:%s" % (idx, recurrence))

    do_recurs = do_recurrence
    do_rec = do_recurrence

    def do_start(self, id_date):
        """Set the Start / creation date:
        GTD> start #id YYYY-MM-DD"""

        # Parse command line
        idx, date = self._parse_args(id_date)

        if idx and date and re.match(DATE_MATCH, date):
            self.do_modify("%d S:%s" % (idx, date))

    def do_due(self, id_date):
        """Set the Due (target) date:
        GTD> due #id YYYY-MM-DD"""

        # Parse command line
        idx, date = self._parse_args(id_date)

        if idx and date and re.match(DATE_MATCH, date):
            self.do_modify("%d D:%s" % (idx, date))

    def do_duein(self, id_doff):
        """Set the Due (target) date in +/- days:
        GTD> duein #id +/-days"""

        # Parse command line
        idx, days_offset = self._parse_args(id_doff)

        if idx and days_offset:
            # Compute the new date= now + days_offset
            now = datetime.datetime.now()
            dayend = now + datetime.timedelta(int(days_offset))
            date = dayend.strftime("%Y-%m-%d")

            self.do_modify("%d D:%s" % (idx, date))

    def do_end(self, id_date):
        """Set the End, closure date:
        GTD> end #id YYYY-MM-DD"""

        # Parse command line
        idx, date = self._parse_args(id_date)

        if idx and date and re.match(DATE_MATCH, date):
            self.do_modify("%d E:%s" % (idx, date))

    #
    # Sorted/ordered display.
    #

    def do_listall(self, nb):
        """List #nb tasks (close or not and ordered by #id):
        GTD> listall [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = self.todo
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    do_la = do_listall

    def do_list(self, nb):
        """List #nb open tasks (ordered by #id):
        GTD> list [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [ t for t in self.todo if t['complete'] < 100 ]  # filter done/close tasks
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    do_ls = do_list

    def do_listinbox(self, nb):
        """List #nb quick added tasks (i.e. without context; ordered by #id):
        GTD> listinbox [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [ t for t in self.todo if t['complete'] < 100 and not t['context'] ]  # without context
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d inbox tasks found" % len(tasks)

    do_li = do_listinbox

    def do_listref(self, nb):
        """List #nb ref tasks (ordered by #id):
        GTD> listref [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [ t for t in self.todo if t.has_key('reference') and t['reference'] ]  # filter ref tasks
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d references found" % len(tasks)

    do_lr = do_listref

    def do_show(self, id):
        """Show details of the given task:
        GTD> show #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            # Frist, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                print self._show(task)

    do_view = do_show
    do_v = do_show

    def do_sort(self, nb):
        """Sort #nb tasks by priority (appear only after start date):
        GTD> sort [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [ t for t in self.todo.sort() if t['complete'] < 100 ]

        # Appear only after start date
        today = datetime.datetime.now()
        tasks = [ t for t in tasks if t['start'] <= today ]

        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    def do_today(self, nb):
        """Sort #nb tasks by priority, and shows only today
        (the time now before and after start date):
        GTD> today [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [ t for t in self.todo.sort() if t['complete'] < 100 ]

        # Appear only after start date
        today = datetime.datetime.now()
        tasks = [ t for t in tasks if t['start'] <= today ]

        if nb: # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            if t['due']:
                if t['due'] <= today:
                    print self._disp(t)
            else:
                print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d today tasks found" % len(tasks)

    do_now = do_today

    def do_listpri(self, regexp):
        """Sort tasks matching given regexp (appear only after start date):
        GTD> listpri [regexp]"""

        todos = [ t for t in self.todo.sort() if t['complete'] < 100 ]

        # Appear only after start date
        today = datetime.datetime.now()
        todos = [ t for t in todos if t['start'] <= today ]

        # Then, retrieve the pattern (into full desc tasks)
        expr = re.compile(regexp, re.IGNORECASE)
        tasks = [ t for t in todos if expr.search(self._dump_line(t)) ]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    do_lp = do_listpri

    def do_order(self, nb_attr):
        """Order #nb tasks by context/project/status/reference and priority
        (appear only after start date):
        GTD> order #nb [context|project|status|reference]"""

        # Parse command line
        nb, attr = self._parse_args(nb_attr)

        if not attr or attr not in ['context', 'project', 'status', 'reference']:
            attr = 'context'  # by default order by context

        for c, ts in self.todo.order(attr).items():
            if attr == 'reference':
                tasks = [ t for t in ts ]  # even if completed!
            else:
                tasks = [ t for t in ts if t['complete'] < 100 ]

            if not tasks: continue  # empty!

            # Appear only after start date
            today = datetime.datetime.now()
            tasks = [ t for t in tasks if t['start'] <= today ]

            # Section title
            print self._colorize(eval(attr.upper() + '_CHAR') + c.capitalize())

            if nb: tasks = tasks[:nb]  # display only nb tasks
            for t in tasks:
                print self._disp(t)

            # Show number of tasks as result of search if more than 10
            if len(tasks) > 10:
                print "%d tasks found" % len(tasks)

    do_o = do_order

    def do_status(self, line):
        """Display projects statuses (percent complete):
        GTD> status"""

        for p, ts in self.todo.order('project').items():
            # Thomas: don't show completed projects

            percent = 0  # compute project's percent complete
            next = ""
            for t in ts:
                if t['complete']:
                    percent += int(t['complete'])
                # Thomas: find next action
                if t['status'] and not t['complete'] and 'next' in t['status']:
                    next = " !next: %s" % t['title']

            percent /= len(ts)
            if percent == 100: continue

            # Section title
            print self._colorize(PROJECT_CHAR + p.capitalize()),
            print "%d%%%s" % (percent, self._colorize(next))

    do_summary = do_status

    def do_contexts(self, line):
        """Display contexts and next task for each context:
        GTD> contexts"""

        for context, tasks in self.todo.order('context').items():
            percent = 0  # compute project's percent complete
            count = 0
            next = None
            for t in tasks:
                print t['title'], t.priority(), t['complete']
                percent += int(t['complete'])
                if t['complete'] != 100:
                    print "->", t['title']
                    count += 1
                if not next and t['status'] and 'Next' in t['status'] and t['complete'] != 100:
                    next = t

            percent /= len(tasks)
            if percent == 100: continue

            # Section title
            print self._colorize(CONTEXT_CHAR + context) + ':',
            print "%d" % count,
            if next:
                projects = next['project']
                if projects:
                    print self._colorize(PROJECT_CHAR + projects[0]),
                print self._colorize("!Next: %s" % next['title'])
            else:
                print

    def do_deadlines(self, line):
        """Display upcoming tasks ordered by deadline:
        GTD> deadlines #nb"""

        # Parse command line
        nb = self._parse_args(line)[0]

        ordered = self._get_deadlines_ordered()

        # Keep only upcoming tasks
        now = datetime.datetime.now()
        daystart = datetime.datetime(year=now.year,
                                     month=now.month,
                                     day=now.day)
        dayend = daystart + datetime.timedelta(days=1)
        ordered = [t for t in ordered if t.get('due', None) >= daystart]

        if nb: ordered = ordered[:nb]  # display only nb tasks

        for t in ordered:
            # note about due
            s = self._get_deadline_string(t)

            print self._disp(t) + s

        # Show number of tasks as result of search if more than 10
        if len(ordered) > 10:
            print "%d due tasks found" % len(ordered)

    def do_overdue(self, line):
        """Display overdue tasks ordered by deadline (including due today):
        GTD> overdue #nb"""

        # Parse command line
        nb = self._parse_args(line)[0]

        ordered = self._get_deadlines_ordered()

        # Keep only overdue tasks
        now = datetime.datetime.now()
        daystart = datetime.datetime(year=now.year,
                                     month=now.month,
                                     day=now.day)
        dayend = daystart + datetime.timedelta(days=1)
        ordered = [t for t in ordered if t.get('due', None) < dayend]

        if nb: ordered = ordered[:nb]  # display only nb tasks

        for t in ordered:
            # note about due
            s = self._get_deadline_string(t)

            print self._disp(t) + s

        # Show number of tasks as result of search if more than 10
        if len(ordered) > 10:
            print "%d overdue tasks found" % len(ordered)

    #
    # Select/monitor.
    #

    def do_select(self, cmd):
        """ Select a task:
        GTD> select #id"""

        # Parse command line
        idx = self._parse_args(cmd)[0]

        task = self.todo.find('id', idx)
        if not task:
            print "Task not found"
            return

        self.tasks_selected.append(idx)

    def do_deselect(self, cmd):
        """ Deselect a task:
        GTD> deselect #id"""

        # Parse command line
        idx = self._parse_args(cmd)[0]

        if idx not in self.tasks_selected:
            print "Task not found"
            return

        self.tasks_selected.remove(idx)

    def do_selected(self, line=None):
        """Show selected tasks sorted by priority:
        GTD> selected"""

        for t in (self.todo.find("id", idx) for idx in self.tasks_selected):
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(self.tasks_selected) > 10:
            print "%d selected tasks found" % len(self.tasks_selected)

    #
    # Re-search.
    #

    def do_search(self, regexp):
        """Retrieve tasks matching given regexp:
        GTD> search regexp"""

        self._search(regexp)

    do_s = do_search

    def do_searchall(self, regexp):
        """Retrieve tasks matching given regexp, including completed ones:
        GTD> searchall regexp"""

        self._search(regexp, completed=True)

    do_sa = do_searchall

    #
    # IO functions.
    #

    def do_load(self, todotxt):
        """Load from a todotxt file:
        GTD> load path/to/todo.txt"""

        self.todotxt = todotxt  # ok, save file path
        try:
            f = open(self.todotxt, 'r')
            try:
                self.todo.erase()  # clean
                nb = 0
                for line in f:
                    #print line,
                    if line.lstrip().startswith('#'): continue
                    self._add(line.strip())
                    nb += 1
            finally:
                f.close()
                print "%d tasks loaded from '%s'" % (nb, todotxt)
        except IOError, err:
            # 'file not found' exception?
            if err.errno != errno.ENOENT: raise  # raise all others
            print err  # and continue (new file created)
Beispiel #4
0
class GTD(cmd.Cmd):
    """GTD(cmd.Cmd) class."""
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.intro = """
  yaGTD is a free software available under the terms of the GNU GPL.
  Refer to the file COPYING (which should be included in this distribution)
  for the specific terms of this licence.
  """
        self.prompt = "GTD> "
        self.todo = ToDo()  # the to-do list is here!

        self.todotxt = TODO_TXT
        self.donetxt = DONE_TXT

        self.colorize = False
        self.tasks_selected = []

    #
    # Private functions.
    #

    def _parse_args(self, args):
        """Parse command arguments= num + str."""

        num = string = None

        # Parse command args
        n = re.search(r"^(?x)(\d+)", args)  # num
        s = re.search(r" +([@!:\+\-\w ]+)$", args)  # string

        if n: num = int(n.group(1))
        if s: string = s.group(1)

        #print "parsed", num, string
        return (num, string)

    def _parse_batch(self, args):  # BB
        """Return search criterion and elemenst to add/change."""

        regexp = action = ''
        try:
            regexp, action = args.split(' ', 1)
        except:
            pass
        return regexp, action

    def _colorize(self, string):
        """Colorize the given 'string'."""

        s = string

        if self.colorize:
            # Scan to colorize attributes (accordingly to global settings) ...
            for attr in Task.attributes_list[3:]:
                color_fct = lambda m: eval(attr.upper() + '_COLOR') + m.group(
                    0) + DEFAULT_COLOR
                s = eval(attr.upper() + '_REGEXP').sub(color_fct, s)

        return s

    def _parse_line(self, line):
        """Return a dictionary (task mapping) from 'line' parsing."""

        t = {}  # Task mapping
        title = line  # the 'title' extracted from line

        # Parse for GTD attributes
        for attr in ['context', 'project', 'status', 'reference']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                t[attr] = matches

        # Parse additional properties
        for attr in ['urgency', 'importance', 'complete']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                t[attr] = int(matches[-1])  # keep only last!

        # Parse timedelta
        for attr in ['time', 'recurrence']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                match = matches[-1]  # keep only last!
                hours = minutes = 0  # compute hours

                if attr == 'time':  # compute time requiered (in working hours)
                    if match[1].upper() == 'W':  # weeks
                        hours = int(match[0]) * gtd.WEEK_IN_HOURS
                    elif match[1].upper() == 'D':  # days
                        hours = int(match[0]) * gtd.DAY_IN_HOURS
                    elif match[1].upper() == 'H':  # hours
                        hours = int(match[0])
                    elif match[1].upper() == 'M':  # minutes
                        minutes = int(match[0])
                    else:
                        pass  # invalid time range indicator

                elif attr == 'recurrence':  # compute full hours
                    if match[1].upper() == 'W':  # weeks
                        hours = int(match[0]) * 7 * 24
                    elif match[1].upper() == 'D':  # days
                        hours = int(match[0]) * 24
                    elif match[1].upper() == 'H':  # hours
                        hours = int(match[0])
                    elif match[1].upper() == 'M':  # minutes
                        minutes = int(match[0])
                    else:
                        pass  # invalid time range indicator

                t[attr] = datetime.timedelta(hours=hours, minutes=minutes)

        # Parse dates
        for attr in ['start', 'due', 'end']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                year, month, day = matches[-1].split('-')  # keep only last!
                t[attr] = datetime.datetime(int(year), int(month), int(day))

            else:  # check if it is in format of special day
                title = eval(attr.upper() + '_DOW_REGEXP').sub('', title)

                matches = eval(attr.upper() + '_DOW_REGEXP').findall(line)
                if matches:
                    today = datetime.datetime.today()
                    today_dow = today.weekday()  # monday is first
                    given_dow = DOW.index(matches[-1])

                    day_diff = given_dow - today_dow
                    if day_diff < 0:
                        day_diff += 7

                    day = today + datetime.timedelta(int(day_diff))
                    t[attr] = day

        # Post-processing
        if t.has_key('end') or t.has_key(
                'reference'):  # ignore completed and archived tasks
            t['complete'] = 100

        # if due < start then correct due by setting it to start
        if t.has_key('due') and t.has_key('start'):
            if t['due'] < t['start']:
                t['due'] = t['start']

        # Set the title
        t['title'] = " ".join(title.split())  # remove useless blank chars too

        return t

    def _dump_line(self, task):
        """Return a formatted line from the given 'task'."""

        s = task['title']  # init the line with the title

        # Dump GTD attributes
        for attr in ['context', 'project', 'status', 'reference']:
            if task.has_key(attr):
                for value in task[attr]:
                    s += " " + eval(attr.upper() + '_CHAR') + value

        # Dump additional properties
        for attr in ['urgency', 'importance', 'complete']:
            if task.has_key(attr) and task[attr]:
                s += " " + eval(attr.upper() + '_CHAR') + str(task[attr])

        # Parse timedelta
        for attr in ['time', 'recurrence']:
            if task.has_key(attr) and task[attr]:
                hours = task[attr].days * 24 + task[attr].seconds / 3600
                if hours > 0:
                    s += " " + eval(attr.upper() + '_CHAR') + str(hours) + "H"
                else:  # less than 1 hour!
                    minutes = task[attr].seconds / 60
                    s += " " + eval(attr.upper() +
                                    '_CHAR') + str(minutes) + "M"

        # Parse dates
        for attr in ['start', 'due', 'end']:
            if task.has_key(attr) and task[attr]:
                s += " " + eval(attr.upper() +
                                '_CHAR') + task[attr].strftime("%Y-%m-%d")

        return s

    def _disp(self, task):
        """Display the 'id' and a summary of the 'task'."""

        if FORMATTED_DISPLAY:
            task_line = self._dump_line(task)
        else:
            task_line = task['title']

        id_str = "%3d:(%f)" % (task['id'], task.priority())
        if self.colorize:  # colorize #id
            id_str = COLOR_CODES['cyan'] + id_str + DEFAULT_COLOR

        return "%s %s" % (id_str, self._colorize(task_line))

    def _show(self, task):
        """Display details of the given 'task'."""

        s = ""
        for attrib in Task.attributes_list[1:]:
            if task.has_key(attrib) and task[attrib]:
                if s: s = s + "\n"
                s = s + attrib.capitalize() + ": "
                if attrib == 'urgency':
                    s = s + str(gtd.URGENCY[task[attrib]])
                elif attrib == 'importance':
                    s = s + str(gtd.IMPORTANCE[task[attrib]])
                elif attrib == 'complete':
                    s = s + str(task[attrib]) + '%'
                else:
                    s = s + str(task[attrib])
        return s

    def _add(self, line):
        """Adds task from the given line and returns the new task_id."""

        if line:
            # Create the new task
            task = Task()

            # Parse input line for GTD and additional attributes
            task.add(**self._parse_line(line))

            # And, add it to the to-do list
            task_id = self.todo.add(task)

            # And, set the start date if none
            self.do_append(
                "%d S:%s" %
                (task_id, datetime.datetime.now().strftime("%Y-%m-%d")))

            return task_id

    def _replace(self, idx, line):
        """Replace the task given by its idx by the new line."""

        # Frist, we need to find the task
        task = self.todo.find('id', idx)
        if task:
            i = self.todo.index(task)

            task = Task()  # create a new task

            # Parse input line for GTD and additional attributes
            task.add(**self._parse_line(line))
            task.add(id=idx)

            # And, replace it into the to-do list
            self.todo[i] = task

            # And, set the start date if none
            self.do_append("%d S:%s" %
                           (idx, datetime.datetime.now().strftime("%Y-%m-%d")))

    def _search(self, regexp, completed=False, quiet=False):
        """Retrieve tasks matching given regexp.
        If completed True, also look for completed tasks."""

        # Rk: improved search function that retrieve regexp
        #     over all tasks attributes

        if completed:
            todos = [t for t in self.todo.sort()]  # first sort by priority
        else:
            todos = [t for t in self.todo.sort() if t['complete'] < 100]

        # Then, retrieve the pattern (into full desc tasks)
        expr = re.compile(regexp, re.IGNORECASE)
        tasks = [t for t in todos if expr.search(self._dump_line(t))]

        if not quiet:
            for t in tasks:
                print self._disp(t)

            # Show number of tasks as result of search if more than 10
            if len(tasks) > 10:
                print "%d tasks found" % len(tasks)

        # BB: This tiny line will be damn useful for further use...
        return tasks

    def _searchplus(self, regexp):  # BB
        """It searches, and returns the IDs."""

        lines = self._search(regexp, quiet=True)
        return [line['id'] for line in lines]

    def _get_deadlines_ordered(self):
        """Get tasks ordered by deadline, oldest first."""

        tasks = {}  # due -> task
        for task in self.todo:
            if not task['due'] or task['complete'] == 100:
                continue
            if not task['due'] in tasks:
                tasks[task['due']] = []
            tasks[task['due']].append(task)

        dues = tasks.keys()
        dues.sort()

        ordered = []

        for due in dues:
            ordered.extend(tasks[due])

        return ordered

    def _get_deadline_string(self, task):
        """Return formatted string of a deadline task."""

        now = datetime.datetime.now()
        daystart = datetime.datetime(year=now.year,
                                     month=now.month,
                                     day=now.day)
        dayend = daystart + datetime.timedelta(days=1)

        s = ""
        due = task.get('due', None)
        if due:
            left = due - daystart
            if due < daystart:
                days = -left.days
                if days == 1:
                    s = " [overdue one day]"
                else:
                    s = " [overdue %s days]" % days
            elif due < dayend:
                s = " [due today]"
            else:
                if left.days == 1:
                    s = " [one day left]"
                else:
                    s = " [%s days left]" % left.days

        return s

        s = ""
        due = task.get('due', None)
        if due:
            left = due - now
            if left < datetime.timedelta(days=1):
                s = " [due today]"
            elif left > datetime.timedelta(0):
                s = " [%s days left]" % left.days

    #
    # Edition.
    #

    def do_add(self, line):
        """Add a new task (from the input line):
        GTD> add My new task. @context p:Project"""

        task_id = self._add(line)
        if task_id:
            print "Task #%d added" % task_id

    do_a = do_add

    def do_del(self, id):
        """Delete given task:
        GTD> del #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.todo.supp(idx)
            print "Task #%d deleted" % idx

    do_rm = do_del

    def do_edit(self, id):
        """Interactively edit task given by #id:
        GTD> edit #id
        GTD edit> My task to edit. @context p:Project"""

        try:
            import readline
        except ImportError:
            print "Cannot edit without the 'readline' module!"
            return

        # Parse command line
        idx = self._parse_args(id)[0]

        if not idx:
            return

        task = self.todo.find('id', idx)
        if not task:
            return

        def pre_input_hook():
            readline.insert_text(self._dump_line(task))
            readline.redisplay()

            # Unset the hook again
            readline.set_pre_input_hook(None)

        readline.set_pre_input_hook(pre_input_hook)

        line = raw_input("GTD edit> ")
        # Remove edited line from history:
        #   oddly, get_history_item is 1-based,
        #   but remove_history_item is 0-based
        readline.remove_history_item(readline.get_current_history_length() - 1)
        self._replace(idx, line)
        print "Task #%d updated" % idx

    def do_close(self, id):
        """Close the given task:
        GTD> close #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            # First, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                if task['recurrence']:
                    if not task['due']:
                        task['due'] = datetime.datetime.now()
                    task['start'] = task['start'] + task[
                        'recurrence']  # reinit start date
                    task['due'] = task['due'] + task['recurrence']
                    print "Task #%d (%s) rescheduled (%s)." % (
                        idx, task['title'], task['due'])
                else:
                    task['end'] = datetime.datetime.now()
                    task['complete'] = 100
                    print "Task #%d (%s) completed." % (idx, task['title'])

    do_done = do_close
    do_do = do_close

    def do_replace(self, id_line):
        """Replace the entire task by a new one:
        GTD> replace #id My new task. @computer p:Project"""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            self._replace(idx, line)
            print "Task #%d replaced" % idx

    do_sub = do_replace

    def do_extend(self, id_desc):
        """Add more text (description) to task:
        GTD> extend #id Additional description."""

        # Parse command line
        idx, desc = self._parse_args(id_desc)

        if idx and desc:
            task = self.todo.find('id', idx)
            if task:
                task[
                    'title'] += " " + desc  # TODO: allow to attach additional notes
                print "Task #%d extended" % idx

    do_notes = do_extend

    def do_append(self, id_line):
        """Add new elements to task but leave existing elements unchanged:
        GTD> append #id @newcontext p:NewProject"""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            # Frist, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                # Parse additional input line
                attrs = self._parse_line(line)

                # Then, modify the task
                task_modified = False
                for attr, value in attrs.items():
                    if value and not task[attr]:  # not override!
                        task[attr] = value
                        task_modified = True
                if task_modified:
                    print "Task #%d appended" % idx

    do_addto = do_append

    def do_appendall(self, args):  # BB
        """Add new elements to several tasks but leaves other elements unchanged:
        GTD> appendall regexp @newcontext p:NewProject"""

        regexp, action = self._parse_batch(args)

        tasks = self._searchplus(regexp)
        for taskid in tasks:
            self.do_append('%d %s' % (taskid, action))

    def do_modify(self, id_line):
        """Add/change elements of task but leave each other unchanged:
        GTD> modify #id @othercontext p:UpdtProject"""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            # First, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                # Parse additional input line
                attrs = self._parse_line(line)

                # Then, modify the task
                task_modified = False
                for attr, value in attrs.items():
                    if value:  # override!
                        task[attr] = value
                        task_modified = True
                if task_modified:
                    print "Task #%d modified" % idx
            else:
                print "Please check the id of the task to modify."
        else:
            print "Please specify the id of the task to modify first."

    do_mod = do_modify

    def do_modifyall(self, args):  # BB
        """Add/change elements of several tasks but leave each other unchanged:
        GTD> modifyall regexp @othercontext p:UpdtProject"""

        regexp, action = self._parse_batch(args)

        tasks = self._searchplus(regexp)
        for taskid in tasks:
            self.do_modify('%d %s' % (taskid, action))

    #
    # Specific GTD commands.
    #

    def do_someday(self, id):
        """Mark the given task as !someday:
        GTD> someday #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.do_modify("%d %s" % (idx, "!someday"))

    do_maybe = do_someday

    def do_waitingfor(self, id):
        """Mark the given task as !waitingfor:
        GTD> waitingfor #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.do_modify("%d %s" % (idx, "!waitingfor"))

    def do_ref(self, id_ref):
        """Archive the given task as Reference:
        GTD> ref #id refname 
        """

        # Parse command line
        idx, reference = self._parse_args(id_ref)

        if idx and reference:
            self.do_append("%d ref:%s" % (idx, reference))

    #
    # Additional options from Stephen Covey.
    #

    def do_urgency(self, id_level):
        """Set the Urgency (5=today, 4=this_week, 3=this_month, 2=this_quarter, 1=this_year):
        GTD> urgency #id lvl"""

        # Parse command line
        idx, level = self._parse_args(id_level)

        if idx and level and re.match(DIGIT_MATCH, level):
            self.do_modify("%d U:%d" % (idx, int(level)))

    do_u = do_urgency

    def do_importance(self, id_level):
        """Set the Importance (5=crucial,4=high,3=normal,2=low,1=someday):
        GTD> importance #id lvl"""

        # Parse command line
        idx, level = self._parse_args(id_level)

        if idx and level and re.match(DIGIT_MATCH, level):
            self.do_modify("%d I:%d" % (idx, int(level)))

    do_i = do_importance

    def do_complete(self, id_percent):
        """Set the percent complete:
        GTD> complete #id percentage"""

        # Parse command line
        idx, percent = self._parse_args(id_percent)

        if idx and percent and re.match(NUMBER_MATCH, percent):
            self.do_modify("%d C:%d" % (idx, int(percent)))

    do_c = do_complete

    def do_time(self, id_time):
        """Set the Time requiered to accomplish the task (in Weeks, Days, Hours or Minutes):
        GTD> time #id n[WDHM]"""

        # Parse command line
        idx, time = self._parse_args(id_time)

        if idx and time and re.match(TIMEDELTA_MATCH, time):
            self.do_modify("%d T:%s" % (idx, time))

    def do_recurrence(self, id_rec):
        """Set the Recurrence of the task (in Weeks, Days or Hours):
        GTD> recurrence #id n[WDH]"""

        # Parse command line
        idx, recurrence = self._parse_args(id_rec)

        if idx and recurrence and re.match(TIMEDELTA_MATCH, recurrence):
            self.do_modify("%d R:%s" % (idx, recurrence))

    do_recurs = do_recurrence
    do_rec = do_recurrence

    def do_start(self, id_date):
        """Set the Start / creation date:
        GTD> start #id YYYY-MM-DD"""

        # Parse command line
        idx, date = self._parse_args(id_date)

        if idx and date and re.match(DATE_MATCH, date):
            self.do_modify("%d S:%s" % (idx, date))

    def do_due(self, id_date):
        """Set the Due (target) date:
        GTD> due #id YYYY-MM-DD"""

        # Parse command line
        idx, date = self._parse_args(id_date)

        if idx and date and re.match(DATE_MATCH, date):
            self.do_modify("%d D:%s" % (idx, date))

    def do_duein(self, id_doff):
        """Set the Due (target) date in +/- days:
        GTD> duein #id +/-days"""

        # Parse command line
        idx, days_offset = self._parse_args(id_doff)

        if idx and days_offset:
            # Compute the new date= now + days_offset
            now = datetime.datetime.now()
            dayend = now + datetime.timedelta(int(days_offset))
            date = dayend.strftime("%Y-%m-%d")

            self.do_modify("%d D:%s" % (idx, date))

    def do_end(self, id_date):
        """Set the End, closure date:
        GTD> end #id YYYY-MM-DD"""

        # Parse command line
        idx, date = self._parse_args(id_date)

        if idx and date and re.match(DATE_MATCH, date):
            self.do_modify("%d E:%s" % (idx, date))

    #
    # Sorted/ordered display.
    #

    def do_listall(self, nb):
        """List #nb tasks (close or not and ordered by #id):
        GTD> listall [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = self.todo
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    do_la = do_listall

    def do_list(self, nb):
        """List #nb open tasks (ordered by #id):
        GTD> list [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [t for t in self.todo
                 if t['complete'] < 100]  # filter done/close tasks
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    do_ls = do_list

    def do_listinbox(self, nb):
        """List #nb quick added tasks (i.e. without context; ordered by #id):
        GTD> listinbox [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [
            t for t in self.todo if t['complete'] < 100 and not t['context']
        ]  # without context
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d inbox tasks found" % len(tasks)

    do_li = do_listinbox

    def do_listref(self, nb):
        """List #nb ref tasks (ordered by #id):
        GTD> listref [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [
            t for t in self.todo if t.has_key('reference') and t['reference']
        ]  # filter ref tasks
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d references found" % len(tasks)

    do_lr = do_listref

    def do_show(self, id):
        """Show details of the given task:
        GTD> show #id"""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            # Frist, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                print self._show(task)

    do_view = do_show
    do_v = do_show

    def do_sort(self, nb):
        """Sort #nb tasks by priority (appear only after start date):
        GTD> sort [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [t for t in self.todo.sort() if t['complete'] < 100]

        # Appear only after start date
        today = datetime.datetime.now()
        tasks = [t for t in tasks if t['start'] <= today]

        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    def do_today(self, nb):
        """Sort #nb tasks by priority, and shows only today
        (the time now before and after start date):
        GTD> today [#nb]"""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [t for t in self.todo.sort() if t['complete'] < 100]

        # Appear only after start date
        today = datetime.datetime.now()
        tasks = [t for t in tasks if t['start'] <= today]

        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            if t['due']:
                if t['due'] <= today:
                    print self._disp(t)
            else:
                print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d today tasks found" % len(tasks)

    do_now = do_today

    def do_listpri(self, regexp):
        """Sort tasks matching given regexp (appear only after start date):
        GTD> listpri [regexp]"""

        todos = [t for t in self.todo.sort() if t['complete'] < 100]

        # Appear only after start date
        today = datetime.datetime.now()
        todos = [t for t in todos if t['start'] <= today]

        # Then, retrieve the pattern (into full desc tasks)
        expr = re.compile(regexp, re.IGNORECASE)
        tasks = [t for t in todos if expr.search(self._dump_line(t))]

        for t in tasks:
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(tasks) > 10:
            print "%d tasks found" % len(tasks)

    do_lp = do_listpri

    def do_order(self, nb_attr):
        """Order #nb tasks by context/project/status/reference and priority
        (appear only after start date):
        GTD> order #nb [context|project|status|reference]"""

        # Parse command line
        nb, attr = self._parse_args(nb_attr)

        if not attr or attr not in [
                'context', 'project', 'status', 'reference'
        ]:
            attr = 'context'  # by default order by context

        for c, ts in self.todo.order(attr).items():
            if attr == 'reference':
                tasks = [t for t in ts]  # even if completed!
            else:
                tasks = [t for t in ts if t['complete'] < 100]

            if not tasks: continue  # empty!

            # Appear only after start date
            today = datetime.datetime.now()
            tasks = [t for t in tasks if t['start'] <= today]

            # Section title
            print self._colorize(eval(attr.upper() + '_CHAR') + c.capitalize())

            if nb: tasks = tasks[:nb]  # display only nb tasks
            for t in tasks:
                print self._disp(t)

            # Show number of tasks as result of search if more than 10
            if len(tasks) > 10:
                print "%d tasks found" % len(tasks)

    do_o = do_order

    def do_status(self, line):
        """Display projects statuses (percent complete):
        GTD> status"""

        for p, ts in self.todo.order('project').items():
            # Thomas: don't show completed projects

            percent = 0  # compute project's percent complete
            next = ""
            for t in ts:
                if t['complete']:
                    percent += int(t['complete'])
                # Thomas: find next action
                if t['status'] and not t['complete'] and 'next' in t['status']:
                    next = " !next: %s" % t['title']

            percent /= len(ts)
            if percent == 100: continue

            # Section title
            print self._colorize(PROJECT_CHAR + p.capitalize()),
            print "%d%%%s" % (percent, self._colorize(next))

    do_summary = do_status

    def do_contexts(self, line):
        """Display contexts and next task for each context:
        GTD> contexts"""

        for context, tasks in self.todo.order('context').items():
            percent = 0  # compute project's percent complete
            count = 0
            next = None
            for t in tasks:
                print t['title'], t.priority(), t['complete']
                percent += int(t['complete'])
                if t['complete'] != 100:
                    print "->", t['title']
                    count += 1
                if not next and t['status'] and 'Next' in t[
                        'status'] and t['complete'] != 100:
                    next = t

            percent /= len(tasks)
            if percent == 100: continue

            # Section title
            print self._colorize(CONTEXT_CHAR + context) + ':',
            print "%d" % count,
            if next:
                projects = next['project']
                if projects:
                    print self._colorize(PROJECT_CHAR + projects[0]),
                print self._colorize("!Next: %s" % next['title'])
            else:
                print

    def do_deadlines(self, line):
        """Display upcoming tasks ordered by deadline:
        GTD> deadlines #nb"""

        # Parse command line
        nb = self._parse_args(line)[0]

        ordered = self._get_deadlines_ordered()

        # Keep only upcoming tasks
        now = datetime.datetime.now()
        daystart = datetime.datetime(year=now.year,
                                     month=now.month,
                                     day=now.day)
        dayend = daystart + datetime.timedelta(days=1)
        ordered = [t for t in ordered if t.get('due', None) >= daystart]

        if nb: ordered = ordered[:nb]  # display only nb tasks

        for t in ordered:
            # note about due
            s = self._get_deadline_string(t)

            print self._disp(t) + s

        # Show number of tasks as result of search if more than 10
        if len(ordered) > 10:
            print "%d due tasks found" % len(ordered)

    def do_overdue(self, line):
        """Display overdue tasks ordered by deadline (including due today):
        GTD> overdue #nb"""

        # Parse command line
        nb = self._parse_args(line)[0]

        ordered = self._get_deadlines_ordered()

        # Keep only overdue tasks
        now = datetime.datetime.now()
        daystart = datetime.datetime(year=now.year,
                                     month=now.month,
                                     day=now.day)
        dayend = daystart + datetime.timedelta(days=1)
        ordered = [t for t in ordered if t.get('due', None) < dayend]

        if nb: ordered = ordered[:nb]  # display only nb tasks

        for t in ordered:
            # note about due
            s = self._get_deadline_string(t)

            print self._disp(t) + s

        # Show number of tasks as result of search if more than 10
        if len(ordered) > 10:
            print "%d overdue tasks found" % len(ordered)

    #
    # Select/monitor.
    #

    def do_select(self, cmd):
        """ Select a task:
        GTD> select #id"""

        # Parse command line
        idx = self._parse_args(cmd)[0]

        task = self.todo.find('id', idx)
        if not task:
            print "Task not found"
            return

        self.tasks_selected.append(idx)

    def do_deselect(self, cmd):
        """ Deselect a task:
        GTD> deselect #id"""

        # Parse command line
        idx = self._parse_args(cmd)[0]

        if idx not in self.tasks_selected:
            print "Task not found"
            return

        self.tasks_selected.remove(idx)

    def do_selected(self, line=None):
        """Show selected tasks sorted by priority:
        GTD> selected"""

        for t in (self.todo.find("id", idx) for idx in self.tasks_selected):
            print self._disp(t)

        # Show number of tasks as result of search if more than 10
        if len(self.tasks_selected) > 10:
            print "%d selected tasks found" % len(self.tasks_selected)

    #
    # Re-search.
    #

    def do_search(self, regexp):
        """Retrieve tasks matching given regexp:
        GTD> search regexp"""

        self._search(regexp)

    do_s = do_search

    def do_searchall(self, regexp):
        """Retrieve tasks matching given regexp, including completed ones:
        GTD> searchall regexp"""

        self._search(regexp, completed=True)

    do_sa = do_searchall

    #
    # IO functions.
    #

    def do_load(self, todotxt):
        """Load from a todotxt file:
        GTD> load path/to/todo.txt"""

        self.todotxt = todotxt  # ok, save file path
        try:
            f = open(self.todotxt, 'r')
            try:
                self.todo.erase()  # clean
                nb = 0
                for line in f:
                    #print line,
                    if line.lstrip().startswith('#'): continue
                    self._add(line.strip())
                    nb += 1
            finally:
                f.close()
                print "%d tasks loaded from '%s'" % (nb, todotxt)
        except IOError, err:
            # 'file not found' exception?
            if err.errno != errno.ENOENT: raise  # raise all others
            print err  # and continue (new file created)
Beispiel #5
0
class GTD(cmd.Cmd):
    """GTD(cmd.Cmd) class."""
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.intro = """
  yaGTD is a free software available under the terms of the GNU GPL.
  Refer to the file COPYING (which should be included in this distribution)
  for the specific terms of this licence.
  """
        self.prompt = "GTD> "
        self.todo = ToDo()  # the to-do list is here!

        self.todotxt = TODO_TXT

        self.colorize = False

    #
    # Private functions.
    #

    def _parse_args(self, args):
        """Parse command arguments= num + str."""

        num = string = None

        # Parse command args
        n = re.search(r"^(?x)(\d+)", args)  # num
        s = re.search(r" +([@!:\-\w ]+)$", args)  # string

        if n: num = int(n.group(1))
        if s: string = s.group(1)

        #print "parsed", num, string
        return (num, string)

    def _colorize(self, string):
        """Colorize the given 'string'."""

        s = string

        if self.colorize:
            # Scan to colorize attributes (accordingly to global settings) ...
            for attr in Task.attributes_list[3:]:
                color_fct = lambda m: eval(attr.upper() + '_COLOR') + m.group(
                    0) + DEFAULT_COLOR
                s = eval(attr.upper() + '_REGEXP').sub(color_fct, s)

        return s

    def _parse_line(self, line):
        """Return a dictionary (task mapping) from 'line' parsing."""

        t = {}  # Task mapping
        title = line  # the 'title' extracted from line

        # Parse for GTD attributes
        for attr in ['context', 'project', 'status', 'reference']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                t[attr] = matches

        # Parse additional properties
        for attr in ['urgency', 'importance', 'complete']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                t[attr] = int(matches[-1])  # keep only last!

        # Parse timedelta
        for attr in ['time', 'recurrence']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                match = matches[-1]  # keep only last!
                hours = minutes = 0  # compute hours

                if attr == 'time':  # compute time requiered (in working hours)
                    if match[1].upper() == 'W':  # weeks
                        hours = int(match[0]) * gtd.WEEK_IN_HOURS
                    elif match[1].upper() == 'D':  # days
                        hours = int(match[0]) * gtd.DAY_IN_HOURS
                    elif match[1].upper() == 'H':  # hours
                        hours = int(match[0])
                    elif match[1].upper() == 'M':  # minutes
                        minutes = int(match[0])
                    else:
                        pass  # invalid time range indicator

                elif attr == 'recurrence':  # compute full hours
                    if match[1].upper() == 'W':  # weeks
                        hours = int(match[0]) * 7 * 24
                    elif match[1].upper() == 'D':  # days
                        hours = int(match[0]) * 24
                    elif match[1].upper() == 'H':  # hours
                        hours = int(match[0])
                    elif match[1].upper() == 'M':  # minutes
                        minutes = int(match[0])
                    else:
                        pass  # invalid time range indicator

                t[attr] = datetime.timedelta(hours=hours, minutes=minutes)

        # Parse dates
        for attr in ['start', 'due', 'end']:
            title = eval(attr.upper() + '_REGEXP').sub('', title)

            matches = eval(attr.upper() + '_REGEXP').findall(line)
            if matches:
                year, month, day = matches[-1].split('-')  # keep only last!
                t[attr] = datetime.datetime(int(year), int(month), int(day))

        # Post-processing
        if t.has_key('end') or t.has_key(
                'reference'):  # ignore completed and archived tasks
            t['complete'] = 100

        # Set the title
        t['title'] = " ".join(title.split())  # remove useless blank chars too

        return t

    def _dump_line(self, task):
        """Return a formatted line from the given 'task'."""

        s = task['title']  # init the line with the title

        # Dump GTD attributes
        for attr in ['context', 'project', 'status', 'reference']:
            if task.has_key(attr):
                for value in task[attr]:
                    s += " " + eval(attr.upper() + '_CHAR') + value

        # Dump additional properties
        for attr in ['urgency', 'importance', 'complete']:
            if task.has_key(attr) and task[attr]:
                s += " " + eval(attr.upper() + '_CHAR') + str(task[attr])

        # Parse timedelta
        for attr in ['time', 'recurrence']:
            if task.has_key(attr) and task[attr]:
                hours = task[attr].days * 24 + task[attr].seconds / 3600
                if hours > 0:
                    s += " " + eval(attr.upper() + '_CHAR') + str(hours) + "H"
                else:  # less than 1 hour!
                    minutes = task[attr].seconds / 60
                    s += " " + eval(attr.upper() +
                                    '_CHAR') + str(minutes) + "M"

        # Parse dates
        for attr in ['start', 'due', 'end']:
            if task.has_key(attr) and task[attr]:
                s += " " + eval(attr.upper() +
                                '_CHAR') + task[attr].strftime("%Y-%m-%d")

        return s

    def _disp(self, task):
        """Display the 'id' and a summary of the 'task'."""

        task_line = task['title']
        if __debug__:
            task_line = self._dump_line(task)

        id_str = "%3d:" % task['id']
        if __debug__:
            id_str = "%3d:(%f)" % (task['id'], task.priority())
        if self.colorize:  # colorize #id
            id_str = COLOR_CODES['cyan'] + id_str + DEFAULT_COLOR

        return "%s %s" % (id_str, self._colorize(task_line))

    def _show(self, task):
        """Display details of the given 'task'."""

        s = ""
        for attrib in Task.attributes_list[1:]:
            if task.has_key(attrib) and task[attrib]:
                if s: s = s + "\n"
                s = s + attrib.capitalize() + ": "
                s = s + str(task[attrib])
        return s

    def _add(self, line):
        """Adds task from the given line and returns the new task_id."""

        if line:
            # Create the new task
            task = Task()

            # Parse input line for GTD and additional attributes
            task.add(**self._parse_line(line))

            # And, add it to the to-do list
            task_id = self.todo.add(task)

            # And, set the start date
            self.do_append(
                "%d S:%s" %
                (task_id, datetime.datetime.now().strftime("%Y-%m-%d")))

            return task_id

    def _replace(self, idx, line):
        """Replace the task given by its idx by the new line."""

        # Frist, we need to find the task
        task = self.todo.find('id', idx)
        if task:
            i = self.todo.index(task)

            task = Task()  # create a new task

            # Parse input line for GTD and additional attributes
            task.add(**self._parse_line(line))
            task.add(id=idx)

            # And, replace it into the to-do list
            self.todo[i] = task

    def _search(self, regexp, completed=False):
        """Retrieve tasks matching given regexp.
        If completed True, also look for completed tasks."""

        # Rk: improved search function that retrieve regexp
        #     over all tasks attributes

        if completed:
            todos = [t for t in self.todo.sort()]  # first sort by priority
        else:
            todos = [t for t in self.todo.sort() if t['complete'] < 100]

        # Then, retrieve the pattern (into full desc tasks)
        expr = re.compile(regexp, re.IGNORECASE)
        tasks = [t for t in todos if expr.search(self._dump_line(t))]

        for t in tasks:
            print self._disp(t)

    #
    # Edition.
    #

    def do_add(self, line):
        """Add a new task (from the input line)."""

        task_id = self._add(line)
        if task_id:
            print "Added task #%d" % task_id

    def do_del(self, id):
        """Delete task given by #id."""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.todo.supp(idx)

    do_rm = do_del

    def do_edit(self, id):
        """Interactively edit task given by #id."""

        try:
            import readline
        except ImportError:
            print "Cannot edit without the 'readline' module!"
            return

        # Parse command line
        idx = self._parse_args(id)[0]

        if not idx:
            return

        task = self.todo.find('id', idx)
        if not task:
            return

        def pre_input_hook():
            readline.insert_text(self._dump_line(task))
            readline.redisplay()

            # Unset the hook again
            readline.set_pre_input_hook(None)

        readline.set_pre_input_hook(pre_input_hook)

        line = raw_input("GTD edit> ")
        # Remove edited line from history:
        #   oddly, get_history_item is 1-based,
        #   but remove_history_item is 0-based
        readline.remove_history_item(readline.get_current_history_length() - 1)
        self._replace(idx, line)

    def do_close(self, id):
        """Close the given task (#id)."""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            # First, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                if task['recurrence']:
                    task['start'] = task['due']  # reinit start date
                    task['due'] = task['start'] + task['recurrence']
                else:
                    task['end'] = datetime.datetime.now()
                    task['complete'] = 100

    do_done = do_close

    def do_replace(self, id_line):
        """Replace the entire task #id by a new one (line)."""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            self._replace(idx, line)

    do_sub = do_replace

    def do_extend(self, id_desc):
        """Add more text (description) to task #id."""

        # Parse command line
        idx, desc = self._parse_args(id_desc)

        if idx and desc:
            task = self.todo.find('id', idx)
            if task:
                task[
                    'title'] += " " + desc  # TODO: allow to attach additional notes

    do_notes = do_extend

    def do_append(self, id_line):
        """Add new elements (line) to task #id but leave existing elemenents unchanged."""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            # Frist, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                # Parse additional input line
                attrs = self._parse_line(line)

                # Then, modify the task
                for attr, value in attrs.items():
                    if value and not task[attr]:  # not override!
                        task[attr] = value

    def do_modify(self, id_line):
        """Add/change elements (line) of task #id but leave each other unchanged."""

        # Parse command line
        idx, line = self._parse_args(id_line)

        if idx and line:
            # Frist, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                # Parse additional input line
                attrs = self._parse_line(line)

                # Then, modify the task
                for attr, value in attrs.items():
                    if value:  # override!
                        task[attr] = value

    def do_archive(self, line):
        """Archive completed tasks."""

        print "Not Yet Implemented!"

    #
    # Specific GTD commands.
    #

    def do_someday(self, id):
        """Mark the given task (#id) as @someday."""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.do_modify("%d %s" % (idx, "@someday"))

    do_maybe = do_someday

    def do_waitingfor(self, id):
        """Mark the given task (#id) as @waitingfor."""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            self.do_modify("%d %s" % (idx, "@waitingfor"))

    def do_ref(self, id_ref):
        """Archive the given task (#id) as Reference."""

        # Parse command line
        idx, reference = self._parse_args(id_ref)

        if idx:
            self.do_append("%d ref:%s" % (idx, reference))

    #
    # Sorted/ordered display.
    #

    def do_listall(self, nb):
        """List #nb tasks (ordered by #id)."""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = self.todo
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

    do_la = do_listall

    def do_list(self, nb):
        """List #nb open tasks (ordered by #id)."""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [t for t in self.todo
                 if t['complete'] < 100]  # filter done/close tasks
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

    do_ls = do_list

    def do_listref(self, nb):
        """List #nb ref tasks (ordered by #id)."""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [
            t for t in self.todo if t.has_key('reference') and t['reference']
        ]  # filter ref tasks
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

    do_lr = do_listref

    def do_show(self, id):
        """Show details of the given task #id."""

        # Parse command line
        idx = self._parse_args(id)[0]

        if idx:
            # Frist, we need to find the task
            task = self.todo.find('id', idx)
            if task:
                print self._show(task)

    def do_sort(self, nb):
        """Sort #nb tasks by priority."""

        # Parse command line
        nb = self._parse_args(nb)[0]

        tasks = [t for t in self.todo.sort() if t['complete'] < 100]
        if nb:  # display only nb tasks
            tasks = tasks[:nb]

        for t in tasks:
            print self._disp(t)

    do_listpri = do_sort

    def do_order(self, nb_attr):
        """Order #nb tasks by context/project/status/reference (and priority)."""

        # Parse command line
        nb, attr = self._parse_args(nb_attr)

        if not attr or attr not in [
                'context', 'project', 'status', 'reference'
        ]:
            attr = 'context'  # by default order by context

        for c, ts in self.todo.order(attr).items():
            # Section title
            print self._colorize(eval(attr.upper() + '_CHAR') + c.capitalize())

            if attr == 'reference':
                tasks = [t for t in ts]  # even if completed!
            else:
                tasks = [t for t in ts if t['complete'] < 100]
            if nb: tasks = tasks[:nb]  # display only nb tasks
            for t in tasks:
                print self._disp(t)

    def do_status(self, line):
        """Display projects statuses (percent complete)."""

        for p, ts in self.todo.order('project').items():
            # Thomas: don't show completed projects

            percent = 0  # compute project's percent complete
            next = ""
            for t in ts:
                if t['complete']:
                    percent += int(t['complete'])
                # Thomas: find next action
                if t['status'] and not t['complete'] and 'next' in t['status']:
                    next = " !next: %s" % t['title']

            percent /= len(ts)
            if percent == 100:
                continue
            # Section title
            print self._colorize(PROJECT_CHAR + p.capitalize()),

            print "%d%%%s" % (percent, self._colorize(next))

    do_summary = do_status

    #
    # Re-search.
    #

    def do_search(self, regexp):
        """Retrieve tasks matching given regexp."""

        self._search(regexp)

    def do_searchall(self, regexp):
        """Retrieve tasks matching given regexp, including completed ones."""

        self._search(regexp, completed=True)

    #
    # IO functions.
    #

    def do_load(self, todotxt):
        """Load from a todotxt file."""

        try:
            f = open(todotxt, 'r')
            try:
                self.todo.erase()  # clean
                nb = 0
                for line in f:
                    #print line,
                    if line.lstrip().startswith('#'): continue
                    self._add(line.strip())
                    nb += 1
            finally:
                f.close()
                self.todotxt = todotxt  # ok, save file path
                print "%d tasks loaded from '%s'" % (nb, todotxt)
        except IOError, err:
            # 'file not found' exception?
            if err.errno != errno.ENOENT: raise  # raise all others
            print err  # and continue