File: dicteditor.py

package info (click to toggle)
opendict 0.6.5-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,056 kB
  • ctags: 884
  • sloc: python: 6,382; makefile: 135; sh: 71; xml: 41
file content (182 lines) | stat: -rw-r--r-- 4,666 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
#
# OpenDict
# Copyright (c) 2003-2006 Martynas Jocius <martynas.jocius@idiles.com>
# Copyright (c) 2007 IDILES SYSTEMS, UAB <support@idiles.com>
#
# 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 2 of the License, or
# (at your opinion) any later version.
#
# This program is distributed in the hope that will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more detals.
#
# You shoud have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
#

"""
Dictionary editor module
"""


class Translation:
    """Translation in Slowo format

    Keeps one original word and a dictionary of translations
    """

    def __init__(self):
        """Initialize"""

        self.word = None
        self.translations = {}
        

    def setWord(self, word):
        """Set word"""

        self.word = word


    def getWord(self):
        """Return word"""

        return self.word


    def addTranslation(self, trans, comment=None):
        """Set translation object"""
        
        self.translations[trans] = comment


    def setTranslations(self, newTrans):
        """Set translation dictionary"""

        self.translations = newTrans


    def getTranslations(self):
        """Return translation object"""

        return self.translations


class Editor:
    """Slowo dictionary editor"""

    def __init__(self, filePath=None):
        """Initialize variables and load dictionary if requested"""

        self.filePath = filePath
        self.units = []
        self.encoding = 'UTF-8'

        if filePath:
            self.load(filePath)


    def load(self, filePath):
        """Load dictionary into memory"""

        self.filePath = filePath
        self.units = []

        try:
            fd = open(filePath)

            for line in fd:
                try:
                    line = unicode(line, self.encoding)
                except Exception, e:
                    raise Exception, "Unable to encode text in %s" \
                          % self.encoding
                
                word, end = line.split('=')
                word = word.strip()
                
                translation = Translation()
                translation.setWord(word)

                chunks = end.split(';')
                for chunk in chunks:
                    chunk = chunk.strip()
                    if not chunk:
                        continue
                    
                    try:
                        trans, comment = chunk.split('//')
                    except:
                        trans = chunk
                        comment = None

                    trans = trans.strip()
                    if comment:
                        comment = comment.strip()

                    translation.addTranslation(trans, comment)
                self.units.append(translation)

            fd.close()

        except Exception, e:
            raise Exception, "Unable to read dictionary: %s" % e


    def save(self, filePath=None):
        """Write data to disk"""

        if not filePath:
            filePath = self.filePath

        try:
            fd = open(filePath, 'w')

            for unit in self.getUnits():
                outstr = "%s = " % unit.getWord()
                chunks = []

                for trans, comment in unit.getTranslations().items():
                    if comment:
                        chunks.append("%s // %s" % (trans, comment))
                    else:
                        chunks.append(trans)
                outstr += u' ; '.join(chunks) + u' ;'
                outstr = outstr.encode(self.encoding)
                print >> fd, outstr
        except Exception, e:
            raise Exception, "Unable to save dictionary: %s" % e


    def getUnit(self, word):
        """Return Translation object for the word"""

        for unit in self.units:
            if unit.getWord() == word:
                return unit

        return None


    def addUnit(self, unit):
        """Adds translation unit to dictionary"""

        self.units.append(unit)


    def removeUnit(self, unit):
        """Removes translation unit defined by word"""

        self.units.remove(unit)


    def getUnits(self):
        """Return list of translation objects"""

        return self.units