Repository navigation
Expand file tree
/
Copy pathBuildView.py
More file actions
249 lines (193 loc) · 9.78 KB
/
Copy pathBuildView.py
File metadata and controls
249 lines (193 loc) · 9.78 KB
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
# @author: Michael Wagner
# @organization: TU Dresden
# @contact: michael.wagner@tu-dresden.de
import xml.etree.ElementTree as ElementTree
import re
et = ElementTree
def create_view(catalog_file, outfiles):
# creation of a view for import in RDMO
tree, ns = parse_and_get_ns(catalog_file)
# tree = et.parse(catalog_file)
root = tree.getroot()
for lang in outfiles:
# create a catalog for each language
outfile = outfiles[lang]
fid = open(outfile, "w")
fid.write('<?xml version="1.0" encoding="UTF-8"?>\n')
fid.write('<rdmo')
# write namespaces
for ns_act in ns:
fid.write(' xmlns:' + ns_act + '="' + ns[ns_act] + '"')
fid.write('>\n')
uri_prefix = None
dataset_collections_all = {}
dataset_collections_shortnames = {}
questionset_is_collection = None
question_is_collection = None
attribute_value = None
template = str()
for child in root:
if child.tag == "catalog":
# get catalog element
catalog = child.attrib['{' + ns['dc'] + '}uri']
uri_prefix = child.find("uri_prefix").text
if uri_prefix[-1] == "/":
# remove trailing slash
uri_prefix = uri_prefix[0:-1]
key = child.find("key").text
try:
comment = child.find("comment").text
except AttributeError:
comment = None
title_dict = get_element_dict(child.findall("title"), lang)
try:
help_dict = get_element_dict(child.findall("help"), lang)
except AttributeError:
help_dict = None
fid.write(' <view dc:uri="' + uri_prefix + '/views/' + key + '">\n')
fid.write(' <uri_prefix>' + uri_prefix + '</uri_prefix>\n')
fid.write(' <key>' + key + '</key>\n')
if comment is not None:
fid.write(' <dc:comment>' + comment + '</comment>\n')
else:
fid.write(' <dc:comment />\n')
for title in title_dict:
fid.write(' <title lang="' + title + '">' + title_dict[title] + '</title>\n')
for help_act in help_dict:
fid.write(' <help lang="' + help_act + '">' + help_dict[help_act] + '</help>\n')
fid.write(' <catalogs>\n')
fid.write(' <catalog dc:uri="' + catalog + '"/>\n')
fid.write(' </catalogs>\n')
fid.write(' <template>\n')
template = template + "{% load view_tags %}\n\n"
template = template + ' <h1>' + title_dict[lang] + '</h1>\n'
elif child.tag == "section":
# get section element
title_dict = get_element_dict(child.findall("title"), lang)
template = template + "\n"
template = template + ' <h2>' + title_dict[lang] + '</h2>\n'
elif child.tag == "questionset":
# get questionset element
title_dict = get_element_dict(child.findall("title"), lang)
help_dict = get_element_dict(child.findall("help"), lang)
template = template + "\n"
template = template + ' <h3>' + title_dict[lang] + '</h3>\n'
if help_dict[lang] is not None:
template = template + ' <i>' + help_dict[lang] + '</i>\n'
if child.find("is_collection").text.lower() == 'true':
questionset_is_collection = True
attribute = child.find("attribute").attrib
attribute_value = attribute['{' + ns['dc'] + '}uri']
if attribute_value not in dataset_collections_all:
attribute_shortname, attribute_shortnames, attribute_postfix = \
get_attribute_shortnames(attribute_value, uri_prefix)
template = template + "\n"
template = template + "{% get_set '" + attribute_postfix + "' as " + attribute_shortnames + ' %}\n'
dataset_collections_all[attribute_value] = attribute_shortnames
dataset_collections_shortnames[attribute_value] = attribute_shortname
else:
questionset_is_collection = False
if child.tag == "question":
# get question element
if child.find("is_collection").text.lower() == 'true':
question_is_collection = True
else:
question_is_collection = False
question_attribute = child.find("attribute").attrib
question_attribute_value = question_attribute['{' + ns['dc'] + '}uri']
question_attribute_postfix = get_attribute_shortnames(question_attribute_value, uri_prefix)[2]
text_dict = get_element_dict(child.findall("text"), lang)
help_dict = get_element_dict(child.findall("help"), lang)
template = template + "\n"
template = template + '<p>\n'
template = template + ' <br>\n'
template = template + ' <b>' + text_dict[lang] + '</b>\n'
if help_dict[lang] is not None:
template = template + ' <i>' + help_dict[lang] + '</i>\n'
template = template + '</p>\n'
if questionset_is_collection:
template = template + '{% for ' + dataset_collections_shortnames[attribute_value] + ' in ' + \
dataset_collections_all[attribute_value] + ' %}\n'
template = template + '<p>\n'
template = template + ' <i>' + dataset_collections_shortnames[attribute_value] + ' {{ ' + \
dataset_collections_shortnames[attribute_value] + '.value }}:</i>\n'
if question_is_collection:
template = template + ' {% render_set_value_list ' + dataset_collections_shortnames[
attribute_value] + " '" + question_attribute_postfix + "' %}\n"
else:
template = template + ' {% render_set_value ' + dataset_collections_shortnames[
attribute_value] + " '" + question_attribute_postfix + "' %}\n"
template = template + '</p>\n'
template = template + '{% endfor %}\n'
else:
template = template + '<p>\n'
if question_is_collection:
template = template + " {% render_value_list '" + question_attribute_postfix + "' %}\n"
else:
template = template + " {% render_value '" + question_attribute_postfix + "' %}\n"
template = template + '</p>\n'
# change template content to escaped xml content
fid.write(escape_xml_characters(template))
# fid.write(template)
fid.write(' </template>\n')
fid.write(' </view>\n')
fid.write('</rdmo>\n')
fid.close()
def get_element_dict(elements, lang):
# produce a dictionary with language abbreviation and content
element_dict = {}
for element in elements:
if element.attrib["lang"] in lang:
element_dict[element.attrib["lang"]] = element.text
return element_dict
def get_attribute_shortnames(attribute_value, uri_prefix):
# get short names for attributes
attribute_prefix = uri_prefix + "/domain/"
attribute_postfix = attribute_value.replace(attribute_prefix, "")
attribute_last = re.split('/', attribute_postfix)
attribute_shortname = attribute_last[len(attribute_last) - 1]
attribute_shortnames = attribute_last[len(attribute_last) - 1] + '_all'
return attribute_shortname, attribute_shortnames, attribute_postfix
def parse_and_get_ns(file):
# parse a xml file and get a dict of namespaces
# taken from https://stackoverflow.com/questions/1953761/accessing-xmlns-attribute-with-python-elementree
events = "start", "start-ns"
root = None
ns = {}
for event, elem in et.iterparse(file, events):
if event == "start-ns":
if elem[0] in ns and ns[elem[0]] != elem[1]:
# NOTE: It is perfectly valid to have the same prefix refer
# to different URI namespaces in different parts of the
# document. This exception serves as a reminder that this
# solution is not robust. Use at your own peril.
raise KeyError("Duplicate prefix with different URI found.")
ns[elem[0]] = "%s" % elem[1]
elif event == "start":
if root is None:
root = elem
return et.ElementTree(root), ns
def escape_xml_characters(in_str):
# replace xml control characters from string
control_char = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
}
out_str = in_str
for i in control_char:
out_str = out_str.replace(i, control_char[i])
return out_str
def control_create_view(infile, **outfiles):
create_view(infile, outfiles)
def main():
# infile = "infiles/example1_questions.xml"
infile = "infiles/qa_questions.xml"
# define languages of interest -> create view for each language
control_create_view(infile, en="outfiles/qa_questions_view_template_en.xml")
# control_create_view(infile, en="outfiles/example1_view_template_en.xml",
# de="outfiles/example1_view_template_de.xml")
if __name__ == '__main__':
main()