cronrec.py
author Eugen Sawin <sawine@me73.com>
Tue, 05 Oct 2010 14:44:31 +0200
changeset 9 fab8e1981155
parent 8 42c4c96e3ecd
child 10 37ab6d0ddb30
permissions -rwxr-xr-x
Fixed end and resume issues. Added logging functions.
     1 #!/usr/local/bin/python
     2 
     3 """
     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
     7 """
     8 
     9 import os
    10 import sys
    11 import argparse
    12 from datetime import datetime
    13 
    14 import db
    15 
    16 WD = "working_path"
    17 CONFIG = {WD: str}
    18 CONFIG_SEP = "="
    19 DB_FILE = "cronrec.db"
    20 
    21 config = {}
    22 
    23 # Try to get the user's home directory path
    24 try: # Windows
    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("~")
    29 
    30 CONFIG_FILE = "%s/.cronrecrc" % HOMEDIR
    31 
    32 def read_config():
    33 	config = {}
    34 	with open(CONFIG_FILE, "r") as config_stream:
    35 		config_lines = [l.split(CONFIG_SEP) for l in config_stream.readlines() 
    36 						if CONFIG_SEP in l]
    37 		for key, value in config_lines:
    38 			key = key.strip().lower()
    39 			value = value.strip()
    40 			if key in CONFIG:
    41 				config[key] = CONFIG[key](value)
    42 	return config
    43 
    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]))
    48 
    49 def db_file():
    50 	global config
    51 	if WD not in config:
    52 		print "Working directory path is not configured. \
    53 Please use the init command."
    54 		return None
    55 	return config[WD] + "/" + DB_FILE
    56 	
    57 def init(args):
    58 	global config
    59 	last_wd = None
    60 	if WD in config:
    61 		last_wd = config[WD]
    62 	config[WD] = args.working_path
    63 	write_config(config)
    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])
    69 	else:
    70 		print "Working directory remains at %s." % config[WD]
    71 	if not path_exists:
    72 		print "Warning: working directory %s does not exist." % config[WD]
    73 		os.makedirs(config[WD])
    74 		print "Working directory %s created." % config[WD]
    75 	elif db_exists:
    76 		print "Database file %s already exists, please delete it before \
    77 initiating a new database at this location." % db_file()
    78 	if not db_exists:
    79 		db.init(db_file())	
    80 
    81 def begin(args):
    82 	project = None
    83 	activity = None
    84 	label = args.label.strip()
    85 	if ":" in label:
    86 		project, activity = label.split(":")
    87 	else:
    88 		project = label
    89 	time = datetime.now()
    90 	db.resume(db_file(), None, None, time)
    91 	db.begin(db_file(), project, activity, time)	
    92     
    93 def end(args):
    94 	project = None
    95 	activity = None
    96 	label = args.label.strip()
    97 	log = args.m
    98 	if ":" in label:
    99 		project, activity = label.split(":")
   100 	else:
   101 		project = label
   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)	
   105 
   106 def pause(args):
   107 	db.pause(db_file(), None, None, datetime.now())	 
   108 
   109 def resume(args):
   110 	db.resume(db_file(), None, None, datetime.now(), args.m)	
   111 
   112 def status(args):
   113 	task = db.status(db_file())
   114 	if task:
   115 		print "Currently active task"
   116 		print "Project: %s Activity: %s" % task
   117 	else:
   118 		print "There is no active task."
   119 
   120 def main():
   121 	global config
   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)
   128 
   129 	sub_begin = subs.add_parser("begin")
   130 	sub_begin.add_argument("label", type=str)
   131 	sub_begin.set_defaults(func=begin)
   132 
   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)
   137 
   138 	sub_end = subs.add_parser("pause")
   139 	sub_end.set_defaults(func=pause)
   140 
   141 	sub_end = subs.add_parser("resume")
   142 	sub_end.add_argument("-m", type=str)
   143 	sub_end.set_defaults(func=resume)
   144 
   145 	sub_end = subs.add_parser("status")
   146 	sub_end.set_defaults(func=status)
   147 
   148 	args = parser.parse_args()
   149 	args.func(args)
   150     
   151 if __name__ == "__main__":
   152 	main()