import_variant.py
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#-*- coding:utf-8 -*-
from django.core.management.base import BaseCommand, CommandError
from common.util import debug
from dictionary.models import TableTemplate, Cell, TableCell, PartOfSpeech, \
Variant, PatternType, InflectionCharacteristic, BaseFormLabel
class Command(BaseCommand):
args = '<variant name> <input file name>'
help = 'Imports variant from text file'
def handle(self, variant, filename, **options):
import_variant(variant, open(filename))
def import_variant(variant, input_file):
for line in input_file:
if line.startswith(variant):
line = line.strip().decode('utf-8')
data = line.split('\t')
_variant, pos, p_type, ic, row, col, rspan, cspan, bfl, tag, pref, suf, \
index = data
# trochę copypasta
lc = PartOfSpeech.objects.get(symbol=pos).lexical_class
v, _created = Variant.objects.get_or_create(id=variant)
tt_data = {
'variant': v,
'pattern_type': PatternType.objects.get(entry=p_type, lexical_class=lc),
'inflection_characteristic': InflectionCharacteristic.objects.get(
entry=ic, part_of_speech__symbol=pos),
}
tt, _created = TableTemplate.objects.get_or_create(**tt_data)
c = Cell()
c.table_template = tt
c.base_form_label = BaseFormLabel.objects.get(entry=bfl)
c.tag = tag
c.prefix = pref
c.suffix = suf
c.index = int(index)
c.save()
if row:
tc = TableCell()
tc.cell = c
tc.row = int(row)
tc.col = int(col)
tc.rowspan = int(rspan)
tc.colspan = int(cspan)
tc.save()