From 6453bb4008557655500c8c3d42db48014b484c47 Mon Sep 17 00:00:00 2001 From: Benjamin Mako Hill Date: Mon, 13 Aug 2012 17:07:31 -0400 Subject: [PATCH] added a new configuration file and changed code/templates to use it --- config.py | 33 +++++++++++++++++++++++++++++++++ render.py | 28 ++++++++++++++++------------ templates/email.txt | 14 ++++++++------ templates/ledger | 2 +- templates/week.tmpl | 10 +++++----- update-participants.py | 8 ++------ weekly-update.py | 8 +++----- 7 files changed, 68 insertions(+), 35 deletions(-) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..d112ea5 --- /dev/null +++ b/config.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +## -*- coding: utf-8 -*- + +import os.path +import datetime +import subprocess + +HERE = os.path.dirname(__file__) +START = datetime.datetime(2011, 10, 24, 6) + +XMLRPC_ENDPOINT = 'http://iron-blogger.mako.cc/xmlrpc.php' +USER = 'mako' +BLOG_ID = 1 +PARTICIPANTS_PAGE_ID = 12 + +FINE_SIZE = 5 +CURRENCY = "$" + +# check the version of ledger to find out which commands to use +if subprocess.check_output(['ledger', '--version'])[7] == 3: + BALANCE_CMD = ['ledger', '-f', os.path.join(HERE,'ledger'), + '--no-color', '-n', 'balance'] + + DEBTS_CMD = ['ledger', '-f', os.path.join(HERE, 'ledger'), + '--flat', '--no-total', '--no-color', + 'balance', 'Pool:Owed:'] + +else: + BALANCE_CMD = ['ledger', '-f', os.path.join(HERE, 'ledger'), + '-n', 'balance'] + DEBTS_CMD = ['ledger', '-f', os.path.join(HERE, 'ledger'), + '-n', 'balance', 'Pool:Owed:'] + diff --git a/render.py b/render.py index 9e1fbdc..58a1fc4 100755 --- a/render.py +++ b/render.py @@ -7,21 +7,22 @@ import sys import os import os.path import subprocess +import re from mako.template import Template -START = datetime.datetime(2011, 10, 24, 6) -HERE = os.path.dirname(__file__) +from config import * def get_balance(acct): - p = subprocess.Popen(['ledger', '-f', os.path.join(HERE,'ledger'), - '-n', 'balance', acct], + print acct + balance_cmd_tmp = BALANCE_CMD + balance_cmd_tmp.append(acct) + p = subprocess.Popen(balance_cmd_tmp, stdout=subprocess.PIPE) (out, _) = p.communicate() - return float(out.split()[0][1:]) + return float(re.sub(r'\s*(\d+)\s+.*', r'\1', out)) def get_debts(): - p = subprocess.Popen(['ledger', '-f', os.path.join(HERE, 'ledger'), - '-n', 'balance', 'Pool:Owed:'], + p = subprocess.Popen(DEBTS_CMD, stdout=subprocess.PIPE) (out, _) = p.communicate() debts = [] @@ -29,7 +30,7 @@ def get_debts(): if not line: continue (val, acct) = line.split() user = acct[len("Pool:Owed:"):] - val = float(val[len("$"):]) + val = float(re.sub(r'(\D)?(\d+)$', r'\2', val)) debts.append((user, val)) return debts @@ -100,10 +101,10 @@ def render_template(path, week=None, **kwargs): punted.sort(key=user_key) for u in userlist: - user_start = parse(u.start, default=START) + user_start = datetime.datetime(*(u.start.timetuple()[:6])) if u.end and parse(u.end, default=START) <= week_start: continue - + if should_skip(u.skip, week): pass elif user_start > week_start: @@ -115,11 +116,14 @@ def render_template(path, week=None, **kwargs): debts = get_debts() - return Template(filename=path, output_encoding='utf-8').render( + return Template(filename=path, input_encoding='utf-8', + output_encoding='utf-8', + default_filters=['decode.utf8']).render( week=week, week_start=week_start,week_end=week_end, good=good, lame=lame, skip=skip, userlist=userlist, pool=get_balance('Pool'), paid=get_balance('Pool:Paid'), - debts=debts, punted=punted, **kwargs) + debts=debts, punted=punted, currency=CURRENCY, fine=FINE_SIZE, + **kwargs) if __name__ == '__main__': if len(sys.argv) < 2: diff --git a/templates/email.txt b/templates/email.txt index 667eaef..5f7891d 100644 --- a/templates/email.txt +++ b/templates/email.txt @@ -4,7 +4,7 @@ To: iron-blogger@mako.cc SLACKERS: ${", ".join(sorted([u.username for u in lame]))} % if punt: -PUNTED for balance ≥$30: ${", ".join(sorted(punt))} +PUNTED for balance ≥${currency}${fine * 6}: ${", ".join(sorted(punt))} % endif People who posted: @@ -23,16 +23,18 @@ People who have not yet started: % endif Beer pool: -This week: $${5 * len(lame)}.00 -Total: $${pool} -Paid: $${paid} +This week: ${currency}${fine * len(lame)}.00 +Total: ${currency}${pool} +Paid: ${currency}${paid} Individual debts: % for (u, v) in sorted(debts, key=lambda p:p[1], reverse=True): -${"%20s $%d" % (u, v)} + ${u} ${currency}${v} % endfor +% if punted: -PREVIOUSLY PUNTED (pay $30 balance to return): +PREVIOUSLY PUNTED (pay ${currency}${fine * 6} balance to return): % for (u) in sorted(punted, key=lambda p:p.username): ${"%20s (%s)" % (u.username, u.end)} % endfor +% endif diff --git a/templates/ledger b/templates/ledger index 022477b..3478d8b 100644 --- a/templates/ledger +++ b/templates/ledger @@ -1,5 +1,5 @@ % for u in sorted(lame, key=lambda u:u.username): ${week_end.strftime("%F")} Week ${week} - User:${u.username} $-5 + User:${u.username} -${fine} Pool:Owed:${u.username} % endfor diff --git a/templates/week.tmpl b/templates/week.tmpl index 4df9ca6..dee7ee4 100644 --- a/templates/week.tmpl +++ b/templates/week.tmpl @@ -40,9 +40,9 @@ Results for week beginning ${week_start.strftime("%F")}

Beer pool:

- - - + + +
This week: $${5 * len(lame)}
Total: $${pool}
Paid: $${paid}
This week: ${currency}${fine * len(lame)}
Total: ${currency}${pool}
Paid: ${currency}${paid}

Debts:

@@ -53,7 +53,7 @@ Results for week beginning ${week_start.strftime("%F")} \ % endif <% i += 1 %>\ -${u} $${v}\ +${u} ${currency}${v}\ % if i % 3 == 0: %endif @@ -68,4 +68,4 @@ Results for week beginning ${week_start.strftime("%F")} % for (u) in sorted(punted, key=lambda p:p.username):
  • ${u.username} (${u.end})
  • % endfor - \ No newline at end of file + diff --git a/update-participants.py b/update-participants.py index fa65ccc..29b002c 100755 --- a/update-participants.py +++ b/update-participants.py @@ -6,11 +6,7 @@ import sys import xmlrpclib import subprocess - -XMLRPC_ENDPOINT = 'http://iron-blogger.mako.cc/xmlrpc.php' -USER = 'mako' -BLOG_ID = 1 -PAGE_ID = 12 +import config try: subprocess.call(['stty', '-echo']) @@ -20,7 +16,7 @@ finally: subprocess.call(['stty', 'echo']) x = xmlrpclib.ServerProxy(XMLRPC_ENDPOINT) -page = x.wp.getPage(BLOG_ID, PAGE_ID, USER, passwd) +page = x.wp.getPage(BLOG_ID, PARTICIPANTS_PAGE_ID, USER, passwd) text = render.render_template('templates/users.tmpl') page['description'] = text diff --git a/weekly-update.py b/weekly-update.py index 7a44dff..8b60e52 100755 --- a/weekly-update.py +++ b/weekly-update.py @@ -7,9 +7,7 @@ import subprocess import datetime import yaml -XMLRPC_ENDPOINT = 'http://iron-blogger.mako.cc/xmlrpc.php' -USER = 'mako' -BLOG_ID = 1 +from config import * dry_run = False @@ -34,11 +32,11 @@ punt = [] with open('ledger', 'a') as f: f.write("\n") for (user, debt) in debts: - if debt < 30: continue + if debt < (FINE_SIZE * 6): continue punt.append(user) f.write("""\ %(date)s Punt - Pool:Owed:%(user)s $-%(debt)s + Pool:Owed:%(user)s -%(debt)s User:%(user)s """ % {'user': user, 'debt': debt, 'date': date}) -- 2.39.5