import_skr.py 3.36 KB
# -*- coding: utf-8 -*-

from django.db import connection, transaction
from django.core.management.base import BaseCommand
from django.db.models import Max

from common.util import no_history
from dictionary.management.commands.import_data import get_cursor
from dictionary.models import Vocabulary, Qualifier, Lexeme, Pattern, LexemeAttribute, LexemeAV, LexemeInflectionPattern


class Command(BaseCommand):
    args = '<input db filename>'
    help = 'Imports initial data'

    def handle(self, db_name, **options):
        import_skr(db_name)

SKR_ATTRS = (
    (u'dokładniej', 'dokładniej', True),
    (u'jeszcze dokładniej', 'jeszcze_dokładniej', False),
    (u'rozwinięcie', 'glossa', False),
    (u'tłumaczenie', 'tlumaczenie', False),
)

def import_skr(db_name):
    transaction.commit_unless_managed()
    transaction.enter_transaction_management()
    transaction.managed()
    cursor = connection.cursor()
    sqlite_cursor = get_cursor(db_name)
    no_history()

    sgjp = Vocabulary.objects.get(id='SGJP')
    zmiotki = Vocabulary.objects.get(id='zmiotki')
    pattern = Pattern.objects.get(type__lexical_class_id='skr')

    qual_dict = dict(Qualifier.objects.values_list('label', 'id'))
    result = sqlite_cursor.execute(
        'SELECT pochodzenie FROM skroty UNION SELECT kwalifikator FROM skroty')
    for row in result:
        for q_label in row[0].split(','):
            q_label = q_label.strip()
            if q_label != '' and q_label not in qual_dict:
                q = Qualifier.objects.create(
                    vocabulary=sgjp, label=q_label, type='zakr')
                qual_dict[q_label] = q.id

    attrs = {}
    for attr_name, field, closed in SKR_ATTRS:
        la, created = LexemeAttribute.objects.get_or_create(
            name=attr_name, closed=closed)
        for pos in ('skrl', 'skrw', 'skrf'):
            la.parts_of_speech.add(pos)
        attrs[attr_name] = la
    next_id = Lexeme.objects.aggregate(Max('id'))['id__max'] + 1

    for row in sqlite_cursor.execute('SELECT * FROM skroty'):
        entry = row['leksem']
        good = row['status'][0] in '&+'
        vocab = sgjp if good else zmiotki
        status = Lexeme.STATUS_CONFIRMED if good else Lexeme.STATUS_CANDIDATE
        pos = 'skr' + row['pos'][2]
        quals = row['pochodzenie'].split(', ')
        quals += row['kwalifikator'].split(',')
        quals = [qual.strip() for qual in quals if qual.strip()]
        valence = row['łączliwość']
        source = row['źródło']
        comment = row['komentarz1']
        if row['motywacja']:
            if comment:
                comment += '\n' + row['motywacja']
            else:
                comment = row['motywacja']
        l = Lexeme.objects.create(
            entry=entry, part_of_speech_id=pos, valence=valence, source=source,
            comment=comment, owner_vocabulary=vocab, status=status, id=next_id,
            source_id=row['Identyfikator'])
        next_id += 1
        vocab.add_lexeme(l)
        LexemeInflectionPattern.objects.create(
            lexeme=l, pattern=pattern, index=1, root=entry)
        for attr_name, field, closed in SKR_ATTRS:
            attrs[attr_name].add_lexeme(l, row[field])
        for qual in quals:
            l.qualifiers.add(qual_dict[qual])
        l.refresh_qualifiers_cache()

    cursor.close()
    sqlite_cursor.close()
    transaction.commit()
    transaction.leave_transaction_management()