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

Benjamin Mako Hill || Want to submit a patch?