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