Module init_scripterng
[hide private]
[frames] | no frames]

Source Code for Module init_scripterng

 1  # Add path of init_scripterng to Python module search path 
 2  import sys, os 
 3  scripterng_path = os.path.dirname(os.path.abspath(__file__)) 
 4  sys.path.insert(0, scripterng_path) 
 5  print >> sys.stderr, "%s added to PYTHONPATH" % scripterng_path 
 6   
 7  # Look for PyQt 
 8  try: 
 9      from PyQt4.QtCore import PYQT_VERSION_STR,  QObject,  QCoreApplication 
10      from PyQt4.QtGui import qApp,  QMenu 
11  except ImportError: 
12      print >> sys.stderr, "Python cannot find the Qt4 bindings." 
13      print >> sys.stderr, "Please make sure, that the needed packages are installed." 
14      print >> sys.stderr, "On Ubuntu and Debian(-like) distribubutions you have to install python-qt4" 
15      raise 
16  try: 
17      from PyQt4 import QtScript 
18  except ImportError, e: 
19      print >> sys.stderr, "You seem to have Qt4 bindings without QtScript support." 
20      print >> sys.stderr, "This is currently a requirement." 
21      print >> sys.stderr, "Please make sure you have also libqt4-script installed." 
22      raise 
23   
24  # Shows nice looking error dialog if an unhandled exception occures. 
25  import excepthook 
26  excepthook.install() 
27   
28   
29  # Make sure PyQt is new enough 
30  if float(PYQT_VERSION_STR[:3]) < 4.4: 
31      print >> sys.stderr, "Your installed PyQt4 is older than version 4.4" 
32      print >> sys.stderr, "A newer version is needed. Please upgrade your packages." 
33      raise ImportError, "PyQt4 not new enough" 
34   
35  # Import helper modules 
36  from scripterng_hooks import MenuHooks 
37  from mikro import create_pyqt_object, Error as MiKroError 
38   
39   
40  # ScripterNG and i18n should be available everywhere globally 
41  import __builtin__ 
42  __builtin__.ScripterNG = create_pyqt_object(qApp).ScripterNG 
43  ScripterNG.qt.setParent(None) 
44   
45  __builtin__.app = ScripterNG # shorter name for lazy people 
46  __builtin__.i18n = lambda s: unicode(QCoreApplication.translate("ScripterNG", s)) 
47  ScripterNG.Error = MiKroError 
48  ScripterNG.path = scripterng_path 
49   
50   
51   
52 -class ScripterNGMenu(QObject):
53 """ 54 ScripterNG menu item in mainWindow menubar 55 """ 56
57 - def __init__(self, parent):
58 QObject.__init__(self, parent) 59 self.setObjectName("Menu") 60 self.popup = QMenu(i18n("ScripterNG")) 61 MenuHooks().insertMenuAfter("E&xtras", self.popup) 62 self._load_entries()
63 64
65 - def _load_entries(self):
66 for path in [scripterng_path, os.path.expanduser("~/.scribus/scripterng/")]: 67 autoload_path = os.path.join(path, "autoload") 68 if not os.path.exists(autoload_path): 69 continue 70 sys.path.insert(0, autoload_path) 71 from scribusscript import load_scripts 72 self.autoload_scripts = scripts = load_scripts(autoload_path) 73 for sd in scripts: 74 try: 75 sd.install() 76 except: 77 excepthook.show_current_error(i18n("Error installing %r") % sd.name)
78 79
80 - def addAction(self, title, callback, *args):
81 self.popup.addAction(title, callback, *args)
82 83
84 - def addSeparator(self):
85 self.popup.addSeparator()
86 87
88 -def createMenu(mainWindow):
89 ScripterNG.menu = ScripterNGMenu(mainWindow)
90 91 ScripterNG.connect("createMenu(QMainWindow*)", createMenu) 92