-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_sdwis_data.py
284 lines (252 loc) · 11.1 KB
/
get_sdwis_data.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
# -*- coding: utf-8 -*-
import csv, os, errno, threading, queue, sys
import EPA.Envirofacts.Configuration as config
import EPA.Envirofacts.SDWIS.Violation as violation
import EPA.Envirofacts.SDWIS.WaterSystem as water_system
from time import sleep
from optparse import OptionParser
from datetime import datetime
from utilities import suppress_stdout, get_timespan, print_progressbar, RestApiResultType
def wrapper(func, args, queue):
try:
queue.put(func(*args))
except Exception as e:
queue.put(e)
finally:
return
def spinning_cursor():
while True:
for cursor in '|/-\\':
yield cursor
def run_thread(thread, queue, show_spinner=True):
spinner = spinning_cursor()
thread.start()
while thread.isAlive():
if show_spinner:
sys.stdout.write(next(spinner))
sys.stdout.flush()
sys.stdout.write('\b')
sleep(0.25)
thread.join()
r = queue.get()
if isinstance(r, Exception):
raise r
return r
def parse_results(result, water_system = True):
final_result = []
v_count = 0
csv_list = result
agency_header = 'water_system.primacy_agency_code'
if not water_system:
agency_header = 'violation.primacy_agency_code'
if not csv_list and len(csv_list) <= 0:
return None
total = len(csv_list)
pbar_length = print_progressbar(v_count, total, bar_length=20, suffix='(%d/%d)' % (v_count, total))
for w in csv_list:
v_count += 1
#Put any post processing logic here
if w[agency_header] == config.state.lower():
final_result.append(w)
pbar_length = print_progressbar(v_count, total, bar_length=20, suffix='(%d/%d) ' % (v_count, total), old_length=pbar_length)
print_progressbar(v_count, total, bar_length=20, suffix='(%d/%d)' % (v_count, total), old_length=pbar_length)
return final_result
def write_results(filename, result):
if not filename:
print('filename cannot be empty or null')
return
if not result and len(result) > 0:
return
try:
os.remove(filename)
except OSError as e:
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise
with open(filename, 'w', newline='') as f:
w = csv.DictWriter(f, result[0].keys())
#w = csv.writer(f)
w.writeheader()
w.writerows(result)
def main():
system_count = 0
count = options.max_records
system_results = []
results = []
current_step = 1
q = queue.Queue()
config.state = options.state
config.result_format = RestApiResultType.CSV
print('====== Water safety violations for the state: %s ======' % config.state_fullname)
if not options.v_only:
sys.stdout.write('---Getting water system count (step %d/%d): ' % (current_step, script_steps))
start_time = datetime.now().timestamp()
try:
t = threading.Thread(target=wrapper, args=(water_system.get_count_by_state, [], q))
system_count = run_thread(t, q)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
system_count = 0
else:
sys.stdout.write(' [done]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
current_step = current_step + 1
sys.stdout.write('---Getting %d water systems (step %d/%d): ' % (system_count, current_step, script_steps))
start_time = datetime.now().timestamp()
try:
t = threading.Thread(target=wrapper, args=(water_system.get_by_state, [], q))
system_results = run_thread(t, q)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
system_results = []
else:
sys.stdout.write(' [done]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
current_step = current_step + 1
if get_all_violations:
sys.stdout.write('---Getting violation count (step %d/%d): ' % (current_step, script_steps))
start_time = datetime.now().timestamp()
try:
t = threading.Thread(target=wrapper, args=(violation.get_count_by_state, [], q))
count = run_thread(t, q)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
count = 0
else:
sys.stdout.write(' [done]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
current_step = current_step + 1
sys.stdout.write('---Getting %d violations (step %d/%d): ' % (count, current_step, script_steps))
start_time = datetime.now().timestamp()
try:
if get_all_violations:
if options.v_only:
t = threading.Thread(target=wrapper, args=(violation.get_by_state, [], q))
results = run_thread(t, q)
else:
if system_results and len(system_results) > 0:
s_count = 0
pbar_length = print_progressbar(s_count, system_count, bar_length=20, suffix='(%d/%d)' % (s_count, system_count))
for s in system_results:
s_count += 1
t = threading.Thread(target=wrapper, args=(violation.get_by_water_system, (s['water_system.pwsid'],), q))
r = run_thread(t, q, False)
if r and len(r) > 0:
results += r
pbar_length = print_progressbar(s_count, system_count, bar_length=20, suffix='(%d/%d)' % (s_count, system_count), old_length=pbar_length)
print_progressbar(s_count, system_count, bar_length=20, suffix='(%d/%d)' % (s_count, system_count), old_length=pbar_length)
else:
sys.stdout.write(' [skipping]')
else:
t = threading.Thread(target=wrapper, args=(violation.get_by_state, (config.state, count,), q))
results = run_thread(t, q)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
results = []
else:
sys.stdout.write(' [done]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
current_step = current_step + 1
if not options.v_only:
sys.stdout.write('---Parsing water system results (step %d/%d): ' % (current_step, script_steps))
start_time = datetime.now().timestamp()
final_system_results = []
if system_results and len(system_results) > 0:
try:
t = threading.Thread(target=wrapper, args=(parse_results, (system_results,), q))
final_system_results = run_thread(t, q, False)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
final_system_results = []
else:
sys.stdout.write(' [done]')
else:
sys.stdout.write(' [skipping]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
current_step = current_step + 1
sys.stdout.write('---Parsing violation results (step %d/%d): ' % (current_step, script_steps))
start_time = datetime.now().timestamp()
final_results = []
if results and len(results) > 0:
try:
t = threading.Thread(target=wrapper, args=(parse_results, (results, False,), q))
final_results = run_thread(t, q, False)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
final_results = []
else:
sys.stdout.write(' [done]')
else:
sys.stdout.write(' [skipping]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
current_step = current_step + 1
if not options.v_only:
sys.stdout.write('---Saving water system results to %s(step %d/%d): ' % (system_filename, current_step, script_steps))
start_time = datetime.now().timestamp()
if final_system_results and len(final_system_results) > 0:
try:
t = threading.Thread(target=wrapper, args=(write_results, (system_filename, final_system_results,), q))
run_thread(t, q)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
else:
sys.stdout.write(' [done]')
else:
sys.stdout.write(' [skipping]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
sys.stdout.write('---Saving violation results to %s (step %d/%d): ' % (violation_filename, current_step, script_steps))
start_time = datetime.now().timestamp()
if final_results and len(final_results) > 0:
try:
t = threading.Thread(target=wrapper, args=(write_results, (violation_filename, final_results,), q))
run_thread(t, q)
except Exception as e:
sys.stdout.write(' [failed] [Exception: %s] ' % e)
else:
sys.stdout.write(' [done]')
else:
sys.stdout.write(' [skipping]')
end_time = datetime.now().timestamp()
sys.stdout.write(' [%s]\n' % get_timespan(start_time, end_time))
script_steps = 8
system_filename = 'watersystems.csv'
violation_filename = 'violations.csv'
get_all_violations = True
if __name__ == "__main__":
try:
parser = OptionParser()
parser.add_option("-s", "--state", dest = "state", default = 'DE',
help = "state to get violations for (2 letter abbreviation. ie . de, pa, ca, tx, etc.")
parser.add_option("-q", "--quiet",
action = "store_false", dest = "verbose", default = True,
help = "don't print status messages to stdout")
parser.add_option("-m", "--max", dest = "max_records", default = 10, type = int,
help = "maximum violations to get")
parser.add_option("-V", "--violations-only", action = "store_true", dest = "v_only", default = False,
help = "get violations only")
(options, args) = parser.parse_args()
if options.max_records > 0:
script_steps = script_steps - 1
get_all_violations = False
if options.v_only:
script_steps = script_steps - 4
script_start_time = datetime.now().timestamp()
if not options.verbose:
with suppress_stdout():
main()
else:
main()
script_end_time = datetime.now().timestamp()
sys.stdout.write('\rScript completed in: %s\n' % get_timespan(script_start_time, script_end_time))
except KeyboardInterrupt:
print('process interrupted')
except Exception as e:
print(e)
finally:
sys.exit()