This repository has been archived by the owner on May 23, 2023. It is now read-only.
forked from okfn/iatitools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-iati2sqlite.py
executable file
·226 lines (198 loc) · 7.51 KB
/
2-iati2sqlite.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
#!/usr/bin/env python
from lxml import etree
from pprint import pprint
import csv
from lib import db
from lib.model import *
from sqlalchemy import *
db.models.metadata.create_all()
from datetime import date, datetime
import os
import re
def nodecpy(out, node, name, attrs={}, convert=unicode):
if ((node is None) or (node.text is None)):
return
if node.text:
out[name] = convert(node.text)
for k, v in attrs.items():
try:
out[name + '_' + v] = node.get(k)
except AttributeError:
pass
def getValue(value):
try:
return float(value)
except ValueError:
nicevalue = re.sub(",","",value)
return float(nicevalue)
def parse_tx(tx):
out = {}
value = tx.find('value')
if value is not None:
out['value_date'] = value.get('value-date')
out['value_currency'] = value.get('currency')
out['value'] = getValue(value.text)
fields = [
('description', 'description', {}),
('transaction-type', 'transaction_type', {'code'}),
('flow-type', 'flow_type', {'code'}),
('finance-type', 'finance_type', {'code'}),
('tied-status', 'tied_status', {'code'}),
('aid-type', 'aid_type', {'code'}),
('disbursement-channel', 'disbursement_channel', {'code'}),
('provider-org', 'provider_org', {'ref'}),
('receiver-org', 'receiver_org', {'ref'})
]
getFieldsData(fields, tx, out)
date = tx.find('transaction-date')
get_date(out, date, 'date_iso', 'transaction_')
if not (out.has_key('transaction_date_iso')):
out['transaction_date_iso'] = out['value_date']
return out
def underscore(value):
return re.sub("-","_",value)
def get_date(out, date, type=None, prefix="date_"):
if date is None:
return False
if not type:
type = underscore(date.get('type'))
if not type:
return
if date is not None:
out[prefix+type] = date.get('iso-date')
if (not date.get('iso-date')) and date.text:
out[prefix+type] = date.text
def getFieldsData(fields, activity, out):
for field in fields:
xpath = field[0]
fieldname = field[1]
attribs = dict([(k, k) for k in field[2]])
nodecpy(out, activity.find(xpath), fieldname, attribs)
def getSector(sector, iati_identifier):
temp = {}
nodecpy(temp, sector,
'sector',
{'vocabulary': 'vocabulary',
'percentage': 'percentage',
'code': 'code'})
sector_data = {
'activity_iati_identifier': iati_identifier,
'name': temp.get('sector', ''),
'code': temp.get('sector_code', ''),
'percentage': temp.get('percentage', '100'),
'vocabulary': temp.get('vocabulary', 'DAC')
}
return sector_data
def parse_activity(activity, out, package_filename):
out['default_currency'] = activity.get("default-currency")
fields = [
('reporting-org', 'reporting_org', {'ref', 'type'}),
('iati-identifier', 'iati_identifier', {}),
('title', 'title', {}),
('description', 'description', {}),
('activity-website', 'activity_website', {}),
('recipient-region', 'recipient_region', {'code'}),
('recipient-country', 'recipient_country', {'code'}),
('collaboration-type', 'collaboration_type', {'code'}),
('default-flow-type', 'flow_type', {'code'}),
('default-finance-type', 'finance_type', {'code'}),
('default-aid-type', 'aid_type', {'code'}),
('default-tied-status', 'tied_status', {'code'}),
('activity-status', 'status', {'code'}),
('legacy-data', 'legacy', {'name', 'value'}),
('participating-org[@role="Funding"]', 'funding_org', {'ref', 'type'}),
('participating-org[@role="Extending"]', 'extending_org', {'ref', 'type'}),
('participating-org[@role="Implementing"]', 'implementing_org', {'ref', 'type'}),
('contact-info/organisation', 'contact_organisation', {}),
('contact-info/mailing-address', 'contact_mailing_address', {}),
('contact-info/telephone', 'contact_telephone', {}),
('contact-info/email', 'contact_email', {})
]
getFieldsData(fields, activity, out)
for date in activity.findall('activity-date'):
get_date(out,date)
iati_identifier = activity.findtext('iati-identifier')
for sector in activity.findall('sector'):
sector_data = getSector(sector, iati_identifier)
missingfields(sector, Sector, package_filename)
s = Sector(**sector_data)
db.session.add(s)
for ra in activity.findall('related-activity'):
try:
activityiatiid = activity.findtext('iati-identifier')
related_activity = {
'activity_id': activityiatiid,
'relref': ra.get('ref'),
'reltype': ra.get('type')
}
missingfields(related_activity, RelatedActivity, package_filename)
rela = RelatedActivity(**related_activity)
db.session.add(rela)
except ValueError:
pass
for tx in activity.findall("transaction"):
transaction = parse_tx(tx)
transaction['iati_identifier'] = out['iati_identifier']
missingfields(transaction, Transaction, package_filename)
t = Transaction(**transaction)
db.session.add(t)
missingfields(out, Activity, package_filename)
x = Activity(**out)
db.session.add(x)
return (out)
def missingfields(dict_, obj, package):
missing = [ k for k in dict_ if k not in obj.__table__.c.keys() ]
if missing:
logtext = "Missing fields in package " + package + ": " + str(obj.__name__) + " " + str(missing) + "\n"
log(logtext)
for m in missing:
del dict_[m]
def log(logtext):
inp=file('log-' + str(date.today()) + '.txt', 'a')
inp.write(str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + " " + logtext)
inp.close()
def load_file(file_name, context=None):
doc = etree.parse(file_name)
if context is None:
context = {}
context['source_file'] = file_name
print "Parsing ", file_name
for activity in doc.findall("iati-activity"):
out = parse_activity(activity, context.copy(), file_name)
print "Writing to database..."
db.session.commit()
print "Written to database."
def load_package():
if (len(sys.argv) > 1):
packagedir = sys.argv[1]
else:
packagedir = 'packages/'+str(date.today())
print "No package folder defined (you can supply the argument YYYY-MM-DD for a particular date of packages), so using today's date\n"
logtext = "No package folder defined, so reverting to today's date\n"
log(logtext)
path = packagedir
listing = os.listdir(path)
totalfiles = len(listing)
print "Found", totalfiles, "files."
filecount = 1
for infile in listing:
try:
print ""
print "Loading file", filecount, "of", totalfiles, "(", round(((float(filecount)/float(totalfiles))*100),2), "%)"
filecount = filecount +1
load_file(path + '/' + infile)
except Exception, e:
print 'Failed:', e
logtext = "Error in file: " + infile + " - " + str(e) + "\n"
log(logtext)
pass
if __name__ == '__main__':
import sys
try:
load_package()
except Exception, e:
print 'Failed:', e
logtext = "Couldn't load package: " + str(e) + "\n"
log(logtext)
print db.session.query(Activity).count()
print db.session.query(Transaction).count()