forked from EUDAT-B2FIND/md-ingestion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverting.py
318 lines (272 loc) · 13.6 KB
/
converting.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
# -*- coding: utf-8 -*-
"""converting.py - class for B2FIND converting :
- Converter maps harvested and specific MD records onto B2FIND schema
Copyright (c) 2013 Heinrich Widmann (DKRZ)
Further contributions by
2013 John Mrziglod (DKRZ)
2014 Mikael Karlsson (CSC)
2017 Claudia Martens (DKRZ)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
# from future
from __future__ import absolute_import
from __future__ import print_function
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
__author__ = "Heinrich Widmann"
# system relevant modules:
import os, glob, sys
import time, datetime, subprocess
# program relevant modules:
import logging
import traceback
import re
# needed for MAPPER :
import codecs
import xml.etree.ElementTree as ET
import simplejson as json
import io
from pyparsing import *
import Levenshtein as lvs
import iso639
from collections import OrderedDict, Iterable
PY2 = sys.version_info[0] == 2
if PY2:
from urllib2 import urlopen
from urllib2 import HTTPError,URLError
else:
from urllib.request import urlopen
from urllib.error import HTTPError,URLError
class Converter(object):
"""
### CONVERTER - class
# Parameters:
# -----------
# Public Methods:
# ---------------
# convert(request) - converts records according to request on target schema (e.g. CERA) XMLs
# using templates and stores resulting files in subdirectory ' ...-<targetschema>/xml'
#
"""
def __init__ (self, OUT, base_outdir,fromdate):
##HEW-D logging = logging.getLogger()
self.base_outdir = base_outdir
self.OUT = OUT
self.fromdate = fromdate
self.logger = OUT.logger
# Read in B2FIND metadata schema and fields
schemafile = '%s/mapfiles/b2find_schema.json' % (os.getcwd())
with open(schemafile, 'r') as f:
self.b2findfields=json.loads(f.read(), object_pairs_hook=OrderedDict)
def json2xml(self,json_obj, line_padding="", mdftag="", mapdict="b2findfields"):
result_list = list()
json_obj_type = type(json_obj)
if json_obj_type is list:
for sub_elem in json_obj:
result_list.append(json2xml(sub_elem, line_padding, mdftag, mapdict))
return "\n".join(result_list)
if json_obj_type is dict:
for tag_name in json_obj:
sub_obj = json_obj[tag_name]
if tag_name in mapdict :
tag_name=mapdict[tag_name]
if not isinstance(tag_name,list) : tag_name=[tag_name]
for key in tag_name:
result_list.append("%s<%s%s>" % (line_padding, mdftag, key))
if type(sub_obj) is list:
for nv in sub_obj:
if tag_name == 'tags' or tag_name == 'KEY_CONNECT.GENERAL_KEY':
result_list.append("%s%s" % (line_padding, nv["name"].strip()))
else:
result_list.append("%s%s" % (line_padding, nv.strip()))
else:
result_list.append(self.json2xml(sub_obj, "\t" + line_padding, mdftag, mapdict))
result_list.append("%s</%s%s>" % (line_padding, mdftag, key))
else:
self.logger.debug ('[WARNING] : Field %s can not mapped to B2FIND schema' % tag_name)
continue
return "\n".join(result_list)
return "%s%s" % (line_padding, json_obj)
def convert(self,request):
# Converts JSON files to XML files formatted in target format, e.g. 'CERA' (exp_ and ds2_ files)
results = {
'count':0,
'tcount':0,
'ecount':0,
'time':0
}
# set processing parameters
community=request[0]
mdprefix=request[3]
mdsubset=request[4] if len(request)>4 else None
target_mdschema=request[5] if len(request)>5 else None
# set subset:
if (not mdsubset):
subset = 'SET_1' ## or 2,...
elif mdsubset.endswith('_'): # no OAI subsets, but store in sub dirs
subset = mdsubset+'1' ## or 2,...
elif mdsubset[-1].isdigit() and mdsubset[-2] == '_' :
subset = mdsubset
else:
subset = mdsubset+'_1'
self.logger.info(' |- Subset: \t%s' % subset )
# check for target_mdschema and set subset and path
if (target_mdschema):
# data subset dir :
outpath = '/'.join([self.base_outdir,community+'-'+mdprefix+'-'+target_mdschema,subset,'xml'])
self.logger.info('\t|- Data out path:\t%s' % outpath)
else:
self.logger.critical('For OAI converter processing target metaschema must be given!')
sys.exit()
inpath = '/'.join([self.base_outdir,community+'-'+mdprefix,subset])
# check data in and out path
if not os.path.exists(inpath+'/json') or not os.listdir(inpath + '/json'):
self.logger.error('[ERROR] Can not access input data path %s' % (inpath+'/json') )
return results
elif not os.path.exists(outpath) :
self.logger.warning('[ERROR] Create not existing output data path %s' % (outpath) )
os.makedirs(outpath)
# run oai-converting
# find all .json files in inpath/json:
files = list(filter(lambda x: x.endswith('.json'), os.listdir(inpath+'/json')))
results['tcount'] = len(files)
##oaiset=path.split(target_mdschema)[0].split('_')[0].strip('/')
##oaiset=os.path.basename(path)
## outpath=path.split(community)[0]+'/b2find-oai_b2find/'+community+'/'+mdprefix +'/'+path.split(mdprefix)[1].split('_')[0]+'/xml'
##HEW-D outpath=path.split(community)[0]+'b2find-oai_b2find/'+community+'/'+mdprefix +'/xml'
self.logger.debug(' %s Converting of files in %s' % (time.strftime("%H:%M:%S"),inpath))
self.logger.debug(' | %-4s | %-40s | %-40s |\n |%s|' % ('#','infile','outfile',"-" * 53))
fcount = 0
oldperc = 0
start = time.time()
# Read in B2FIND metadata schema and fields
schemafile = '%s/mapfiles/b2find_schema.json' % (os.getcwd())
with open(schemafile, 'r') as f:
b2findfields=json.loads(f.read())
for filename in files:
## counter and progress bar
fcount+=1
self.logger.debug('Next file to convert : %s' % inpath+'/json/'+filename)
perc=int(fcount*100/int(len(files)))
bartags=int(perc/5)
if perc%10 == 0 and perc != oldperc :
oldperc=perc
print ("\r\t[%-20s] %d / %d%% in %d sec" % ('='*bartags, fcount, perc, time.time()-start ))
sys.stdout.flush()
createdate = str(datetime.datetime.utcnow())
jsondata = dict()
self.logger.debug(' |- %s INFO JSON2XML - Processing: %s/%s' % (time.strftime("%H:%M:%S"),os.path.basename(inpath),filename))
if ( os.path.getsize(inpath+'/json/'+filename) > 0 ):
self.logger.debug('Read json file %s' % inpath+'/json/'+filename)
with open(inpath+'/json/'+filename, 'r') as f:
try:
jsondata=json.loads(f.read())
except:
self.logger.error(' | [ERROR] Can not access json file %s' % inpath+'/json/'+filename)
results['ecount'] += 1
continue
else:
self.logger.critical('Can not access json file %s' % inpath+'/json/'+filename)
results['ecount'] += 1
continue
### oai-convert !!
if target_mdschema == 'cera':
##HEW-T print('JJJJJJJJ %s' % jsondata['oai_identifier'])
if 'oai_identifier' in jsondata :
identifier=jsondata['oai_identifier'][0]
else:
identifier=os.path.splitext(filename)[0]
convertfile='%s/mapfiles/%s%s.%s' % (os.getcwd(),'json2',target_mdschema,'json')
with open(convertfile, 'r') as f:
try:
mapdict=json.loads(f.read())
except:
self.logger.error(' | [ERROR] Cannot load the convert file %s' % convertfile)
sys.exit()
for filetype in ['ds2','exp']:
outfile=outpath+'/'+filetype+'_'+community+'_'+identifier+'.xml'
### load xml template
templatefile='%s/mapfiles/%s_%s_%s.%s' % (os.getcwd(),target_mdschema,filetype,'template','xml')
with open(templatefile, 'r') as f:
try:
dsdata= f.read() ##HEW-D ET.parse(templatefile).getroot()
except Exception :
self.logger.error(' | Cannot load tempalte file %s' % (templatefile))
data=dict()
jsondata['community']=community
##HEW-D dsdata = Template(dsdata)
for facetdict in b2findfields.values() :
facet=facetdict["ckanName"]
##HEW-T print ('facet %s ' % facet)
if facet in jsondata:
if isinstance(jsondata[facet],list) and len(jsondata[facet])>0 :
if facet == 'tags':
data[facet]=''
for tagndict in jsondata[facet]:
data[facet]+=tagndict['name']
else:
data[facet]=' '.join(jsondata[facet]).strip('\n ')
else :
data[facet]=jsondata[facet]
## outdata = dsdata.substitute(key=data[key])
##HEW-T print('KKKK key %s\t data %s' % (key,data[key]))
else:
data[facet]=''
data['identifier']=identifier
try:
outdata=dsdata%data
except KeyError as err :
self.logger.error("[ERROR] %s\n" % err )
pass
outfile=outpath+'/'+filetype+'_'+identifier+'.xml'
try :
f = open(outfile, 'w')
f.write(outdata.encode('utf-8'))
f.write("\n")
f.close
except IOError :
self.logger.error("[ERROR] Cannot write data in xml file '%s': %s\n" % (outfile))
return(False, outfile , outpath, fcount)
else:
identifier=jsondata["oai_identifier"]
outfile=outpath+'/'+filetype+'/'+community+'_'+identifier+'.xml'
mapdict=self.b2findfields ##HEW-D ??? ckanfields ???
header="""<record xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<identifier>"""+identifier+"""</identifier>
<datestamp>"""+createdate+"""</datestamp>
<setSpec>"""+oaiset+"""</setSpec>
</header>
<metadata>
<oai_b2find:b2find xmlns:b2find="http://purl.org/b2find/elements/1.1/" xmlns:oai_b2find="http://www.openarchives.org/OAI/2.0/oai_b2find/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://b2find.eudat.eu/schema http://b2find.eudat.eu/schema/oai_b2find.xsd">
"""
footer="""
</oai_b2find:b2find>
</metadata>
</record>"""
xmlprefix='b2find'
xmldata=header+self.json2xml(jsondata,'\t',xmlprefix,mapdict)+footer
try:
f = open(outfile, 'w')
f.write(xmldata.encode('utf-8'))
f.write("\n")
f.close
except IOError :
self.logger.error("[ERROR] Cannot write data in xml file '%s': %s\n" % (outfile))
return(False, outfile , outpath, fcount)
self.logger.debug(' | o | %-4d | %-45s | %-45s |' % (fcount,os.path.basename(filename),os.path.basename(outfile)))
self.logger.info('%s INFO B2FIND : %d records converted; %d records caused error(s).' % (time.strftime("%H:%M:%S"),fcount,results['ecount']))
# count ... all .xml files in path/b2find
results['count'] = len(list(filter(lambda x: x.endswith('.xml'), os.listdir(outpath))))
print (' \t|- %-10s |@ %-10s |\n\t| Provided | Converted | Failed |\n\t| %8d | %6d | %6d |' % ( 'Finished',time.strftime("%H:%M:%S"),
results['tcount'],
fcount,
results['ecount']
))
return results