Mercurial > hg > python
annotate mailer.py @ 18:1cef0e5f5851
add -m for .sig
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Mon, 20 Jul 2020 11:21:30 +0100 |
parents | 2d7c91f89f6b |
children | 535e3327ed1b |
rev | line source |
---|---|
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
1 #!/usr/bin/python |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
2 '''Attempt at flexible mailout functionality |
18 | 3 Usage: mailer.py [-n] [-s] [-C cc string] [-c COLSPEC[,COLSPEC]*] [-B bcc string] [-b COLSPEC[,COLSPEC]*] [-S col[,col]*] [-a COLSPEC[,COLSPEC]*] [-p COLPAT]* [-SA file[,file]*] [-R reply-to] [-m .sigfilename] COLSPEC[,COLSPEC]* subject {addr-file|-} body-file |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
4 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
5 Sends the body as a message from me with subject to destinations per |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
6 lines in the addr-file selected by COLSPECs (to:) or -c/-b COLSPECs (Cc:/Bcc:) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
7 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
8 -n for dry run, prints to stdout |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
9 -c for Cc column(s) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
10 -C for static Cc |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
11 -b for Bcc columns(s) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
12 -B for static Bcc |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
13 -a for attachment file column(s) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
14 -A for attachment file pattern column(s) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
15 -SA for static attachment files |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
16 -u Use unicode for attachments |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
17 -s for substitute into body |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
18 -S for columns to substitute as such |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
19 -p for augmentation pattern for a column |
4 | 20 -R for replyTo address |
18 | 21 -m for .sig file, None for none |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
22 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
23 COLSPEC is of the form a[:n[:f[:g]]] selects from addr-file, which must be tsv |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
24 a gives the column for an email address |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
25 n (optional) gives column for a name |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
26 f gives format for the name: FS, SF or S.F for |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
27 forenames surname (fornames space separated) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
28 surname forenames (space separated) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
29 surname, forenames (space separated) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
30 default is FS |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
31 _ will be replaced by space in surnames |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
32 g gives column for gender (for pronouns), m or f |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
33 COLPAT takes the form i:template, where i selects an address column |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
34 and template is a string containing exactly 1 "%s", which is replaced with |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
35 the column value to give the string which will be used for COLSPEC |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
36 references to that column, e.g. 1:S%s@sms.ed.ac.uk |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
37 if column 1 contains bare student numbers |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
38 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
39 -s enables body substitution. body may contain |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
40 %(fi)s first forename of column i |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
41 %(si)s surname |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
42 %(fsi)s all forenames |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
43 %(i)s the undivided original and/or -S col value |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
44 if there is a supplied gender |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
45 %(pni)s 'he'/'she' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
46 %(pai)s 'him'/'her' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
47 %(pgi)s 'his/her' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
48 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
49 All column indices are 1-origin, as for cut''' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
50 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
51 import smtplib, sys, re, os.path, codecs |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
52 from email.mime.text import MIMEText |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
53 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
54 addrPat=re.compile("<([^>]*)>") |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
55 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
56 def usage(hint=None): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
57 if hint is None: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
58 print __doc__ |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
59 exit() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
60 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
61 print >>sys.stderr,"Trouble with your commandline at %s\n %s"%(hint, |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
62 __doc__) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
63 exit(1) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
64 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
65 def parseCols(specs,where): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
66 return [Column(s,where) for s in specs.split(',')] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
67 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
68 def parsePat(spec): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
69 (c,t)=spec.split(':') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
70 c=int(c) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
71 found=False |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
72 for colTab in (ccCols,bccCols,toCols,attCols): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
73 if c in colTab: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
74 colTab[c].addTemplate(t) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
75 found=True |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
76 if not found: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
77 print >>sys.stderr, "Warning, template supplied for column %s, but no use of the column found!"%c |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
78 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
79 def addrList(addrFields,cols,att=False): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
80 global someExpand |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
81 if att and someExpand: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
82 # There were some file patterns |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
83 return itertools.chain(*(c.fullAddr(addrFields,True) for c in cols.values())) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
84 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
85 return [c.fullAddr(addrFields) for c in cols.values()] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
86 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
87 def addrLine(hdr,addrFields,cols): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
88 return "%s: %s"%(hdr,", ".join(addrList(addrFields,cols))) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
89 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
90 def subDict(addrFields): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
91 res={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
92 for c in names.values(): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
93 c.subDo(addrFields,res) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
94 for c in subs.values(): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
95 if c not in names: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
96 c.subDo(addrFields,res) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
97 return res |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
98 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
99 bccCols={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
100 ccCols={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
101 attCols={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
102 toCols={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
103 names={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
104 subs={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
105 CC=[] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
106 BCC=[] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
107 rawCols={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
108 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
109 class Column: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
110 _expand=False |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
111 def __init__(self,spec,where): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
112 global names, subs |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
113 parts=spec.split(':') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
114 if (len(parts)<1 or len(parts)>4): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
115 print >>sys.stderr, "col spec. must have 1--4 :-separated parts: %s"%parts |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
116 usage('colspec') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
117 self.a=int(parts[0]) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
118 if len(parts)>1: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
119 self.n=int(parts[1]) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
120 if len(parts)>2: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
121 self.f=parts[2] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
122 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
123 self.f='FS' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
124 if len(parts)>3: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
125 self.g=int(parts[3]) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
126 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
127 self.g=None |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
128 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
129 self.n=None |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
130 if self.a<=0: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
131 print >>sys.stderr, "addr column index %s not allowed -- 1-origin indexing"%self.a |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
132 exit(2) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
133 if self.a in where: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
134 print >>sys.stderr, "duplicate column %s"%self.a |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
135 exit(2) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
136 if self.n is not None: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
137 if self.n<=0: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
138 print >>sys.stderr, "name column index %s not allowed -- 1-origin indexing"%self.n |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
139 exit(3) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
140 if self.n in where: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
141 print >>sys.stderr, "can't use column %s as both name and address"%self.n |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
142 exit(3) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
143 if self.n in names: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
144 print >>sys.stderr, "attempt to redefine %s from \"%s\" to \"%s\""%(self.n,names[self.n],self) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
145 exit(3) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
146 if self.f not in ('FS','SF','S.F'): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
147 print >>sys.stderr, "name format %s not recognised"%self.f |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
148 exit(4) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
149 where[self.a]=self |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
150 if self.n is not None: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
151 if isinstance(self,RawColumn): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
152 subs[self.n]=self |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
153 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
154 names[self.n]=self |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
155 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
156 def __str__(self): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
157 if self.n is None: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
158 return str(self.a) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
159 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
160 return "%s:%s"%(self.a,self.n) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
161 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
162 def __repr__(self): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
163 return str(self) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
164 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
165 def addTemplate(self,template): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
166 try: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
167 print >>sys.stderr,"Attempt to overwrite existing template \"%s\" for %s with \"%s\""%(self.template, |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
168 self.a, |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
169 template) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
170 except AttributeError: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
171 self.template=template |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
172 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
173 def name(self): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
174 return self.n |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
175 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
176 def expAddr(self,fields): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
177 addr=fields[self.a-1] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
178 try: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
179 return self.template%addr |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
180 except AttributeError: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
181 return addr |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
182 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
183 def fullAddr(self,fields,att=False): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
184 global someExpand |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
185 if self.n is None: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
186 res=self.expAddr(fields) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
187 if att and someExpand: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
188 if self._expand: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
189 return glob.iglob(res) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
190 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
191 return [res] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
192 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
193 return res |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
194 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
195 return '"%s" <%s>'%(fields[self.n-1].replace('_',' '),self.expAddr(fields)) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
196 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
197 def subDo(self,addrFields,dict): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
198 f=addrFields[self.n-1] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
199 dict[str(self.n)]=f |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
200 nparts=f.split(' ') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
201 if self.f=='FS': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
202 sur=nparts.pop() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
203 elif self.f=='SF': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
204 sur=nparts.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
205 elif self.f=='S.F': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
206 sur=nparts.pop(0)[:-1] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
207 fores=nparts |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
208 dict['fs%s'%self.n]=' '.join(fores) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
209 dict['f%s'%self.n]=fores[0] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
210 dict['s%s'%self.n]=sur.replace('_',' ') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
211 if self.g is not None: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
212 gg=addrFields[self.g-1] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
213 if gg=='m': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
214 dict['pn%s'%self.n]='he' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
215 dict['pa%s'%self.n]='him' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
216 dict['pg%s'%self.n]='his' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
217 elif gg=='f': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
218 dict['pn%s'%self.n]='she' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
219 dict['pa%s'%self.n]='her' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
220 dict['pg%s'%self.n]='her' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
221 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
222 print >>sys.stderr,"Warning, unrecognised gender in column %s: %s"%(self.n,gg) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
223 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
224 def setExpand(self): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
225 self._expand=True |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
226 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
227 class RawColumn(Column): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
228 '''Not for person names, just raw text''' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
229 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
230 def subDo(self,addrFields,dict): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
231 f=addrFields[self.n-1] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
232 dict[str(self.n)]=f |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
233 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
234 def doAtt(msg,att,codec): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
235 (mt,enc)=mimetypes.guess_type(att) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
236 (tp,subtp)=mt.split('/',2) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
237 if tp=='text': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
238 attf=codecs.open(att,'r',codec) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
239 atm=MIMEText(attf.read(),subtp,codec) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
240 elif tp=='application': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
241 from email.mime.application import MIMEApplication |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
242 attf=open(att,'r') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
243 atm=MIMEApplication(attf.read(),subtp) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
244 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
245 print >>sys.stderr, "Help: Media type %s (for attachment %s) not supported"%(mt,att) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
246 exit(5) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
247 atm.add_header('Content-Disposition','attachment', |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
248 filename=os.path.basename(att)) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
249 msg.attach(atm) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
250 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
251 dryrun=False |
4 | 252 replyTo=None |
18 | 253 sigfile=None |
4 | 254 |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
255 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
256 doSub=False |
4 | 257 |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
258 pats=[] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
259 someExpand=False |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
260 codec='iso-8859-1' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
261 staticAtts=[] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
262 while sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
263 if sys.argv[0]=='-n': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
264 dryrun=True |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
265 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
266 elif sys.argv[0]=='-c' and ccCols=={}: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
267 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
268 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
269 parseCols(sys.argv.pop(0),ccCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
270 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
271 usage('cc') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
272 elif sys.argv[0]=='-C' and CC==[]: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
273 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
274 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
275 CC=sys.argv.pop(0).split(',') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
276 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
277 usage('CC') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
278 elif sys.argv[0]=='-b' and bccCols=={}: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
279 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
280 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
281 parseCols(sys.argv.pop(0),bccCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
282 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
283 usage('bcc') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
284 elif sys.argv[0]=='-B' and BCC==[]: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
285 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
286 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
287 BCC=sys.argv.pop(0).split(',') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
288 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
289 usage('BCC') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
290 elif sys.argv[0] in ('-a','-A','-SA'): # and attCols=={} |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
291 expand=sys.argv[0]=='-A' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
292 static=sys.argv[0]=='-SA' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
293 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
294 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
295 if static: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
296 staticAtts=sys.argv.pop(0).split(',') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
297 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
298 pc=parseCols(sys.argv.pop(0),attCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
299 if expand: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
300 import itertools, glob |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
301 someExpand=True |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
302 for c in pc: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
303 c.setExpand() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
304 from email.mime.multipart import MIMEMultipart |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
305 import mimetypes |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
306 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
307 usage('attachment') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
308 elif sys.argv[0]=='-u': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
309 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
310 codec='utf-8' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
311 elif sys.argv[0]=='-s': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
312 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
313 doSub=True |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
314 elif sys.argv[0]=='-S' and rawCols=={}: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
315 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
316 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
317 for c in sys.argv.pop(0).split(','): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
318 RawColumn("%s:%s"%(c,c),rawCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
319 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
320 usage('raw subs') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
321 elif sys.argv[0]=='-p': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
322 sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
323 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
324 pats.append(sys.argv.pop(0)) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
325 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
326 usage('pat') |
4 | 327 elif sys.argv[0]=='-R': |
328 sys.argv.pop(0) | |
329 if sys.argv: | |
330 replyTo=sys.argv.pop(0) | |
331 else: | |
332 usage('reply to') | |
18 | 333 elif sys.argv[0]=='-m': |
334 sys.argv.pop(0) | |
335 if sys.argv: | |
336 sigfile=sys.argv.pop(0) | |
337 else: | |
338 usage('sig file name') | |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
339 elif sys.argv[0][0]=='-': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
340 print sys.argv |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
341 usage() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
342 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
343 break |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
344 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
345 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
346 parseCols(sys.argv.pop(0),toCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
347 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
348 usage('to') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
349 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
350 pats=[parsePat(p) for p in pats] |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
351 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
352 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
353 subj=sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
354 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
355 usage('subj') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
356 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
357 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
358 af=sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
359 if af=='-': |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
360 addrFile=sys.stdin |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
361 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
362 try: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
363 addrFile=open(af,'r') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
364 except: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
365 usage('addr: %s'%sys.exc_value) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
366 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
367 usage('addr') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
368 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
369 if sys.argv: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
370 bf=sys.argv.pop(0) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
371 try: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
372 bodyFile=open(bf,'r') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
373 except: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
374 usage('body: %s'%sys.exc_value) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
375 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
376 usage('body') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
377 |
18 | 378 signature=None |
379 if sigfile!='None': | |
380 if sigfile is None: | |
381 sigfile="/home/ht/.signature" | |
382 try: | |
383 sig=open(sigfile,"r") | |
384 signature=sig.read().rstrip() | |
385 except: | |
386 if sigfile!="/home/ht/.signature": | |
387 raise | |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
388 |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
389 CS=', ' |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
390 body=bodyFile.read().rstrip() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
391 if not dryrun: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
392 mailer=smtplib.SMTP() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
393 mailer.connect() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
394 for l in addrFile: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
395 addrFields=l.rstrip().split('\t') |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
396 if doSub: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
397 bodyPlus=body%subDict(addrFields) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
398 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
399 bodyPlus=body |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
400 if signature is not None: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
401 bodyPlus+="\n--\n" |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
402 bodyPlus+=signature |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
403 if attCols or staticAtts: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
404 msg=MIMEMultipart() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
405 msg.attach(MIMEText(bodyPlus)) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
406 else: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
407 msg=MIMEText(bodyPlus) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
408 #to=addrLine("To",addrFields,toCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
409 to=addrList(addrFields,toCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
410 #msg=to |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
411 #recips=addrPat.findall(to) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
412 msg['To']=CS.join(to) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
413 recips=[]+list(to) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
414 cc=CC |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
415 if ccCols: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
416 cc+=addrList(addrFields,ccCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
417 if cc!=[]: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
418 msg["Cc"]=CS.join(cc) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
419 recips+=list(cc) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
420 bcc=BCC |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
421 if bccCols: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
422 bcc+=addrList(addrFields,bccCols) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
423 if bcc!=[]: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
424 msg["Bcc"]=CS.join(bcc) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
425 recips+=list(bcc) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
426 msg["Subject"]=subj |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
427 for att in staticAtts: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
428 doAtt(msg,att,codec) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
429 if attCols: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
430 for att in addrList(addrFields,attCols,True): |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
431 doAtt(msg,att,codec) |
4 | 432 if replyTo is not None: |
433 msg["Reply-To"]=replyTo | |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
434 if dryrun: |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
435 print recips |
4 | 436 print msg.keys() |
2
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
437 print msg.as_string() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
438 exit() |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
439 print "mailing to %s"%recips |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
440 mailer.sendmail("ht@inf.ed.ac.uk",recips,msg.as_string()) |
e07789816ca5
adding more python files from lib/python on origen
Henry Thompson <ht@markup.co.uk>
parents:
diff
changeset
|
441 mailer.quit() |