| 1 |
# Richard Darst, June 2010
|
| 2 |
|
| 3 |
import mailbox
|
| 4 |
import string
|
| 5 |
import sys
|
| 6 |
import os
|
| 7 |
import re
|
| 8 |
|
| 9 |
import emailutil
|
| 10 |
from emailutil import Message, Email
|
| 11 |
import dc_objs
|
| 12 |
|
| 13 |
templateFname = sys.argv[1]
|
| 14 |
basename, ext = os.path.splitext(templateFname)
|
| 15 |
template = open(templateFname).read()
|
| 16 |
|
| 17 |
template_new = [ ]
|
| 18 |
headers = { }
|
| 19 |
for line in template.split('\n'):
|
| 20 |
if template_new:
|
| 21 |
template_new.append(line)
|
| 22 |
elif not line and not template_new:
|
| 23 |
# skip blank lines before template body starts
|
| 24 |
continue
|
| 25 |
elif line and line[0] == '#':
|
| 26 |
line = line[1:].strip()
|
| 27 |
headers[line.split(':',1)[0].strip()] = line.split(':',1)[1].strip()
|
| 28 |
else:
|
| 29 |
template_new.append(line)
|
| 30 |
print headers
|
| 31 |
template = "\n".join(template_new)
|
| 32 |
|
| 33 |
People = dc_objs.get_people(where=headers['Where'])
|
| 34 |
del headers['Where']
|
| 35 |
|
| 36 |
if 'Filter' in headers:
|
| 37 |
def f(P):
|
| 38 |
from dc_objs import CAT_B, CAT_BA, CAT_BF, CAT_BAF, CAT_PRO, CAT_COR, CAT_NONE
|
| 39 |
from dc_objs import CAT_SPONS, CAT_PAID, CAT_UNSPONS
|
| 40 |
from dc_objs import DC_PLAN, DC_NOPLAN, DC_NO, DC_YES
|
| 41 |
from dc_objs import FOOD_REG, FOOD_VEG, FOOD_VEGAN, FOOD_OTHER, FOOD_NO
|
| 42 |
from dc_objs import ACCOM_NO
|
| 43 |
#print eval(headers['Filter'])
|
| 44 |
return eval(headers['Filter'])
|
| 45 |
|
| 46 |
People = filter(f, People)
|
| 47 |
del headers['Filter']
|
| 48 |
|
| 49 |
# Include only Penta IDs found in this header
|
| 50 |
if 'PentaIDs' in headers:
|
| 51 |
PentaIDs = headers['PentaIDs']
|
| 52 |
PentaIDs = re.split('[, ]+', PentaIDs)
|
| 53 |
PentaIDs = set(int(x) for x in PentaIDs if x.strip())
|
| 54 |
People = [P for P in People if P.person_id in PentaIDs]
|
| 55 |
del headers['PentaIDs']
|
| 56 |
# Include only Penta IDs found in this file
|
| 57 |
if 'PentaIDsFile' in headers:
|
| 58 |
PentaIDs = open(headers['PentaIDsFile']).read()
|
| 59 |
PentaIDs = re.split('[^0-9]+', PentaIDs)
|
| 60 |
PentaIDs = set(int(x) for x in PentaIDs if x.strip())
|
| 61 |
People = [P for P in People if P.person_id in PentaIDs]
|
| 62 |
del headers['PentaIDsFile']
|
| 63 |
# Remove all Penta IDs found in this header
|
| 64 |
if 'NotPentaIDs' in headers:
|
| 65 |
PentaIDs = headers['NotPentaIDs']
|
| 66 |
PentaIDs = re.split('[, ]+', PentaIDs)
|
| 67 |
PentaIDs = set(int(x) for x in PentaIDs if x.strip())
|
| 68 |
People = [P for P in People if P.person_id not in PentaIDs]
|
| 69 |
del headers['NotPentaIDs']
|
| 70 |
# Remove all Penta IDs found in this filename
|
| 71 |
if 'NotPentaIDsFile' in headers:
|
| 72 |
PentaIDs = open(headers['NotPentaIDsFile']).read()
|
| 73 |
PentaIDs = re.split('[^0-9]+', PentaIDs)
|
| 74 |
PentaIDs = set(int(x) for x in PentaIDs if x.strip())
|
| 75 |
People = [P for P in People if P.person_id in PentaIDs]
|
| 76 |
del headers['NotPentaIDsFile']
|
| 77 |
|
| 78 |
|
| 79 |
# Print out summary of who gets what
|
| 80 |
for P in People:
|
| 81 |
print P.regSummary(), P.personHeader()
|
| 82 |
print "Total people:", len(People)
|
| 83 |
|
| 84 |
|
| 85 |
# See if there are unknown headers in the file:
|
| 86 |
if set(headers.keys()) > set(('Subject', )):
|
| 87 |
print
|
| 88 |
print "Error: Unknown headers detected:"
|
| 89 |
print set(headers.keys()) - set(('Subject', ))
|
| 90 |
sys.exit(1)
|
| 91 |
|
| 92 |
#sys.exit()
|
| 93 |
|
| 94 |
mbox = mailbox.mbox(basename+'.mbox')
|
| 95 |
mbox.clear() ; mbox.flush()
|
| 96 |
|
| 97 |
for P in People:
|
| 98 |
|
| 99 |
Body = template % P.__dict__
|
| 100 |
|
| 101 |
msg = emailutil.Message(
|
| 102 |
From=Email("DebConf Registration Team", "registration@debconf.org"),
|
| 103 |
To=Email(P.name, P.email),
|
| 104 |
Subject=headers['Subject'],
|
| 105 |
Body=Body,
|
| 106 |
extraMsgid="".join(x for x in basename
|
| 107 |
if x in string.ascii_letters+string.digits),
|
| 108 |
ReplyTo=Email("DebConf Registration Team", "registration@debconf.org"),
|
| 109 |
Cc=Email("DebConf Registration Team", "registration@debconf.org"),
|
| 110 |
)
|
| 111 |
mbox.add(str(msg))
|