-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepare.py
executable file
·582 lines (466 loc) · 15.8 KB
/
prepare.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""This program is a utility file which executes a set of specified functions on a element tree of a xml file.
The current functions are limited to JATS and BITS XML Scheme of the Library of Congress. However the methods defined in
this program can be used to modify any element tree. Some of the current available functions are sorting footnotes,
numbering elements of a certain type or setting unique ids to a certain element types.
Usage:
prepare.py <input_file> <path> [options]
Options:
-c --citations-to-references
-d --debug Enable debug output
-f --sort-footnotes=<tag list as comma seperated lists>
-h --help
-m --metadata=<file__name_schema.xml>
-n --set-numbering-tags=<elemennt types as comma seperated lists>
-r --clean-references
-s --sort-references=<tag list as comma seperated lists>
-t --stand-alone
-u --set-uuids=<element types as comma seperated list>
-v --set-numbering-values=<numbering values, additionaly roman numbers e.g.xref,ref-type,fn,{1:2} >
All the are done in the global element tree for performance reasons.
References
----------
* Web : https://github.com/withanage/mpt
* Repository and issue-tracker: https://github.com/withanage/mpt/issues
* Licensed under terms of GPL 3 license (LICENSE.md)
"""
__author__ = "Dulip Withanage"
PYTHON_IMPORT_FAILED_LXML_MODULE = 'Failed to import python lxml module'
import os
import sys
import uuid
from debug import Debuggable, Debug
from docopt import docopt
from globals import GV
from settingsconfiguration import Settings
try:
from lxml import etree
from lxml import objectify
except ImportError:
print(PYTHON_IMPORT_FAILED_LXML_MODULE)
sys.exit(1)
class Prepare(Debuggable):
"""
Standalone Processing object to combine, clean and modify a JATS XML file and optionally inject BITS Metadata headers.
Features
--------
add Id numbering for any tag type, clean comments, remove unused references,
set numbering, add unique ids to certain tag types, sort references
"""
def __init__(self):
self.args = self.read_command_line()
self.debug = Debug()
self.settings = Settings(self.args)
self.gv = GV(self.settings)
Debuggable.__init__(self, 'Main')
if self.args.get('--debug'):
self.debug.enable_debug()
self.dr = self.args.get('<path>')
self.f = self.args.get('<input_file>')
self.stand_alone = self.args.get('--stand-alone')
self.tr = etree.parse(os.path.join(self.dr, self.f))
@staticmethod
def read_command_line():
"""
Reads and generates a docopt dictionary from the command line parameters.
Returns
-------
docopt : dictionary
A dictionary, where keys are names of command-line elements such as and values are theparsed values of those
elements.
"""
return docopt(__doc__, version='xml 0.1')
def citations_to_references(self):
""" Removes mixed-citation block, adds as a <sec> Section element
Returns
-------
tr : elementtree
"""
t = self.tr.getroot()
bd = t.find('.//body')
sc = etree.Element('sec')
ttl = etree.Element('title')
ttl.text = 'References'
sc.append(ttl)
mc = t.findall('.//mixed-citation')
if len(mc) > 0:
for r in mc:
r.tag = 'p'
sc.append(r)
bd.append(sc)
rlst = t.find('.//ref-list')
rlst.getparent().remove(rlst)
bck = t.find('.//back')
bck.append(etree.Element('ref-list'))
return self.tr
def clean_references(self):
""" removes references, which are not linked.
Parameters
-----------
tag : str
name of the XML tag
Returns
-------
tr : elementtree
See Also
--------
remove_element, remove_tags
"""
r = self.tr.getroot()
for e in r.findall('.//back/ref-list/ref'):
if e.attrib.get('id'):
if r.find(".//xref[@ref-type='bibr'][@rid='" + e.attrib.get('id') + "']") is None:
self.remove_element(e)
else:
self.remove_element(e)
for e in r.findall(".//xref[@ref-type='bibr']"):
if r.find(".//back/ref-list/ref[@id='" + e.attrib.get('rid') + "']") is None:
if e.getparent() is not None:
for c in e.getparent().getiterator():
if c.tag == 'xref' and c.attrib.get('ref-type') == 'bibr':
self.remove_tags(c)
return self.tr
def remove_tags(self, e):
"""
Takes an etree element and replaces it with its own text
Parameters
----------
e : element
Element to be replaced
"""
if e.getparent() is not None:
previous = e.getprevious()
if previous is not None:
if previous.tail:
if e.text:
previous.tail = previous.tail + e.text
if e.tail:
previous.tail = previous.tail + e.tail
e.getparent().remove(e)
def remove_element(self, e):
"""
Remove any element only if it has a parent
Parameters
----------
e : element
Element to be replaced
"""
if e.getparent() is not None:
e.getparent().remove(e)
def set_uuids_for_back_matter(self, tags):
"""
Add unique id tags to any of the sub-elements of the back matter
Parameters
----------
tags: list
list of elements
Returns
-------
tr : elementtree
"""
for s in tags:
f = {}
ref_type = 'bibr' if s == 'ref' else s
fns = self.tr.getroot().findall(
''.join(['.//xref/[@ref-type="', ref_type, '"]']))
for i in fns:
rid = ''.join(['bibd', str(uuid.uuid4())])
f[i.attrib['rid']] = rid
i.set('rid', rid)
for m in list(f.keys()):
n = self.tr.getroot().find(
''.join(['.//' + s + '/[@id="', m, '"]']))
if n is not None:
n.set('id', f[m]) if len(n) > 0 else ''
return self.tr
def set_numbering_values(
self,
tag,
attr,
value,
count,
range_list):
"""
Adds numerical values to a tag in arguments list
Parameters
---------
tag: str
xml tag name
attr: str
attribute name
value :str
value name
count : int
current sequence number
range_list : list
lower and upper level for the numbering
See Also
--------
set_roman_numbers
"""
searchTag = './/' + tag + '[@' + attr + '="' + value + '"]'
elems = self.tr.getroot().findall(searchTag)
range_count = 1
for elem in elems:
elem.text, range_count = self.set_roman_numbers(
count, range_count, range_list)
count += 1
return self.tr, count
def convert_int_to_roman(self, i):
"""
Converts an integer number into a roman number
Parameters
---------
i : int
integer number
Returns
-------
result : str
Roman number
"""
result = []
for integer, numeral in self.gv.numeral_map:
count = i // integer
result.append(numeral * count)
i -= integer * count
return ''.join(result)
def set_roman_numbers(self, count, r_count, range_list):
"""
Converts a given set of elements defined by range_array into roman numbers
Parameters
---------
count :int
r_count : int
range_list : list
lower and upper level for the numbering
Returns
-------
val : str
r_count: int
See Also
--------
convert_int_to_roman
"""
val = str(count)
if int(range_list[0]) <= count <= int(range_list[1]):
val = self.convert_int_to_roman(r_count).lower()
r_count += 1
else:
val = str(count - r_count + 1)
return val, r_count
def merge_metadata(self, metadata):
"""
reads a metadata file path and merge its content into the metadata section
Parameters
----------
metadata : str
suffix of the metadata files
Returns
-------
tr : elementTree
Element tree of the current file
See Also
-------
create_metadata_path
"""
r = self.tr.getroot()
pth = self.create_metadata_path(metadata)
if os.path.isfile(pth):
fr = r.find('.//front')
if len(fr):
bg = r.find('.//body').getparent()
fr.getparent().remove(fr)
bpm = etree.parse(pth).find('.//book-part-meta')
if bpm is None:
bpm = etree.parse(pth).find('.')
if bpm is not None:
if bpm.getroottree().getroot().tag == 'front':
bg.insert(0, bpm)
else:
self.debug.print_debug(
self, 'front or bookpart metadata unspecified')
sys.exit(1)
else:
bg.insert(0, bpm)
else:
self.debug.print_debug(self, 'front metadata unspecified')
else:
self.debug.print_debug(self, pth +
self.gv.PROJECT_INPUT_FILE_DOES_NOT_EXIST)
sys.exit(1)
return self.tr
def create_metadata_path(self, metadata):
"""
creates the correct folder path for the metadata file. Metadata files should be in a folder : metadata
Parameters
----------
metadata : str
Suffix of the metadata files
Returns
-------
pth : str
Correct path of the metadata file in the folder structure
Notes
-----
We assume that metadata files are stored in a sub-folder named metadata
"""
p = os.path.dirname(self.f).split(os.sep)
f = os.path.basename(self.f)
name, ext = os.path.splitext(f)
file_name = [name, '.', metadata, ext]
if not self.stand_alone or not os.path.exists(os.sep.join(p)):
del p[-4:]
p.append('metadata')
p.append(''.join(file_name))
pth = os.sep.join(p)
return pth
def sort_by_tags(self, tag_list, elem):
"""
Sorts a list of elements alphabetically
Parameters
----------
tag_list : list
A list of tag types
elem : Element
Element to be modified
"""
data = []
for e in elem:
vl = []
for tag in tag_list:
vl.append(e.findtext(".//" + tag))
vl.append(e)
data.append(tuple(vl))
data.sort()
elem[:] = [item[-1] for item in data]
def sort_references(self, tag_list):
"""
Sort references based on the sub-elements list
Parameters
----------
tag_list : list
A list of tag types
Returns
-------
tr : elementTree
Element tree of the current file
See Also
--------
sort_by_tags
"""
elem = self.tr.find('./back/ref-list')
self.sort_by_tags(tag_list, elem)
return self.tr
def sort_footnotes(self, tag_list):
"""
Sort footnotes based on the sub-elements list
Parameters
----------
tag_list : list
A list of tag types
Returns
-------
tr : elementTree
Element tree of the current file
See Also
--------
sort_by_tags
"""
elem = self.tr.find('./back/fn-group')
self.sort_by_tags(tag_list, elem)
return self.tr
def process(self):
"""
Process JATS-XML file and do all transformations into the elementtree
See Also
--------
merge_metadata, set_numbering_tags,set_uuids_for_back_matter,sort_footnotes,sort_references,set_numbering_values
"""
citations_to_references = self.args.get('--citations-to-references')
clean_references = self.args.get('--clean-references')
set_numbering_tags = self.args.get('--set-numbering-tags')
set_unique_ids = self.args.get('--set-uuids')
sort_footnotes = self.args.get('--sort-footnotes')
sort_references = self.args.get('--sort-references')
set_numbering_values = self.args.get('--set-numbering-values')
metadata = self.args.get('--metadata')
self.tr = self.merge_metadata(metadata) if metadata else self.tr
self.tr = self.citations_to_references() if citations_to_references else self.tr
self.tr = self.clean_references() if clean_references else self.tr
self.tr = self.gv.set_numbering_tags(set_numbering_tags.split(
','), self.tr) if set_numbering_tags else self.tr
self.tr = self.set_uuids_for_back_matter(
set_unique_ids.split(',')) if set_unique_ids else self.tr
self.tr = self.sort_footnotes(
sort_footnotes.split(',')) if sort_footnotes else self.tr
self.tr = self.sort_references(
sort_references.split(',')) if sort_references else self.tr
for s in set_numbering_values.split(';'):
vals = s.split(',')
count = 1
range_count = [0, 0]
if len(vals) > 3:
r = vals[3].lstrip('{').rstrip('}').split(':')
range_count = [int(r[0]), int(r[1])]
self.tr, count = self.set_numbering_values(
vals[0], vals[1], vals[2], count, range_count)
self.gv.create_dirs_recursive(self.dr.split('/'))
self.create_xml_file(
os.path.join(
self.dr, os.path.basename(
self.f)))
def get_module_name(self):
"""
Reads the name of the module for debugging and logging
Returns
-------
name string
Name of the Module
"""
name = 'prepare'
return name
def create_xml_file(self, pth):
"""
Write the current elementTree into the file path
Parameters
----------
pth : str
Correct path of the metadata file in the folder structure
Raises
------
IOError
I/O operation fails
Notes
-----
Default configuration writes a normalized XML file with XML scheme
"""
try:
self.tr.write(
pth,
pretty_print=False,
xml_declaration=True
)
print()
except IOError as e:
print(e)
self.debug.print_debug(self, self.XML_FILE_NOT_CREATED)
def run(self):
"""
Runs the configuration on the processing object
See Also
--------
process
"""
self.process()
def main():
"""
Initializes a processing object from a xml file and and runs it.
See Also
--------
run
"""
xp = Prepare()
xp.run()
if __name__ == '__main__':
main()