/
export_group_vcard.py
209 lines (148 loc) · 5.22 KB
/
export_group_vcard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# coding=utf-8
"""Online Scout Manager Interface.
Usage:
export_group_vcard.py [-d] [--term=<term>] [--email=<address>]
<apiid> <token> <outdir> <section>...
export_group_vcard.py (-h | --help)
export_group_vcard.py --version
Options:
<section> Section to export.
<outdir> Output directory for vcard files.
-d,--debug Turn on debug output.
--email=<email> Send to only this email address.
--term=<term> Which OSM term to use [default: current].
-h,--help Show this screen.
--version Show version.
"""
import os.path
import logging
import itertools
import functools
import smtplib
from docopt import docopt
import osm
import vobject as vo
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from group import Group, OSM_REF_FIELD
from update import MAPPING
from export_vcards import (
parse_tel, next_f, get,
member2vcard)
log = logging.getLogger(__name__)
DEF_CACHE = "osm.cache"
DEF_CREDS = "osm.creds"
FROM = "Richard Taylor <r.taylor@bcs.org.uk>"
def send(to, subject, vcards, fro=FROM):
for dest in to:
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = fro
msg['To'] = dest
body = MIMEText(vcards, 'vcard')
body.add_header('Content-Disposition', 'attachment',
filename="group.vcf")
msg.attach(body)
hostname = 'localhost'
s = smtplib.SMTP(hostname)
try:
s.sendmail(fro, dest, msg.as_string())
except:
log.error(msg.as_string(),
exc_info=True)
s.quit()
def member2vcards(member, section):
"""
Create up to three vcards for each member entry.
1. YP member
2. Dad
3. Mum
"""
ret = [member2vcard(member, section)]
# No need to continue if we are doing an adult.
if section == 'Adult' or (
member.get('Patrol', '') == 'Leaders' and
int(member.age().days / 365) > 18):
return ret
for parent in ['contact_primary_1', 'contact_primary_2']:
f = functools.partial(get, member=member, section=parent)
full_name = "{} {}".format(f('firstname'),
f('lastname'))
if full_name.strip() != '':
ret += [process_parent(full_name, member, section, f), ]
return ret
def process_parent(name, member, section, f):
# If the name does not appear to have lastname part
# add it from the member name.
if len(name.strip().split(' ')) < 2:
name = "{} {}".format(name.strip(), member['last_name'])
j = vo.vCard()
uid = j.add('UID')
uid.value = "{}{}.OSM@thegrindstone.me.uk".format(
name.replace(" ", ""),
member[OSM_REF_FIELD])
j.add('n')
j.n.value = vo.vcard.Name(
family=f('lastname') if f('lastname').strip() else member['last_name'],
given=f('firstname'))
j.add('fn')
j.fn.value = name
next_ = next_f(j, 0).next_f
for _ in ['phone1', 'phone2']:
number, name = parse_tel(f(_), _)
next_('tel', name, number)
for _ in ['email1', 'email2']:
if f(_).strip() == "":
# Ignore empty emails.
continue
# If the email is marked as private, add it as a note.
if (f(_).startswith('x ') or
f("{}_leaders".format(_)) != "yes"):
next_('note', _,
"Private email address: {}".format(f(_)))
else:
next_('email', _, f(_))
next_('adr', 'Primary',
vo.vcard.Address(
street=f('address1'),
city=f('address2'),
region=f('address3'),
country=f('address4'),
code=f('postcode')))
org = j.add('org')
org.value = [section, ]
note = j.add('note')
note.value = "Child: {} {} ({})\n".format(
member['first_name'], member['last_name'], section)
cat = j.add('CATEGORIES')
cat.value = ("7th", "7th Lichfield Parent")
return j.serialize()
def _main(osm, auth, sections, outdir, email, term):
assert os.path.exists(outdir) and os.path.isdir(outdir)
group = Group(osm, auth, MAPPING.keys(), term)
for section in sections:
assert section in group.SECTIONIDS.keys(), \
"section must be in {!r}.".format(group.SECTIONIDS.keys())
vcards = []
for section in sections:
section_vcards = [member2vcards(member, section) for
member in group.section_all_members(section)]
# flatten list of lists.
vcards += list(itertools.chain(*section_vcards))
open(os.path.join(outdir, "group.vcf"), 'w').writelines(vcards)
if email:
send([email, ], "OSM Group vcards", "".join(vcards))
if __name__ == '__main__':
args = docopt(__doc__, version='OSM 2.0')
if args['--debug']:
level = logging.DEBUG
else:
level = logging.WARN
if args['--term'] in [None, 'current']:
args['--term'] = None
logging.basicConfig(level=level)
log.debug("Debug On\n")
auth = osm.Authorisor(args['<apiid>'], args['<token>'])
auth.load_from_file(open(DEF_CREDS, 'r'))
_main(osm, auth, args['<section>'],
args['<outdir>'], args['--email'], args['--term'])