You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lexonomy_custom_editor/src/export.py

220 lines
8.1 KiB

from browser import document
from model.tags import export_tag
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")
entry_xml = doc.firstChild
# create head
head = entry.original_xml.querySelector("head")
if head is None:
head = doc.createElement("head")
entry.original_xml.appendChild(head)
# entry_xml.appendChild(head.cloneNode(True))
status = _original_xml_query_selector("head status", entry, doc)
status.textContent = entry.status
# head.appendChild(status.cloneNode(True))
# headword = doc.createElement("headword")
# headword_lemma = doc.createElement("lemma")
headword = _original_xml_query_selector("head headword", entry, doc)
headword_lemma = _original_xml_query_selector("head headword lemma", entry, doc)
headword_lemma.textContent = entry.headword
if entry.headword_type is not None:
headword_lemma.setAttribute("type", entry.headword_type)
if entry.headword_audio is not None:
headword_lemma.setAttribute("audio", entry.headword_audio)
# headword.appendChild(headword_lemma.cloneNode(True))
# head.appendChild(headword.cloneNode(True))
homonymy = _original_xml_query_selector("head headword homonymy", entry, doc)
original_homonymy = entry.original_xml.querySelectorAll("head headword homonymy homonymyFeature")
# todo: How do you know which one is changed/created anew etc - Perhaps go with position in array and save it in homonymy object or something?
for hFeature in entry.homonymy:
feature = doc.createElement("homonymyFeature")
if hFeature.id >= 0:
feature = original_homonymy[hFeature.id]
else:
homonymy.appendChild(feature)
feature.textContent = hFeature.value
# Can't use hFeature.name, because Python has name reserved and so it becomes py_name in JS
feature.setAttribute("name", hFeature["name"])
console.log(homonymy)
# console.log(entry.original_homonymy.querySelector("head headwo")
# if({}) works uncorrectly in transcrypt
if len(entry.lexical_unit) > 0:
lexunit = _original_xml_query_selector("head lexicalUnit", entry, doc)
# lexunit = doc.createElement("lexicalUnit")
lexunit.setAttribute("id", entry.lexical_unit["id"])
lexunit.setAttribute("type", "single")
lexeme = _original_xml_query_selector("head lexicalUnit lexeme", entry, doc)
# lexeme = doc.createElement("lexeme")
lexeme.setAttribute("lexical_unit_lexeme_id", entry.lexical_unit["id"])
lexeme.textContent = entry.lexical_unit["text"]
# lexunit.appendChild(lexeme.cloneNode(True))
# head.appendChild(lexunit.cloneNode(True))
# Example of keeping original xml and adding changes to it only
grammar = _original_xml_query_selector("head grammar", entry, doc)
grammar_category = _original_xml_query_selector("head grammar category", entry, doc)
grammar_category.textContent = entry.grammar
# head.appendChild(grammar.cloneNode(True))
console.log(entry.original_xml)
if len(entry.measure) > 0:
measure_list = _original_xml_query_selector("head measureList", entry, doc)
measure = _original_xml_query_selector("head measureList measure", entry, doc)
# 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"]
# head.appendChild(measure_list)
# Same problem as homonymy
variants = doc.createElement("variantList")
head.appendChild(variants)
for v in entry.variants:
variant = doc.createElement("variant")
variant.textContent = v
variants.appendChild(variant)
# relist = _original_xml_query_selector("head relatedEntryList", entry, doc)
relist = doc.createElement("relatedEntryList")
head.appendChild(relist.cloneNode(True))
for re in entry.related_entries:
relateEntry = doc.createElement("relatedEntry")
relateEntry.textContent = re
relist.appendChild(relateEntry)
head.appendChild(_export_label_list(doc, entry.labels))
# comment = doc.createElement("comment")
comment = _original_xml_query_selector("head comment", entry, doc)
comment.textContent = entry.comment
# head.appendChild(comment.cloneNode(True))
# now lets do body
body = entry.original_xml.querySelector("body")
if body is None:
body = doc.createElement("body")
entry.original_xml.appendChild(body)
# entry_xml.appendChild(body)
sense_list = doc.createElement("senseList")
for sense in entry.senses:
sense_list.appendChild(export_sense(doc, sense))
return entry.original_xml
def export_sense(doc, sense):
sense_xml = doc.createElement("sense")
sense_xml.appendChild(_export_label_list(doc, sense.labels))
definition_list = doc.createElement("definitionList")
sense_xml.appendChild(definition_list)
for typ, text in sense.definition.items():
definition = doc.createElement("definition")
definition.textContent = text
definition.setAttribute("type", typ)
definition_list.appendChild(definition)
translation_container_list = doc.createElement("translationContainerList")
export_translation_list(doc, sense, translation_container_list)
sense_xml.appendChild(translation_container_list)
example_container_list = doc.createElement("exampleContainerList")
sense_xml.appendChild(example_container_list)
for example in sense.examples:
example_container = example.export(doc)
translation_container_list = doc.createElement("translationContainerList")
export_translation_list(doc, example, translation_container_list)
example_container.appendChild(translation_container_list)
example_container_list.appendChild(example_container)
return sense_xml
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)
def export_translation(doc, translation):
translation_xml = doc.createElement("translationContainer")
translation_xml.appendChild(_export_label_list(doc, translation.tags))
actual_t = doc.createElement("translation")
actual_t.textContent = translation.translation
actual_t.setAttribute("targetLang", translation.targetLang)
if translation.audio:
actual_t.setAttribute("audio", translation.audio)
if translation.source:
actual_t.setAttribute("source", translation.source)
translation_xml.appendChild(actual_t)
if len(translation.explanationList) > 0 :
explanationList = _export_explanation_list(doc, translation.explanationList)
translation_xml.appendChild(explanationList)
return translation_xml
def _export_explanation_list(doc, lst):
result = doc.createElement('explanationList')
for explanation in lst:
result.appendChild(explanation.export(doc))
return result
def _export_label_list(doc, lst):
result = doc.createElement("labelList")
for key, value in lst:
key, value = export_tag(key, value)
label_el = doc.createElement("label")
label_el.textContent = value
label_el.setAttribute('type', key)
result.appendChild(label_el)
return result
def _original_xml_query_selector(selector, entry, doc, parent_selector = selector.rsplit(' ', 1)[0]):
query = entry.original_xml.querySelector(selector)
if query is None:
query = doc.createElement(selector.rsplit(' ', 1)[1])
entry.original_xml.querySelector(parent_selector).appendChild(query)
return query