-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_activity_report.py
337 lines (284 loc) · 14.2 KB
/
parse_activity_report.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
Copyright 2015 Michael Parker
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import ConfigParser
import argparse
import csv
import re
import scoutbook.util
# This is the Scoutbook logs.csv output format
#"BSA Member ID","First Name","Middle Name","Last Name","Log Type","Date","Nights","Days","Miles","Hours","Frost Points","Location/Name","Notes"
def init_log_file(log_fp):
"""Initialize the given filename and return a csv writer object.
"""
# Should be brand new but lets just make sure
log_fp.truncate()
# Go ahead and write out our header
log_fp.write('"BSA Member ID","First Name","Middle Name","Last Name","Log Type","Date","Nights","Days","Miles","Hours","Frost Points","Location/Name","Notes"\n')
return csv.writer(log_fp, quoting=csv.QUOTE_ALL)
# The output functions will be called via a configurable lambda so we only have one
# argument to help facilitate that configuration. Break out the one array argument
# into individual components for output to the appropriate file.
def output_service_record(record):
# Service entries put the credit value in the Hours field
(bsa_id,
first,
middle_name,
last,
activity_date,
credit,
location,
remarks) = record
activity_file_mapping['Service'].writerow([bsa_id, # BSA Member ID
first, # First Name
middle_name, # Middle Name
last, # Last Name
'Service', # Log Type
activity_date, # Date
'', # Nights
'', # Days
'', # Miles
credit, # Hours
'', # Frost Points
location, # Location/Name
remarks # Note
])
def output_hiking_record(record):
# Hiking entries put the credit value in the Miles field
(bsa_id,
first,
middle_name,
last,
activity_date,
credit,
location,
remarks) = record
activity_file_mapping['Hiking'].writerow([bsa_id, # BSA Member ID
first, # First Name
middle_name, # Middle Name
last, # Last Name
'Hiking', # Log Type
activity_date, # Date
'', # Nights
'', # Days
credit, # Miles
'', # Hours
'', # Frost Points
location, # Location/Name
remarks # Note
])
def output_camping_record(record):
# Camping entries put credit in Nights and Days as well as Frost Points
(bsa_id,
first,
middle_name,
last,
activity_date,
credit,
location,
remarks) = record
# Troopmaster doesn't have the concept of Days so we assume Days is
# One more than nights.
nights = int(credit)
days = nights + 1
# Troopmaster does not have a concept of Frost Points so just use 0
activity_file_mapping['Camping'].writerow([bsa_id, # BSA Member ID
first, # First Name
middle_name, # Middle Name
last, # Last Name
'Camping', # Log Type
activity_date, # Date
str(nights), # Nights
str(days), # Days
'', # Miles
'', # Hours
'0', # Frost Points
location, # Location/Name
remarks # Notes
])
# This will hold a pointer to the csv writer object for each of these activities
activity_file_mapping = {
'Camping': None,
'Service': None,
'Hiking': None,
}
# Regular Expression necessary to parse the various fields from the report.
# This should cover everything unless Troopmaster adds something that isn't covered.
activity_fields = {
'Activity Date': re.compile(r'^Activity Date:\s+(\d+/\d+/\d+)'),
'Activity Type': re.compile('^Activity Type:\s+(.+)'),
'Location': re.compile('^Location:\s+(.+)'),
'Remarks': re.compile('^Remarks:\s+(.+)'),
'Nights': re.compile('^Nights:\s+(\d+)'),
'Hours': re.compile('^Hours:\s+([\d\.\d]+)'),
'Miles': re.compile('^Miles:\s+(\d+)'),
'Amount': re.compile('^Amount:\s+(\d+)'),
'Num Scouts Attended': re.compile('(\d+) Scouts Attended'),
'Num Adults Attended': re.compile('(\d+) Adults Attended'),
}
# Generally this text signals the start of the adult/scout listing, we want
# to know when that starts so our code doesn't get confused by other fields.
markername = re.compile(r'Marker Name')
# Regular Expression to parse out names and credit values.
people = re.compile(r'((\S+)\s+([\w-]+),\s+([\w-]+))+')
# We can parse multiple reports at one time, this regular expression helps
# us know that we are seeing a new report.
newreport = re.compile(r'^Activity Level:')
def check_match(exp, line):
m = exp.search(line)
if m:
return m.group(1).strip()
# Do the hard work
def parse_activity(buf):
found_matches = {}
markername_p = False
for line in buf:
# The markername appears right before we start seeing a list of names
# this helps keep the activity metadata fields separate and avoids
# some accidental parsing.
if markername.search(line):
markername_p = True
m = people.findall(line)
if markername_p and m:
# It's a name line, we may have multiples
for item in m:
credit = item[1]
first = item[3]
last = item[2]
bsa_id = ''
middle_name = ''
# Credit value of 'X' means that we should use the overall
# activity value.
# Every name associated with this activities will be credited
# with a single overall value, where that value comes from depends
# on the type of activity
# It appears that Troopmaster might let you change this character,
# going to stick with the 'X' for now but note that you might need
# to change if you've used something else.
if credit == 'X':
field = scoutbook.util.lookup_mapping('Activity Credit Map',
found_matches['Activity Type'])
try:
credit = found_matches[field]
except KeyError:
# XXX - not sure this is right but in the face of no other
# information its the best we can do I guess
credit = 0
# Attempt to find this person in the scout or adult data file
# this isn't perfect but as long as you don't have too many
# overlapping scouts/adults it will work well.
# If you do have some overlapping names then output Scout Only
# and Adult Only reports from Troopmaster and parse them separately.
key = "%s %s" % (first, last)
data = scoutbook.util.lookup_scout_by_name(key)
if data:
bsa_id = data['member_id']
first = data['first_name']
middle_name = data['middle_name']
last = data['last_name']
else:
data = scoutbook.util.lookup_adult_by_name(key)
if data:
bsa_id = data['member_id']
first = data['first_name']
middle_name = data['middle_name']
last = data['last_name']
remarks = ''
if 'Remarks' in found_matches:
remarks = found_matches['Remarks']
if 'Location' not in found_matches:
print "There is a problem."
print line
print found_matches
next
try:
activity_type = found_matches['Activity Type']
except:
activity_type = None
# Output for each type is configurable so we must go lookup the output
# function from the configuration. If the output function exists
# then it will be called, otherwise we log it as an unsupported
# activity type.
output_function = scoutbook.util.lookup_mapping('Activity Output Lambdas',
activity_type)
if output_function:
# function is just a string from the config, so eval it to use it
output_function = eval(output_function)
try:
output_function(
[bsa_id, first, middle_name, last,
found_matches['Activity Date'],
credit,
found_matches['Location'],
remarks])
except:
print buf
raise
else:
print "Unsupported Activity Type: %s" % (found_matches['Activity Type'],)
else:
for act in activity_fields:
val = check_match(activity_fields[act], line)
if val:
found_matches[act] = val
#print "%s: %s" % (act, val)
def main():
parser = argparse.ArgumentParser(description="Process Troopmaster Scout File")
parser.add_argument('infile', type=argparse.FileType('rb'))
parser.add_argument('--config', required=True, type=argparse.FileType('rb'),
metavar='activity.cfg', default='activity.cfg')
parser.add_argument('--adult-infile', required=False, type=argparse.FileType('rb'),
metavar='Adult.txt', default='Adult.txt',
help='Troopmaster Adult export file.')
parser.add_argument('--scout-infile', required=False, type=argparse.FileType('rb'),
metavar='Scout.txt', default='Scout.txt',
help='Troopmaster Scout export file.')
parser.add_argument('--hiking-logs', required=False, type=argparse.FileType('wb'),
metavar='hikinglogs.csv', default='hikinglogs.csv',
help='CSV file containing Hiking information,')
parser.add_argument('--service-logs', required=False, type=argparse.FileType('wb'),
metavar='servicelogs.csv', default='servicelogs.csv',
help='CSV file containing Service information,')
parser.add_argument('--camping-logs', required=False, type=argparse.FileType('wb'),
metavar='campinglogs.csv', default='campinglogs.csv',
help='CSV file containing Camping information,')
args = parser.parse_args()
config = ConfigParser.ConfigParser(allow_no_value=True)
config.optionxform = str # Makes items case sensitive
config.readfp(args.config)
scoutbook.util.populate_mapping(config, 'Activity Credit Map')
scoutbook.util.populate_mapping(config, 'Activity Output Lambdas')
# Troopmaster seems to output names in it's activity report using just the
# first and last name of the scout/adult, and usually with the nickname.
# Since we want to output first/middle/last in addition to any BSA ID we have
# then we want to parse the Scout and Adult output files to help fill in that
# information.
# Luckily it appears that Troopmaster only outputs "active" scouts/adults in
# the activity reports, so as long as the activity reports and scout/adult
# output files are generated at the same time we shouldn't get any unknown
# entries.
scoutbook.util.read_scout_file(args.scout_infile)
scoutbook.util.read_adult_file(args.adult_infile)
activity_file_mapping['Camping'] = init_log_file(args.camping_logs)
activity_file_mapping['Service'] = init_log_file(args.service_logs)
activity_file_mapping['Hiking'] = init_log_file(args.hiking_logs)
buf = []
for line in args.infile:
if newreport.findall(line):
parse_activity(buf)
buf = []
else:
buf.append(line)
# We will be left with one file buffer to parse, so go parse that
parse_activity(buf)
if __name__ == '__main__':
main()