begin reports

Signed-off-by: Nico Schottelius <nico@brief.schottelius.org>
This commit is contained in:
Nico Schottelius 2012-08-03 23:32:11 +02:00
parent 5d0580d930
commit 2de70a14af

88
ctt.py
View file

@ -16,12 +16,14 @@ import sys
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
VERSION = "0.1"
# Our output format # Our output format
def user_datetime(when): def user_datetime(when):
"""Print time for the user""" """Print time for the user"""
return when.ctime() return when.ctime()
class CTT: class Tracker:
def __init__(self, project): def __init__(self, project):
self.project = project self.project = project
self.tracked_time = False self.tracked_time = False
@ -41,8 +43,14 @@ class CTT:
self.start = datetime.datetime.now() self.start = datetime.datetime.now()
try: try:
# Dummy read data to wait for keyboard irq # Wait for the exception to pop up - FIXME: find better method
input()
# Using input, Ctrl-C is displayed as ^C on the screen - ugly
#input()
# Sleep 42 years - should be longer than anybody trying to track time
time.sleep(86400 * 365 * 42)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
@ -56,26 +64,48 @@ class CTT:
start_seconds = self.start.strftime("%s") start_seconds = self.start.strftime("%s")
stop_seconds = self.stop.strftime("%s") stop_seconds = self.stop.strftime("%s")
delta_seconds = self.delta()
delta_seconds = self.duration().total_seconds()
time_dir = os.path.join(self.project_dir, start_seconds) time_dir = os.path.join(self.project_dir, start_seconds)
os.makedirs(time_dir, mode=0o700, exist_ok=True) os.makedirs(time_dir, mode=0o700, exist_ok=True)
filename = os.path.join(time_dir, "delta") filename = os.path.join(time_dir, "delta")
with open(filename, "w") as fd: with open(filename, "w") as fd:
fd.write("%s\n" % delta_seconds) fd.write("%s\n" % delta_seconds)
def delta(self): def delta(self, in_seconds=True):
if self.tracked_time: if self.tracked_time:
delta = self.stop - self.start delta = self.stop - self.start
else: else:
delta = datetime.timedelta() delta = datetime.timedelta()
if in_seconds:
delta = delta.total_seconds()
return delta return delta
class Report(object):
"""Create a report on tracked time"""
def __init__(self, args):
pass
@staticmethod
def default_dates():
"""Return default start and end of of time
start: first of last month
end: last of last month
"""
now = datetime.datetime.now()
first_day = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
end_date = first_day - datetime.timedelta(days=1)
start_date = end_date.replace(day=1)
return (start_date, end_date)
# Setup locale for calendar printing # Setup locale for calendar printing
# Setup locale to get Timezone information? # Setup locale to get Timezone information?
#print(locale.getlocale()) #print(locale.getlocale())
@ -83,24 +113,46 @@ class CTT:
# Record project # Record project
# Record tags # Record tags
parser = argparse.ArgumentParser() def cmd_track(args):
parser.add_argument("project", help="Project to track time for", nargs=1) """Command line handler for time tracking"""
parser.add_argument("-t", "--track", help="Track time (until Ctrl-C is pressed)", tracker = Tracker(args.project[0])
action="store_true") tracker.track_time()
tracker.write_time()
print(tracker.delta())
args = parser.parse_args(sys.argv[1:]) def cmd_report(args):
"""Command line handler for time reporting"""
#report = Report(args.project[0])
print(Report.default_dates())
def parse_argv(argv):
parser = {}
parser['main'] = argparse.ArgumentParser(description='ctt ' + VERSION)
parser['sub'] = parser['main'].add_subparsers(title="Commands")
parser['track'] = parser['sub'].add_parser('track')
parser['track'].set_defaults(func=cmd_track)
parser['track'].add_argument("project", help="Project to track time for", nargs=1)
parser['report'] = parser['sub'].add_parser('report')
parser['report'].set_defaults(func=cmd_report)
parser['report'].add_argument("project", help="Project to report time for", nargs=1)
parser['report'].add_argument("-s", "--start", help="Start datetime (first of last month)", nargs=1)
parser['report'].add_argument("-e", "--end", help="End datetime (last of last month)", nargs=1)
#parser['track'].add_argument("-t", "--tag", help="Add tags",
# action="store_true")
args = parser['main'].parse_args()
print(args) print(args)
ctt = CTT(args.project[0]) args.func(args)
ctt.track_time()
ctt.write_time()
print(ctt.duration())
if __name__ == "__main__":
parse_argv(sys.argv[1:])
sys.exit(0) sys.exit(0)
# Setup signal handler # Setup signal handler
# Start tracking # Start tracking
# Save stuff to our home directory # Save stuff to our home directory