def format_row(log): date_id = LogHandler.get_date_id(log['from']) date = date_id if date_id not in done else '' start = LogHandler.format_clocktime(log['from']) end = LogHandler.format_clocktime(log['to']) elapsed = LogHandler.format_duration(log['to'] - log['from']) elapsed_in_day = LogHandler.format_duration( duration_in_day[date_id]) if date_id not in done else '' delta = LogHandler.format_duration_difference( duration_in_day[date_id], timedelta_should_per_day) if date_id not in done else '' content = os.linesep.join([ format_cell(elem) for elem in [date, start, end, elapsed, elapsed_in_day, delta] ]) done.add(date_id) return content
def report(path: str, logs: list, name: str, should_hour_per_day: float): assert len(logs) > 0, 'empty log' done = set() timedelta_should_per_day = datetime.timedelta(hours=should_hour_per_day) duration_in_day = LogHandler.count_duration_per_day(logs) total_duration = functools.reduce( lambda s, d: s + d, (duration for duration in duration_in_day.values())) def format_cell(content: str): return f'\\intbl {content} \\cell ' def format_row(log): date_id = LogHandler.get_date_id(log['from']) date = date_id if date_id not in done else '' start = LogHandler.format_clocktime(log['from']) end = LogHandler.format_clocktime(log['to']) elapsed = LogHandler.format_duration(log['to'] - log['from']) elapsed_in_day = LogHandler.format_duration( duration_in_day[date_id]) if date_id not in done else '' delta = LogHandler.format_duration_difference( duration_in_day[date_id], timedelta_should_per_day) if date_id not in done else '' content = os.linesep.join([ format_cell(elem) for elem in [date, start, end, elapsed, elapsed_in_day, delta] ]) done.add(date_id) return content cells = [f'\\cellx{r}' for r in (800, 1800, 2800, 3800, 4900, 6000)] table_head = '\\trowd\\pard\\trqc ' + ' '.join(cells) with open(path, 'w') as fs: fs.write('{\\rtf1\\ansi\\deff0' + os.linesep) fs.write('\\qr \\sb300 {\\loch %s}' % (LogHandler.format_date(LogHandler.get_now()), ) + os.linesep) fs.write('\\par\\pard\\sb300\\plain {\\loch Dear %s,}' % (name, ) + os.linesep) fs.write( '\\par\\pard\\sb300\\sa300\\plain {\\loch your working time in %s from %s to %s is as follows:}' % (LogHandler.get_month_id(logs[0]['from']), LogHandler.format_date(logs[0]['from']), LogHandler.format_date(logs[-1]['from'])) + os.linesep) fs.write('\\par\\pard\\sb100\\sa100\\qc' + os.linesep) fs.write( table_head.replace( '\\cellx', '\\clbrdrt\\brdrth\\clbrdrb\\brdrs\\cellx').replace( '\\pard', '') + os.linesep) fs.write(''.join([ f'\\intbl {title} \\cell ' for title in ['Date', 'Start', 'End', 'Elapsed', 'Sum', 'Change'] ]) + '\\row' + os.linesep) for id_log, log in enumerate(logs, 1): fs.write((table_head if id_log < len(logs) else table_head. replace('\\cellx', '\\clbrdrb\\brdrth\\cellx')) + os.linesep) fs.write(format_row(log) + os.linesep) fs.write('\\row\\pard' + os.linesep) fs.write( '\\par\\pard\\sb100\\plain {\\loch The total working time for the %d days with time tracking is %s, the balance for this period is %s (with %s planned per day).}' % (len(duration_in_day), LogHandler.format_duration(total_duration), LogHandler.format_duration_difference( total_duration, datetime.timedelta(hours=(should_hour_per_day * len(duration_in_day)))), LogHandler.format_duration(timedelta_should_per_day)) + os.linesep) fs.write('\\par\\pard\\sb300\\plain {\\loch Sincerely yours}' + os.linesep) fs.write('\\par\\pard\\sb300\\plain {\\loch clock_in}' + os.linesep) fs.write('}' + os.linesep)