]> projects.mako.cc - iron-blogger/blob - render.py
Punts for 2012-12-03
[iron-blogger] / render.py
1 #!/usr/bin/python
2 import yaml
3 from dateutil.parser import parse
4 import datetime
5 import dateutil.tz as tz
6 import sys
7 import os
8 import os.path
9 import subprocess
10 import re
11 from mako.template import Template
12
13 from config import *
14
15 def get_balance(acct):
16     p = subprocess.Popen(BALANCE_CMD + [acct],
17                          stdout=subprocess.PIPE)
18     (out, _) = p.communicate()
19     return float(re.sub(r'\s*(\d+)\s+.*', r'\1', out))
20
21 def get_debts():
22     p = subprocess.Popen(DEBTS_CMD,
23                          stdout=subprocess.PIPE)
24     (out, _) = p.communicate()
25     debts = []
26     for line in out.split("\n"):
27         if not line: continue
28         (val, acct) = line.split()
29         user = acct[len("Pool:Owed:"):]
30         if not user: continue
31         val = float(re.sub(r'(\D)?(\d+)$', r'\2', val))
32         debts.append((user, val))
33     return debts
34
35 def to_week_num(date):
36     return (parse(date, default=START) - START).days / 7
37
38 def parse_skip(rec):
39     spec = rec.get('skip', [])
40     out = []
41     for s in spec:
42         if isinstance(s, list):
43             out.append(map(to_week_num, s))
44         else:
45             out.append(to_week_num(s))
46     return out
47
48 def should_skip(skips, week):
49     for e in skips:
50         if e == week:
51             return True
52         if isinstance(e, list) and e[0] <= week and e[1] > week:
53             return True
54     return False
55
56 def render_template(path, week=None, **kwargs):
57     with open('out/report.yml') as r:
58         report = yaml.safe_load(r)
59
60     with open('bloggers.yml') as f:
61         users = yaml.safe_load(f)
62     if week:
63         week = parse(week, default=START)
64     else:
65         week = START
66
67     week = (week - START).days / 7
68     week_start = START + (week * datetime.timedelta(7))
69     week_end   = START + ((week + 1) * datetime.timedelta(7))
70
71     good = []
72     lame = []
73     skip = []
74     userlist = []
75     punted = []
76
77     class User(object):
78         pass
79
80     for (un, rec) in users.items():
81         u = User()
82         u.username = un
83         u.links = rec['links']
84         u.start = rec['start']
85         u.end   = rec.get('end')
86         u.skip  = parse_skip(rec)
87         u.weeks = report.get(un, [])
88
89         userlist.append(u)
90
91         # create a subset of punted users
92         if u.end:
93             punted.append(u)
94
95     def user_key(u):
96         return (u.start, u.username)
97
98     userlist.sort(key=user_key)
99     punted.sort(key=user_key)
100
101     for u in userlist:
102         user_start = datetime.datetime(*(u.start.timetuple()[:6]))
103         if u.end and datetime.datetime(*(u.end.timetuple()[:6])) <= week_start:
104             continue
105         
106         if should_skip(u.skip, week):
107             pass
108         elif user_start > week_start:
109             skip.append(u)
110         elif len(u.weeks) <= week or not u.weeks[week]:
111             lame.append(u)
112         else:
113             good.append(u)
114
115     debts = get_debts()
116
117     return Template(filename=path, input_encoding='utf-8',
118                     output_encoding='utf-8',
119                     default_filters=['decode.utf8']).render(
120         week=week, week_start=week_start,week_end=week_end,
121         good=good, lame=lame, skip=skip, userlist=userlist,
122         pool=get_balance('Pool'), paid=get_balance('Pool:Paid'),
123         debts=debts, punted=punted, currency=CURRENCY, fine=FINE_SIZE,
124         **kwargs)
125
126 if __name__ == '__main__':
127     if len(sys.argv) < 2:
128         print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]"
129         sys.exit(1)
130
131     template = sys.argv[1]
132     week = None
133     if len(sys.argv) > 2: week = sys.argv[2]
134     print render_template(template, week)

Benjamin Mako Hill || Want to submit a patch?