Skip to content

Instantly share code, notes, and snippets.

@5263
Created July 13, 2012 16:46
Show Gist options
  • Save 5263/3105917 to your computer and use it in GitHub Desktop.
Save 5263/3105917 to your computer and use it in GitHub Desktop.
start and end hamster activities by manipulationg the DB
#!/usr/bin/env python
import sqlite3,datetime,sys
def buildactdict(c):
c.execute('SELECT id,name from categories')
catdict=dict(((cat[0], cat[1]) for cat in c.fetchall()))
c.execute('SELECT id,name,category_id from activities')
actdict=dict(((act[0], '%s@%s' % (act[1],catdict.get(act[2]))) \
for act in c.fetchall()))
return actdict
def findunfinished(c):
c.execute('SELECT id from facts WHERE end_time is NULL')
return [i[0] for i in c.fetchall()]
def factdetails(c,uid):
c.execute('SELECT * FROM facts WHERE id =?',(uid,))
t1=c.fetchone()
return uid, buildactdict(c)[t1[1]],(t1[3] or datetime.datetime.now())-t1[2]
def endfact(c,uid):
c.execute("UPDATE facts SET 'end_time' = ? WHERE id=?",\
(datetime.datetime.now(),uid))
def startact(c,actid,desc=None):
c.execute(\
"""INSERT INTO "facts" (activity_id,start_time,end_time,description)
VALUES(?,?,NULL,?)""",(actid,datetime.datetime.now(),desc))
conn = sqlite3.connect('.local/share/hamster-applet/hamster.db',detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
unfinished= findunfinished(c)
if unfinished:
print 'running:'
for uid in unfinished:
print factdetails(c,uid)
if len(sys.argv) >1:
if sys.argv[1] == 'close':
endfact(c,int(sys.argv[2]))
elif sys.argv[1] == 'closeall':
for uid in unfinished:
endfact(c,uid)
elif sys.argv[1] == 'activities':
for pair in buildactdict(c).iteritems():
print '%3d %s' % pair
elif sys.argv[1] == 'start':
if len(sys.argv) >=4:
desc=sys.argv[3]
else:
desc=None
startact(c,int(sys.argv[2]),desc)
else:
print arg
conn.commit()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment