Skip to content

Instantly share code, notes, and snippets.

@marcolussetti
Last active December 4, 2019 18:23
Show Gist options
  • Save marcolussetti/278e308c0270ea7febe1c8f571cb03ed to your computer and use it in GitHub Desktop.
Save marcolussetti/278e308c0270ea7febe1c8f571cb03ed to your computer and use it in GitHub Desktop.
worklog.py - An extremely simple worklog to CSV tracker with no dependencies (well, besides Python 3).
Date Description Task Difference Processed
20191204 0800:00 This is an example description Example Task False
#!/usr/bin/env python3
import os
import argparse
import datetime
import csv
import math
TARGET_FILE = "~/.worklog.csv"
# Date,Description,Task,Difference,Processed
LAST_X_TO_PRINT = 5
def log_entry(description, time):
# Add new entry
timeformatted = time.strftime('%Y%m%d %H%M:%S')
with open(TARGET_FILE, 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([timeformatted, description, args.jira if "jira" in args else '""', "", False])
# Calculate prior entry duration
with open(TARGET_FILE, 'r') as csv_file:
lines = list(csv.reader(csv_file))
if len(lines) > 2:
prior_entry = lines[-2]
pior_entry_time = datetime.datetime.strptime(prior_entry[0], '%Y%m%d %H%M:%S')
prior_entry_duration = time - pior_entry_time
duration_hours = prior_entry_duration.total_seconds() / 3600
duration_minutes = (prior_entry_duration.total_seconds() - math.floor(duration_hours) * 3600) / 60
floored_hours = int(math.floor(duration_hours))
ceiled_minutes = int(math.ceil(duration_minutes))
if floored_hours > 0:
duration = "{}h {}m".format(floored_hours, ceiled_minutes)
else:
duration = "{}m".format(ceiled_minutes)
prior_entry[3] = duration
lines[-2] = prior_entry
with open(TARGET_FILE, 'w') as csv_file:
csv.writer(csv_file).writerows(lines)
# Read updated entries
with open(TARGET_FILE, 'r') as csv_file:
lines = list(csv.reader(csv_file))
for line in lines[-LAST_X_TO_PRINT:]:
print(line)
def mark_done():
with open(TARGET_FILE, 'r') as csv_file:
lines = list(csv.reader(csv_file))
for line in lines[1:]:
line[-1] = True
with open(TARGET_FILE, 'w') as csv_file:
csv.writer(csv_file).writerows(lines)
def unprocessed_items():
with open(TARGET_FILE, 'r') as csv_file:
lines = list(csv.reader(csv_file))
for line in [line for line in lines[1:] if line[-1] != 'True']:
print(line)
parser = argparse.ArgumentParser(description="Record time")
parser.add_argument('-j', '--jira', help="JIRA issue associated. Can be used for generic projects, etc.")
parser.add_argument('-d', '--done', action="store_true", help="Mark that the worklog has been transferred to another source")
parser.add_argument('-t', '--time', help="Override the time. Accepts value in the %H%M format (e.g. 1315)")
parser.add_argument('-p', '--process', action="store_true", help="Return all unprocessed items")
parser.add_argument('description', nargs="*", help="Description")
args = parser.parse_args()
if args.done:
# Marks as done previous entries
mark_done()
elif args.process:
# Return all outstanding items
unprocessed_items()
elif args.description and len (args.description) > 0:
if args.time:
nowdate = datetime.datetime.now().strftime('%Y%m%d')
time = datetime.datetime.strptime(f"{nowdate} {args.time}:00", '%Y%m%d %H%M:%S')
else:
time = datetime.datetime.now()
description = " ".join(args.description)
log_entry(description, time)
else:
# Read updated entries
with open(TARGET_FILE, 'r') as csv_file:
lines = list(csv.reader(csv_file))
for line in lines[-LAST_X_TO_PRINT:]:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment