File: base.py

package info (click to toggle)
stardicter 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 268 kB
  • sloc: python: 1,047; sh: 124; makefile: 4
file content (488 lines) | stat: -rw-r--r-- 14,209 bytes parent folder | download | duplicates (3)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# -*- coding: utf-8 -*-
#
# Copyright © 2006 - 2017 Michal Čihař <michal@cihar.com>
#
# This file is part of Stardicter <https://cihar.com/software/slovnik/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""Base module for stardciter convertors."""

from __future__ import unicode_literals

import codecs
import datetime
import gzip
import hashlib
import json
from operator import attrgetter
import os
import re
import struct
import time

from six.moves.urllib.request import urlopen
from six import BytesIO

from stardicter.word import Word


README_TEXT = r'''{title}
{line}

This is autogenerated dictionary for StarDict.

Data were downloaded from following website:
<{url}>

The original source is available under {license}.

Dictionary was generated using:
Stardicter version {version}

You can get conversion script from:
<https://cihar.com/software/slovnik/>

Install dictionary by copying dictionary files to dic/ folder in
StarDict. On Linux it is usually /usr/share/stardict/dic/, on Windows
C:\Program files\stardict\dic\.
'''

STRIPTAGS = re.compile(r"<.*?>", re.DOTALL)

CONFIGFILE = os.path.expanduser('~/.stardicter')

# type of word (used as title)
FMT_TYPE = '<span size="larger" color="darkred" weight="bold">{0}</span>\n'

AUTHOR = 'Stardicter'
URL = 'https://cihar.com/software/slovnik/'


class StardictWriter(object):
    '''
    Generic writer for stardict dictionary.
    '''
    url = None
    name = 'Generic'
    prefix = ''
    source = 'aa'
    target = 'bb'
    license = ''
    bidirectional = True
    download_url = None
    download_charset = 'utf-8'
    download_gzip = False

    def __init__(self, ascii=False, notags=False, keyprefix='',
                 source='', target='', file=None):
        self.words = {}
        self.reverse = {}
        self.description = ''
        self.ascii = ascii
        self.notags = notags
        self._data = None
        self._checksum = None
        self.keyprefix = keyprefix
        if source:
            self.source = source
        if target:
            self.target = target
        self.file = file

    @property
    def data(self):
        '''
        Returns downloaded data file.
        '''
        if self._data is None:
            self._data = self.download()
        return self._data

    @property
    def lines(self):
        '''
        Returns lines.
        '''
        return self.data.splitlines()

    @property
    def checksum(self):
        '''
        Returns data checksum.
        '''
        if self._checksum is None:
            self._checksum = self.get_checksum()
        return self._checksum

    def get_filename(self, forward=True):
        '''
        Returns filename for dictionary.
        '''
        if forward:
            name = '{0}-{1}'.format(self.source, self.target)
        else:
            name = '{0}-{1}'.format(self.target, self.source)

        suffix = ''
        if self.ascii:
            suffix += '-ascii'
        if self.notags:
            suffix += '-notags'

        return '{0}{1}{2}'.format(self.prefix, name, suffix)

    def get_name(self, forward=True):
        '''
        Returns dictionary name.
        '''
        return self.name

    def is_data_line(self, line):
        '''
        Checks whether line is used for checksum. Can be used to exclude
        timestamps from data.
        '''
        return True

    def is_header_line(self, line):
        '''
        Checks whether line is header.
        '''
        return line[0] == '#'

    def add_description(self, line):
        '''
        Adds description from line.
        '''
        self.description += line[1:] + '\n'

    def get_checksum(self):
        '''
        Calculated dictionary checksum.
        '''
        md5 = hashlib.md5()
        for line in self.lines:
            if self.is_data_line(line):
                md5.update(line.encode('utf-8'))
        return md5.hexdigest()

    def download(self):
        '''
        Downloads dictionary.
        '''
        if self.file:
            handle = self.file
            self.download_gzip = self.file.name.endswith('.gz')
        else:
            if self.download_url is None:
                return 'word\ttranslation\ttype\tnote\tauthor'
            handle = urlopen(self.download_url)
        if self.download_gzip:
            stringio = BytesIO(handle.read())
            handle.close()
            handle = gzip.GzipFile(fileobj=stringio)
        text = handle.read()
        return text.decode(self.download_charset)

    def parse_line(self, line):
        '''
        Parses single line with word.
        '''
        return [Word.from_slovnik(line)]

    def parse(self):
        '''
        Parses dictionary.
        '''
        for line in self.lines:
            # Skip blank lines
            if line.strip() == '':
                continue

            # Description from header
            if self.is_header_line(line):
                self.add_description(line)
                continue

            # Parse line
            words = self.parse_line(line)

            for word in words:

                # Skip not translated words
                if not word.word or not word.translation:
                    continue

                # Store word
                if len(word.word) < 256:
                    if word.word not in self.words:
                        self.words[word.word] = []

                    self.words[word.word].append(word)

                # Other direction
                if self.bidirectional and len(word.translation) < 256:
                    if word.translation not in self.reverse:
                        self.reverse[word.translation] = []
                    self.reverse[word.translation].append(word.reverse())

        # Sort by translation alphabetically

        for word in self.words:
            self.words[word].sort(key=attrgetter('translation'))

        for word in self.reverse:
            self.reverse[word].sort(key=attrgetter('translation'))

    def convert(self, text, convert=True):
        '''
        Converts text to match wanted format.
        '''
        if self.notags:
            text = STRIPTAGS.sub('', text)

        if not convert:
            return text

        if self.ascii:
            return text.encode('ascii', 'deaccent')

        return text.encode('utf-8')

    def formatentry(self, words):
        '''
        Formats dictionary entry.
        '''
        # sort alphabetically
        # array for different word types
        alltypes = [
            'n:',
            'v:',
            'adj:',
            'adv:',
            'prep:',
            'conj:',
            'interj:',
            'num:',
            '',
        ]
        # variables used for data
        result = ''
        typed = {}
        # array holding typed words
        for key in alltypes:
            typed[key] = []
        # process all translations
        for word in words:
            tokens = word.wtype.split()
            saved = False
            for key in alltypes:
                # check if translation is current type
                if key in tokens:
                    saved = True
                    # remove type from translation, it will be in title
                    del tokens[tokens.index(key)]
                    word.wtype = ' '.join(tokens)
                    # handle irregullar word specially (display them first)
                    if '[neprav.]' in tokens:
                        typed[key].insert(0, word)
                    else:
                        typed[key].append(word)
                    break
            if not saved:
                typed[''].append(word)

        # and finally convert entries to text
        for typ in alltypes:
            if typed[typ]:
                # header to display
                if typ == '':
                    result += '\n'
                else:
                    result += FMT_TYPE.format(typ)
                for word in typed[typ]:
                    result += '    '
                    result += word.format()

        return result

    def getsortedwords(self, words):
        '''
        Returns keys of hash sorted case insensitive.
        '''
        if self.ascii:
            tuples = [
                (item.encode('ascii', 'deaccent').lower(), item)
                for item in words
            ]
        else:
            tuples = [
                (item.encode('utf-8').lower(), item)
                for item in words
            ]
        tuples.sort()
        return [item[1] for item in tuples]

    def write_words(self, basefilename, name, words):
        '''
        Writes word list to dictionary files.
        '''
        # initialize variables
        offset = 0
        count = 0
        idxsize = 0

        # File names
        dictn = '{0}.dict'.format(basefilename)
        idxn = '{0}.idx'.format(basefilename)

        # Write dictionary and index
        with open(dictn, 'wb') as dictf, open(idxn, 'wb') as idxf:
            for key in self.getsortedwords(words):
                # format single entry
                entry = self.convert(self.formatentry(words[key]))

                # write dictionary text
                dictf.write(entry)

                # write index entry
                idxf.write(self.convert(key))
                idxf.write(b'\0')
                idxf.write(struct.pack(b'!I', offset))
                idxf.write(struct.pack(b'!I', len(entry)))

                # calculate offset for next index entry
                offset += len(entry)
                count += 1

            # index size is needed in ifo
            idxsize = idxf.tell()

        self._write_ifo(name, basefilename, count, idxsize)

    def _write_ifo(self, name, basefilename, count, idxsize):
        '''
        Writes info file.
        '''
        filename = '{0}.ifo'.format(basefilename)
        with codecs.open(filename, 'w', 'utf-8') as handle:
            handle.write('StarDict\'s dict ifo file\n')
            handle.write('version=2.4.2\n')
            handle.write(self.convert('bookname={0}\n'.format(name), False))
            handle.write('wordcount={0}\n'.format(count))
            handle.write('idxfilesize={0}\n'.format(idxsize))
            handle.write(self.convert('author={0}\n'.format(AUTHOR), False))
            handle.write(self.convert('website={0}\n'.format(URL), False))
            # we're using pango markup for all entries
            handle.write('sametypesequence=g\n')
            now = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
            today = datetime.datetime.utcfromtimestamp(now)
            handle.write(today.strftime('date=%Y.%m.%d\n'))

    def write_dict(self, directory):
        '''
        Writes dictionary into directory.
        '''
        # Write readme
        with open(os.path.join(directory, 'README'), 'wb') as readme:
            readme.write(self.get_readme().encode('utf-8'))
            if self.description:
                readme.write(
                    '\nOriginal description of dictionary:\n{0}'.format(
                        self.description
                    ).encode('utf-8')
                )
        # Write forward dictioanry
        self.write_words(
            os.path.join(directory, self.get_filename(True)),
            self.get_name(True),
            self.words
        )
        # Write reverse dictionary
        if self.bidirectional:
            self.write_words(
                os.path.join(directory, self.get_filename(False)),
                self.get_name(False),
                self.reverse
            )

    def get_readme(self):
        '''
        Generates README text for dictionary.
        '''
        from stardicter import __version__
        title = '{0} for StarDict'.format(self.name)
        return README_TEXT.format(
            title=title,
            line='-' * len(title),
            url=self.url,
            license=self.license,
            version=__version__,
        )

    def get_config_key(self):
        '''
        Key used to store MD5 in config.
        '''
        return 'md5-{0}{1}'.format(self.keyprefix, self.get_filename())

    def load_config(self):
        '''
        Loads checksum cache.
        '''
        try:
            with open(CONFIGFILE) as handle:
                return json.load(handle)
        except (ValueError, IOError):
            return {}

    def save_config(self, changes):
        '''
        Loads checksum cache.
        '''
        config = self.load_config()
        config.update(changes)
        with codecs.open(CONFIGFILE, 'wb', 'utf-8') as handle:
            json.dump(config, handle, indent=2)

    def was_changed(self):
        '''
        Detects whether dictionary has same content as on last run.
        '''
        key = self.get_config_key()
        config = self.load_config()
        if key not in config:
            return True
        return self.checksum != config[key]

    def save_checksum(self):
        '''
        Saves checksum to configuration.
        '''
        key = self.get_config_key()
        self.save_config({key: self.checksum})

    def get_source_name(self):
        """Name for source file."""
        name = os.path.basename(self.download_url)
        if name.endswith('.gz'):
            name = name[:-3]
        return name

    def write_source(self, directory):
        """Write source file."""
        filename = os.path.join(directory, self.get_source_name())
        with open(filename, 'wb') as handle:
            handle.write(self.data.encode(self.download_charset))