files/hours
2011-06-10 14:20:34 -04:00

102 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python
# Author: Josh Holtrop
# Simple script to show me my week's hours as far as my Ubuntu box knows them
import sys
import re
import argparse
from datetime import *
LOGFILE = '/var/log/auth.log'
now = datetime.now()
monday = (now - timedelta(now.weekday())).date()
def get_dt_from_log_line(line):
m = re.match(r'(\S\S\S)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+.*', line)
if m is not None:
month_name, day, t_hr, t_min, t_sec = m.group(1, 2, 3, 4, 5)
try:
month = datetime.strptime(month_name, '%b').month
except:
return None
year = now.year
if month == 12 and now.month == 1:
year -= 1
dt = datetime(*map(int, [year, month, day, t_hr, t_min, t_sec]))
return dt
return None
def main(argv):
total_hours = 40
parser = argparse.ArgumentParser('hours')
parser.add_argument('-t', '--total', type=float)
args = parser.parse_args(argv[1:])
if args.total is not None:
total_hours = args.total
times = []
for i in range(7):
times.append([None, None])
f = open(LOGFILE, 'r')
while True:
line = f.readline()
if line == '':
break
if re.search(r'gnome-screensaver.*unlocked.login.keyring', line):
# found a login line
dt = get_dt_from_log_line(line)
idx = (dt.date() - monday).days
if times[idx][0] is None or dt < times[idx][0]:
times[idx][0] = dt
elif re.search(r'lock-screen:', line):
# found a logout line
dt = get_dt_from_log_line(line)
idx = (dt.date() - monday).days
if times[idx][1] is None or dt > times[idx][1]:
times[idx][1] = dt
f.close()
now_idx = (now.date() - monday).days
if times[now_idx][1] is None or now > times[now_idx][1]:
times[now_idx][1] = now
def fmt_time_dt(dt):
s = dt.strftime('%I:%M') + dt.strftime('%p')[0].lower()
if s[0] == '0':
s = s[1:]
return s
border = lambda: sys.stdout.write('-' * 40 + '\n')
total_seconds = 0
border()
for time in times:
if time[0] is not None:
sys.stdout.write('%-14s' % (time[0].strftime('%d %A') + ':'))
sys.stdout.write(fmt_time_dt(time[0]))
sys.stdout.write(' - ')
if time[1] is not None:
sys.stdout.write(fmt_time_dt(time[1]))
seconds = (time[1] - time[0]).seconds
hours = seconds / 60.0 / 60.0
sys.stdout.write(' (%.2f hours)' % hours)
total_seconds += seconds
else:
sys.stdout.write('???')
sys.stdout.write('\n')
border()
sys.stdout.write('Total: %.2f hours' % (seconds / 60.0 / 60.0))
secs_in_total_hours = total_hours * 60 * 60
if total_seconds < secs_in_total_hours:
out_time = now + timedelta(0, secs_in_total_hours - total_seconds)
sys.stdout.write('; %.1f at: %s %s' % (total_hours,
out_time.strftime('%a %d'), fmt_time_dt(out_time)))
sys.stdout.write('\n')
if __name__ == "__main__":
sys.exit(main(sys.argv))