# HG changeset patch # User Eugen Sawin # Date 1285856354 -7200 # Node ID d1c9dec8b0592302a9ad495d2f6985eacd2f5af5 # Parent 918f055663658d229c51fe106d83c0f4852b2f85 More sensible init procedure. diff -r 918f05566365 -r d1c9dec8b059 cronrec.py --- a/cronrec.py Thu Sep 30 03:52:34 2010 +0200 +++ b/cronrec.py Thu Sep 30 16:19:14 2010 +0200 @@ -3,25 +3,42 @@ """ A time recording tool. Author: Eugen Sawin (sawine@me73.com) +Dependencies: libsqlite3-dev """ import os +import sys import argparse +from datetime import datetime +import sqlite3 WD = "working_path" CONFIG = {WD: str} CONFIG_SEP = "=" +DB_FILE = "cronrec.db" config = {} +# Trying to get the user's home directory path try: # Windows from win32com.shell import shellcon, shell HOMEDIR = shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA, 0, 0) -except ImportError: # Linux +except ImportError: # Linux (hopefully) HOMEDIR = os.path.expanduser("~") CONFIG_FILE = "%s/.cronrecrc" % HOMEDIR +DEF_PROJECT = "default" +DEF_ACTIVITY = "default" + +def db_session(): + global config + if WD not in config: + print "Error: working directory is not configured. Use init command." + sys.exit() + con = sqlite3.connect(config[WD] + "/" + DB_FILE) + return con, con.cursor() + def read_config(): config = {} with open(CONFIG_FILE, "r") as config_stream: @@ -39,40 +56,78 @@ config_input.write("\n".join([CONFIG_SEP.join((k, v)) for k, v in config.iteritems() if k in CONFIG])) +def db_init(db_file): + con, cur = db_session() + sql = "create table projects()" + def init(args): + global config last_wd = None if WD in config: last_wd = config[WD] - config[WD] = args.dbpath + config[WD] = args.working_path write_config(config) - if last_wd != config[WD]: - print "Changed working directory from %s to %s" % (last_wd, config[WD]) - elif not last_wd: - print "Set new working directory to %s" % (config[WD]) + path_exists = os.path.exists(config[WD]) and os.path.isdir(config[WD]) + db_file = config[WD] + "/" + DB_FILE + db_exists = path_exists and os.path.exists(db_file) + and os.path.isfile(db_file) + if last_wd != config[WD]: + print "Changed working directory from %s to %s." % (last_wd, config[WD]) else: - print "Working directory remains at %s" % config[WD] + print "Working directory remains at %s." % config[WD] + if not path_exists: + print "Warning: working directory %s does not exist." % config[WD] + os.makedirs(config[WD]) + print "Working directory %s created." % config[WD] + elif db_exists: + print "Database file %s already exists, please delete it before\ + initiating a new database at this location." % db_file + if not db_exists: + db_init(db_file) + +def db_begin(project, activity, time): + con, cur = db_session() + cur.execute("") + con.commit() + cur.close() def begin(args): - print "begins" + project = None + activity = None + label = args.label.strip() + if ":" in label: + project, activity = label.split(":") + else: + project = label + if not project: + project = DEF_PROJECT + if not activity: + activity = DEF_ACTIVITY + db_begin(project, activity, datetime.now()) + print "begins %s:%s" % (project, activity) def end(args): print "ends" - -if __name__ == "__main__": + +def main(): + global config config = read_config() parser = argparse.ArgumentParser(description="Records time.") subs = parser.add_subparsers() sub_begin = subs.add_parser("init") - sub_begin.add_argument("dbpath") + sub_begin.add_argument("working_path", type=str) sub_begin.set_defaults(func=init) sub_begin = subs.add_parser("begin") - sub_begin.add_argument("label") + sub_begin.add_argument("label", type=str) sub_begin.set_defaults(func=begin) sub_end = subs.add_parser("end") - sub_end.add_argument("label") + sub_end.add_argument("label", type=str) sub_end.set_defaults(func=end) args = parser.parse_args() args.func(args) + +if __name__ == "__main__": + main()