3 from dateutil.parser import parse
5 import dateutil.tz as tz
10 from mako.template import Template
12 START = datetime.datetime(2011, 10, 24, 6)
13 HERE = os.path.dirname(__file__)
15 def get_balance(acct):
16 p = subprocess.Popen(['ledger', '-f', os.path.join(HERE,'ledger'),
17 '-n', 'balance', acct],
18 stdout=subprocess.PIPE)
19 (out, _) = p.communicate()
20 return float(out.split()[0][1:])
23 p = subprocess.Popen(['ledger', '-f',
24 os.path.join(HERE, 'ledger'),
25 '--no-color', '--flat',
26 '--no-total', 'balance', 'Pool:Owed:'],
27 stdout=subprocess.PIPE)
28 (out, _) = p.communicate()
30 for line in out.split("\n"):
32 (val, acct) = line.split()
33 user = acct[len("Pool:Owed:"):]
34 val = float(val[len("$"):])
35 debts.append((user, val))
38 def to_week_num(date):
39 return (parse(date, default=START) - START).days / 7
42 spec = rec.get('skip', [])
45 if isinstance(s, list):
46 out.append(map(to_week_num, s))
48 out.append(to_week_num(s))
51 def should_skip(skips, week):
55 if isinstance(e, list) and e[0] <= week and e[1] > week:
59 def render_template(path, week=None, **kwargs):
60 with open('out/report.yml') as r:
61 report = yaml.safe_load(r)
63 with open('bloggers.yml') as f:
64 users = yaml.safe_load(f)
66 week = parse(week, default=START)
70 week = (week - START).days / 7
71 week_start = START + (week * datetime.timedelta(7))
72 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, [])
94 return (u.start, u.username)
96 userlist.sort(key=user_key)
99 user_start = parse(u.start, default=START)
100 if u.end and parse(u.end, default=START) <= week_start:
103 if should_skip(u.skip, week):
105 elif user_start > week_start:
107 elif len(u.weeks) <= week or not u.weeks[week]:
114 return Template(filename=path, output_encoding='utf-8').render(
115 week=week, week_start=week_start,week_end=week_end,
116 good=good, lame=lame, skip=skip, userlist=userlist,
117 pool=get_balance('Pool'), paid=get_balance('Pool:Paid'),
118 debts=debts, **kwargs)
120 if __name__ == '__main__':
121 if len(sys.argv) < 2:
122 print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]"
125 template = sys.argv[1]
127 if len(sys.argv) > 2: week = sys.argv[2]
128 print render_template(template, week)