Snippets

Dimitar Trendafilov Unschedule - http://www.lifeclever.com/how-to-unschedule-your-work-and-enjoy-guilt-free-play/

Created by Dimitar Trendafilov
#!/usr/bin/env python

from __future__ import print_function
import cPickle
import datetime
import os
import os.path


class Unschedule:
    def __init__(self, dbname):
        self._dbname = dbname
        self._load()

    def _load(self):
        if os.path.isfile(self._dbname):
            with open(self._dbname, 'rb') as db:
                self._db = cPickle.load(db)
        else:
            self._db = []

    def _store(self):
        with open(self._dbname, 'wb') as db:
            cPickle.dump(self._db, db, cPickle.HIGHEST_PROTOCOL)

    def _current(self):
        for i in range(len(self._db)):
            if self._db[i][3] == 'started':
                return i
        return -1

    def start(self, subject):
        assert(self._current() == -1)
        self._db.append((datetime.datetime.now(), None, subject, 'started'))
        self._store()

    def _set_status(self, status):
        c = self._current()
        assert(c != -1)
        pomodoro = self._db[c]
        self._db[c] = (pomodoro[0], datetime.datetime.now(), pomodoro[2],
                       status)
        self._store()
        return self._db[c][1] - self._db[c][0], pomodoro[2]

    def finish(self):
        time, subject = self._set_status('done')
        print('Good {0} on {1}'.format(time, subject))

    def cancel(self):
        self._set_status('canceled')

    def restart(self):
        time, subject = self._set_status('canceled')
        self.start(subject)

    def _tocalendar(self, pomodoro):
        return (pomodoro[2],
                pomodoro[0].date(), pomodoro[0].time().strftime('%H:%M:%S'),
                pomodoro[1].date(), pomodoro[1].time().strftime('%H:%M:%S'),
                'False', 'True')

    def calendar(self, output):
        import csv
        with open(output, 'wb') as csvfile:
            csvfile.write(('Subject,Start Date,Start Time,End Date,End Time,'
                           'All Day Event,Private\r\n'))
            writer = csv.writer(csvfile)
            done = (p for p in self._db if p[3] == 'done')
            for p in done:
                writer.writerow(self._tocalendar(p))

    def spreadsheet(self, output):
        import csv
        with open(output, 'wb') as csvfile:
            csvfile.write('Start,End,Subject,Status\r\n')
            writer = csv.writer(csvfile)
            for p in self._db:
                writer.writerow(p)

    def clear(self):
        os.remove(self._dbname)

    def list(self):
        for p in self._db:
            end = p[1] if p[1] else datetime.datetime.now()
            print('{1} {2} {4:^10} {0:^32} {3}'.format(end - p[0], *p))

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description="unschedule")
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-s', '--start', metavar='subject', dest='start',
                       help='start a new pomodoro', default=None)
    group.add_argument('-f', '--finish', action='store_const', const='finish',
                       help='finish the last pomodoro', dest='action')
    group.add_argument('-c', '--cancel', action='store_const', const='cancel',
                       help='cancel the last pomodoro', dest='action')
    group.add_argument('-r', '--restart', action='store_const', const='restart',
                       help='restart the last pomodoro', dest='action')
    group.add_argument('-l', '--list', action='store_const', const='list',
                       help='list all pomodori', dest='action')
    group.add_argument('--calendar', metavar='csv', dest='calendar',
                       help='dump the log for Google Calendar', default=None)
    group.add_argument('--sheet', metavar='csv', dest='sheet',
                       help='dump the log for spreadsheet', default=None)
    group.add_argument('--clear', action='store_const', const='clear',
                       help='clear the log file')

    parser.add_argument('--db', metavar='path', dest='db',
                        help='path to the db file',
                        default=os.path.expanduser('~/.unschedule.pickle'))

    options = parser.parse_args()
    unschedule = Unschedule(options.db)

    if options.start:
        unschedule.start(options.start)
    elif options.action == 'finish':
        unschedule.finish()
    elif options.action == 'cancel':
        unschedule.cancel()
    elif options.action == 'restart':
        unschedule.restart()
    elif options.action == 'list':
        unschedule.list()
    elif options.action == 'clear':
        unschedule.clear()
    elif options.calendar:
        unschedule.calendar(options.calendar)
    elif options.sheet:
        unschedule.spreadsheet(options.sheet)
    else:
        parser.print_usage()

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.