root/trunk/opendict.py

Revision 25, 5.9 kB (checked in by nerijusb, 8 months ago)

python-xml (PyXML) no longer needed

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