forked from dean-breed/IATI-Covid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_spec.py
965 lines (919 loc) · 62.2 KB
/
transaction_spec.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
import progressbar
from lxml import etree
import datetime
import dateutil
import pdb
import json
# Two dimension exchange rate dictionary. Access exchange rates by currency and year like ratedf[currencyCode][year]
with open('ex_rates.json') as f:
ratedf = json.load(f)
ratedf["GPB"] = ratedf["GBP"]
ratedf["gbp"] = ratedf["GBP"]
ratedf["EURO"] = ratedf["EUR"]
ratedf["Euro"] = ratedf["EUR"]
ratedf["Eur"] = ratedf["EUR"]
ratedf["CDN"] = ratedf["CAD"]
ratedf["usd"] = ratedf["USD"]
ratedf["GHC"] = ratedf["GHS"]
ratedf["ZMK"] = ratedf["ZMW"]
ratedf["USS"] = ratedf["USD"]
ratedf["USN"] = ratedf["USD"]
ratedf["BEF"] = ratedf["EUR"]
ratedf["FIM"] = ratedf["EUR"]
ratedf["KSH"] = ratedf["KES"]
ratedf["GIP"] = ratedf["GBP"]
ratedf["FKP"] = ratedf["GBP"]
ratedf["AON"] = ratedf["AOA"]
ratedf["UYI"] = ratedf["UYU"]
ratedf["NUL"] = {"2000": 0}
# Used for ambiguously structed arrays resulting from XML queries. If an array has any entries, take the first one.
def default_first(array):
# If an array isn't empty, give us the first element
return array[0] if array is not None and len(array) > 0 else None
# Used for ambiguous result default replacement. If value doesn't exist, replace it with the default.
def replace_default_if_none(value, default):
if value is None:
return default
elif str.strip(value) == "":
return default
else:
return value
# Used for ambiguous recoding. If code exists, try and use the dictionary to look up the result.
def recode_if_not_none(code, dictionary):
if code is None:
return None
elif str.strip(code) == "":
return None
else:
try:
return dictionary[code]
except KeyError:
return None
# Used for currency conversion. Works like recode_if_not_none but for our 2-dimension exchange rate dictionary
def convert_usd(value, year, currency, ratedf):
if value == 0:
return 0
elif value is None or year is None or currency is None:
return None
try:
conversion_factor = ratedf[currency][str(year)]
if conversion_factor > 0:
return value*conversion_factor
else:
return None
except KeyError:
return None
# A class that will hold the flattening function and dictionary definitions
class IatiFlat(object):
def __init__(self):
self.header = ["iati_identifier", "x_transaction_number", "reporting_org_ref", "reporting_org_narrative", "reporting_org_secondary_reporter", "reporting_org_type_code", "title_narrative", "recipient_country_code", "recipient_country_percentage", "transaction_recipient_country_code", "x_country_code", "x_country_percentage", "recipient_region_vocabulary", "recipient_region_code", "recipient_region_percentage", "transaction_recipient_region_vocabulary", "transaction_recipient_region_code", "x_region_vocabulary", "x_region_code", "x_region_percentage", "sector_vocabulary", "sector_code", "sector_percentage", "transaction_sector_vocabulary", "transaction_sector_code", "x_sector_vocabulary", "x_default_vocabulary", "x_sector_code", "x_sector_percentage", "x_dac3_sector_code", "transaction_type_code", "transaction_date_iso_date", "transaction_value_date", "x_transaction_date", "x_transaction_year", "default_currency", "transaction_value_currency", "x_currency", "transaction_value", "x_transaction_value", "x_transaction_value_usd", "default_flow_type_code", "transaction_flow_type_code", "x_flow_type_code", "default_finance_type_code", "transaction_finance_type_code", "x_finance_type_code", "default_aid_type_vocabulary", "default_aid_type_code", "transaction_aid_type_vocabulary", "transaction_aid_type_code", "x_mod_aid_type_vocabulary", "x_mod_aid_type_code", "x_dac_aid_type_code", "default_tied_status_code", "transaction_tied_status_code", "x_tied_status_code", "transaction_disbursement_channel_code", "description_narrative", "transaction_description_narrative", "humanitarian", "transaction_humanitarian", "humanitarian_scope_type", "humanitarian_scope_vocabulary", "humanitarian_scope_code", "humanitarian_scope_narrative", "x_hum_emergency_vocabulary", "x_hum_emergency_code", "x_hum_appeal_vocabulary", "x_hum_appeal_code", "transaction_provider_org_narrative", "transaction_provider_org_provider_activity_id", "transaction_provider_org_ref", "transaction_provider_org_type", "transaction_receiver_org_narrative", "transaction_receiver_org_receiver_activity_id", "transaction_receiver_org_ref", "transaction_receiver_org_type", "transaction_ref", "participating_org_narrative", "participating_org_type", "participating_org_role", "participating_org_ref", "tag_narrative", "tag_vocabulary", "tag_code", "x_reporting_org_type", "x_transaction_type", "x_country", "x_finance_type", "x_aid_type", "x_dac3_sector", "x_di_sector"]
self.dictionaries = {}
# Defaults, can be overwritten with next function
self.dictionaries["ratedf"] = ratedf
self.dictionaries["organisation_type"] = {
"10": "Government",
"11": "Local Government",
"15": "Other Public Sector",
"21": "International NGO",
"22": "National NGO",
"23": "Regional NGO",
"24": "Partner Country based NGO",
"30": "Public Private Partnership",
"40": "Multilateral",
"60": "Foundation",
"70": "Private Sector",
"71": "Private Sector in Provider Country",
"72": "Private Sector in Aid Recipient Country",
"73": "Private Sector in Third Country",
"80": "Academic, Training and Research",
"90": "Other"
}
self.dictionaries["transaction_type"] = {
"1": "Incoming Funds",
"2": "Outgoing Commitment",
"3": "Disbursement",
"4": "Expenditure",
"5": "Interest Payment",
"6": "Loan Repayment",
"7": "Reimbursement",
"8": "Purchase of Equity",
"9": "Sale of Equity",
"10": "Credit Guarantee",
"11": "Incoming Commitment",
"12": "Outgoing Pledge",
"13": "Incoming Pledge"
}
self.dictionaries["country"] = {
"AF": "Afghanistan",
"AX": "Åland Islands",
"AL": "Albania",
"DZ": "Algeria",
"AS": "American Samoa",
"AD": "Andorra",
"AO": "Angola",
"AI": "Anguilla",
"AQ": "Antarctica",
"AG": "Antigua and Barbuda",
"AR": "Argentina",
"AM": "Armenia",
"AW": "Aruba",
"AU": "Australia",
"AT": "Austria",
"AZ": "Azerbaijan",
"BS": "Bahamas (the)",
"BH": "Bahrain",
"BD": "Bangladesh",
"BB": "Barbados",
"BY": "Belarus",
"BE": "Belgium",
"BZ": "Belize",
"BJ": "Benin",
"BM": "Bermuda",
"BT": "Bhutan",
"BO": "Bolivia (Plurinational State of)",
"BQ": "Bonaire, Sint Eustatius and Saba",
"BA": "Bosnia and Herzegovina",
"BW": "Botswana",
"BV": "Bouvet Island",
"BR": "Brazil",
"IO": "British Indian Ocean Territory (the)",
"BN": "Brunei Darussalam",
"BG": "Bulgaria",
"BF": "Burkina Faso",
"BI": "Burundi",
"KH": "Cambodia",
"CM": "Cameroon",
"CA": "Canada",
"CV": "Cabo Verde",
"KY": "Cayman Islands (the)",
"CF": "Central African Republic (the)",
"TD": "Chad",
"CL": "Chile",
"CN": "China",
"CX": "Christmas Island",
"CC": "Cocos (Keeling) Islands (the)",
"CO": "Colombia",
"KM": "Comoros (the)",
"CG": "Congo (the)",
"CD": "Congo (the Democratic Republic of the)",
"CK": "Cook Islands (the)",
"CR": "Costa Rica",
"CI": "Côte d'Ivoire",
"HR": "Croatia",
"CU": "Cuba",
"CW": "Curaçao",
"CY": "Cyprus",
"CZ": "Czechia",
"DK": "Denmark",
"DJ": "Djibouti",
"DM": "Dominica",
"DO": "Dominican Republic (the)",
"EC": "Ecuador",
"EG": "Egypt",
"SV": "El Salvador",
"GQ": "Equatorial Guinea",
"ER": "Eritrea",
"EE": "Estonia",
"ET": "Ethiopia",
"FK": "Falkland Islands (the) [Malvinas]",
"FO": "Faroe Islands (the)",
"FJ": "Fiji",
"FI": "Finland",
"FR": "France",
"GF": "French Guiana",
"PF": "French Polynesia",
"TF": "French Southern Territories (the)",
"GA": "Gabon",
"GM": "Gambia (the)",
"GE": "Georgia",
"DE": "Germany",
"GH": "Ghana",
"GI": "Gibraltar",
"GR": "Greece",
"GL": "Greenland",
"GD": "Grenada",
"GP": "Guadeloupe",
"GU": "Guam",
"GT": "Guatemala",
"GG": "Guernsey",
"GN": "Guinea",
"GW": "Guinea-Bissau",
"GY": "Guyana",
"HT": "Haiti",
"HM": "Heard Island and McDonald Islands",
"VA": "Holy See (the)",
"HN": "Honduras",
"HK": "Hong Kong",
"HU": "Hungary",
"IS": "Iceland",
"IN": "India",
"ID": "Indonesia",
"IR": "Iran (Islamic Republic of)",
"IQ": "Iraq",
"IE": "Ireland",
"IM": "Isle of Man",
"IL": "Israel",
"IT": "Italy",
"JM": "Jamaica",
"JP": "Japan",
"JE": "Jersey",
"JO": "Jordan",
"KZ": "Kazakhstan",
"KE": "Kenya",
"KI": "Kiribati",
"KP": "Korea (the Democratic People's Republic of)",
"KR": "Korea (the Republic of)",
"XK": "Kosovo",
"KW": "Kuwait",
"KG": "Kyrgyzstan",
"LA": "Lao People's Democratic Republic (the)",
"LV": "Latvia",
"LB": "Lebanon",
"LS": "Lesotho",
"LR": "Liberia",
"LY": "Libya",
"LI": "Liechtenstein",
"LT": "Lithuania",
"LU": "Luxembourg",
"MO": "Macao",
"MK": "North Macedonia",
"MG": "Madagascar",
"MW": "Malawi",
"MY": "Malaysia",
"MV": "Maldives",
"ML": "Mali",
"MT": "Malta",
"MH": "Marshall Islands (the)",
"MQ": "Martinique",
"MR": "Mauritania",
"MU": "Mauritius",
"YT": "Mayotte",
"MX": "Mexico",
"FM": "Micronesia (Federated States of)",
"MD": "Moldova (the Republic of)",
"MC": "Monaco",
"MN": "Mongolia",
"ME": "Montenegro",
"MS": "Montserrat",
"MA": "Morocco",
"MZ": "Mozambique",
"MM": "Myanmar",
"NA": "Namibia",
"NR": "Nauru",
"NP": "Nepal",
"NL": "Netherlands (the)",
"AN": "NETHERLAND ANTILLES",
"NC": "New Caledonia",
"NZ": "New Zealand",
"NI": "Nicaragua",
"NE": "Niger (the)",
"NG": "Nigeria",
"NU": "Niue",
"NF": "Norfolk Island",
"MP": "Northern Mariana Islands (the)",
"NO": "Norway",
"OM": "Oman",
"PK": "Pakistan",
"PW": "Palau",
"PS": "Palestine, State of",
"PA": "Panama",
"PG": "Papua New Guinea",
"PY": "Paraguay",
"PE": "Peru",
"PH": "Philippines (the)",
"PN": "Pitcairn",
"PL": "Poland",
"PT": "Portugal",
"PR": "Puerto Rico",
"QA": "Qatar",
"RE": "Réunion",
"RO": "Romania",
"RU": "Russian Federation (the)",
"RW": "Rwanda",
"BL": "Saint Barthélemy",
"SH": "Saint Helena, Ascension and Tristan da Cunha",
"KN": "Saint Kitts and Nevis",
"LC": "Saint Lucia",
"MF": "Saint Martin (French part)",
"PM": "Saint Pierre and Miquelon",
"VC": "Saint Vincent and the Grenadines",
"WS": "Samoa",
"SM": "San Marino",
"ST": "Sao Tome and Principe",
"SA": "Saudi Arabia",
"SN": "Senegal",
"RS": "Serbia",
"SC": "Seychelles",
"SL": "Sierra Leone",
"SG": "Singapore",
"SX": "Sint Maarten (Dutch part)",
"SK": "Slovakia",
"SI": "Slovenia",
"SB": "Solomon Islands",
"SO": "Somalia",
"ZA": "South Africa",
"GS": "South Georgia and the South Sandwich Islands",
"SS": "South Sudan",
"ES": "Spain",
"LK": "Sri Lanka",
"SD": "Sudan (the)",
"SR": "Suriname",
"SJ": "Svalbard and Jan Mayen",
"SZ": "Eswatini",
"SE": "Sweden",
"CH": "Switzerland",
"SY": "Syrian Arab Republic",
"TW": "Taiwan (Province of China)",
"TJ": "Tajikistan",
"TZ": "Tanzania, United Republic of",
"TH": "Thailand",
"TL": "Timor-Leste",
"TG": "Togo",
"TK": "Tokelau",
"TO": "Tonga",
"TT": "Trinidad and Tobago",
"TN": "Tunisia",
"TR": "Turkey",
"TM": "Turkmenistan",
"TC": "Turks and Caicos Islands (the)",
"TV": "Tuvalu",
"UG": "Uganda",
"UA": "Ukraine",
"AE": "United Arab Emirates (the)",
"GB": "United Kingdom of Great Britain and Northern Ireland (the)",
"US": "United States of America (the)",
"UM": "United States Minor Outlying Islands (the)",
"UY": "Uruguay",
"UZ": "Uzbekistan",
"VU": "Vanuatu",
"VE": "Venezuela (Bolivarian Republic of)",
"VN": "Viet Nam",
"VG": "Virgin Islands (British)",
"VI": "Virgin Islands (U.S.)",
"WF": "Wallis and Futuna",
"EH": "Western Sahara",
"YE": "Yemen",
"ZM": "Zambia",
"ZW": "Zimbabwe"
}
self.dictionaries["finance_type"] = {
"1": "GNI: Gross National Income",
"110": "Standard grant",
"1100": "Guarantees/insurance",
"111": "Subsidies to national private investors",
"2": "ODA % GNI",
"210": "Interest subsidy",
"211": "Interest subsidy to national private exporters",
"3": "Total Flows % GNI",
"310": "Capital subscription on deposit basis",
"311": "Capital subscription on encashment basis",
"4": "Population",
"410": "Aid loan excluding debt reorganisation",
"411": "Investment-related loan to developing countries",
"412": "Loan in a joint venture with the recipient",
"413": "Loan to national private investor",
"414": "Loan to national private exporter",
"421": "Standard loan",
"422": "Reimbursable grant",
"423": "Bonds",
"424": "Asset-backed securities",
"425": "Other debt securities",
"431": "Subordinated loan",
"432": "Preferred equity",
"433": "Other hybrid instruments",
"451": "Non-banks guaranteed export credits",
"452": "Non-banks non-guaranteed portions of guaranteed export credits",
"453": "Bank export credits",
"510": "Common equity",
"511": "Acquisition of equity not part of joint venture in developing countries",
"512": "Other acquisition of equity",
"520": "Shares in collective investment vehicles",
"530": "Reinvested earnings",
"610": "Debt forgiveness: ODA claims (P)",
"611": "Debt forgiveness: ODA claims (I)",
"612": "Debt forgiveness: OOF claims (P)",
"613": "Debt forgiveness: OOF claims (I)",
"614": "Debt forgiveness: Private claims (P)",
"615": "Debt forgiveness: Private claims (I)",
"616": "Debt forgiveness: OOF claims (DSR)",
"617": "Debt forgiveness: Private claims (DSR)",
"618": "Debt forgiveness: Other",
"620": "Debt rescheduling: ODA claims (P)",
"621": "Debt rescheduling: ODA claims (I)",
"622": "Debt rescheduling: OOF claims (P)",
"623": "Debt rescheduling: OOF claims (I)",
"624": "Debt rescheduling: Private claims (P)",
"625": "Debt rescheduling: Private claims (I)",
"626": "Debt rescheduling: OOF claims (DSR)",
"627": "Debt rescheduling: Private claims (DSR)",
"630": "Debt rescheduling: OOF claim (DSR – original loan principal)",
"631": "Debt rescheduling: OOF claim (DSR – original loan interest)",
"632": "Debt rescheduling: Private claim (DSR – original loan principal)",
"633": "Debt forgiveness/conversion: export credit claims (P)",
"634": "Debt forgiveness/conversion: export credit claims (I)",
"635": "Debt forgiveness: export credit claims (DSR)",
"636": "Debt rescheduling: export credit claims (P)",
"637": "Debt rescheduling: export credit claims (I)",
"638": "Debt rescheduling: export credit claims (DSR)",
"639": "Debt rescheduling: export credit claim (DSR – original loan principal)",
"710": "Foreign direct investment, new capital outflow (includes reinvested earnings if separate identification not available)",
"711": "Other foreign direct investment, including reinvested earnings",
"712": "Foreign direct investment, reinvested earnings",
"810": "Bank bonds",
"811": "Non-bank bonds",
"910": "Other bank securities/claims",
"911": "Other non-bank securities/claims",
"912": "Purchase of securities from issuing agencies",
"913": "Securities and other instruments originally issued by multilateral agencies"
}
self.dictionaries["aid_type"] = {
"A01": "General budget support",
"A02": "Sector budget support",
"B01": "Core support to NGOs, other private bodies, PPPs and research institutes",
"B02": "Core contributions to multilateral institutions",
"B03": "Contributions to specific-purpose programmes and funds managed by implementing partners",
"B04": "Basket funds/pooled funding",
"C01": "Project-type interventions",
"D01": "Donor country personnel",
"D02": "Other technical assistance",
"E01": "Scholarships/training in donor country",
"E02": "Imputed student costs",
"F01": "Debt relief",
"G01": "Administrative costs not included elsewhere",
"H01": "Development awareness",
"H02": "Refugees/asylum seekers in donor countries",
"H03": "Asylum-seekers ultimately accepted",
"H04": "Asylum-seekers ultimately rejected",
"H05": "Recognised refugees"
}
self.dictionaries["dac_3_sector"] = {
"111": "Education, Level Unspecified",
"112": "Basic Education",
"113": "Secondary Education",
"114": "Post-Secondary Education",
"121": "Health, General",
"122": "Basic Health",
"123": "Non-communicable diseases (NCDs)",
"130": "Population Policies/Programmes & Reproductive Health",
"140": "Water Supply & Sanitation",
"151": "Government & Civil Society-general",
"152": "Conflict, Peace & Security",
"160": "Other Social Infrastructure & Services",
"210": "Transport & Storage",
"220": "Communications",
"230": "ENERGY GENERATION AND SUPPLY",
"231": "Energy Policy",
"232": "Energy generation, renewable sources",
"233": "Energy generation, non-renewable sources",
"234": "Hybrid energy plants",
"235": "Nuclear energy plants",
"236": "Energy distribution",
"240": "Banking & Financial Services",
"250": "Business & Other Services",
"311": "Agriculture",
"312": "Forestry",
"313": "Fishing",
"321": "Industry",
"322": "Mineral Resources & Mining",
"323": "Construction",
"331": "Trade Policies & Regulations",
"332": "Tourism",
"410": "General Environment Protection",
"430": "Other Multisector",
"510": "General Budget Support",
"520": "Development Food Assistance",
"530": "Other Commodity Assistance",
"600": "Action Relating to Debt",
"720": "Emergency Response",
"730": "Reconstruction Relief & Rehabilitation",
"740": "Disaster Prevention & Preparedness",
"910": "Administrative Costs of Donors",
"920": "SUPPORT TO NON- GOVERNMENTAL ORGANISATIONS (NGOs)",
"930": "Refugees in Donor Countries",
"998": "Unallocated / Unspecified"
}
self.dictionaries["di_sector"] = {
"111": "Education",
"112": "Education",
"113": "Education",
"114": "Education",
"121": "Health",
"122": "Health",
"123": "Health",
"130": "Health",
"140": "Water and sanitation",
"151": "Governance and security",
"152": "Governance and security",
"160": "Other social services",
"210": "Infrastructure",
"220": "Infrastructure",
"230": "Infrastructure",
"231": "Infrastructure",
"232": "Infrastructure",
"233": "Infrastructure",
"234": "Infrastructure",
"235": "Infrastructure",
"236": "Infrastructure",
"240": "Banking and business",
"250": "Banking and business",
"311": "Agriculture and food security",
"312": "Agriculture and food security",
"313": "Agriculture and food security",
"321": "Industry and trade",
"322": "Industry and trade",
"323": "Industry and trade",
"331": "Industry and trade",
"332": "Industry and trade",
"410": "Environment",
"430": "Other",
"510": "General Budget Support",
"520": "Agriculture and food security",
"530": "Other",
"600": "Debt relief",
"720": "Humanitarian",
"730": "Humanitarian",
"740": "Humanitarian",
"910": "Other",
"920": "Other",
"930": "Other",
"998": "Other"
}
def define_dict(self, name, dictionary):
self.dictionaries[name] = dictionary
# Main flattening function here. Input is the XML root of the XML document, and output is an array of arrays with flattened data.
def flatten_activities(self, root):
for dictionary_name in ["ratedf"]:
assert dictionary_name in self.dictionaries, "Missing dictionary: {}".format(dictionary_name)
output = []
try:
version = root.attrib["version"]
except KeyError:
# Defaults to 2.02 if the document happens to be missing an IATI version
version = '2.02'
# Find all activities
activity_len = len(root.findall("iati-activity"))
# Set up a quick progress bar for tracking processing; iterate through every activity
bar = progressbar.ProgressBar()
for i in bar(range(0, activity_len)):
activity = root.xpath('iati-activity[%s]' % (i + 1))[0]
# Capture iati identifier
iati_identifier = default_first(activity.xpath("iati-identifier/text()"))
reporting_org_ref = default_first(activity.xpath("reporting-org/@ref"))
reporting_org_narrative = default_first(activity.xpath("reporting-org/narrative/text()"))
reporting_org_secondary_reporter = default_first(activity.xpath("reporting-org/@secondary-reporter"))
reporting_org_secondary_reporter = replace_default_if_none(reporting_org_secondary_reporter, "0")
reporting_org_type_code = default_first(activity.xpath("reporting-org/@type"))
recipient_country_code_list = []
recipient_country_percentage_list = []
recipient_country_percentage_sum = 0
recipient_countries = activity.findall("recipient-country")
for recipient_country in recipient_countries:
attribs = recipient_country.attrib
attrib_keys = list(attribs.keys())
percentage = attribs['percentage'] if 'percentage' in attrib_keys else "100"
if percentage is not None:
percentage = percentage.replace("%", "")
try:
recipient_country_percentage_sum += float(percentage)
except ValueError:
pass
code = attribs['code'] if 'code' in attrib_keys else None
if code is not None:
recipient_country_code_list.append(code)
recipient_country_percentage_list.append(percentage)
recipient_country_code = "|".join(recipient_country_code_list)
recipient_country_percentage = "|".join(recipient_country_percentage_list)
recipient_region_vocabulary_list = []
recipient_region_code_list = []
recipient_region_percentage_list = []
recipient_countries = activity.findall("recipient-region")
for recipient_region in recipient_countries:
attribs = recipient_region.attrib
attrib_keys = list(attribs.keys())
percentage = attribs['percentage'] if 'percentage' in attrib_keys else "100"
if percentage is not None:
percentage = percentage.replace("%", "")
vocab = attribs['vocabulary'] if 'vocabulary' in attrib_keys else None
if vocab is None:
vocab = ""
code = attribs['code'] if 'code' in attrib_keys else None
if code is not None:
recipient_region_vocabulary_list.append(vocab)
recipient_region_code_list.append(code)
recipient_region_percentage_list.append(percentage)
recipient_region_vocabulary = "|".join(recipient_region_vocabulary_list)
recipient_region_code = "|".join(recipient_region_code_list)
recipient_region_percentage = "|".join(recipient_region_percentage_list)
sector_code_list = []
sector_percentage_list = []
sector_vocabulary_list = []
activity_sectors = activity.findall("sector")
for activity_sector in activity_sectors:
attribs = activity_sector.attrib
attrib_keys = list(attribs.keys())
percentage = attribs['percentage'] if 'percentage' in attrib_keys else "100"
if percentage is not None:
percentage = percentage.replace("%", "")
if percentage is None:
percentage = ""
vocab = attribs['vocabulary'] if 'vocabulary' in attrib_keys else None
if vocab is None:
vocab = ""
code = attribs['code'] if 'code' in attrib_keys else None
if code is not None:
sector_code_list.append(code)
sector_percentage_list.append(percentage)
sector_vocabulary_list.append(vocab)
sector_code = "|".join(sector_code_list)
sector_percentage = "|".join(sector_percentage_list)
sector_vocabulary = "|".join(sector_vocabulary_list)
humanitarian = default_first(activity.xpath("@humanitarian"))
humanitarian_scope_narrative = default_first(activity.xpath("humanitarian-scope/narrative/text()"))
humanitarian_scope_type = default_first(activity.xpath("humanitarian-scope/@type"))
humanitarian_scope_vocabulary = default_first(activity.xpath("humanitarian-scope/@vocabulary"))
humanitarian_scope_code = default_first(activity.xpath("humanitarian-scope/@code"))
x_hum_emergency_vocabulary = humanitarian_scope_vocabulary if humanitarian_scope_type == "1" else ""
x_hum_emergency_code = humanitarian_scope_code if humanitarian_scope_type == "1" else ""
x_hum_appeal_vocabulary = humanitarian_scope_vocabulary if humanitarian_scope_type == "2" else ""
x_hum_appeal_code = humanitarian_scope_code if humanitarian_scope_type == "2" else ""
title_narrative = default_first(activity.xpath("title/narrative/text()"))
description_narrative = default_first(activity.xpath("description/narrative/text()"))
tag_code = default_first(activity.xpath("tag/@code"))
tag_vocabulary = default_first(activity.xpath("tag/@vocabulary"))
tag_narrative = default_first(activity.xpath("tag/narrative/text()"))
participating_org_ref_list = []
participating_org_type_list = []
participating_org_role_list = []
participating_org_narrative_list = []
participating_orgs = activity.findall("participating-org")
for participating_org in participating_orgs:
attribs = participating_org.attrib
attrib_keys = list(attribs.keys())
ref = attribs['ref'] if 'ref' in attrib_keys else ""
participating_org_ref_list.append(ref)
p_type = attribs['type'] if 'type' in attrib_keys else ""
participating_org_type_list.append(p_type)
role = attribs['role'] if 'role' in attrib_keys else ""
participating_org_role_list.append(role)
p_name = default_first(participating_org.xpath("narrative/text()"))
p_name = p_name if p_name else ""
participating_org_narrative_list.append(p_name)
participating_org_ref = "|".join(participating_org_ref_list)
participating_org_type = "|".join(participating_org_type_list)
participating_org_role = "|".join(participating_org_role_list)
participating_org_narrative = "|".join(participating_org_narrative_list)
child_tags = [child.tag for child in activity.getchildren()]
defaults = {}
default_tags = ["default-currency", "default-flow-type", "default-finance-type", "default-aid-type", "default-tied-status"]
for tag in default_tags:
if tag in activity.attrib.keys():
defaults[tag] = activity.attrib[tag]
defaults[tag+"-code"] = None
defaults[tag+"-vocabulary"] = None
elif tag in child_tags:
defaults[tag] = None
defaults[tag+"-code"] = default_first(activity.xpath("{}/@code".format(tag)))
defaults[tag+"-vocabulary"] = default_first(activity.xpath("{}/@vocabulary".format(tag)))
else:
defaults[tag] = None
defaults[tag+"-code"] = None
defaults[tag+"-vocabulary"] = None
has_transactions = "transaction" in child_tags
if has_transactions:
transactions = activity.findall("transaction")
x_transaction_number = 0
for transaction in transactions:
x_transaction_number += 1
transaction_recipient_country_code = default_first(transaction.xpath("recipient-country/@code"))
if transaction_recipient_country_code:
x_country_code = transaction_recipient_country_code
x_country_percentage = "100"
else:
x_country_code = recipient_country_code
x_country_percentage = recipient_country_percentage
transaction_recipient_region_code = default_first(transaction.xpath("recipient-region/@code"))
transaction_recipient_region_vocabulary = default_first(transaction.xpath("recipient-region/@vocabulary"))
if transaction_recipient_region_code:
x_region_code = transaction_recipient_region_code
x_region_vocabulary = transaction_recipient_region_vocabulary
x_region_percentage = "100"
else:
x_region_code = recipient_region_code
x_region_vocabulary = recipient_region_vocabulary
x_region_percentage = recipient_region_percentage
if recipient_country_percentage_sum >= 100 or transaction_recipient_country_code:
x_region_code = ""
x_region_vocabulary = ""
x_region_percentage = ""
transaction_sector_code = default_first(transaction.xpath("sector/@code"))
transaction_sector_vocabulary = default_first(transaction.xpath("sector/@vocabulary"))
if transaction_sector_code:
x_sector_code = transaction_sector_code
x_sector_percentage = "100"
x_sector_vocabulary = ""
else:
x_sector_code = sector_code
x_sector_percentage = sector_percentage
x_sector_vocabulary = sector_vocabulary
if x_sector_vocabulary == "" and len(x_sector_code.split("|")[0]) == 5:
x_sector_vocabulary = "1"
elif x_sector_vocabulary == "":
x_sector_vocabulary = "97"
transaction_type_code = default_first(transaction.xpath("transaction-type/@code"))
transaction_date_iso_date = default_first(transaction.xpath("transaction-date/@iso-date"))
transaction_value_date = default_first(transaction.xpath("value/@value-date"))
x_transaction_date = replace_default_if_none(transaction_date_iso_date, transaction_value_date)
x_transaction_year = ""
try:
year = int(x_transaction_date[:4]) if x_transaction_date is not None else None
x_transaction_year = year if year is not None else ""
except ValueError:
year = None
transaction_value_currency = default_first(transaction.xpath("value/@currency"))
default_currency = defaults["default-currency"]
x_currency = replace_default_if_none(transaction_value_currency, default_currency)
if x_currency == "":
x_currency = None
if x_currency is not None:
x_currency = x_currency.replace(" ", "")
transaction_value = default_first(transaction.xpath("value/text()"))
try:
transaction_value = float(transaction_value.replace(" ", "").replace(",", "")) if transaction_value is not None else None
transaction_convertable = True
except ValueError:
transaction_convertable = False
if not transaction_value:
transaction_convertable = False
default_flow_type_code = defaults["default-flow-type-code"]
transaction_flow_type_code = default_first(transaction.xpath("flow-type/@code"))
x_flow_type_code = replace_default_if_none(transaction_flow_type_code, default_flow_type_code)
default_finance_type_code = defaults["default-finance-type-code"]
transaction_finance_type_code = default_first(transaction.xpath("finance-type/@code"))
x_finance_type_code = replace_default_if_none(transaction_finance_type_code, default_finance_type_code)
default_aid_type_vocabulary = defaults["default-aid-type-vocabulary"]
default_aid_type_code = defaults["default-aid-type-code"]
transaction_aid_type_vocabulary = default_first(transaction.xpath("aid-type/@vocabulary"))
transaction_aid_type_code = default_first(transaction.xpath("aid-type/@code"))
x_mod_aid_type_vocabulary = ""
x_mod_aid_type_code = ""
x_dac_aid_type_code = ""
if transaction_aid_type_code:
if transaction_aid_type_vocabulary in ["2", "3", "4"]:
x_mod_aid_type_vocabulary = transaction_aid_type_vocabulary
x_mod_aid_type_code = transaction_aid_type_code
else:
x_dac_aid_type_code = transaction_aid_type_code
elif default_aid_type_code:
if default_aid_type_vocabulary in ["2", "3", "4"]:
x_mod_aid_type_vocabulary = default_aid_type_vocabulary
x_mod_aid_type_code = default_aid_type_code
else:
x_dac_aid_type_code = default_aid_type_code
default_tied_status_code = defaults["default-tied-status-code"]
transaction_tied_status_code = default_first(transaction.xpath("tied-status/@code"))
x_tied_status_code = replace_default_if_none(transaction_tied_status_code, default_tied_status_code)
transaction_disbursement_channel_code = default_first(transaction.xpath("disbursment-channel/@code"))
transaction_description_narrative = default_first(transaction.xpath("description/narrative/text()"))
transaction_humanitarian = default_first(transaction.xpath("@humanitarian"))
transaction_provider_org_narrative = default_first(transaction.xpath("provider-org/narrative/text()"))
transaction_provider_org_provider_activity_id = default_first(transaction.xpath("provider-org/@provider-activity-id"))
transaction_provider_org_ref = default_first(transaction.xpath("provider-org/@ref"))
transaction_provider_org_type = default_first(transaction.xpath("provider-org/@type"))
transaction_receiver_org_narrative = default_first(transaction.xpath("receiver-org/narrative/text()"))
transaction_receiver_org_receiver_activity_id = default_first(transaction.xpath("receiver-org/@receiver-activity-id"))
transaction_receiver_org_ref = default_first(transaction.xpath("receiver-org/@ref"))
transaction_receiver_org_type = default_first(transaction.xpath("receiver-org/@type"))
transaction_ref = default_first(transaction.xpath("@ref"))
x_transaction_value = transaction_value
x_transaction_value_usd = ""
x_country_code_list = x_country_code.split("|")
x_country_percentage_list = x_country_percentage.split("|")
x_region_code_list = x_region_code.split("|")
x_region_percentage_list = x_region_percentage.split("|")
x_reporting_org_type = ""
x_transaction_type = ""
x_finance_type = ""
x_aid_type = ""
x_dac3_sector = ""
x_di_sector = ""
x_country = ""
x_reporting_org_type = recode_if_not_none(reporting_org_type_code, self.dictionaries["organisation_type"])
x_transaction_type = recode_if_not_none(transaction_type_code, self.dictionaries["transaction_type"])
x_finance_type = recode_if_not_none(x_finance_type_code, self.dictionaries["finance_type"])
x_aid_type = recode_if_not_none(transaction_aid_type_code, self.dictionaries["aid_type"])
x_sector_vocabulary_list = x_sector_vocabulary.split("|")
x_default_vocabulary = max(set(x_sector_vocabulary_list), key=x_sector_vocabulary_list.count)
x_sector_code_list = x_sector_code.split("|")
x_sector_percentage_list = x_sector_percentage.split("|")
if len(x_sector_vocabulary_list) > 0:
for k in range(0, len(x_sector_vocabulary_list)):
x_sector_code = x_sector_code_list[k]
x_sector_vocabulary = x_sector_vocabulary_list[k]
if x_default_vocabulary != x_sector_vocabulary:
x_default_vocabulary = ""
x_sector_percentage = x_sector_percentage_list[k]
try:
float_x_sector_percentage = float(x_sector_percentage)
except ValueError:
float_x_sector_percentage = 100
x_dac3_sector_code = ""
if x_sector_vocabulary == "1":
x_dac3_sector_code = x_sector_code[:3]
elif x_sector_vocabulary == "2":
x_dac3_sector_code = x_sector_code
x_dac3_sector = recode_if_not_none(x_dac3_sector_code, self.dictionaries["dac_3_sector"])
x_di_sector = recode_if_not_none(x_dac3_sector_code, self.dictionaries["di_sector"])
if len(x_country_code_list) > 0:
for j in range(0, len(x_country_code_list)):
x_country_code = x_country_code_list[j]
x_country = recode_if_not_none(x_country_code, self.dictionaries["country"])
x_country_percentage = x_country_percentage_list[j]
try:
float_x_country_percentage = float(x_country_percentage)
except ValueError:
float_x_country_percentage = 100
if transaction_convertable:
x_transaction_value = transaction_value * (float_x_country_percentage/100) * (float_x_sector_percentage/100)
if x_transaction_value and x_currency:
if x_currency in self.dictionaries["ratedf"]:
x_transaction_value_usd = convert_usd(x_transaction_value, year, x_currency, self.dictionaries["ratedf"])
else:
import pdb; pdb.set_trace()
row = [iati_identifier, x_transaction_number, reporting_org_ref, reporting_org_narrative, reporting_org_secondary_reporter, reporting_org_type_code, title_narrative, recipient_country_code, recipient_country_percentage, transaction_recipient_country_code, x_country_code, x_country_percentage, recipient_region_vocabulary, recipient_region_code, recipient_region_percentage, transaction_recipient_region_vocabulary, transaction_recipient_region_code, x_region_vocabulary, x_region_code, x_region_percentage, sector_vocabulary, sector_code, sector_percentage, transaction_sector_vocabulary, transaction_sector_code, x_sector_vocabulary, x_default_vocabulary, x_sector_code, x_sector_percentage, x_dac3_sector_code, transaction_type_code, transaction_date_iso_date, transaction_value_date, x_transaction_date, x_transaction_year, default_currency, transaction_value_currency, x_currency, transaction_value, x_transaction_value, x_transaction_value_usd, default_flow_type_code, transaction_flow_type_code, x_flow_type_code, default_finance_type_code, transaction_finance_type_code, x_finance_type_code, default_aid_type_vocabulary, default_aid_type_code, transaction_aid_type_vocabulary, transaction_aid_type_code, x_mod_aid_type_vocabulary, x_mod_aid_type_code, x_dac_aid_type_code, default_tied_status_code, transaction_tied_status_code, x_tied_status_code, transaction_disbursement_channel_code, description_narrative, transaction_description_narrative, humanitarian, transaction_humanitarian, humanitarian_scope_type, humanitarian_scope_vocabulary, humanitarian_scope_code, humanitarian_scope_narrative, x_hum_emergency_vocabulary, x_hum_emergency_code, x_hum_appeal_vocabulary, x_hum_appeal_code, transaction_provider_org_narrative, transaction_provider_org_provider_activity_id, transaction_provider_org_ref, transaction_provider_org_type, transaction_receiver_org_narrative, transaction_receiver_org_receiver_activity_id, transaction_receiver_org_ref, transaction_receiver_org_type, transaction_ref, participating_org_narrative, participating_org_type, participating_org_role, participating_org_ref, tag_narrative, tag_vocabulary, tag_code, x_reporting_org_type, x_transaction_type, x_country, x_finance_type, x_aid_type, x_dac3_sector, x_di_sector]
output.append(row)
elif len(x_region_code_list) > 0:
for j in range(0, len(x_region_code_list)):
x_region_code = x_region_code_list[j]
x_region_percentage = x_region_percentage_list[j]
try:
float_x_region_percentage = float(x_region_percentage)
except ValueError:
float_x_region_percentage = 100
if transaction_convertable:
x_transaction_value = transaction_value * (float_x_region_percentage/100) * (float_x_sector_percentage/100)
if x_transaction_value and x_currency:
if x_currency in self.dictionaries["ratedf"]:
x_transaction_value_usd = convert_usd(x_transaction_value, year, x_currency, self.dictionaries["ratedf"])
else:
import pdb; pdb.set_trace()
row = [iati_identifier, x_transaction_number, reporting_org_ref, reporting_org_narrative, reporting_org_secondary_reporter, reporting_org_type_code, title_narrative, recipient_country_code, recipient_country_percentage, transaction_recipient_country_code, x_country_code, x_country_percentage, recipient_region_vocabulary, recipient_region_code, recipient_region_percentage, transaction_recipient_region_vocabulary, transaction_recipient_region_code, x_region_vocabulary, x_region_code, x_region_percentage, sector_vocabulary, sector_code, sector_percentage, transaction_sector_vocabulary, transaction_sector_code, x_sector_vocabulary, x_default_vocabulary, x_sector_code, x_sector_percentage, x_dac3_sector_code, transaction_type_code, transaction_date_iso_date, transaction_value_date, x_transaction_date, x_transaction_year, default_currency, transaction_value_currency, x_currency, transaction_value, x_transaction_value, x_transaction_value_usd, default_flow_type_code, transaction_flow_type_code, x_flow_type_code, default_finance_type_code, transaction_finance_type_code, x_finance_type_code, default_aid_type_vocabulary, default_aid_type_code, transaction_aid_type_vocabulary, transaction_aid_type_code, x_mod_aid_type_vocabulary, x_mod_aid_type_code, x_dac_aid_type_code, default_tied_status_code, transaction_tied_status_code, x_tied_status_code, transaction_disbursement_channel_code, description_narrative, transaction_description_narrative, humanitarian, transaction_humanitarian, humanitarian_scope_type, humanitarian_scope_vocabulary, humanitarian_scope_code, humanitarian_scope_narrative, x_hum_emergency_vocabulary, x_hum_emergency_code, x_hum_appeal_vocabulary, x_hum_appeal_code, transaction_provider_org_narrative, transaction_provider_org_provider_activity_id, transaction_provider_org_ref, transaction_provider_org_type, transaction_receiver_org_narrative, transaction_receiver_org_receiver_activity_id, transaction_receiver_org_ref, transaction_receiver_org_type, transaction_ref, participating_org_narrative, participating_org_type, participating_org_role, participating_org_ref, tag_narrative, tag_vocabulary, tag_code, x_reporting_org_type, x_transaction_type, x_country, x_finance_type, x_aid_type, x_dac3_sector, x_di_sector]
output.append(row)
else:
if transaction_convertable:
x_transaction_value = transaction_value * (float_x_sector_percentage/100)
if x_transaction_value and x_currency:
if x_currency in self.dictionaries["ratedf"]:
x_transaction_value_usd = convert_usd(x_transaction_value, year, x_currency, self.dictionaries["ratedf"])
else:
import pdb; pdb.set_trace()
row = [iati_identifier, x_transaction_number, reporting_org_ref, reporting_org_narrative, reporting_org_secondary_reporter, reporting_org_type_code, title_narrative, recipient_country_code, recipient_country_percentage, transaction_recipient_country_code, x_country_code, x_country_percentage, recipient_region_vocabulary, recipient_region_code, recipient_region_percentage, transaction_recipient_region_vocabulary, transaction_recipient_region_code, x_region_vocabulary, x_region_code, x_region_percentage, sector_vocabulary, sector_code, sector_percentage, transaction_sector_vocabulary, transaction_sector_code, x_sector_vocabulary, x_default_vocabulary, x_sector_code, x_sector_percentage, x_dac3_sector_code, transaction_type_code, transaction_date_iso_date, transaction_value_date, x_transaction_date, x_transaction_year, default_currency, transaction_value_currency, x_currency, transaction_value, x_transaction_value, x_transaction_value_usd, default_flow_type_code, transaction_flow_type_code, x_flow_type_code, default_finance_type_code, transaction_finance_type_code, x_finance_type_code, default_aid_type_vocabulary, default_aid_type_code, transaction_aid_type_vocabulary, transaction_aid_type_code, x_mod_aid_type_vocabulary, x_mod_aid_type_code, x_dac_aid_type_code, default_tied_status_code, transaction_tied_status_code, x_tied_status_code, transaction_disbursement_channel_code, description_narrative, transaction_description_narrative, humanitarian, transaction_humanitarian, humanitarian_scope_type, humanitarian_scope_vocabulary, humanitarian_scope_code, humanitarian_scope_narrative, x_hum_emergency_vocabulary, x_hum_emergency_code, x_hum_appeal_vocabulary, x_hum_appeal_code, transaction_provider_org_narrative, transaction_provider_org_provider_activity_id, transaction_provider_org_ref, transaction_provider_org_type, transaction_receiver_org_narrative, transaction_receiver_org_receiver_activity_id, transaction_receiver_org_ref, transaction_receiver_org_type, transaction_ref, participating_org_narrative, participating_org_type, participating_org_role, participating_org_ref, tag_narrative, tag_vocabulary, tag_code, x_reporting_org_type, x_transaction_type, x_country, x_finance_type, x_aid_type, x_dac3_sector, x_di_sector]
output.append(row)
else:
if x_default_vocabulary != x_sector_vocabulary:
x_default_vocabulary = ""
if len(x_country_code_list) > 0:
for j in range(0, len(x_country_code_list)):
x_country_code = x_country_code_list[j]
x_country = recode_if_not_none(x_country_code, self.dictionaries["country"])
x_country_percentage = x_country_percentage_list[j]
try:
float_x_country_percentage = float(x_country_percentage)
except ValueError:
float_x_country_percentage = 100
if transaction_convertable:
x_transaction_value = transaction_value * (float_x_country_percentage/100) * (float_x_sector_percentage/100)
if x_transaction_value and x_currency:
if x_currency in self.dictionaries["ratedf"]:
x_transaction_value_usd = convert_usd(x_transaction_value, year, x_currency, self.dictionaries["ratedf"])
else:
import pdb; pdb.set_trace()
row = [iati_identifier, x_transaction_number, reporting_org_ref, reporting_org_narrative, reporting_org_secondary_reporter, reporting_org_type_code, title_narrative, recipient_country_code, recipient_country_percentage, transaction_recipient_country_code, x_country_code, x_country_percentage, recipient_region_vocabulary, recipient_region_code, recipient_region_percentage, transaction_recipient_region_vocabulary, transaction_recipient_region_code, x_region_vocabulary, x_region_code, x_region_percentage, sector_vocabulary, sector_code, sector_percentage, transaction_sector_vocabulary, transaction_sector_code, x_sector_vocabulary, x_default_vocabulary, x_sector_code, x_sector_percentage, x_dac3_sector_code, transaction_type_code, transaction_date_iso_date, transaction_value_date, x_transaction_date, x_transaction_year, default_currency, transaction_value_currency, x_currency, transaction_value, x_transaction_value, x_transaction_value_usd, default_flow_type_code, transaction_flow_type_code, x_flow_type_code, default_finance_type_code, transaction_finance_type_code, x_finance_type_code, default_aid_type_vocabulary, default_aid_type_code, transaction_aid_type_vocabulary, transaction_aid_type_code, x_mod_aid_type_vocabulary, x_mod_aid_type_code, x_dac_aid_type_code, default_tied_status_code, transaction_tied_status_code, x_tied_status_code, transaction_disbursement_channel_code, description_narrative, transaction_description_narrative, humanitarian, transaction_humanitarian, humanitarian_scope_type, humanitarian_scope_vocabulary, humanitarian_scope_code, humanitarian_scope_narrative, x_hum_emergency_vocabulary, x_hum_emergency_code, x_hum_appeal_vocabulary, x_hum_appeal_code, transaction_provider_org_narrative, transaction_provider_org_provider_activity_id, transaction_provider_org_ref, transaction_provider_org_type, transaction_receiver_org_narrative, transaction_receiver_org_receiver_activity_id, transaction_receiver_org_ref, transaction_receiver_org_type, transaction_ref, participating_org_narrative, participating_org_type, participating_org_role, participating_org_ref, tag_narrative, tag_vocabulary, tag_code, x_reporting_org_type, x_transaction_type, x_country, x_finance_type, x_aid_type, x_dac3_sector, x_di_sector]
output.append(row)
elif len(x_region_code_list) > 0:
for j in range(0, len(x_region_code_list)):
x_region_code = x_region_code_list[j]
x_region_percentage = x_region_percentage_list[j]
try:
float_x_region_percentage = float(x_region_percentage)
except ValueError:
float_x_region_percentage = 100
if transaction_convertable:
x_transaction_value = transaction_value * (float_x_region_percentage/100) * (float_x_sector_percentage/100)
if x_transaction_value and x_currency:
if x_currency in self.dictionaries["ratedf"]:
x_transaction_value_usd = convert_usd(x_transaction_value, year, x_currency, self.dictionaries["ratedf"])
else:
import pdb; pdb.set_trace()
row = [iati_identifier, x_transaction_number, reporting_org_ref, reporting_org_narrative, reporting_org_secondary_reporter, reporting_org_type_code, title_narrative, recipient_country_code, recipient_country_percentage, transaction_recipient_country_code, x_country_code, x_country_percentage, recipient_region_vocabulary, recipient_region_code, recipient_region_percentage, transaction_recipient_region_vocabulary, transaction_recipient_region_code, x_region_vocabulary, x_region_code, x_region_percentage, sector_vocabulary, sector_code, sector_percentage, transaction_sector_vocabulary, transaction_sector_code, x_sector_vocabulary, x_default_vocabulary, x_sector_code, x_sector_percentage, x_dac3_sector_code, transaction_type_code, transaction_date_iso_date, transaction_value_date, x_transaction_date, x_transaction_year, default_currency, transaction_value_currency, x_currency, transaction_value, x_transaction_value, x_transaction_value_usd, default_flow_type_code, transaction_flow_type_code, x_flow_type_code, default_finance_type_code, transaction_finance_type_code, x_finance_type_code, default_aid_type_vocabulary, default_aid_type_code, transaction_aid_type_vocabulary, transaction_aid_type_code, x_mod_aid_type_vocabulary, x_mod_aid_type_code, x_dac_aid_type_code, default_tied_status_code, transaction_tied_status_code, x_tied_status_code, transaction_disbursement_channel_code, description_narrative, transaction_description_narrative, humanitarian, transaction_humanitarian, humanitarian_scope_type, humanitarian_scope_vocabulary, humanitarian_scope_code, humanitarian_scope_narrative, x_hum_emergency_vocabulary, x_hum_emergency_code, x_hum_appeal_vocabulary, x_hum_appeal_code, transaction_provider_org_narrative, transaction_provider_org_provider_activity_id, transaction_provider_org_ref, transaction_provider_org_type, transaction_receiver_org_narrative, transaction_receiver_org_receiver_activity_id, transaction_receiver_org_ref, transaction_receiver_org_type, transaction_ref, participating_org_narrative, participating_org_type, participating_org_role, participating_org_ref, tag_narrative, tag_vocabulary, tag_code, x_reporting_org_type, x_transaction_type, x_country, x_finance_type, x_aid_type, x_dac3_sector, x_di_sector]
output.append(row)
else:
if transaction_convertable:
x_transaction_value = transaction_value * (float_x_sector_percentage/100)
if x_transaction_value and x_currency:
if x_currency in self.dictionaries["ratedf"]:
x_transaction_value_usd = convert_usd(x_transaction_value, year, x_currency, self.dictionaries["ratedf"])
else:
import pdb; pdb.set_trace()
row = [iati_identifier, x_transaction_number, reporting_org_ref, reporting_org_narrative, reporting_org_secondary_reporter, reporting_org_type_code, title_narrative, recipient_country_code, recipient_country_percentage, transaction_recipient_country_code, x_country_code, x_country_percentage, recipient_region_vocabulary, recipient_region_code, recipient_region_percentage, transaction_recipient_region_vocabulary, transaction_recipient_region_code, x_region_vocabulary, x_region_code, x_region_percentage, sector_vocabulary, sector_code, sector_percentage, transaction_sector_vocabulary, transaction_sector_code, x_sector_vocabulary, x_default_vocabulary, x_sector_code, x_sector_percentage, x_dac3_sector_code, transaction_type_code, transaction_date_iso_date, transaction_value_date, x_transaction_date, x_transaction_year, default_currency, transaction_value_currency, x_currency, transaction_value, x_transaction_value, x_transaction_value_usd, default_flow_type_code, transaction_flow_type_code, x_flow_type_code, default_finance_type_code, transaction_finance_type_code, x_finance_type_code, default_aid_type_vocabulary, default_aid_type_code, transaction_aid_type_vocabulary, transaction_aid_type_code, x_mod_aid_type_vocabulary, x_mod_aid_type_code, x_dac_aid_type_code, default_tied_status_code, transaction_tied_status_code, x_tied_status_code, transaction_disbursement_channel_code, description_narrative, transaction_description_narrative, humanitarian, transaction_humanitarian, humanitarian_scope_type, humanitarian_scope_vocabulary, humanitarian_scope_code, humanitarian_scope_narrative, x_hum_emergency_vocabulary, x_hum_emergency_code, x_hum_appeal_vocabulary, x_hum_appeal_code, transaction_provider_org_narrative, transaction_provider_org_provider_activity_id, transaction_provider_org_ref, transaction_provider_org_type, transaction_receiver_org_narrative, transaction_receiver_org_receiver_activity_id, transaction_receiver_org_ref, transaction_receiver_org_type, transaction_ref, participating_org_narrative, participating_org_type, participating_org_role, participating_org_ref, tag_narrative, tag_vocabulary, tag_code, x_reporting_org_type, x_transaction_type, x_country, x_finance_type, x_aid_type, x_dac3_sector, x_di_sector]
output.append(row)
return output