-
Notifications
You must be signed in to change notification settings - Fork 2
/
fech.py
450 lines (365 loc) · 14.6 KB
/
fech.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# porting of fetch to python code borrowed from https://github.com/NYTimes/Fech
# wich is under the apache license
# https://github.com/NYTimes/Fech/blob/master/LICENSE
import re
import traceback
#state enum
STATE_UNKNOWN = 0
STATE_HEADER = 1
STATE_BODY = 2
STATE_END = 3
from fechbase import SkipException
import tempfile # import a temp file to unpack a zip file entry into.
import zipfile
import os
import cache
import fechout
import sys
#import fec.version.v1.F1
#import fec.version.v1 as v1
import fec.version.v1
import fec.version.v2
test2= fec.version.v2.Version()
import fec.version.v3
import fec.version.v5_0
import fec.version.v5_1
import fec.version.v5_2
import fec.version.v5_3
import fec.version.v6_1
import fec.version.v6_2
import fec.version.v6_3
import fec.version.v6_4
import fec.version.v7_0
import fec.version.v8_0
#print sys.path
#sys.path
# before we import these, we must define the base classes, do that here so we have everything in one file
BASEURL = 'ftp://ftp.fec.gov/FEC/electronic/'
def listing(year):
u"""
execute the listing
"""
dirlisting = cache.cacheweb('ftp://ftp.fec.gov/FEC/electronic/')
for filename in dirlisting.split():
#print("consider file %s" % filename)
if (filename.find(".zip") >0 ):
if not re.match(year,filename) :
print "skip file %s" % filename
continue
#print("going to get file %s" % BASEURL + filename)
cached_file = cache.cachewebfile(BASEURL + filename)
#print("cached file %s" % cached_file)
csv_data = ZipCSV()
# parser = fech_rendered_maps.FieldParser()
parser = Parser()
output_object = fechout.Fechout()
output_object.set_input_url(BASEURL + filename)
output_object.set_input_zipfilename(filename)
#parser.set_zipfilename(filename)
csv_data.process_generate (
BASEURL,
filename,
cached_file,
"FecElectronicFilings",
parser,
output_object)
class ZipCSV:
def chunks(self):
""" Yield successive n-sized chunks from l.
http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
"""
n=CHUNKSIZE
for i in xrange(0, len(self._rows), n):
yield self._rows[i:i+n]
def process_generate (self, baseurl, urlfile, filename, classname, parser, out):
zfile = zipfile.ZipFile(filename)
for name in zfile.namelist():
try :
(dirname, ifilename) = os.path.split(name)
# print "eval filename %s from zip %s" % (ifilename, filename)
out_file = out.create_file(ifilename, filename, baseurl, urlfile)
if (not out_file.exists()):
print "before reading filename %s from zip %s" % (ifilename, filename)
d = zfile.read(name)
(fd,fname)= tempfile.mkstemp()
tmpfile = open(fname, 'w')
tmpfile.write(d)
d=None # discard the data
tmpfile.close()
tmpfile=None
print "after reading filename %s from zip %s" % (ifilename, filename)
parser.parse_file_data(ifilename, filename, fname , out_file)
print "after parsing filename %s from zip %s" % (ifilename, filename)
#out_file.close()
os.close(fd)
print "after closing filename %s from zip %s" % (ifilename, filename)
d= None
#else:
# print "skipping filename %s from zip %s" % (ifilename, filename)
out_file = None
except SkipException, e:
print "Parsing Failed filename %s source %s" % (ifilename, filename)
except Exception, e:
print "error ifilename: %s zip %s outfile:%s" % (ifilename, filename, out_file)
#parser.parse_file_data(ifilename, filename, d, out_file)
#traceback.print_stack()
traceback.print_exc()
print (e)
raise e
def dbg (x):
# traceback.print_stack(limit=2)
# print (x)
pass
class Document:
def __init__(self, url, filename):
self.url = url
self.filename = filename
# global instance
class Header:
def __init__(self,header,fec,version):
from versions import Versions
self.version_proc = Versions()
self.header = header
self.fec = fec
self.version = version
def version_factory(self):
item= self.version_proc.lookup(self.version)
item.do_init()
return item
class FileObject:
def __init__(self):
self.attributes = {}
class Parser:
def set_zipfilename(self, zipfile):
self.zip_filename = zipfile
def version_field_name (self):
return u"FEC_Ver_# "
def __init__(self):
self.state=STATE_UNKNOWN
self.zip_filename=None
self.header=None
self.header_version=None
# Converts symbols and strings to Regexp objects for use in regex-keyed maps.
# Assumes that symbols should be matched literally, strings unanchored.
# @param [String,Symbol,Regexp] label the object to convert to a Regexp
def regexify(self, label):
if label in self.row_types_regex:
return self.row_types[label]
else:
return r"^#{label.to_s}$"
def startHeader(self):
self.state = STATE_HEADER
# dbg ( "start HEADER")
def header_line(self, line):
if line == 'Schedule_Counts:':
return
parts = line.split('=')
if (len(parts) > 1):
self.current.attributes[parts[0]] = parts[1].strip().rstrip()
else:
raise Exception(line)
def endHeader(self):
# dbg ( "END HEADER" )
self.state = STATE_BODY
def HDR(self, l, quote=""):
if l.find('HDRFEC') > -1 :
self.sep=''
elif l.find('"HDR""FEC"') > -1 :
self.sep=''
else:
self.sep=','
parts = []
count = 0
for x in l.split(self.sep):
x = x.replace("\"","")
count = count +1
parts.append(x)
if count < 3 :
self.state = STATE_BODY
return None
header = parts[0]
fec = parts[1]
version = parts[2]
#dbg ( "HEADER LINE header %s fec %s version %s " % ( header, fec, version))
# self.version = Version(version)
self.header=Header(header,fec,version)
self.header_version = self.header.version_factory()
self.header_version.set_sep(self.sep)
self.header_version.parse(parts)
self.state = STATE_BODY
def delimiter(self, filing_version):
if (filing_version.to_f < 6):
return ","
return "\034"
def filing_url(self):
return "http://query.nictusa.com/dcdev/posted/#{filing_id}.fec"
# example : "HDR""FEC""6.1""Trail Blazer""5.0.2945.29436""""F3N Generated on 1/24/2008 6:26:16 PM"
def parse_line(self, l):
dbg ( "check '%s' '%d'" % (l, self.state))
if l.find("\034") > 0:
dbg ( "found 034 in %s" % l)
#if first.index("\034").nil?
if re.match(r'\/\* Header', l):
self.startHeader()
return self.state
if re.match(r'\/\* End Header', l):
self.endHeader()
return self.state
if re.match(r'HDR', l):
self.HDR(l)
return self.state
if re.match(r'\'HDR\'', l):
self.HDR(l, quote='\'')
return self.state
if re.match(r'\"HDR\"', l):
self.HDR(l, quote='\"')
return self.state
if self.state == STATE_HEADER:
dbg ( "in header %s" % l)
self.header_line(l)
return self.state
if self.state == STATE_BODY:
dbg ( "in body %s" % l)
result = self.body_line(l)
if result is None :
self.state = STATE_END
raise Exception("Body parse failed %s" % l)
return result
if self.state == STATE_END:
return None
# call into the base class fech_rendered_maps
return None
def find_version_info(self,line):
version = None
version_field_name = self.version_field_name()
if version_field_name in self.current.attributes:
version = self.current.attributes[version_field_name]
# print ("check version: ",
# "version:",version,
# "attr:",self.current.attributes
# )
if self.header_version is None:
if re.match(r'1\..+', version ):
import fec.version.v1
self.header_version= fec.version.v1.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'2\..+',version ):
import fec.version.v2
self.header_version= fec.version.v2.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'3\..+',version ):
import fec.version.v3
self.header_version= fec.version.v3.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'5\.0.+',version ):
import fec.version.v5_0
self.header_version= fec.version.v5_0.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'5\.1.+',version ):
import fec.version.v5_1
self.header_version= fec.version.v5_1.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'5\.2.+',version ):
import fec.version.v5_2
self.header_version= fec.version.v5_2.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'5\.3.+',version ):
import fec.version.v5_3
self.header_version= fec.version.v5_3.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'6\.1.+',version ):
import fec.version.v6_1
self.header_version= fec.version.v6_1.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'6\.2.+',version ):
import fec.version.v6_2
self.header_version= fec.version.v6_2.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'6\.3.+',version ):
import fec.version.v6_3
self.header_version= fec.version.v6_3.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'6\.4.+',version ):
import fec.version.v6_4
self.header_version= fec.version.v6_4.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'7\..+',version ):
import fec.version.v7_0
self.header_version= fec.version.v7_0.Version()
self.header_version.set_attr_hash(self.current.attributes)
elif re.match(r'8\..+',version ):
import fec.version.v8_0
self.header_version= fec.version.v8_0.Version()
self.header_version.set_attr_hash(self.current.attributes)
else:
raise Exception("Bad Version %s" % version)
else:
if self.header_version is not None:
self.header_version.set_attr_hash(self.current.attributes)
else:
print("check version in : (%s)" % line )
raise Exception("missing version")
def body_line(self, line):
u"""
the body function
"""
if self.header_version is None:
self.find_version_info(line)
if self.header_version is not None:
result = self.header_version.parse_body(line)
if result is None :
raise Exception("failed to parse body %s" % line)
return result
else:
raise Exception("no version")
def write_parts(self, out_file,rows,count):
out_file.file_attributes(self.current.attributes)
if self.header_version is not None:
try:
out_file.create_yaml(rows,count)
except Exception, e:
traceback.print_exc()
print (e)
print (self.header_version)
else:
raise Exception("no header")
# entry point into input, called from zipcsv.py
def parse_file_data(self, filename, sourcefile, tmpfilename, out_file):
with open(tmpfilename, "r") as f:
self.current = FileObject()
count = 1
try :
rows=[]
for line in f:
out_file.raw_line(line)
result = self.parse_line(line)
rows.append(result)
if (len(rows)>400):
self.write_parts(out_file,rows,count)
rows=[]
count = count +1
#final
if (len(rows)>0):
self.write_parts(out_file,rows, count)
except SkipException, e:
pass
except Exception, e:
traceback.print_exc()
print "Parsing Failed filename %s source %s e %s" % (filename, sourcefile, e)
raise e
self.current = None
os.unlink(tmpfilename)
def generate(self, v, name):
c = 0
for f in v:
f = f.strip(" ").rstrip(" ")
f = f.strip("\"").rstrip("\"")
dbg ( " %s=%d" % (f, c))
c = c + 1
#main
#argv1 is the regex to match files
in_pattern=str(sys.argv[1])
pattern=re.compile(in_pattern,re.DEBUG)
if pattern is None:
raise Exception(in_pattern )
listing(pattern)