root/trunk/lib/dicteditor.py

Revision 15, 4.6 kB (checked in by mjoc, 1 year ago)

Atnaujinti vertimai. Pridėti nauji copyright'ai. Pakeista konfigūracija.

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