Snippets
Dimitar Trendafilov Unschedule - http://www.lifeclever.com/how-to-unschedule-your-work-and-enjoy-guilt-free-play/
Created by
Dimitar Trendafilov
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #!/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)
You can clone a snippet to your computer for local editing. Learn more.