forked from IATI/IATI-Codelists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translations_csv_to_xml.py
130 lines (114 loc) · 5.08 KB
/
translations_csv_to_xml.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
"""Script to turn codelist translations from the csv files into xml."""
from lxml import etree
import os
import io
import csv
# Output directories
OUTPUTDIR = ['', '']
# Path to the folder containing the csv files with translations
PATH_TO_CSV = ''
# Paths to the folders containing the embedded and nonembedded xml to be modified,
# ideally it should be the output folder for clv3 after running gen.sh
PATH_TO_XML = ["xml/", "IATI-Codelists-NonEmbedded/xml/"]
# ISO 639 language code in lowercase
LANG = ''
def indent(elem, level=0, shift=2):
"""# Adapted from code at http://effbot.org/zone/element-lib.htm."""
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
def get_xml_list(xml_path):
"""Get a list of xml files."""
for a, b, files in os.walk(xml_path):
return files
def write_narrative(xml, element, lang_string):
"""Write a new tag into the xml and adds the xml:lang attribute with the translated."""
new_narrative = etree.Element("narrative")
new_narrative.text = lang_string
new_narrative.attrib['{http://www.w3.org/XML/1998/namespace}lang'] = LANG
element.append(new_narrative)
def get_codelist_item(code, xml):
"""Match the codelist-item within the xml to the code from the row in the csv."""
for codelist_item in xml.findall('codelist-item'):
if (code == codelist_item.find('code').text):
return codelist_item
def is_translated(element, field):
"""Ensure that the element doesn't already have a translation."""
for narrative in element.findall('narrative'):
try:
if narrative.attrib['{http://www.w3.org/XML/1998/namespace}lang'] == LANG:
# if there's already a french translation, rewrite it with the new one
narrative.text = field
return False
except (KeyError):
pass
return True
def write_row(code, xml, name, description=''):
"""Write the contents of the csv row into the xml."""
codelist_item = get_codelist_item(code, xml)
if description != '':
if is_translated(codelist_item.find('name'), name):
write_narrative(xml, codelist_item.find('name'), name)
if is_translated(codelist_item.find('description'), description):
write_narrative(xml, codelist_item.find('description'), description)
return 2
else:
if is_translated(codelist_item.find('name'), name):
write_narrative(xml, codelist_item.find('name'), name)
return 1
embedded_list = get_xml_list(PATH_TO_XML[0])
nonembedded_list = get_xml_list(PATH_TO_XML[1])
parser = etree.XMLParser(remove_blank_text=True)
for a, b, codelists in os.walk(PATH_TO_CSV):
# Go through the csv filenames
if not codelists:
print("No CSV files were found")
codelist_count = 0
field_count = 0
for codelist_csv in codelists:
codelist_name = os.path.splitext(codelist_csv)[0]
if "{}.xml".format(codelist_name) in embedded_list:
xml_path = PATH_TO_XML[0]
output = OUTPUTDIR[0]
elif "{}.xml".format(codelist_name) in nonembedded_list:
xml_path = PATH_TO_XML[1]
output = OUTPUTDIR[1]
else:
print("{} codelist XML file not found".format(codelist_name))
continue
try:
with io.open(os.path.join(PATH_TO_CSV, codelist_csv), 'r', encoding="utf-8") as csv_file:
codelist_xml = etree.parse('{}{}.xml'.format(xml_path, codelist_name), parser)
reader = csv.DictReader(csv_file)
if "description ({})".format(LANG.upper()) in reader.fieldnames:
for row in reader:
field_count += write_row(
row['code'],
codelist_xml.find('codelist-items'),
row['name ({})'.format(LANG.upper())],
row['description ({})'.format(LANG.upper())]
)
else:
for row in reader:
field_count += write_row(
row['code'],
codelist_xml.find('codelist-items'),
row['name ({})'.format(LANG.upper())]
)
# Output the xml as new files matching the OUTPUTDIR
indent(codelist_xml.getroot(), 0, 4)
codelist_xml.write(os.path.join(output, '{}.xml'.format(codelist_name)), encoding='utf-8')
codelist_count += 1
except (OSError, IOError):
print("{} codelist XML file not found".format(codelist_name))
print("Translated {} fields in {}/{} codelists".format(field_count, codelist_count, len(codelists)))