Fixed major issues with add command. Added automatical deduction of last active project.
5 Author: Eugen Sawin (sawine@me73.com)
6 Dependencies: libsqlite3-dev
12 from datetime import datetime
19 DB_FILE = "cronrec.db"
23 # Trying 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(":")
89 db.begin(db_file(), project, activity, datetime.now())
90 print "begins %s:%s" % (project, activity)
97 config = read_config()
98 parser = argparse.ArgumentParser(description="Records time.")
99 subs = parser.add_subparsers()
100 sub_begin = subs.add_parser("init")
101 sub_begin.add_argument("working_path", type=str)
102 sub_begin.set_defaults(func=init)
104 sub_begin = subs.add_parser("begin")
105 sub_begin.add_argument("label", type=str)
106 sub_begin.set_defaults(func=begin)
108 sub_end = subs.add_parser("end")
109 sub_end.add_argument("label", type=str)
110 sub_end.set_defaults(func=end)
112 args = parser.parse_args()
115 if __name__ == "__main__":