[work] Add parse command

This commit is contained in:
Lara 2020-02-19 12:12:28 -05:00
parent 30d4a9ef90
commit 17ac8d0685

View file

@ -49,27 +49,6 @@ def td_format(td):
return '{:d}:{:02d}'.format(int(hours), int(minutes))
def convert_manual_entry():
for line in sys.stdin:
fields = line.split(",")
if len(fields) < 6:
break
morning = datetime.strptime(fields[1], "%H:%M")
break_start = datetime.strptime(fields[2], "%H:%M")
break_end = datetime.strptime(fields[3], "%H:%M")
evening = datetime.strptime(fields[4], "%H:%M")
break_time = break_end - break_start
full_day = evening - morning
work_day = full_day - break_time
print('{},{},{},{},{},{}'.format(
fields[0],
morning.strftime("%H:%M"),
evening.strftime("%H:%M"),
td_format(break_time),
td_format(work_day),
"".join(fields[5:]).strip()))
def get_today_fields():
try:
with open(TODAY_FILE, "r") as f:
@ -161,7 +140,34 @@ def work_export(args):
def work_parse(args):
print("parse")
new_lines = []
try:
with open(args.file, "r") as f:
for line in f:
fields = line.split(",")
if len(fields) < 6:
break
morning = datetime.strptime(fields[1], "%H:%M")
break_start = datetime.strptime(fields[2], "%H:%M")
break_end = datetime.strptime(fields[3], "%H:%M")
evening = datetime.strptime(fields[4], "%H:%M")
break_time = break_end - break_start
full_day = evening - morning
work_day = full_day - break_time
new_lines.append('{},{},{},{},{},{}'.format(
fields[0],
morning.strftime("%H:%M"),
evening.strftime("%H:%M"),
td_format(break_time),
td_format(work_day),
"".join(fields[5:]).strip()))
except FileNotFoundError:
die("file not found: {}".format(args.file))
# TODO do sanity checking, like if a day already exists
with open(TIME_FILE, "a") as f:
for line in new_lines:
f.write("{}\n".format(line))
log("Written {} new entries to {}".format(len(new_lines), TIME_FILE))
if __name__ == "__main__":
@ -184,6 +190,7 @@ if __name__ == "__main__":
end_parser.set_defaults(func=work_end)
export_parser.set_defaults(func=work_export)
parse_parser.set_defaults(func=work_parse)
parse_parser.add_argument("file")
args = parser.parse_args()
args.func(args)