More sensible init procedure.
1.1 --- a/cronrec.py Thu Sep 30 03:52:34 2010 +0200
1.2 +++ b/cronrec.py Thu Sep 30 16:19:14 2010 +0200
1.3 @@ -3,25 +3,42 @@
1.4 """
1.5 A time recording tool.
1.6 Author: Eugen Sawin (sawine@me73.com)
1.7 +Dependencies: libsqlite3-dev
1.8 """
1.9
1.10 import os
1.11 +import sys
1.12 import argparse
1.13 +from datetime import datetime
1.14 +import sqlite3
1.15
1.16 WD = "working_path"
1.17 CONFIG = {WD: str}
1.18 CONFIG_SEP = "="
1.19 +DB_FILE = "cronrec.db"
1.20
1.21 config = {}
1.22
1.23 +# Trying to get the user's home directory path
1.24 try: # Windows
1.25 from win32com.shell import shellcon, shell
1.26 HOMEDIR = shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA, 0, 0)
1.27 -except ImportError: # Linux
1.28 +except ImportError: # Linux (hopefully)
1.29 HOMEDIR = os.path.expanduser("~")
1.30
1.31 CONFIG_FILE = "%s/.cronrecrc" % HOMEDIR
1.32
1.33 +DEF_PROJECT = "default"
1.34 +DEF_ACTIVITY = "default"
1.35 +
1.36 +def db_session():
1.37 + global config
1.38 + if WD not in config:
1.39 + print "Error: working directory is not configured. Use init command."
1.40 + sys.exit()
1.41 + con = sqlite3.connect(config[WD] + "/" + DB_FILE)
1.42 + return con, con.cursor()
1.43 +
1.44 def read_config():
1.45 config = {}
1.46 with open(CONFIG_FILE, "r") as config_stream:
1.47 @@ -39,40 +56,78 @@
1.48 config_input.write("\n".join([CONFIG_SEP.join((k, v))
1.49 for k, v in config.iteritems() if k in CONFIG]))
1.50
1.51 +def db_init(db_file):
1.52 + con, cur = db_session()
1.53 + sql = "create table projects()"
1.54 +
1.55 def init(args):
1.56 + global config
1.57 last_wd = None
1.58 if WD in config:
1.59 last_wd = config[WD]
1.60 - config[WD] = args.dbpath
1.61 + config[WD] = args.working_path
1.62 write_config(config)
1.63 - if last_wd != config[WD]:
1.64 - print "Changed working directory from %s to %s" % (last_wd, config[WD])
1.65 - elif not last_wd:
1.66 - print "Set new working directory to %s" % (config[WD])
1.67 + path_exists = os.path.exists(config[WD]) and os.path.isdir(config[WD])
1.68 + db_file = config[WD] + "/" + DB_FILE
1.69 + db_exists = path_exists and os.path.exists(db_file)
1.70 + and os.path.isfile(db_file)
1.71 + if last_wd != config[WD]:
1.72 + print "Changed working directory from %s to %s." % (last_wd, config[WD])
1.73 else:
1.74 - print "Working directory remains at %s" % config[WD]
1.75 + print "Working directory remains at %s." % config[WD]
1.76 + if not path_exists:
1.77 + print "Warning: working directory %s does not exist." % config[WD]
1.78 + os.makedirs(config[WD])
1.79 + print "Working directory %s created." % config[WD]
1.80 + elif db_exists:
1.81 + print "Database file %s already exists, please delete it before\
1.82 + initiating a new database at this location." % db_file
1.83 + if not db_exists:
1.84 + db_init(db_file)
1.85 +
1.86 +def db_begin(project, activity, time):
1.87 + con, cur = db_session()
1.88 + cur.execute("")
1.89 + con.commit()
1.90 + cur.close()
1.91
1.92 def begin(args):
1.93 - print "begins"
1.94 + project = None
1.95 + activity = None
1.96 + label = args.label.strip()
1.97 + if ":" in label:
1.98 + project, activity = label.split(":")
1.99 + else:
1.100 + project = label
1.101 + if not project:
1.102 + project = DEF_PROJECT
1.103 + if not activity:
1.104 + activity = DEF_ACTIVITY
1.105 + db_begin(project, activity, datetime.now())
1.106 + print "begins %s:%s" % (project, activity)
1.107
1.108 def end(args):
1.109 print "ends"
1.110 -
1.111 -if __name__ == "__main__":
1.112 +
1.113 +def main():
1.114 + global config
1.115 config = read_config()
1.116 parser = argparse.ArgumentParser(description="Records time.")
1.117 subs = parser.add_subparsers()
1.118 sub_begin = subs.add_parser("init")
1.119 - sub_begin.add_argument("dbpath")
1.120 + sub_begin.add_argument("working_path", type=str)
1.121 sub_begin.set_defaults(func=init)
1.122
1.123 sub_begin = subs.add_parser("begin")
1.124 - sub_begin.add_argument("label")
1.125 + sub_begin.add_argument("label", type=str)
1.126 sub_begin.set_defaults(func=begin)
1.127
1.128 sub_end = subs.add_parser("end")
1.129 - sub_end.add_argument("label")
1.130 + sub_end.add_argument("label", type=str)
1.131 sub_end.set_defaults(func=end)
1.132
1.133 args = parser.parse_args()
1.134 args.func(args)
1.135 +
1.136 +if __name__ == "__main__":
1.137 + main()