-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_setup.py
173 lines (137 loc) · 3.45 KB
/
db_setup.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from models import *
from urlparse import urlparse
from csv import DictReader
tables = []
# read in table_names from tables.tsv
with open('record_layouts/tables.tsv', 'rU') as f:
reader = DictReader(f, delimiter='\t')
# find the Model name in the list of global vars and add it to the list of tables
for row in reader:
tables.append(globals()[row['table'].title()])
db.create_tables(tables, True)
# inserting all the look-up records.
with open('look_ups/sources.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Source.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
with open('look_ups/assemblies.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Assembly.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
with open('look_ups/chambers.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Chamber.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
past_session_urls = [
{
'url': 'http://www.house.mo.gov/sitemap.aspx?pid=24'
, 'name': 'Past House Sessions'
, 'source': 'H'
, 'file_name': 'Need to regularly check: http://www.house.mo.gov/sitemap.aspx?pid=24'
}
, {
'url': 'http://www.senate.mo.gov/pastsessions.htm'
, 'name': 'Past Senate Sessions'
, 'source': 'S'
, 'file_name': 'Need to regularly check: http://www.senate.mo.gov/pastsessions.htm'
}
, {
'url': 'http://s1.sos.mo.gov/elections/resultsandstats/previousElections'
, 'name': 'Past SoS election results'
, 'source': 'SoS'
, 'file_name': 'Need to regularly check http://s1.sos.mo.gov/elections/resultsandstats/previousElections'
}
]
for url in past_session_urls:
url['scheme'] = urlparse(url['url']).scheme
url['netloc'] = urlparse(url['url']).netloc
url['path'] = urlparse(url['url']).path
url['params'] = urlparse(url['url']).params
url['query'] = urlparse(url['url']).query
url['fragment'] = urlparse(url['url']).fragment
try:
with db.atomic():
Source_Doc.create(**url)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
with open('look_ups/session_types.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Session_Type.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
with open('look_ups/bill_types.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Bill_Type.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
with open('look_ups/election_types.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Election_Type.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
with open('look_ups/race_types.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Race_Type.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e
with open('look_ups/parties.csv', 'rU') as f:
reader = DictReader(f)
for row in reader:
try:
with db.atomic():
Party.create(**row)
except Exception as e:
if 'duplicate' in e.message:
pass
else:
print e