3 from dateutil.parser import parse
5 import dateutil.tz as tz
11 from mako.template import Template
15 def get_balance(acct):
16 balance_cmd_tmp = BALANCE_CMD
17 balance_cmd_tmp.append(acct)
18 p = subprocess.Popen(balance_cmd_tmp,
19 stdout=subprocess.PIPE)
20 (out, _) = p.communicate()
21 return float(re.sub(r'\s*(\d+)\s+.*', r'\1', out))
24 p = subprocess.Popen(DEBTS_CMD,
25 stdout=subprocess.PIPE)
26 (out, _) = p.communicate()
28 for line in out.split("\n"):
30 (val, acct) = line.split()
31 user = acct[len("Pool:Owed:"):]
33 val = float(re.sub(r'(\D)?(\d+)$', r'\2', val))
34 debts.append((user, val))
37 def to_week_num(date):
38 return (parse(date, default=START) - START).days / 7
41 spec = rec.get('skip', [])
44 if isinstance(s, list):
45 out.append(map(to_week_num, s))
47 out.append(to_week_num(s))
50 def should_skip(skips, week):
54 if isinstance(e, list) and e[0] <= week and e[1] > week:
58 def render_template(path, week=None, **kwargs):
59 with open('out/report.yml') as r:
60 report = yaml.safe_load(r)
62 with open('bloggers.yml') as f:
63 users = yaml.safe_load(f)
65 week = parse(week, default=START)
69 week = (week - START).days / 7
70 week_start = START + (week * datetime.timedelta(7))
71 week_end = START + ((week + 1) * datetime.timedelta(7))
82 for (un, rec) in users.items():
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, [])
93 # create a subset of punted users
98 return (u.start, u.username)
100 userlist.sort(key=user_key)
101 punted.sort(key=user_key)
104 user_start = datetime.datetime(*(u.start.timetuple()[:6]))
105 if u.end and datetime.datetime(*(u.end.timetuple()[:6])) <= week_start:
108 if should_skip(u.skip, week):
110 elif user_start > week_start:
112 elif len(u.weeks) <= week or not u.weeks[week]:
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,
128 if __name__ == '__main__':
129 if len(sys.argv) < 2:
130 print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]"
133 template = sys.argv[1]
135 if len(sys.argv) > 2: week = sys.argv[2]
136 print render_template(template, week)