Code improvements.

This commit is contained in:
Darko Poljak 2016-04-07 22:22:56 +02:00
parent 4663003b69
commit f5123d3328
1 changed files with 47 additions and 34 deletions

View File

@ -32,6 +32,7 @@ import os.path
import re import re
import sys import sys
import glob import glob
import collections
import ctt import ctt
import ctt.listprojects import ctt.listprojects
@ -71,49 +72,61 @@ class Report(object):
projects.extend(fnames) projects.extend(fnames)
total_time = 0 total_time = 0
entries = {} reports = collections.OrderedDict()
for project in projects: for project in projects:
report = cls(project=project, start_date=args.start, report = cls(project=project, start_date=args.start,
end_date=args.end, output_format=args.output_format, end_date=args.end, output_format=args.output_format,
regexp=args.regexp, ignore_case=args.ignore_case) regexp=args.regexp, ignore_case=args.ignore_case)
project_report = report.report(args.summary) report_data = report.report()
if args.summary: reports[report.project] = (report, report_data)
cls.update_summary_report(entries, project_report)
else:
report.print_report(project_report, args.output_format)
total_time = total_time + report.total_time total_time = total_time + report.total_time
if args.summary: cls.print_reports(reports, args.output_format, args.summary)
cls.print_summary_report(entries, args.output_format)
cls.summary(total_time) cls.summary(total_time)
@staticmethod @staticmethod
def update_summary_report(report, entry): def print_report_time_entries(report_data, output_format, summary):
for time in entry: ''' Print time entries from report_data report using output_format.
if not time in report:
report[time] = [] If summary is True then the order of times (keys) is
report[time].extend(entry[time]) sorted.
'''
if summary:
@staticmethod keys = sorted(report_data.keys())
def print_summary_report(report, output_format):
Report.print_report_entries(report, output_format, sorted_keys=True)
@staticmethod
def print_report_entries(report, output_format, sorted_keys=False):
if sorted_keys:
keys = sorted(report.keys())
else: else:
keys = report.keys() keys = report_data.keys()
for time in keys: for time in keys:
entries = report[time] entries = report_data[time]
for entry in entries: for entry in entries:
print(output_format.format_map(entry)) print(output_format.format_map(entry))
def print_report(self, report, output_format): @staticmethod
self.header() def print_reports(reports, output_format, summary):
self.print_report_entries(report, output_format) ''' Print reports using output_format for each entry.
If summary is True then all time entries from all
projects is extracted to one report dict.
Otherwise, all time entries by each project is printed.
'''
if summary:
summary_report = {}
for project in reports:
report, report_data = reports[project]
for time in report_data:
if summary:
if not time in summary_report:
summary_report[time] = report_data[time]
else:
summary_report[time].extend(report_data[time])
else:
report.header()
Report.print_report_time_entries(report_data,
output_format, summary)
if summary:
Report.print_report_time_entries(summary_report,
output_format, summary)
def _init_date(self, start_date, end_date): def _init_date(self, start_date, end_date):
"""Setup date - either default or user given values""" """Setup date - either default or user given values"""
@ -191,9 +204,6 @@ class Report(object):
else: else:
log.debug("Skipping: %s" % dirname) log.debug("Skipping: %s" % dirname)
def report(self, summary=False):
return self.list_entries()
def header(self): def header(self):
project_name = os.path.basename(self.project) project_name = os.path.basename(self.project)
print("Report for %s between %s and %s" % print("Report for %s between %s and %s" %
@ -220,6 +230,8 @@ class Report(object):
def _get_report_entry(self, time, entry): def _get_report_entry(self, time, entry):
''' Get one time entry data.
'''
report = {} report = {}
start_datetime = datetime.datetime.strptime(time, ctt.DATETIMEFORMAT) start_datetime = datetime.datetime.strptime(time, ctt.DATETIMEFORMAT)
delta = datetime.timedelta(seconds=int(float(entry['delta']))) delta = datetime.timedelta(seconds=int(float(entry['delta'])))
@ -239,7 +251,7 @@ class Report(object):
return report return report
def list_entries(self): def report(self):
"""Return total time tracked""" """Return total time tracked"""
entries = {} entries = {}
@ -248,6 +260,7 @@ class Report(object):
entry = self._report_db[time] entry = self._report_db[time]
report = self._get_report_entry(time, entry) report = self._get_report_entry(time, entry)
if not time in entries: if not time in entries:
entries[time] = [] entries[time] = [report]
entries[time].append(report) else:
entries[time].append(report)
return entries return entries