Moved database function to the db lib. Finished draft of the add command.
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
32 DEF_PROJECT = "default"
33 DEF_ACTIVITY = "default"
37 with open(CONFIG_FILE, "r") as config_stream:
38 config_lines = [l.split(CONFIG_SEP) for l in config_stream.readlines()
40 for key, value in config_lines:
41 key = key.strip().lower()
44 config[key] = CONFIG[key](value)
47 def write_config(config):
48 with open(CONFIG_FILE, "w") as config_input:
49 config_input.write("\n".join([CONFIG_SEP.join((k, v))
50 for k, v in config.iteritems() if k in CONFIG]))
55 print "Working directory path is not configured. \
56 Please use the init command."
58 return config[WD] + "/" + DB_FILE
65 config[WD] = args.working_path
67 path_exists = os.path.exists(config[WD]) and os.path.isdir(config[WD])
68 db_exists = path_exists and os.path.exists(db_file())\
69 and os.path.isfile(db_file())
70 if last_wd != config[WD]:
71 print "Changed working directory from %s to %s." % (last_wd, config[WD])
73 print "Working directory remains at %s." % config[WD]
75 print "Warning: working directory %s does not exist." % config[WD]
76 os.makedirs(config[WD])
77 print "Working directory %s created." % config[WD]
79 print "Database file %s already exists, please delete it before \
80 initiating a new database at this location." % db_file()
87 label = args.label.strip()
89 project, activity = label.split(":")
95 activity = DEF_ACTIVITY
96 db.begin(db_file(), project, activity, datetime.now())
97 print "begins %s:%s" % (project, activity)
104 config = read_config()
105 parser = argparse.ArgumentParser(description="Records time.")
106 subs = parser.add_subparsers()
107 sub_begin = subs.add_parser("init")
108 sub_begin.add_argument("working_path", type=str)
109 sub_begin.set_defaults(func=init)
111 sub_begin = subs.add_parser("begin")
112 sub_begin.add_argument("label", type=str)
113 sub_begin.set_defaults(func=begin)
115 sub_end = subs.add_parser("end")
116 sub_end.add_argument("label", type=str)
117 sub_end.set_defaults(func=end)
119 args = parser.parse_args()
122 if __name__ == "__main__":