# HG changeset patch # User Eugen Sawin # Date 1285945770 -7200 # Node ID 9d0e6ae739cf7b37b22833e8c841ed5566c34873 # Parent 5e41c4b578da1260454865f11dc46cbbda5b4823 Added basic end command. diff -r 5e41c4b578da -r 9d0e6ae739cf cronrec.py --- a/cronrec.py Fri Oct 01 01:55:35 2010 +0200 +++ b/cronrec.py Fri Oct 01 17:09:30 2010 +0200 @@ -90,7 +90,15 @@ print "begins %s:%s" % (project, activity) def end(args): - print "ends" + project = None + activity = None + label = args.label.strip() + if ":" in label: + project, activity = label.split(":") + else: + project = label + db.end(db_file(), project, activity, datetime.now()) + print "ends %s:%s" % (project, activity) def main(): global config diff -r 5e41c4b578da -r 9d0e6ae739cf db.py --- a/db.py Fri Oct 01 01:55:35 2010 +0200 +++ b/db.py Fri Oct 01 17:09:30 2010 +0200 @@ -71,23 +71,42 @@ con.commit() cur.close() -def last_project(db_file, time): +def find_last_project(db_file, time): con, cur = session(db_file) values = (time,) - sql = "select project from tasks where begin < ? order by begin desc" + sql = "select project from tasks where begin < ? and end is null order by \ +begin desc" cur.execute(sql, values) id = cur.fetchone() if id: values = (id[0],) sql = "select name from projects where rowid=?" cur.execute(sql, values) - project = cur.fetchone()[0] + project = cur.fetchone()[0] else: project = DEF_PROJECT con.commit() cur.close() return project +def find_last_activity(db_file, time): + con, cur = session(db_file) + values = (time,) + sql = "select activity from tasks where begin < ? and end is null order by \ +begin desc" + cur.execute(sql, values) + id = cur.fetchone() + if id: + values = (id[0],) + sql = "select name from activities where rowid=?" + cur.execute(sql, values) + activity = cur.fetchone()[0] + else: + activity = DEF_ACTIVITY + con.commit() + cur.close() + return activity + def row_id(db_file, table, name): con, cur = session(db_file) values = (name,) @@ -110,11 +129,30 @@ def activity_id(db_file, name): return row_id(db_file, "activities", name) +def end(db_file, project, activity, time): + if not activity: + activity = find_last_activity(db_file, time) + if not project: + project = find_last_project(db_file, time) + con, cur = session(db_file) + values = (time, project_id(db_file, project), activity_id(db_file, activity)) + sql = "update tasks set end=? where project=? and activity=?" + cur.execute(sql, values) + con.commit() + cur.execute("select * from tasks") + for c in cur: + print c + cur.close() + def begin(db_file, project, activity, time): + last_project = find_last_project(db_file, time) + last_activity = find_last_activity(db_file, time) if not activity: activity = DEF_ACTIVITY if not project: - project = last_project(db_file, time) + project = last_project + if activity != last_activity or project != last_project: + end(db_file, project, last_activity, time) con, cur = session(db_file) values = (time, None, project_id(db_file, project), activity_id(db_file, activity), None)