#!/usr/bin/env python3
"""Time hook lines: words, seconds at a pace, and a pass/over flag against a limit.

usage: python3 hook-timer.py FILE [--wpm 155] [--limit 3.0]
FILE has one hook per line; blank lines are skipped. Words are counted the way the Koldflux Hooks tool counts them:
split on white space and keep the pieces that contain a letter or a digit ("1,000" and "30-second" are one word each).
seconds = words / wpm * 60. A line is OVER when its seconds are above --limit.
"""
import argparse, re, sys

def count_words(text):
    return len([w for w in re.split(r'\s+', text) if re.search(r'[^\W_]', w)])

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('file')
    ap.add_argument('--wpm', type=float, default=155.0)
    ap.add_argument('--limit', type=float, default=3.0)
    a = ap.parse_args()
    if a.wpm <= 0 or a.limit <= 0:
        sys.exit('--wpm and --limit must be above 0')
    budget = int(a.limit * a.wpm / 60)
    print('pace %g wpm, limit %g s = %d words' % (a.wpm, a.limit, budget))
    over = total = 0
    for line in open(a.file, encoding='utf-8'):
        line = line.strip()
        if not line:
            continue
        n = count_words(line)
        s = n / a.wpm * 60
        flag = 'OVER' if s > a.limit else 'ok'
        over += flag == 'OVER'
        total += 1
        print('%2d words %5.2f s  %-4s %s' % (n, s, flag, line))
    print('%d lines, %d over the limit' % (total, over))

main()
