3 from dateutil.parser import parse
5 import dateutil.tz as tz
11 from mako.template import Template
15 def get_balance(acct):
16 p = subprocess.Popen(BALANCE_CMD + [acct],
17 stdout=subprocess.PIPE)
18 (out, _) = p.communicate()
19 return float(re.sub(r'\s*(\d+)\s+.*', r'\1', out))
22 p = subprocess.Popen(DEBTS_CMD,
23 stdout=subprocess.PIPE)
24 (out, _) = p.communicate()
26 for line in out.split("\n"):
28 (val, acct) = line.split()
29 user = acct[len("Pool:Owed:"):]
31 val = float(re.sub(r'(\D)?(\d+)$', r'\2', val))
32 debts.append((user, val))
35 def to_week_num(date):
36 return (parse(date, default=START) - START).days / 7
39 spec = rec.get('skip', [])
42 if isinstance(s, list):
43 out.append(map(to_week_num, s))
45 out.append(to_week_num(s))
48 def should_skip(skips, week):
52 if isinstance(e, list) and e[0] <= week and e[1] > week:
56 def render_template(path, week=None, **kwargs):
57 with open('out/report.yml') as r:
58 report = yaml.safe_load(r)
60 with open('bloggers.yml') as f:
61 users = yaml.safe_load(f)
63 week = parse(week, default=START)
67 week = (week - START).days / 7
68 week_start = START + (week * datetime.timedelta(7))
69 week_end = START + ((week + 1) * datetime.timedelta(7))
80 for (un, rec) in users.items():
83 u.links = rec['links']
84 u.start = rec['start']
85 u.end = rec.get('end')
86 u.skip = parse_skip(rec)
87 u.weeks = report.get(un, [])
91 # create a subset of punted users
96 return (u.start, u.username)
98 userlist.sort(key=user_key)
99 punted.sort(key=user_key)
102 user_start = datetime.datetime(*(u.start.timetuple()[:6]))
103 if u.end and datetime.datetime(*(u.end.timetuple()[:6])) <= week_start:
106 if should_skip(u.skip, week):
108 elif user_start > week_start:
110 elif len(u.weeks) <= week or not u.weeks[week]:
117 return Template(filename=path, input_encoding='utf-8',
118 output_encoding='utf-8',
119 default_filters=['decode.utf8']).render(
120 week=week, week_start=week_start,week_end=week_end,
121 good=good, lame=lame, skip=skip, userlist=userlist,
122 pool=get_balance('Pool'), paid=get_balance('Pool:Paid'),
123 debts=debts, punted=punted, currency=CURRENCY, fine=FINE_SIZE,
126 if __name__ == '__main__':
127 if len(sys.argv) < 2:
128 print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]"
131 template = sys.argv[1]
133 if len(sys.argv) > 2: week = sys.argv[2]
134 print render_template(template, week)