-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlei_to_bods.py
263 lines (224 loc) · 9.98 KB
/
lei_to_bods.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
import json, random
def read_lei(file):
if '-lei2-' in file:
itemtag = 'lei:LEIRecord xmlns'
elif '-rr-' in file:
itemtag = 'rr:RelationshipRecord xmlns'
elif '-repex-' in file:
itemtag = 'repex:Exception xmlns'
else:
raise ValueError("file type invalid")
with open(file) as f:
dd = f.readlines()
f.close()
out = []
record = ''
for line in dd:
if '<' + itemtag in line:
out.append(record)
record = line
else:
record = record + line
out.append(record)
return(out[1:len(out)])
def lei1_to_entity_statement(lei1Object):
splitRecord = lei1Object.split('\n')
statementID = ''
statementType = 'entityStatement'
statementDate = ''
entityType = 'registeredEntity'
name = ''
jurisdiction = ''
identifiers = ''
foundingDate = ''
registeredAddress = ''
registeredAddressPostCode = ''
registeredAdressCountry = ''
registeredAddressSwitch = 0
businessAddress = ''
businessAddressPostCode = ''
businessAddressCountry = ''
businsessAddressSwitch = 0
sourceType = ['officialRegister']
sourceDescription = 'GLEIF'
for line in splitRecord:
if '<lei:LegalName' in line:
name = line.split('>')[1].split('<')[0]
if '<lei:LastUpdateDate>' in line:
statementDate = line.split('>')[1].split('<')[0]
if '<lei:LegalJurisdiction>' in line:
jurisdiction = {'code':line.split('>')[1].split('<')[0]}
if '<lei:LEI>' in line:
identifiers = [{'id':line.split('>')[1].split('<')[0],
'scheme':'XI-LEI',
'schemeName':'Global Legal Entity Identifier Index'}]
statementID = line.split('>')[1].split('<')[0]
if '<lei:EntityCreationDate>' in line:
foundingDate = line.split('>')[1].split('<')[0]
if '<lei:LegalAddress' in line:
registeredAddressSwitch = 1
registeredAddressString = ''
if '</lei:LegalAddress' in line:
registeredAddressSwitch = 0
registeredAddress = {'type':'registered','address':registeredAddressString,'postCode':registeredAddressPostCode,'country':registeredAdressCountry}
if registeredAddressSwitch == 1:
if '<lei:FirstAddressLine>' in line:
registeredAddressString = registeredAddressString + line.split('>')[1].split('<')[0] + ','
if '<lei:City>' in line:
registeredAddressString = registeredAddressString + line.split('>')[1].split('<')[0] + ','
if '<lei:Region>' in line:
registeredAddressString = registeredAddressString + line.split('>')[1].split('<')[0]
if '<lei:Country>' in line:
registeredAdressCountry = line.split('>')[1].split('<')[0]
if '<lei:PostalCode>' in line:
registeredAddressPostCode = line.split('>')[1].split('<')[0]
if '<lei:HeadquartersAddress' in line:
businsessAddressSwitch = 1
businsessAddressString = ''
if '</lei:HeadquartersAddress' in line:
businsessAddressSwitch = 0
businessAddress = {'type':'business','address':businsessAddressString,'postCode':businessAddressPostCode,'country':businessAddressCountry}
if businsessAddressSwitch == 1:
if '<lei:FirstAddressLine>' in line:
businsessAddressString = businsessAddressString + line.split('>')[1].split('<')[0] + ','
if '<lei:City>' in line:
businsessAddressString = businsessAddressString + line.split('>')[1].split('<')[0] + ','
if '<lei:Region>' in line:
businsessAddressString = businsessAddressString + line.split('>')[1].split('<')[0]
if '<lei:Country>' in line:
businessAddressCountry = line.split('>')[1].split('<')[0]
if '<lei:PostalCode>' in line:
businessAddressPostCode = line.split('>')[1].split('<')[0]
if '<lei:ValidationSources>FULLY_CORROBORATED</lei:ValidationSources>' in line:
sourceType.append('verified')
d = {'statementID': statementID,
'statementType':statementType,
'statementDate':statementDate,
'entityType':entityType,
'name':name,
'jurisdiction':jurisdiction,
'identifiers':identifiers,
'foundingDate':foundingDate,
'addresses':[registeredAddress,businessAddress],
'source':{'type':sourceType,'description':sourceDescription}}
out = json.dumps(d,indent = 4)
return(out)
def lei2_relationship_to_ooc_statement(lei2Object):
splitRecord = lei2Object.split('\n')
statementID = ''.join(random.choice('0123456789ABCDEF') for i in range(32))
statementType = 'ownershipOrControlStatement'
statementDate = ''
subjectDescribedByEntityStatement = ''
subjectSwitch = 0
interestedPartyDescribedByEntityStatement = ''
interestedPartySwitch = 0
interestType = 'otherInfluenceOrControl'
interestLevel = 'unknown'
interestStartDate = ''
interestStartDateRelationship = ''
interestStartDateAccounting = ''
interestStartDateSwitch = 0
beneficialOwnershipOrControl = False
sourceType = ['officialRegister']
sourceDescription = 'GLEIF'
for line in splitRecord:
if '<rr:LastUpdateDate>' in line:
statementDate = line.split('>')[1].split('<')[0]
if '<rr:StartNode>' in line:
interestedPartySwitch = 1
if '</rr:StartNode>' in line:
interestedPartySwitch = 0
if interestedPartySwitch == 1 and '<rr:NodeID>' in line:
interestedPartyDescribedByEntityStatement = line.split('>')[1].split('<')[0]
if '<rr:EndNode>' in line:
subjectSwitch = 1
if '</rr:EndNode>' in line:
subjectSwitch = 0
if subjectSwitch == 1 and '<rr:NodeID>' in line:
subjectDescribedByEntityStatement = line.split('>')[1].split('<')[0]
if '<rr:RelationshipType>IS_ULTIMATELY_CONSOLIDATED_BY</rr:RelationshipType>' in line:
interestLevel = 'indirect'
if '<rr:RelationshipType>IS_DIRECTLY_CONSOLIDATED_BY</rr:RelationshipType>' in line:
interestLevel = 'direct'
if '<rr:RelationshipPeriod>' in line:
interestStartDateSwitch = 1
if '</rr:RelationshipPeriod>' in line:
interestStartDateSwitch = 0
if interestStartDateSwitch == 1 and '<rr:StartDate>' in line:
interestStartDate = line.split('>')[1].split('<')[0]
if interestStartDateSwitch == 1 and '<rr:PeriodType>RELATIONSHIP_PERIOD</rr:PeriodType>' in line:
interestStartDateRelationship = interestStartDate
if '<rr:ValidationSources>FULLY_CORROBORATED</rr:ValidationSources>' in line:
sourceType.append('verified')
if interestStartDateRelationship != '':
interestStartDate = interestStartDateRelationship
d = {'statementID': statementID,
'statementType':statementType,
'statementDate':statementDate,
'subject':{'describedByEntityStatement':subjectDescribedByEntityStatement},
'interestedParty':{'describedByEntityStatement':interestedPartyDescribedByEntityStatement},
'interests':[{'type':interestType,
'interestLevel':interestLevel,
'beneficialOwnershipOrControl':beneficialOwnershipOrControl,
'startDate':interestStartDate}],
'source':{'type':sourceType,'description':sourceDescription}}
out = json.dumps(d,indent = 4)
return(out)
def lei2_exception_to_ooc_statement(lei2Object):
splitRecord = lei2Object.split('\n')
statementID = ''.join(random.choice('0123456789ABCDEF') for i in range(32))
statementType = 'ownershipOrControlStatement'
subjectDescribedByEntityStatement = ''
interestedPartyUnspecifiedReason = ''
interestType = 'unknownInterest'
interestLevel = 'unknown'
sourceType = ['officialRegister']
sourceDescription = 'GLEIF'
for line in splitRecord:
if '<repex:ExceptionReason>' in line:
interestedPartyUnspecifiedReason = line.split('>')[1].split('<')[0]
if '<repex:LEI>' in line:
subjectDescribedByEntityStatement = line.split('>')[1].split('<')[0]
if '<repex:ExceptionCategory>ULTIMATE_ACCOUNTING_CONSOLIDATION_PARENT</repex:ExceptionCategory>' in line:
interestLevel = 'indirect'
if '<repex:ExceptionCategory>DIRECT_ACCOUNTING_CONSOLIDATION_PARENT</repex:ExceptionCategory>' in line:
interestLevel = 'direct'
d = {'statementID': statementID,
'statementType':statementType,
'subject':{'describedByEntityStatement':subjectDescribedByEntityStatement},
'interestedParty':{'unspecified':interestedPartyUnspecifiedReason},
'interests':[{'type':interestType,
'interestLevel':interestLevel,
'source':{'type':sourceType,'description':sourceDescription}}]
}
out = json.dumps(d,indent = 4)
return(out)
def lei_statement_to_bods(leifile, write = False, outfile = 'out.json'):
with open(leifile) as f:
dd = f.readlines()
f.close()
itemtags = ['lei:LEIRecord xmlns','rr:RelationshipRecord xmlns','repex:Exception xmlns']
out = []
record = ''
for line in dd:
if any(item in line for item in itemtags):
out.append(record)
record = line
else:
record = record + line
out.append(record)
out = out[1:len(out)]
finalout = '[\n'
for item in out:
if 'lei:LEIRecord xmlns' in item:
finalout = finalout + lei1_to_entity_statement(item) + ',\n'
elif 'rr:RelationshipRecord xmlns' in item:
finalout = finalout + lei2_relationship_to_ooc_statement(item) + ',\n'
elif 'repex:Exception xmlns' in item:
finalout = finalout + lei2_exception_to_ooc_statement(item) + ',\n'
finalout = finalout[0:(len(finalout)-2)] + '\n]'
if write == True:
with open(outfile, 'w') as f:
f.write(finalout)
f.close()
return(finalout)