2019-11-11 22:04:45 +00:00
|
|
|
from browser import document
|
2020-01-05 09:38:53 +00:00
|
|
|
from model.tags import export_tag
|
2019-11-11 22:04:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def export_to_xml(model):
|
|
|
|
xml_document = export_entry(model.entry)
|
|
|
|
serializer = __new__(XMLSerializer())
|
|
|
|
return serializer.serializeToString(xml_document);
|
|
|
|
|
|
|
|
|
|
|
|
def export_entry(entry):
|
|
|
|
parser = __new__(DOMParser())
|
|
|
|
doc = parser.parseFromString("<entry />", "text/xml")
|
2019-11-20 17:58:48 +00:00
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
entry_xml = doc.firstChild
|
2019-11-20 17:58:48 +00:00
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
# create head
|
|
|
|
head = doc.createElement("head")
|
|
|
|
entry_xml.appendChild(head)
|
2019-11-20 17:58:48 +00:00
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
status = doc.createElement("status")
|
|
|
|
status.textContent = entry.status
|
|
|
|
head.appendChild(status)
|
|
|
|
|
|
|
|
headword = doc.createElement("headword")
|
|
|
|
headword_lemma = doc.createElement("lemma")
|
|
|
|
headword_lemma.textContent = entry.headword
|
|
|
|
headword.appendChild(headword_lemma)
|
|
|
|
head.appendChild(headword)
|
|
|
|
|
2019-12-27 17:57:22 +00:00
|
|
|
# if({}) works uncorrectly in transcrypt
|
|
|
|
if len(entry.lexical_unit) > 0:
|
|
|
|
lexunit = doc.createElement("lexicalUnit")
|
|
|
|
lexunit.setAttribute("id", entry.lexical_unit["id"])
|
|
|
|
lexunit.setAttribute("type", "single")
|
|
|
|
lexeme = doc.createElement("lexeme")
|
|
|
|
lexeme.setAttribute("lexical_unit_lexeme_id", entry.lexical_unit["id"])
|
|
|
|
lexeme.textContent = entry.lexical_unit["text"]
|
|
|
|
|
|
|
|
lexunit.appendChild(lexeme)
|
|
|
|
head.appendChild(lexunit)
|
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
grammar = doc.createElement("grammar")
|
|
|
|
grammar_category = doc.createElement("category")
|
|
|
|
grammar_category.textContent = entry.grammar
|
|
|
|
grammar.appendChild(grammar_category)
|
|
|
|
head.appendChild(grammar)
|
|
|
|
|
2019-12-27 17:57:22 +00:00
|
|
|
if len(entry.measure) > 0:
|
|
|
|
measure_list = doc.createElement("measureList")
|
|
|
|
measure = doc.createElement("measure")
|
|
|
|
measure.setAttribute("source", entry.measure["source"])
|
|
|
|
measure.setAttribute("type", entry.measure["type"])
|
|
|
|
measure.textContent = entry.measure["text"]
|
|
|
|
|
|
|
|
measure_list.appendChild(measure)
|
|
|
|
head.appendChild(measure_list)
|
2020-01-24 00:49:33 +00:00
|
|
|
|
2019-11-20 17:58:48 +00:00
|
|
|
variants = doc.createElement("variantList")
|
|
|
|
head.appendChild(variants)
|
|
|
|
|
|
|
|
for v in entry.variants:
|
|
|
|
variant = doc.createElement("variant")
|
|
|
|
variant.textContent = v
|
|
|
|
variants.appendChild(variant)
|
2020-01-21 21:18:43 +00:00
|
|
|
|
|
|
|
relist = doc.createElement("relatedEntryList")
|
|
|
|
head.appendChild(relist)
|
|
|
|
|
|
|
|
for re in entry.related_entries:
|
|
|
|
relateEntry = doc.createElement("relatedEntry")
|
|
|
|
relateEntry.textContent = re
|
|
|
|
relist.appendChild(relateEntry)
|
2019-11-20 18:09:12 +00:00
|
|
|
|
|
|
|
head.appendChild(_export_label_list(doc, entry.labels))
|
|
|
|
|
|
|
|
comment = doc.createElement("comment")
|
|
|
|
comment.textContent = entry.comment
|
|
|
|
head.appendChild(comment)
|
2019-11-20 17:58:48 +00:00
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
# now lets do body
|
|
|
|
body = doc.createElement("body")
|
|
|
|
entry_xml.appendChild(body)
|
|
|
|
|
2019-11-13 22:16:28 +00:00
|
|
|
sense_list = doc.createElement("senseList")
|
2019-11-11 22:04:45 +00:00
|
|
|
body.appendChild(sense_list)
|
|
|
|
|
|
|
|
for sense in entry.senses:
|
|
|
|
sense_list.appendChild(export_sense(doc, sense))
|
|
|
|
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def export_sense(doc, sense):
|
|
|
|
sense_xml = doc.createElement("sense")
|
2019-11-20 17:58:48 +00:00
|
|
|
sense_xml.appendChild(_export_label_list(doc, sense.labels))
|
2019-11-11 22:04:45 +00:00
|
|
|
|
|
|
|
definition_list = doc.createElement("definitionList")
|
|
|
|
sense_xml.appendChild(definition_list)
|
|
|
|
|
2020-01-14 19:59:15 +00:00
|
|
|
for typ, text in sense.definition.items():
|
|
|
|
definition = doc.createElement("definition")
|
|
|
|
definition.textContent = text
|
|
|
|
definition.setAttribute("type", typ)
|
|
|
|
definition_list.appendChild(definition)
|
2019-11-11 22:04:45 +00:00
|
|
|
|
|
|
|
translation_container_list = doc.createElement("translationContainerList")
|
2020-01-23 21:51:15 +00:00
|
|
|
export_translation_list(doc, sense, translation_container_list)
|
2019-11-11 22:04:45 +00:00
|
|
|
sense_xml.appendChild(translation_container_list)
|
|
|
|
|
2019-11-13 22:16:28 +00:00
|
|
|
example_container_list = doc.createElement("exampleContainerList")
|
|
|
|
sense_xml.appendChild(example_container_list)
|
|
|
|
|
|
|
|
for example in sense.examples:
|
2020-01-24 00:49:33 +00:00
|
|
|
example_container = example.export(doc)
|
2020-01-23 21:51:15 +00:00
|
|
|
export_translation_list(doc, example, example_container)
|
2020-01-24 00:49:33 +00:00
|
|
|
example_container_list.appendChild(example_container)
|
2019-11-13 22:16:28 +00:00
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
return sense_xml
|
|
|
|
|
2020-01-23 21:51:15 +00:00
|
|
|
def export_translation_list(doc, py_parent, xml_parent):
|
|
|
|
for cidx, cluster in enumerate(py_parent.translations):
|
|
|
|
for translation in cluster:
|
|
|
|
translation_container = export_translation(doc, translation)
|
|
|
|
translation_container.setAttribute("cluster", str(cidx + 1))
|
|
|
|
xml_parent.appendChild(translation_container)
|
|
|
|
|
|
|
|
|
2019-11-11 22:04:45 +00:00
|
|
|
def export_translation(doc, translation):
|
|
|
|
translation_xml = doc.createElement("translationContainer")
|
|
|
|
|
|
|
|
actual_t = doc.createElement("translation")
|
|
|
|
actual_t.textContent = translation.translation
|
2020-01-04 13:35:53 +00:00
|
|
|
actual_t.setAttribute("targetLang", translation.targetLang)
|
|
|
|
|
2019-11-20 17:58:48 +00:00
|
|
|
if translation.source:
|
|
|
|
actual_t.setAttribute("source", translation.source)
|
2019-11-11 22:04:45 +00:00
|
|
|
translation_xml.appendChild(actual_t)
|
|
|
|
|
2019-11-15 21:22:03 +00:00
|
|
|
explanation = doc.createElement("explanation")
|
|
|
|
explanation.textContent = translation.explanation
|
|
|
|
translation_xml.appendChild(explanation)
|
|
|
|
|
2019-11-20 17:58:48 +00:00
|
|
|
translation_xml.appendChild(_export_label_list(doc, translation.tags))
|
2019-11-11 22:04:45 +00:00
|
|
|
return translation_xml
|
|
|
|
|
2019-11-20 17:58:48 +00:00
|
|
|
|
|
|
|
def _export_label_list(doc, lst):
|
|
|
|
result = doc.createElement("labelList")
|
|
|
|
for key, value in lst:
|
2020-01-05 09:38:53 +00:00
|
|
|
key, value = export_tag(key, value)
|
|
|
|
|
2019-11-20 17:58:48 +00:00
|
|
|
label_el = doc.createElement("label")
|
|
|
|
label_el.textContent = value
|
|
|
|
label_el.setAttribute('type', key)
|
|
|
|
result.appendChild(label_el)
|
|
|
|
return result
|