]> projects.mako.cc - iron-blogger/blob - render.py
0a7bf46f488d68095a370ac722467aa8631d2c82
[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(2009, 12, 21, 6)
13 HERE  = os.path.dirname(__file__)
14
15 def render_template(path, week=None):
16     with open('out/report.yml') as r:
17         report = yaml.safe_load(r)
18
19     with open('bloggers.yml') as f:
20         users = yaml.safe_load(f)
21     if week:
22         week = parse(week, default=START)
23     else:
24         week = START
25
26     week = (week - START).days / 7
27     week_start = START + (week * datetime.timedelta(7))
28     week_end   = START + ((week + 1) * datetime.timedelta(7))
29
30     good = []
31     lame = []
32     skip = []
33     userlist = []
34
35     class User(object):
36         pass
37
38     for (un, rec) in users.items():
39         u = User()
40         u.username = un
41         u.links = rec['links']
42         u.start = rec['start']
43         u.end   = rec.get('end')
44         u.skip  = [(parse(x, default=START) - START).days / 7
45                    for x in rec.get('skip', [])]
46         u.weeks = report.get(un, [])
47
48         userlist.append(u)
49
50     def user_key(u):
51         return (u.start, u.username)
52
53     userlist.sort(key=user_key)
54
55     for u in userlist:
56         user_start = parse(u.start, default=START)
57         if u.end and parse(u.end, default=START) <= week_start:
58             continue
59
60         if week in u.skip:
61             pass
62         elif user_start > week_start:
63             skip.append(u)
64         elif len(u.weeks) <= week or not u.weeks[week]:
65             lame.append(u)
66         else:
67             good.append(u)
68
69     def get_balance(acct):
70         p = subprocess.Popen(['ledger', '-f', os.path.join(HERE,'ledger'),
71                               '-n', 'balance', acct],
72                              stdout=subprocess.PIPE)
73         (out, _) = p.communicate()
74         return int(out.split()[0][1:])
75
76     p = subprocess.Popen(['ledger', '-f', os.path.join(HERE,'ledger'),
77                           '-n', 'balance', 'Pool:Owed:'],
78                          stdout=subprocess.PIPE)
79     (out, _) = p.communicate()
80     debts = []
81     for line in out.split("\n"):
82         if not line: continue
83         (val, acct) = line.split()
84         user = acct[len("Pool:Owed:"):]
85         val  = int(val[len("$"):])
86         debts.append((user, val))
87
88     return Template(filename=path, output_encoding='utf-8').render(
89         week=week, week_start=week_start,week_end=week_end,
90         good=good, lame=lame, skip=skip, userlist=userlist,
91         pool=get_balance('Pool'), paid=get_balance('Pool:Paid'),
92         debts=debts)
93
94 if __name__ == '__main__':
95     if len(sys.argv) < 2:
96         print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]"
97         sys.exit(1)
98
99     template = sys.argv[1]
100     week = None
101     if len(sys.argv) > 2: week = sys.argv[2]
102     print render_template(template, week)

Benjamin Mako Hill || Want to submit a patch?