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

Benjamin Mako Hill || Want to submit a patch?