Entry.py 2.26 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from importer.Syntax import Syntax
from importer.Meanings import Meanings
from importer.Semantics import Semantics
from importer.Examples import Examples
from importer.Global import set_current_entry
from connections.models import POS, Status
import connections.models

class Entry:

    def __init__(self, entry_tree, entry_meanings, meanings, frames, examples_in_data, examples_out_file, misconnected_out_file):
        self._base = entry_tree._children[0]._children[0]._content
        self._pos = entry_tree._children[0]._children[1]._content
        print("processing: " + self._base)
        self._status = entry_tree._children[1]._children[0]._children[0]._content
        self._syntax = Syntax.fromTree(entry_tree._children[2])
        self._meanings = None
        self._semantics = None
        if len(entry_tree._children) >= 7:
            self._meanings = Meanings.fromTree(entry_tree._children[5])
            self._semantics = Semantics.fromTree(self._base, self._pos, entry_tree._children[4], frames, self._meanings, self._syntax, entry_tree._children[6])
        self._examples = Examples.fromTree(entry_tree._children[3], self._syntax, self._semantics, self._base, self._meanings, examples_in_data, examples_out_file, misconnected_out_file)

    def store(self, all_meanings, stored_positions):
        set_current_entry(self)
        pos = POS.objects.get(tag=self._pos)
        status = Status.objects.get(key=self._status)
        # @TODO: nie ma frequency w xml-u
        entry = connections.models.Entry(name=self._base, pos=pos, status=status, frequency_1M=0, frequency_300M=0)
        entry.save()
        try:
            self._syntax.store(entry, stored_positions)
            if self._semantics is not None:
                self._semantics.store(entry, all_meanings)
            entry.save()
            if self._meanings:
                for meaning in self._meanings._meanings:
                    mng = all_meanings[meaning][1]
                    mng.assign_entry(entry)
            self._examples.store(entry, all_meanings)
        except Exception as e:
            entry.import_error = True
            entry.save()
            raise

    def __str__(self):
        return self._pos + '(' + self._base + ',' + str(self._syntax) + ').'