File: textdict.py

package info (click to toggle)
dico 2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,908 kB
  • sloc: ansic: 85,886; sh: 50,207; lex: 2,624; yacc: 1,437; tcl: 1,434; python: 1,281; makefile: 1,266; perl: 881; lisp: 489; awk: 157; pascal: 125; cpp: 44; fortran: 25; asm: 19; sed: 16; xml: 8
file content (144 lines) | stat: -rw-r--r-- 4,231 bytes parent folder | download
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2008-2019 Sergey Poznyakoff
# Copyright (C) 2008, 2012 Wojciech Polak
# 
# GNU Dico 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, or (at your option)
# any later version.
# 
# GNU Dico 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 GNU Dico.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import dico
import sys

class DicoResult:
    result = {}

    compcount = 0
    
    def __init__ (self, *argv):
        self.result = argv[0]
        if len (argv) == 2:
                self.compcount = argv[1]

    def count (self):
        return len (self.result)

    def output (self, n):
        pass

    def append (self, elt):
        self.result.append (elt)

        
class DicoDefineResult (DicoResult):
    def output (self, n):
        print("%d. %s" % (n + 1, self.result[n]))
        print("---------", end="")

class DicoMatchResult (DicoResult):
    def output (self, n):
        sys.stdout.softspace = 0
        print(self.result[n], end="")

class DicoModule:
    adict =  {}
    dbname = ''
    filename = ''
    mod_descr = ''
    mod_info = []
    mime_headers = []
    langlist = ()

    def __init__ (self, *argv):
        self.filename = argv[0]
        pass
    
    def open (self, dbname):
        self.dbname = dbname
        if sys.version_info[0] <= 2:
            file = open (self.filename, "r")
        else:
            file = open (self.filename, "r", encoding="utf8")
        for line in file:
            if line.startswith ('--'):
                continue
            if line.startswith ('descr: '):
                self.mod_descr = line[7:].strip (' \n')
                continue
            if line.startswith ('info: '):
                self.mod_info.append (line[6:].strip (' \n'))
                continue
            if line.startswith ('lang: '):
                s = line[6:].strip (' \n').split(':', 2)
                if (len(s) == 1):
                    self.langlist = (s[0].split (), s[0].split ())
                else:
                    self.langlist = (s[0].split (), s[1].split ())
                continue
            if line.startswith ('mime: '):
                self.mime_headers.append(line[6:].strip (' \n'))
                continue
            f = line.strip (' \n').split (' ', 1)
            if len (f) == 2:
                self.adict[f[0].lower()] = f[1].strip (' ')
        file.close()
        return True

    def close (self):
        return True

    def descr (self):
        return self.mod_descr

    def lang (self):
        return self.langlist
    
    def info (self):
        return '\n'.join (self.mod_info)
    
    def define_word (self, word):
        if word in self.adict:
            return DicoDefineResult ([self.adict[word]])
        return False
    
    def match_word (self, strat, key):
        if strat.name == "exact":
            if key.word.lower () in self.adict:
                return DicoMatchResult ([self.adict[key.word.lower()]])
        elif strat.name == "prefix":
            res = []
            for w in self.adict:
                if w.startswith (key.word):
                    res.append (w)
            if len (res):
                return DicoMatchResult (res, len (self.adict))
        elif strat.has_selector:
            res = DicoMatchResult ([], len (self.adict))
            for k in self.adict:
                if strat.select (k, key):
                    res.append (k)
            if res.count() > 0:
                return res
        return False

    def output (self, rh, n):
        rh.output (n)
        return True

    def result_count (self, rh):
        return rh.count ()

    def compare_count (self, rh):
        return rh.compcount

    def db_mime_header (self):
        return '\n'.join(self.mime_headers)