# count.py: reproduce the "who writes EasyList" counts on sheets.works.
# 1) git clone --filter=blob:none --no-checkout https://github.com/easylist/easylist.git easylist
# 2) cd easylist && git log --format='%ad|%an|%ae|%s' --date=format:%Y > ../commits_msg.txt && cd ..
# 3) python3 count.py  -> prints counts-by-year.csv
import csv,sys
from collections import defaultdict
FANBOY_NAMES={'ryanbr','fanboynz','fanboy'}
FANBOY_EMAILS={'mp3geek@gmail.com','rbrown@brave.com','easylist.subscription+fanboy@gmail.com'}  # these cross-link inside the commits
def person(a,e):
    if a.lower() in FANBOY_NAMES or e in FANBOY_EMAILS: return 'Fanboy (Ryan Brown)'
    if a.lower()=='khrin': return 'Khrin'
    return 'Everyone else'
def generic(s):  # left out: plain "Update" stream, automated ad-server updates, merges
    s=s.strip().lower()
    return s in ('m: update','update','m: updates','m:update','','m: automated adserver update') or s.startswith('merge branch')
by=defaultdict(lambda:defaultdict(int)); allc=defaultdict(int)
for line in open('commits_msg.txt'):
    y,a,e,s=line.rstrip('\n').split('|',3)
    allc[y]+=1
    if not generic(s): by[y][person(a,e)]+=1
w=csv.writer(sys.stdout); w.writerow(['year','all_changes','described_changes','fanboy','khrin','everyone_else','fanboy_share','khrin_share'])
for y in sorted(by):
    d=by[y]; t=sum(d.values()); f=d['Fanboy (Ryan Brown)']; k=d['Khrin']
    w.writerow([y,allc[y],t,f,k,d['Everyone else'],round(f/t,3),round(k/t,3)])
