Fixed end and resume issues. Added logging functions.
1 #!/usr/local/bin/python
4 tim - A time recording tool, because time is money.
5 Author: Eugen Sawin (sawine@me73.com)
6 Dependencies: Python 2.7 and libsqlite3-dev
12 from datetime import datetime
19 DB_FILE = "cronrec.db"
23 # Try to get the user's home directory path
25 from win32com.shell import shellcon, shell
26 HOMEDIR = shell.SHGetFolderPath(0, shellcon.CSIDL_LOCAL_APPDATA, 0, 0)
27 except ImportError: # Linux (hopefully)
28 HOMEDIR = os.path.expanduser("~")
30 CONFIG_FILE = "%s/.cronrecrc" % HOMEDIR
34 with open(CONFIG_FILE, "r") as config_stream:
35 config_lines = [l.split(CONFIG_SEP) for l in config_stream.readlines()
37 for key, value in config_lines:
38 key = key.strip().lower()
41 config[key] = CONFIG[key](value)
44 def write_config(config):
45 with open(CONFIG_FILE, "w") as config_input:
46 config_input.write("\n".join([CONFIG_SEP.join((k, v))
47 for k, v in config.iteritems() if k in CONFIG]))
52 print "Working directory path is not configured. \
53 Please use the init command."
55 return config[WD] + "/" + DB_FILE
62 config[WD] = args.working_path
64 path_exists = os.path.exists(config[WD]) and os.path.isdir(config[WD])
65 db_exists = path_exists and os.path.exists(db_file())\
66 and os.path.isfile(db_file())
67 if last_wd != config[WD]:
68 print "Changed working directory from %s to %s." % (last_wd, config[WD])
70 print "Working directory remains at %s." % config[WD]
72 print "Warning: working directory %s does not exist." % config[WD]
73 os.makedirs(config[WD])
74 print "Working directory %s created." % config[WD]
76 print "Database file %s already exists, please delete it before \
77 initiating a new database at this location." % db_file()
84 label = args.label.strip()
86 project, activity = label.split(":")
90 db.resume(db_file(), None, None, time)
91 db.begin(db_file(), project, activity, time)
96 label = args.label.strip()
99 project, activity = label.split(":")
102 time = datetime.now()
103 db.resume(db_file(), db.find_active_task(db_file()), time, log)
104 db.end(db_file(), project, activity, time, log)
107 db.pause(db_file(), None, None, datetime.now())
110 db.resume(db_file(), None, None, datetime.now(), args.m)
113 task = db.status(db_file())
115 print "Currently active task"
116 print "Project: %s Activity: %s" % task
118 print "There is no active task."
122 config = read_config()
123 parser = argparse.ArgumentParser(description="Records time.")
124 subs = parser.add_subparsers()
125 sub_begin = subs.add_parser("init")
126 sub_begin.add_argument("working_path", type=str)
127 sub_begin.set_defaults(func=init)
129 sub_begin = subs.add_parser("begin")
130 sub_begin.add_argument("label", type=str)
131 sub_begin.set_defaults(func=begin)
133 sub_end = subs.add_parser("end")
134 sub_end.add_argument("label", type=str, nargs="?", default="all:all")
135 sub_end.add_argument("-m", type=str)
136 sub_end.set_defaults(func=end)
138 sub_end = subs.add_parser("pause")
139 sub_end.set_defaults(func=pause)
141 sub_end = subs.add_parser("resume")
142 sub_end.add_argument("-m", type=str)
143 sub_end.set_defaults(func=resume)
145 sub_end = subs.add_parser("status")
146 sub_end.set_defaults(func=status)
148 args = parser.parse_args()
151 if __name__ == "__main__":