]> projects.mako.cc - iron-blogger/blob - render.py
Rename some scripts to make more sense.
[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 from mako.template import Template
9
10 if len(sys.argv) < 2:
11     print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]" 
12     sys.exit(1)
13
14 template = sys.argv[1]
15 START = datetime.datetime(2009, 12, 21, 6)
16
17 if len(sys.argv) == 3:
18     week = parse(sys.argv[2], default=START)
19 else:
20     week = START
21
22 with open('out/report.yml') as r:
23     report = yaml.safe_load(r)
24
25 with open('bloggers.yml') as f:
26     users = yaml.safe_load(f)
27
28 week = (week - START).days / 7
29 week_start = START + (week * datetime.timedelta(7))
30 week_end   = START + ((week + 1) * datetime.timedelta(7))
31
32 good = []
33 lame = []
34 skip = []
35 userlist = []
36
37 class User(object):
38     pass
39
40 for (un, rec) in users.items():
41     u = User()
42     u.username = un
43     u.links = rec['links']
44     u.start = rec['start']
45     u.weeks = report.get(un, [])
46
47     userlist.append(u)
48
49 def user_key(u):
50     return (u.start, u.username)
51
52 userlist.sort(key=user_key)
53
54 for u in userlist:
55     user_start = parse(u.start, default=START)
56     if user_start > week_start:
57         skip.append(u)
58     elif len(u.weeks) <= week or not u.weeks[week]:
59         lame.append(u)
60     else:
61         good.append(u)
62
63 print Template(filename=template, output_encoding='utf-8').render(
64     week=week, week_start=week_start,week_end=week_end,
65     good=good, lame=lame, skip=skip, userlist=userlist)

Benjamin Mako Hill || Want to submit a patch?