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

Benjamin Mako Hill || Want to submit a patch?