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/model/entry.py

140 lines
5.3 KiB

from model.sense import Sense
from model.data import Data
from model.tags import import_label_list
from lib.snabbdom import h
import message as M
from view import View
from view.utils import clean_label
class Entry(Data):
def __init__(self):
self.status = ""
self.headword = ""
self.homonymy = []
self.headword_type = None
self.grammar = ""
self.comment = ""
self.variants = []
self.related_entries = []
self.lexical_unit = {}
self.measure = {}
self.labels = []
self.senses = []
def import_xml(self, entry_xml):
status = entry_xml.querySelector("head status")
headword = entry_xml.querySelector("head headword lemma")
grammar = entry_xml.querySelector("head grammar category")
comment = entry_xml.querySelector("head comment")
self.status = status.textContent if status else ""
self.headword = headword.textContent if headword else ""
self.headword_type = headword.getAttribute("type") if headword else None
self.grammar = grammar.textContent if grammar else ""
self.comment = comment.textContent if comment else ""
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.related_entries = [re.textContent for re in entry_xml.querySelectorAll("head relatedEntryList relatedEntry")]
lex_unit = entry_xml.querySelector("lexical_unit lexeme,lexicalUnit lexeme")
if lex_unit:
self.lexical_unit['id'] = lex_unit.getAttribute("lexical_unit_lexeme_id")
self.lexical_unit['text'] = lex_unit.textContent
measure = entry_xml.querySelector("measureList measure")
if measure:
self.measure["source"] = measure.getAttribute("source")
self.measure["type"] = measure.getAttribute("type")
self.measure["text"] = measure.textContent
self.labels = import_label_list("head labelList label", entry_xml)
for i, sense_xml in enumerate(entry_xml.querySelectorAll("body senseList sense")):
sense = Sense()
sense.import_xml(sense_xml, i)
self.senses.append(sense)
def view(self, model):
view_sense_list = [sense.view(model, idx) for idx, sense in enumerate(self.senses)]
buttons_left = self._view_button_section(model)
buttons_right = View.view_toggle_buttons(model)
return h("div#entry", {}, [
h("div#entry-status", {}, self.status),
h("div#entry-header", {}, [
h("span#headword", {}, self.headword),
h("span#grammar", {}, self.grammar),
h("span#measure", {}, self.get_measure_text())]),
h("div.flex.five", {}, [
h("div.four-fifth", {}, buttons_left),
h("div.one-fifth", {}, buttons_right)]),
h("div#sense-container", {}, view_sense_list),
h("button.add-button", {"on": {"click": M.msg(M.AddSense)}}, "+")])
def _view_button_section(self, model):
clk = lambda cls: {"on": {"click": M.msg(cls)}}
buttons = [
h("button.normal", clk(M.ShowVariantsEdit), "Variante"),
h("button.success", clk(M.ShowRelatedEntriesEdit), "Povezano"),
h("button.success", clk(M.ShowEntryLabelsEdit), "Oznake"),
h("button.normal", clk(M.ShowCommentEdit), "Opombe"),
h("button.normal", clk(M.ShowHomonymyEdit), "Homonomije"),]
view_buttons = []
view_table = []
if len(self.variants) == 0:
view_buttons.append(buttons[0])
else:
view_table.append((buttons[0], ", ".join(self.variants)))
if len(self.homonymy) == 0:
view_buttons.append(buttons[4])
else:
view_table.append((buttons[4], ", ".join(h.value for h in self.homonymy)))
if len(self.related_entries) == 0:
view_buttons.append(buttons[1])
else:
view_table.append((buttons[1], ", ".join(self.related_entries)))
if len(self.labels) == 0:
view_buttons.append(buttons[2])
else:
labels = ", ".join([clean_label(val) for _, val in self.labels])
view_table.append((buttons[2], labels))
if self.comment == "":
view_buttons.append(buttons[3])
else:
view_table.append((buttons[3], self.comment))
table_rows = [
h("tr", {}, [ h("td", {}, btn), h("td", {}, content)])
for btn, content in view_table]
view_buttons.append(h("table", {}, table_rows))
return h("div", {}, view_buttons)
def get_measure_text(self):
return self.measure["text"] if "text" in self.measure else ""
def remove_translation(self, translation):
for sense in self.senses:
for cluster in sense.translations:
if translation in cluster:
cluster.remove(translation)
return
for example in sense.examples:
for cluster in example.translations:
if translation in cluster:
cluster.remove(translation)
return