]> projects.mako.cc - iron-blogger/blob - render.py
render.py: Break out some more code
[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 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 render_template(path, week=None):
37     with open('out/report.yml') as r:
38         report = yaml.safe_load(r)
39
40     with open('bloggers.yml') as f:
41         users = yaml.safe_load(f)
42     if week:
43         week = parse(week, default=START)
44     else:
45         week = START
46
47     week = (week - START).days / 7
48     week_start = START + (week * datetime.timedelta(7))
49     week_end   = START + ((week + 1) * datetime.timedelta(7))
50
51     good = []
52     lame = []
53     skip = []
54     userlist = []
55
56     class User(object):
57         pass
58
59     for (un, rec) in users.items():
60         u = User()
61         u.username = un
62         u.links = rec['links']
63         u.start = rec['start']
64         u.end   = rec.get('end')
65         u.skip  = [(parse(x, default=START) - START).days / 7
66                    for x in rec.get('skip', [])]
67         u.weeks = report.get(un, [])
68
69         userlist.append(u)
70
71     def user_key(u):
72         return (u.start, u.username)
73
74     userlist.sort(key=user_key)
75
76     for u in userlist:
77         user_start = parse(u.start, default=START)
78         if u.end and parse(u.end, default=START) <= week_start:
79             continue
80
81         if week in u.skip:
82             pass
83         elif user_start > week_start:
84             skip.append(u)
85         elif len(u.weeks) <= week or not u.weeks[week]:
86             lame.append(u)
87         else:
88             good.append(u)
89
90     debts = get_debts()
91
92     return Template(filename=path, output_encoding='utf-8').render(
93         week=week, week_start=week_start,week_end=week_end,
94         good=good, lame=lame, skip=skip, userlist=userlist,
95         pool=get_balance('Pool'), paid=get_balance('Pool:Paid'),
96         debts=debts)
97
98 if __name__ == '__main__':
99     if len(sys.argv) < 2:
100         print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]"
101         sys.exit(1)
102
103     template = sys.argv[1]
104     week = None
105     if len(sys.argv) > 2: week = sys.argv[2]
106     print render_template(template, week)

Benjamin Mako Hill || Want to submit a patch?