root/tags/release-0.6.2/opendict.py

Revision 3, 6.1 kB (checked in by mjoc, 2 years ago)

Ability to activate/deactivate installed dictionaries so that only a
part of them would be shown in the menu at the time.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3
4 # OpenDict
5 # Copyright (c) 2003-2006 Martynas Jocius <mjoc@akl.lt>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your opinion) any later version.
11 #
12 # This program is distributed in the hope that will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more detals.
16 #
17 # You shoud have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # 02111-1307 USA
21
22 import sys
23 import os
24 import imp
25 import traceback
26 import string
27 import time
28
29 # main_is_frozen() returns True when running the exe, and False when
30 # running from a script.
31 def main_is_frozen():
32     return (hasattr(sys, "frozen") or # new py2exe
33             hasattr(sys, "importers") # old py2exe
34             or imp.is_frozen("__main__")) # tools/freeze
35
36 # If application is not frozen to binary, try selecting wxPython 2.6 or 2.5
37 # on multiversioned wxPython installation.
38 if not main_is_frozen():
39     try:
40         import wxversion
41         wxversion.select(["2.5", "2.6-unicode"])
42     except Exception, e:
43         print "You seem to have wxPython 2.4: %s" \
44               % e
45
46 try:
47     import wx
48     import wxPython
49 except ImportError:
50     print >> sys.stderr, "**"
51     print >> sys.stderr, "** Error: wxPython library not found"
52     print >> sys.stderr, "** Please install wxPython 2.5 or later to run OpenDict"
53     print >> sys.stderr, "**"
54     sys.exit(1)
55
56
57 try:
58     import xml.dom.ext
59 except ImportError:
60     print >> sys.stderr, "**"
61     print >> sys.stderr, "** Error: Python/XML library not found"
62     print >> sys.stderr, "** Please install python-xml (PyXML) to run OpenDict"
63     print >> sys.stderr, "**"
64     sys.exit(1)
65
66 # get_main_dir() returns the directory name of the script or the
67 # directory name of the exe
68 def get_main_dir():
69     if main_is_frozen():
70         return os.path.dirname(sys.executable)
71     return os.path.dirname(os.path.realpath(__file__))
72     # or return os.path.dirname(sys.argv[0])
73
74 #
75 # Initial path
76 #
77 sys.path.insert(0, get_main_dir())
78
79 # OpenDict Modules
80 from lib import info
81 from lib.gui.mainwin import MainWindow
82 from lib.gui.errorwin import ErrorWindow
83 from lib.config import Configuration
84 from lib.logger import systemLog, debugLog, DEBUG, INFO, WARNING, ERROR
85 from lib import misc
86 from lib import info
87 from lib import newplugin
88 from lib import plaindict
89 from lib import util
90
91
92 class OpenDictApp(wx.App):
93    """Top-level class of wxWindows application"""
94
95    locale = wx.Locale()
96
97    def OnInit(self):
98
99       _ = wx.GetTranslation
100       _start = time.time()
101
102       wxVersion = []
103       try:
104           wxVersion = wx.__version__
105       except Exception, e:
106           try:
107               wxVersion = wxPython.__version__
108           except:
109               pass
110
111       if wxVersion.split('.') < ['2', '6']:
112           from lib.gui import errorwin
113          
114           # Go away, wxPython 2.4!
115           title = _("wxPython Version Error")
116           msg = _("wxPython %s is installed on this system.\n\n"
117                   "OpenDict %s requires wxPython 2.6 to run smoothly.\n\n"
118                   "You can find wxPython 2.6 at "
119                   "http://www.wxpython.org or you can "
120                   "install it using your system package manager.") \
121                   % (wxVersion, info.VERSION)
122           errorwin.showErrorMessage(title, msg)
123           return False
124
125      
126       util.makeDirectories()
127      
128       systemLog(DEBUG, "Unicode version: %s" % wx.USE_UNICODE)
129      
130       # Init gettext support
131       wx.Locale_AddCatalogLookupPathPrefix(os.path.join(info.GLOBAL_HOME,
132                                                        'po'))
133       self.locale.Init(wx.LANGUAGE_DEFAULT)
134       self.locale.AddCatalog('opendict')
135
136       # Data cache instance
137       self.cache = {}
138      
139       # Dictionaries container
140       # Mapping: name -> object
141       self.dictionaries = {}
142
143       # Failed dictionaries.
144       # For error message that may be shown after creating main window
145       self.invalidDictionaries = []
146      
147       self.config = Configuration()
148       self.config.load()
149
150       self.agreements = util.AgreementsManager(os.path.join(info.LOCAL_HOME,
151                                                             'agreements.txt'))
152      
153      
154      
155       # Set unique ids
156       for plugin in newplugin.loadDictionaryPlugins(self.dictionaries,
157                                                     self.invalidDictionaries):
158          self.config.ids[wx.NewId()] = plugin.getName()
159
160       for plain in plaindict.loadPlainDictionaries(self.dictionaries):
161          self.config.ids[wx.NewId()] = plain.getName()
162
163
164       for d in self.dictionaries.values():
165           if not self.config.activedict.init:
166               if not self.config.activedict.enabled(d.getName()):
167                   d.setActive(active=False)
168           else:
169               # Fill up with names if not initialized yet
170               self.config.activedict.add(d.getName())
171
172
173       windowPos = (int(self.config.get('windowPosX')),
174                                 int(self.config.get('windowPosY')))
175       windowSize = (int(self.config.get('windowWidth')),
176                     int(self.config.get('windowHeight')))
177
178       self.window = MainWindow(None, -1, "OpenDict",
179                                windowPos,
180                                windowSize,
181                                style=wx.DEFAULT_FRAME_STYLE)
182      
183       try:
184           systemLog(INFO, "OpenDict %s" % info.VERSION)
185           systemLog(INFO, "wxPython %s" % wxVersion)
186           systemLog(INFO, "Global home: %s:" % info.GLOBAL_HOME)
187           systemLog(INFO, "Local home: %s" % info.LOCAL_HOME)
188           systemLog(DEBUG, "Loaded in %f seconds" % (time.time() - _start))
189       except Exception, e:
190           print "Logger Error: Unable to write to log (%s)" % e
191      
192       self.window.Show(True)
193
194       return True
195
196
197 if __name__ == "__main__":
198    
199    openDictApp = OpenDictApp(0)
200    openDictApp.MainLoop()
Note: See TracBrowser for help on using the browser.