cronrec.py
author Eugen Sawin <sawine@me73.com>
Fri, 01 Oct 2010 00:45:07 +0200
changeset 3 59413cc48bd3
parent 2 d1c9dec8b059
child 4 5e41c4b578da
permissions -rwxr-xr-x
Moved database function to the db lib. Finished draft of the add command.
     1 #!/usr/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_PROJECT = "default"
    33 DEF_ACTIVITY = "default"
    34 
    35 def read_config():
    36 	config = {}
    37 	with open(CONFIG_FILE, "r") as config_stream:
    38 		config_lines = [l.split(CONFIG_SEP) for l in config_stream.readlines() 
    39 						if CONFIG_SEP in l]
    40 		for key, value in config_lines:
    41 			key = key.strip().lower()
    42 			value = value.strip()
    43 			if key in CONFIG:
    44 				config[key] = CONFIG[key](value)
    45 	return config
    46 
    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]))
    51 
    52 def db_file():
    53 	global config
    54 	if WD not in config:
    55 		print "Working directory path is not configured. \
    56 Please use the init command."
    57 		return None
    58 	return config[WD] + "/" + DB_FILE
    59 	
    60 def init(args):
    61 	global config
    62 	last_wd = None
    63 	if WD in config:
    64 		last_wd = config[WD]
    65 	config[WD] = args.working_path
    66 	write_config(config)
    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])
    72 	else:
    73 		print "Working directory remains at %s." % config[WD]
    74 	if not path_exists:
    75 		print "Warning: working directory %s does not exist." % config[WD]
    76 		os.makedirs(config[WD])
    77 		print "Working directory %s created." % config[WD]
    78 	elif db_exists:
    79 		print "Database file %s already exists, please delete it before \
    80 initiating a new database at this location." % db_file()
    81 	if not db_exists:
    82 		db.init(db_file())	
    83 
    84 def begin(args):
    85 	project = None
    86 	activity = None
    87 	label = args.label.strip()
    88 	if ":" in label:
    89 		project, activity = label.split(":")
    90 	else:
    91 		project = label
    92 	if not project:
    93 		project = DEF_PROJECT
    94 	if not activity:
    95 		activity = DEF_ACTIVITY
    96 	db.begin(db_file(), project, activity, datetime.now())	
    97 	print "begins %s:%s" % (project, activity)
    98     
    99 def end(args):
   100 	print "ends"
   101 
   102 def main():
   103 	global config
   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)
   110 
   111 	sub_begin = subs.add_parser("begin")
   112 	sub_begin.add_argument("label", type=str)
   113 	sub_begin.set_defaults(func=begin)
   114 
   115 	sub_end = subs.add_parser("end")
   116 	sub_end.add_argument("label", type=str)
   117 	sub_end.set_defaults(func=end)
   118 
   119 	args = parser.parse_args()
   120 	args.func(args)
   121     
   122 if __name__ == "__main__":
   123 	main()