2012-08-01 21:00:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2012-08-01 21:54:19 +00:00
|
|
|
import argparse
|
2012-08-01 21:00:25 +00:00
|
|
|
import calendar
|
|
|
|
import datetime
|
|
|
|
|
2012-08-01 22:28:16 +00:00
|
|
|
#import signal
|
2012-08-01 21:54:19 +00:00
|
|
|
|
2012-08-01 21:00:25 +00:00
|
|
|
import locale
|
2012-08-01 22:28:16 +00:00
|
|
|
import logging
|
2012-08-01 21:54:19 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import sys
|
2012-08-01 21:00:25 +00:00
|
|
|
|
2012-08-01 22:28:16 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Our output format
|
|
|
|
def user_datetime(when):
|
|
|
|
"""Print time for the user"""
|
|
|
|
return when.ctime()
|
|
|
|
|
|
|
|
class CTT:
|
|
|
|
def __init__(self, project):
|
|
|
|
self.project = project
|
|
|
|
self.tracked_time = False
|
|
|
|
|
|
|
|
self._init_home()
|
|
|
|
|
|
|
|
|
|
|
|
def _init_home(self):
|
|
|
|
# Create output directory
|
|
|
|
home = os.environ['HOME']
|
|
|
|
self.ctt_dir = os.path.join(home, ".ctt")
|
|
|
|
self.project_dir = os.path.join(self.ctt_dir, self.project)
|
|
|
|
|
|
|
|
|
|
|
|
# Track time and return information from tracking
|
|
|
|
def track_time(self):
|
|
|
|
self.start = datetime.datetime.now()
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Dummy read data to wait for keyboard irq
|
|
|
|
input()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.stop = datetime.datetime.now()
|
|
|
|
|
|
|
|
self.tracked_time = True
|
|
|
|
|
|
|
|
def write_time(self):
|
|
|
|
if not self.tracked_time:
|
|
|
|
return
|
|
|
|
|
|
|
|
start_seconds = self.start.strftime("%s")
|
|
|
|
stop_seconds = self.stop.strftime("%s")
|
|
|
|
time_dir = os.path.join(self.project_dir, start_seconds)
|
|
|
|
os.makedirs(time_dir, mode=0o700, exist_ok=True)
|
|
|
|
|
|
|
|
filename = os.path.join(time_dir, "end")
|
|
|
|
|
|
|
|
with open(filename, "w") as fd:
|
|
|
|
fd.write("%s\n" % stop_seconds)
|
|
|
|
|
|
|
|
def duration(self):
|
|
|
|
if self.tracked_time:
|
|
|
|
delta = self.stop - self.start
|
|
|
|
else:
|
|
|
|
delta = 0
|
|
|
|
|
|
|
|
return delta
|
2012-08-01 21:00:25 +00:00
|
|
|
|
2012-08-01 21:54:19 +00:00
|
|
|
|
|
|
|
# Setup locale for calendar printing
|
|
|
|
# Setup locale to get Timezone information?
|
2012-08-01 22:28:16 +00:00
|
|
|
#print(locale.getlocale())
|
2012-08-01 21:54:19 +00:00
|
|
|
|
|
|
|
# Record project
|
|
|
|
# Record tags
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2012-08-01 22:28:16 +00:00
|
|
|
parser.add_argument("project", help="Project to track time for", nargs=1)
|
|
|
|
parser.add_argument("-t", "--track", help="Track time (until Ctrl-C is pressed)",
|
|
|
|
action="store_true")
|
2012-08-01 21:54:19 +00:00
|
|
|
|
|
|
|
args = parser.parse_args(sys.argv[1:])
|
2012-08-01 22:28:16 +00:00
|
|
|
print(args)
|
|
|
|
|
|
|
|
ctt = CTT(args.project[0])
|
2012-08-01 21:54:19 +00:00
|
|
|
|
2012-08-01 22:28:16 +00:00
|
|
|
ctt.track_time()
|
|
|
|
ctt.write_time()
|
|
|
|
print(ctt.duration())
|
2012-08-01 21:54:19 +00:00
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# Setup signal handler
|
|
|
|
|
|
|
|
# Start tracking
|
|
|
|
# Save stuff to our home directory
|
|
|
|
|
|
|
|
# Create datetime from userinput
|
|
|
|
# Wed Aug 1 23:35:53 2012
|