From 2de70a14af93ae85250e37458db060245f66abcc Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Fri, 3 Aug 2012 23:32:11 +0200 Subject: [PATCH] begin reports Signed-off-by: Nico Schottelius --- ctt.py | 90 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 19 deletions(-) diff --git a/ctt.py b/ctt.py index a2ca979..4e8400a 100755 --- a/ctt.py +++ b/ctt.py @@ -16,12 +16,14 @@ import sys log = logging.getLogger(__name__) +VERSION = "0.1" + # Our output format def user_datetime(when): """Print time for the user""" return when.ctime() -class CTT: +class Tracker: def __init__(self, project): self.project = project self.tracked_time = False @@ -41,8 +43,14 @@ class CTT: self.start = datetime.datetime.now() try: - # Dummy read data to wait for keyboard irq - input() + # Wait for the exception to pop up - FIXME: find better method + + # 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: pass @@ -56,26 +64,48 @@ class CTT: start_seconds = self.start.strftime("%s") stop_seconds = self.stop.strftime("%s") - - delta_seconds = self.duration().total_seconds() + delta_seconds = self.delta() 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, "delta") with open(filename, "w") as fd: fd.write("%s\n" % delta_seconds) - def delta(self): + def delta(self, in_seconds=True): if self.tracked_time: delta = self.stop - self.start else: delta = datetime.timedelta() + if in_seconds: + delta = delta.total_seconds() + 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 to get Timezone information? #print(locale.getlocale()) @@ -83,24 +113,46 @@ class CTT: # Record project # Record tags -parser = argparse.ArgumentParser() -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") +def cmd_track(args): + """Command line handler for time tracking""" + tracker = Tracker(args.project[0]) + tracker.track_time() + tracker.write_time() + print(tracker.delta()) -args = parser.parse_args(sys.argv[1:]) -print(args) +def cmd_report(args): + """Command line handler for time reporting""" + #report = Report(args.project[0]) + print(Report.default_dates()) -ctt = CTT(args.project[0]) +def parse_argv(argv): + parser = {} + parser['main'] = argparse.ArgumentParser(description='ctt ' + VERSION) + parser['sub'] = parser['main'].add_subparsers(title="Commands") -ctt.track_time() -ctt.write_time() -print(ctt.duration()) + 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) -sys.exit(0) + 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) + + args.func(args) + +if __name__ == "__main__": + parse_argv(sys.argv[1:]) + sys.exit(0) # Setup signal handler - # Start tracking # Save stuff to our home directory