File: opendict.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 (190 lines) | stat: -rwxr-xr-x 6,045 bytes parent folder | download
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
183
184
185
186
187
188
189
190
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

# 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

import sys
import os
import imp
import traceback
import string
import time

# main_is_frozen() returns True when running the exe, and False when
# running from a script.
def main_is_frozen():
    return (hasattr(sys, "frozen") or # new py2exe
	    hasattr(sys, "importers") # old py2exe
	    or imp.is_frozen("__main__")) # tools/freeze

# If application is not frozen to binary, try selecting wxPython 3.0
# on multiversioned wxPython installation.
if not main_is_frozen():
    try:
        import wxversion
        wxversion.select(["3.0"])
    except Exception, e:
        print "You seem to have an unsupported wxPython version: %s" \
              % e

try:
    import wx
except ImportError:
    print >> sys.stderr, "**"
    print >> sys.stderr, "** Error: wxPython library not found"
    print >> sys.stderr, "** Please install wxPython 3.0 or later to run OpenDict"
    print >> sys.stderr, "**"
    sys.exit(1)


# get_main_dir() returns the directory name of the script or the
# directory name of the exe
def get_main_dir():
    if main_is_frozen():
	return os.path.dirname(sys.executable)
    return os.path.dirname(os.path.realpath(__file__))
    # or return os.path.dirname(sys.argv[0])

#
# Initial path
#
sys.path.insert(0, get_main_dir())

# OpenDict Modules
from lib import info
from lib.gui.mainwin import MainWindow
from lib.gui.errorwin import ErrorWindow
from lib.config import Configuration
from lib.logger import systemLog, debugLog, DEBUG, INFO, WARNING, ERROR
from lib import misc
from lib import info
from lib import newplugin
from lib import plaindict
from lib import util


class OpenDictApp(wx.App):
   """Top-level class of wxWidgets application"""

   locale = wx.Locale()

   def OnInit(self):

      _ = wx.GetTranslation
      _start = time.time()

      wx.Version = []
      try:
          wx.Version = wx.__version__
      except Exception, e:
          try:
              wx.Version = wx.Python.__version__
          except:
              pass

      if wx.Version.split('.') < ['3', '0']:
          from lib.gui import errorwin
          
          title = _("wxPython Version Error")
          msg = _("wxPython %s is installed on this system.\n\n"
                  "OpenDict %s requires wxPython 3.0 or newer to run smoothly.\n\n"
                  "You can find wxPython at "
                  "http://www.wxpython.org or you can "
                  "install it using your system package manager.") \
                  % (wx.Version, info.VERSION)
          errorwin.showErrorMessage(title, msg)
          return False

      
      util.makeDirectories()
      
      systemLog(DEBUG, "Unicode version: %s" % wx.USE_UNICODE)
      
      # Init gettext support
      wx.Locale_AddCatalogLookupPathPrefix(os.path.join(info.GLOBAL_HOME,
                                                       'po'))
      self.locale.Init(wx.LANGUAGE_DEFAULT)
      self.locale.AddCatalog('opendict')

      # Data cache instance
      self.cache = {}
      
      # Dictionaries container
      # Mapping: name -> object
      self.dictionaries = {}

      # Failed dictionaries.
      # For error message that may be shown after creating main window
      self.invalidDictionaries = []
      
      self.config = Configuration()
      self.config.load()

      self.agreements = util.AgreementsManager(os.path.join(info.LOCAL_HOME,
                                                            'agreements.txt'))
      
      
      
      # Set unique ids
      for plugin in newplugin.loadDictionaryPlugins(self.dictionaries,
                                                    self.invalidDictionaries):
         self.config.ids[wx.NewId()] = plugin.getName()

      for plain in plaindict.loadPlainDictionaries(self.dictionaries):
         self.config.ids[wx.NewId()] = plain.getName()


      for d in self.dictionaries.values():
          if not self.config.activedict.init:
              if not self.config.activedict.enabled(d.getName()):
                  d.setActive(active=False)
          else:
              # Fill up with names if not initialized yet
              self.config.activedict.add(d.getName())


      windowPos = (int(self.config.get('windowPosX')),
                                int(self.config.get('windowPosY')))
      windowSize = (int(self.config.get('windowWidth')),
                    int(self.config.get('windowHeight')))

      self.window = MainWindow(None, -1, "OpenDict",
                               windowPos,
                               windowSize,
                               style=wx.DEFAULT_FRAME_STYLE)
      
      try:
          systemLog(INFO, "OpenDict %s" % info.VERSION)
          systemLog(INFO, "wxPython %s" % wx.Version)
          systemLog(INFO, "Global home: %s:" % info.GLOBAL_HOME)
          systemLog(INFO, "Local home: %s" % info.LOCAL_HOME)
          systemLog(DEBUG, "Loaded in %f seconds" % (time.time() - _start))
      except Exception, e:
          print "Logger Error: Unable to write to log (%s)" % e
      
      self.window.Show(True)

      return True


if __name__ == "__main__":
   
   openDictApp = OpenDictApp(0)
   openDictApp.MainLoop()