root/tags/release-0.6.2/lib/dicteditor.py

Revision 1, 4.5 kB (checked in by mjoc, 2 years ago)

Initial import.

Line 
1 #
2 # OpenDict
3 # Copyright (c) 2005 Martynas Jocius <mjoc@akl.lt>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your opinion) any later version.
9 #
10 # This program is distributed in the hope that will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more detals.
14 #
15 # You shoud have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307 USA
19 #
20
21 """
22 Dictionary editor module
23 """
24
25
26 class Translation:
27     """Translation in Slowo format
28
29     Keeps one original word and a dictionary of translations
30     """
31
32     def __init__(self):
33         """Initialize"""
34
35         self.word = None
36         self.translations = {}
37        
38
39     def setWord(self, word):
40         """Set word"""
41
42         self.word = word
43
44
45     def getWord(self):
46         """Return word"""
47
48         return self.word
49
50
51     def addTranslation(self, trans, comment=None):
52         """Set translation object"""
53        
54         self.translations[trans] = comment
55
56
57     def setTranslations(self, newTrans):
58         """Set translation dictionary"""
59
60         self.translations = newTrans
61
62
63     def getTranslations(self):
64         """Return translation object"""
65
66         return self.translations
67
68
69 class Editor:
70     """Slowo dictionary editor"""
71
72     def __init__(self, filePath=None):
73         """Initialize variables and load dictionary if requested"""
74
75         self.filePath = filePath
76         self.units = []
77         self.encoding = 'UTF-8'
78
79         if filePath:
80             self.load(filePath)
81
82
83     def load(self, filePath):
84         """Load dictionary into memory"""
85
86         self.filePath = filePath
87         self.units = []
88
89         try:
90             fd = open(filePath)
91
92             for line in fd:
93                 try:
94                     line = unicode(line, self.encoding)
95                 except Exception, e:
96                     raise Exception, "Unable to encode text in %s" \
97                           % self.encoding
98                
99                 word, end = line.split('=')
100                 word = word.strip()
101                
102                 translation = Translation()
103                 translation.setWord(word)
104
105                 chunks = end.split(';')
106                 for chunk in chunks:
107                     chunk = chunk.strip()
108                     if not chunk:
109                         continue
110                    
111                     try:
112                         trans, comment = chunk.split('//')
113                     except:
114                         trans = chunk
115                         comment = None
116
117                     trans = trans.strip()
118                     if comment:
119                         comment = comment.strip()
120
121                     translation.addTranslation(trans, comment)
122                 self.units.append(translation)
123
124             fd.close()
125
126         except Exception, e:
127             raise Exception, "Unable to read dictionary: %s" % e
128
129
130     def save(self, filePath=None):
131         """Write data to disk"""
132
133         if not filePath:
134             filePath = self.filePath
135
136         try:
137             fd = open(filePath, 'w')
138
139             for unit in self.getUnits():
140                 outstr = "%s = " % unit.getWord()
141                 chunks = []
142
143                 for trans, comment in unit.getTranslations().items():
144                     if comment:
145                         chunks.append("%s // %s" % (trans, comment))
146                     else:
147                         chunks.append(trans)
148                 outstr += u' ; '.join(chunks) + u' ;'
149                 outstr = outstr.encode(self.encoding)
150                 print >> fd, outstr
151         except Exception, e:
152             raise Exception, "Unable to save dictionary: %s" % e
153
154
155     def getUnit(self, word):
156         """Return Translation object for the word"""
157
158         for unit in self.units:
159             if unit.getWord() == word:
160                 return unit
161
162         return None
163
164
165     def addUnit(self, unit):
166         """Adds translation unit to dictionary"""
167
168         self.units.append(unit)
169
170
171     def removeUnit(self, unit):
172         """Removes translation unit defined by word"""
173
174         self.units.remove(unit)
175
176
177     def getUnits(self):
178         """Return list of translation objects"""
179
180         return self.units
181    
Note: See TracBrowser for help on using the browser.