forked from IATI/IATI-Codelists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2tov3.py
64 lines (49 loc) · 1.77 KB
/
v2tov3.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
from lxml import etree as ET
import sys
"""
Example usage:
cp -r xml xml.old
cd xml.old
for f in *; do python ../v2tov3.py $f > ../xml/$f; done
"""
def update_to_narrative(parent, element_name):
elements = parent.findall(element_name)
if elements:
new_element = ET.Element(element_name)
parent.append(new_element)
for element in elements:
parent.remove(element)
element.tag = 'narrative'
new_element.append(element)
parser = ET.XMLParser(remove_blank_text=True)
tree = ET.parse(sys.argv[1], parser)
metadata = tree.find('metadata')
update_to_narrative(metadata, 'name')
update_to_narrative(metadata, 'description')
update_to_narrative(metadata, 'category')
# Ensure that url is the last element
url = metadata.find('url')
if url is not None:
metadata.remove(url)
metadata.append(url)
for codelist_item in tree.find('codelist-items').findall('codelist-item'):
update_to_narrative(codelist_item, 'name')
update_to_narrative(codelist_item, 'description')
codelist_item[:] = sum((codelist_item.findall(x) for x in ['code', 'name', 'description', 'category', 'url']), [])
# Adapted from code at http://effbot.org/zone/element-lib.htm
def indent(elem, level=0, shift=2):
i = "\n" + level * " " * shift
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " " * shift
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level + 1, shift)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
indent(tree.getroot(), 0, 4)
print(ET.tostring(tree).decode())