cronrec.py
author Eugen Sawin <sawine@me73.com>
Fri, 01 Oct 2010 18:59:45 +0200
changeset 6 6f89b07e12cc
parent 5 9d0e6ae739cf
child 7 878956edb936
permissions -rwxr-xr-x
Fixed some issues with (auto) beginning and ending.
     1 #!/usr/local/bin/python
     2 
     3 """
     4 A time recording tool.
     5 Author: Eugen Sawin (sawine@me73.com)
     6 Dependencies: 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 # Trying 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 	db.begin(db_file(), project, activity, datetime.now())	
    90 	print "begins %s:%s" % (project, activity)
    91     
    92 def end(args):
    93 	project = None
    94 	activity = None
    95 	label = args.label.strip()
    96 	if ":" in label:
    97 		project, activity = label.split(":")
    98 	else:
    99 		project = label
   100 	db.end(db_file(), project, activity, datetime.now())	
   101 	print "ends %s:%s" % (project, activity)
   102 
   103 def main():
   104 	global config
   105 	config = read_config()
   106 	parser = argparse.ArgumentParser(description="Records time.")
   107 	subs = parser.add_subparsers()
   108 	sub_begin = subs.add_parser("init")
   109 	sub_begin.add_argument("working_path", type=str)
   110 	sub_begin.set_defaults(func=init)
   111 
   112 	sub_begin = subs.add_parser("begin")
   113 	sub_begin.add_argument("label", type=str)
   114 	sub_begin.set_defaults(func=begin)
   115 
   116 	sub_end = subs.add_parser("end")
   117 	sub_end.add_argument("label", type=str)
   118 	sub_end.set_defaults(func=end)
   119 
   120 	args = parser.parse_args()
   121 	args.func(args)
   122     
   123 if __name__ == "__main__":
   124 	main()