don't delete unsupported schema on homonymy and string data in head

This commit is contained in:
matic_t 2020-08-24 09:03:14 -07:00
parent d513ec8eaa
commit 36cce3a86e
7 changed files with 91 additions and 49 deletions

View File

@ -7,79 +7,92 @@ def export_to_xml(model):
serializer = __new__(XMLSerializer()) serializer = __new__(XMLSerializer())
return serializer.serializeToString(xml_document) return serializer.serializeToString(xml_document)
def export_entry(entry): def export_entry(entry):
parser = __new__(DOMParser()) parser = __new__(DOMParser())
doc = parser.parseFromString("<entry />", "text/xml") doc = parser.parseFromString("<entry />", "text/xml")
entry_xml = doc.firstChild entry_xml = doc.firstChild
# create head # create head
head = entry.original_xml.querySelector("head")
if head is None:
head = doc.createElement("head") head = doc.createElement("head")
entry_xml.appendChild(head) entry.original_xml.appendChild(head)
# entry_xml.appendChild(head.cloneNode(True))
status = doc.createElement("status") status = _original_xml_query_selector("head status", entry, doc)
status.textContent = entry.status status.textContent = entry.status
head.appendChild(status) # head.appendChild(status.cloneNode(True))
headword = doc.createElement("headword")
headword_lemma = doc.createElement("lemma")
# headword_lemma = entry.original_xml.querySelector("head headword lemma")
# 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 headword_lemma.textContent = entry.headword
if entry.headword_type is not None: if entry.headword_type is not None:
headword_lemma.setAttribute("type", entry.headword_type) headword_lemma.setAttribute("type", entry.headword_type)
if entry.headword_audio is not None: if entry.headword_audio is not None:
headword_lemma.setAttribute("audio", entry.headword_audio) headword_lemma.setAttribute("audio", entry.headword_audio)
headword.appendChild(headword_lemma)
head.appendChild(headword)
homonymy = doc.createElement("homonymy") # headword.appendChild(headword_lemma.cloneNode(True))
headword.appendChild(homonymy) # 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: for hFeature in entry.homonymy:
feature = doc.createElement("homonymyFeature") feature = doc.createElement("homonymyFeature")
feature.textContent = hFeature.value if hFeature.id >= 0:
# Can't use hFeature.name, because Python has name reserver and so it becomes py_name in JS feature = original_homonymy[hFeature.id]
feature.setAttribute("name", hFeature["name"]) else:
homonymy.appendChild(feature) 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({}) works uncorrectly in transcrypt
if len(entry.lexical_unit) > 0: if len(entry.lexical_unit) > 0:
lexunit = doc.createElement("lexicalUnit") lexunit = _original_xml_query_selector("head lexicalUnit", entry, doc)
# lexunit = doc.createElement("lexicalUnit")
lexunit.setAttribute("id", entry.lexical_unit["id"]) lexunit.setAttribute("id", entry.lexical_unit["id"])
lexunit.setAttribute("type", "single") lexunit.setAttribute("type", "single")
lexeme = doc.createElement("lexeme") 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.setAttribute("lexical_unit_lexeme_id", entry.lexical_unit["id"])
lexeme.textContent = entry.lexical_unit["text"] lexeme.textContent = entry.lexical_unit["text"]
lexunit.appendChild(lexeme) # lexunit.appendChild(lexeme.cloneNode(True))
head.appendChild(lexunit) # head.appendChild(lexunit.cloneNode(True))
# Example of keeping original xml and adding changes to it only # Example of keeping original xml and adding changes to it only
grammar_category = entry.original_xml.querySelector("head grammar category") grammar = _original_xml_query_selector("head grammar", entry, doc)
if grammar_category is None: grammar_category = _original_xml_query_selector("head grammar category", entry, doc)
grammar = doc.createElement("grammar")
grammar_category = doc.createElement("category")
grammar.appendChild(grammar_category)
entry.original_xml.querySelector("head").appendChild(grammar_category)
grammar_category.textContent = entry.grammar grammar_category.textContent = entry.grammar
head.appendChild(entry.original_xml.querySelector("head grammar")) # head.appendChild(grammar.cloneNode(True))
console.log(entry.original_xml)
if len(entry.measure) > 0: if len(entry.measure) > 0:
measure_list = doc.createElement("measureList") measure_list = _original_xml_query_selector("head measureList", entry, doc)
measure = doc.createElement("measure") 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("source", entry.measure["source"])
measure.setAttribute("type", entry.measure["type"]) measure.setAttribute("type", entry.measure["type"])
measure.textContent = entry.measure["text"] measure.textContent = entry.measure["text"]
measure_list.appendChild(measure) # head.appendChild(measure_list)
head.appendChild(measure_list)
# Same problem as homonymy
variants = doc.createElement("variantList") variants = doc.createElement("variantList")
head.appendChild(variants) head.appendChild(variants)
@ -88,8 +101,9 @@ def export_entry(entry):
variant.textContent = v variant.textContent = v
variants.appendChild(variant) variants.appendChild(variant)
# relist = _original_xml_query_selector("head relatedEntryList", entry, doc)
relist = doc.createElement("relatedEntryList") relist = doc.createElement("relatedEntryList")
head.appendChild(relist) head.appendChild(relist.cloneNode(True))
for re in entry.related_entries: for re in entry.related_entries:
relateEntry = doc.createElement("relatedEntry") relateEntry = doc.createElement("relatedEntry")
@ -98,21 +112,25 @@ def export_entry(entry):
head.appendChild(_export_label_list(doc, entry.labels)) head.appendChild(_export_label_list(doc, entry.labels))
comment = doc.createElement("comment") # comment = doc.createElement("comment")
comment = _original_xml_query_selector("head comment", entry, doc)
comment.textContent = entry.comment comment.textContent = entry.comment
head.appendChild(comment) # head.appendChild(comment.cloneNode(True))
# now lets do body # now lets do body
body = entry.original_xml.querySelector("body")
if body is None:
body = doc.createElement("body") body = doc.createElement("body")
entry_xml.appendChild(body) entry.original_xml.appendChild(body)
# entry_xml.appendChild(body)
sense_list = doc.createElement("senseList") sense_list = doc.createElement("senseList")
body.appendChild(sense_list)
for sense in entry.senses: for sense in entry.senses:
sense_list.appendChild(export_sense(doc, sense)) sense_list.appendChild(export_sense(doc, sense))
return doc return entry.original_xml
def export_sense(doc, sense): def export_sense(doc, sense):
@ -192,3 +210,10 @@ def _export_label_list(doc, lst):
label_el.setAttribute('type', key) label_el.setAttribute('type', key)
result.appendChild(label_el) result.appendChild(label_el)
return result 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

View File

@ -13,16 +13,19 @@ def generic_list_getter():
def double_list_getter(firstParameter, secondParameter, allowEmptyField = False): def double_list_getter(firstParameter, secondParameter, allowEmptyField = False):
result = [] result = []
for row in document.getElementsByClassName("double-list-row"): for row in document.getElementsByClassName("double-list-row"):
firstValue = row.querySelector("." + firstParameter + "-input").value output = {}
secondValue = row.querySelector("." + secondParameter + "-input").value output[firstParameter] = row.querySelector("." + firstParameter + "-input").value
output[secondParameter] = row.querySelector("." + secondParameter + "-input").value
if row.hasAttribute("data-id"):
output.id = int(row.getAttribute("data-id"))
if (allowEmptyField is False and '' in [firstValue, secondValue]): if (allowEmptyField is False and '' in [output[firstParameter], output[secondParameter]]):
continue continue
if (allowEmptyField is True and all('' == value or value.isspace() for value in [firstValue, secondValue])): if (allowEmptyField is True and all('' == value or value.isspace() for value in [output[firstParameter], output[secondParameter]])):
continue continue
result.append({firstParameter: firstValue, secondParameter: secondValue}) result.append(output)
return result return result

View File

@ -21,7 +21,9 @@ class DeleteRelatedEntries(DeleteVariants):
class DeleteHomonymy(NoReset): class DeleteHomonymy(NoReset):
def update_model(self, model): def update_model(self, model):
for el in document.getElementsByClassName("list-adder-input"): for el in document.getElementsByClassName("name-input"):
el.value = ""
for el in document.getElementsByClassName("value-input"):
el.value = "" el.value = ""

View File

@ -91,6 +91,16 @@ class EditVariants(Message):
class EditHomonymy(Message): class EditHomonymy(Message):
def update_model(self, model): def update_model(self, model):
homonymy = common_accessors.double_list_getter("value", "name") homonymy = common_accessors.double_list_getter("value", "name")
keptHFeatureIds = [feature.id for feature in homonymy]
for index, feature in enumerate(model.entry.original_xml.querySelectorAll("head headword homonymy homonymyFeature")):
if (index not in keptHFeatureIds):
feature.remove()
for index, feature in enumerate(homonymy):
if (feature.id >= 0):
feature.id = index
model.entry.homonymy = homonymy model.entry.homonymy = homonymy

View File

@ -40,7 +40,7 @@ class Entry(Data):
self.grammar = grammar.textContent if grammar else "" self.grammar = grammar.textContent if grammar else ""
self.comment = comment.textContent if comment else "" self.comment = comment.textContent if comment else ""
self.variants = [v.textContent for v in entry_xml.querySelectorAll("head variantList variant")] self.variants = [v.textContent for v in entry_xml.querySelectorAll("head variantList variant")]
self.homonymy = [{"value": v.textContent, "name": v.getAttribute("name")} for v in entry_xml.querySelectorAll("head headword homonymy homonymyFeature ")] self.homonymy = [{"value": v.textContent, "name": v.getAttribute("name"), "id": index} for index, v in enumerate(entry_xml.querySelectorAll("head headword homonymy homonymyFeature "))]
self.related_entries = [re.textContent for re in entry_xml.querySelectorAll("head relatedEntryList relatedEntry")] self.related_entries = [re.textContent for re in entry_xml.querySelectorAll("head relatedEntryList relatedEntry")]
lex_unit = entry_xml.querySelector("lexical_unit lexeme,lexicalUnit lexeme") lex_unit = entry_xml.querySelector("lexical_unit lexeme,lexicalUnit lexeme")

View File

@ -48,9 +48,10 @@ def generic_list_editor(title, element_list_getter):
return content return content
def homonymy_editor(title, current_labels): def homonymy_editor(title, current_labels):
def split_line2(left, right): def split_line2(left, right, labelId):
cls = "flex.two{}".format(".double-list-row") cls = "flex.two{}".format(".double-list-row")
return h("div.{}".format(cls), {}, [ isId = {"attrs": {"data-id": labelId}} if labelId >= 0 else {}
return h("div.{}".format(cls), isId, [
h("div.half", {}, left), h("div.half", {}, right)]) h("div.half", {}, left), h("div.half", {}, right)])
content = [h("p", {}, title)] content = [h("p", {}, title)]
@ -58,11 +59,11 @@ def homonymy_editor(title, current_labels):
name = [] name = []
value = [] value = []
name.append(h("label", {"attrs": {"for": i}}, "Name:")) name.append(h("label", {"attrs": {"for": i}}, "Name:"))
name.append(h("input.name-input", {"props": {"type": "text", "value": feature["name"], "id": i}}, "")) name.append(h("input.name-input", {"props": {"type": "text", "value": feature["name"], "id": i, "data-id": feature.id}}, ""))
value.append(h("label", {"attrs": {"for": i + "-value"}}, "Value:")) value.append(h("label", {"attrs": {"for": i + "-value"}}, "Value:"))
value.append(h("input.value-input", {"props": {"type": "text", "value": feature["value"], "id": i + "-value"}}, "")) value.append(h("input.value-input", {"props": {"type": "text", "value": feature["value"], "id": i + "-value"}}, ""))
content.append(split_line2(name, value)) content.append(split_line2(name, value, feature.id))
content.append(h("button", {"on": {"click": message.msg(message.AddToGenericList, current_labels)}}, "+")) content.append(h("button", {"on": {"click": message.msg(message.AddToGenericList, current_labels)}}, "+"))
return content return content

View File

@ -100,6 +100,7 @@ def edit_variants(entry):
def edit_homonymy(entry): def edit_homonymy(entry):
hget = lambda: entry.copy().homonymy hget = lambda: entry.copy().homonymy
console.log(hget)
content = homonymy_editor("Homonymy", hget) content = homonymy_editor("Homonymy", hget)
return modal_template(content, "Add or remove homonymy features", (message.EditHomonymy,), (message.DeleteHomonymy,)) return modal_template(content, "Add or remove homonymy features", (message.EditHomonymy,), (message.DeleteHomonymy,))