update hooks to use libanki, warn about plugin errors differently

This commit is contained in:
Damien Elmes 2008-12-10 17:37:03 +09:00
parent 8ea3117d67
commit 881a3c2468
4 changed files with 33 additions and 43 deletions

View file

@ -16,6 +16,7 @@ from ankiqt.ui.utils import saveGeom, restoreGeom, saveSplitter, restoreSplitter
from anki.errors import * from anki.errors import *
from anki.db import * from anki.db import *
from anki.stats import CardStats from anki.stats import CardStats
from anki.hooks import runHook
# Deck editor # Deck editor
########################################################################## ##########################################################################
@ -396,7 +397,7 @@ class EditDeck(QMainWindow):
self.connect(self.dialog.actionSelectFacts, SIGNAL("triggered()"), self.selectFacts) self.connect(self.dialog.actionSelectFacts, SIGNAL("triggered()"), self.selectFacts)
self.connect(self.dialog.actionUndo, SIGNAL("triggered()"), self.onUndo) self.connect(self.dialog.actionUndo, SIGNAL("triggered()"), self.onUndo)
self.connect(self.dialog.actionRedo, SIGNAL("triggered()"), self.onRedo) self.connect(self.dialog.actionRedo, SIGNAL("triggered()"), self.onRedo)
self.parent.runHook('editor.setupMenus', self) runHook('editor.setupMenus', self)
def onClose(self): def onClose(self):
saveSplitter(self.dialog.splitter, "editor") saveSplitter(self.dialog.splitter, "editor")

View file

@ -5,6 +5,7 @@ import sys
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
import ankiqt.forms import ankiqt.forms
from anki.hooks import runHook
# Hideable help area widget # Hideable help area widget
########################################################################## ##########################################################################
@ -33,7 +34,7 @@ class HelpArea(object):
self.helpFrame.hide() self.helpFrame.hide()
self.widget.hide() self.widget.hide()
if self.mainWindow: if self.mainWindow:
self.mainWindow.runHook("helpChanged") runHook("helpChanged")
def showText(self, text, py={}): def showText(self, text, py={}):
self.show() self.show()
@ -42,7 +43,7 @@ class HelpArea(object):
self.handlers = py self.handlers = py
self.flush() self.flush()
if self.mainWindow: if self.mainWindow:
self.mainWindow.runHook("helpChanged") runHook("helpChanged")
def flush(self): def flush(self):
if sys.platform.startswith("darwin"): if sys.platform.startswith("darwin"):

View file

@ -18,6 +18,7 @@ from anki.utils import addTags, deleteTags, parseTags
from anki.media import rebuildMediaDir from anki.media import rebuildMediaDir
from anki.db import OperationalError from anki.db import OperationalError
from anki.stdmodels import BasicModel from anki.stdmodels import BasicModel
from anki.hooks import runHook, addHook, removeHook, _hooks
import anki.latex import anki.latex
import anki.lang import anki.lang
import ankiqt import ankiqt
@ -39,10 +40,7 @@ class AnkiQt(QMainWindow):
self.setLang() self.setLang()
self.setupFonts() self.setupFonts()
self.setupBackupDir() self.setupBackupDir()
self.setupHooks()
self.loadPlugins()
self.setupMainWindow() self.setupMainWindow()
self.rebuildPluginsMenu()
self.alterShortcuts() self.alterShortcuts()
self.setupTray() self.setupTray()
self.connectMenuActions() self.connectMenuActions()
@ -61,15 +59,17 @@ class AnkiQt(QMainWindow):
if not self.maybeLoadLastDeck(args): if not self.maybeLoadLastDeck(args):
self.setEnabled(True) self.setEnabled(True)
self.moveToState("auto") self.moveToState("auto")
# run after-init hook
try:
self.runHook('init')
except:
ui.utils.showWarning(_("Broken plugin:\n\n%s") %
traceback.format_exc())
# check for updates # check for updates
self.setupAutoUpdate() self.setupAutoUpdate()
self.setupErrorHandler() self.setupErrorHandler()
self.loadPlugins()
self.rebuildPluginsMenu()
# run after-init hook
try:
runHook('init')
except:
ui.utils.showWarning(_("Broken plugin:\n\n%s") %
traceback.format_exc())
def setupMainWindow(self): def setupMainWindow(self):
self.mainWin = ankiqt.forms.main.Ui_MainWindow() self.mainWin = ankiqt.forms.main.Ui_MainWindow()
@ -117,9 +117,17 @@ class AnkiQt(QMainWindow):
self.timer.setInterval(interval) self.timer.setInterval(interval)
def onTimeout(self): def onTimeout(self):
stdText = _("""\
An error occurred. Please copy the following into a bug report.\n\n""")
pluginText = _("""\
An error occurred in a plugin. Please contact the plugin author.
Please do not file a bug report with Anki.\n\n""")
print self.pool print self.pool
ui.utils.showText(_("""\ if "plugin" in self.pool:
An error occurred. Please copy the following message into a bug report.\n\n""" + self.pool[0:10000])) txt = pluginText
else:
txt = stdText
ui.utils.showText(txt + self.pool[0:10000])
self.pool = "" self.pool = ""
self.timer = None self.timer = None
pipe = ErrorPipe(self) pipe = ErrorPipe(self)
@ -222,7 +230,7 @@ An error occurred. Please copy the following message into a bug report.\n\n""" +
os.chdir(self.deck.mediaDir()) os.chdir(self.deck.mediaDir())
self.showAnswerButton() self.showAnswerButton()
self.updateMarkAction() self.updateMarkAction()
self.runHook('showQuestion') runHook('showQuestion')
elif state == "showAnswer": elif state == "showAnswer":
self.showEaseButtons() self.showEaseButtons()
self.enableCardMenuItems() self.enableCardMenuItems()
@ -404,29 +412,6 @@ getCard() returns:
text = css + '<span class="interface">' + text + "</span>" text = css + '<span class="interface">' + text + "</span>"
return text return text
# Hooks
##########################################################################
def setupHooks(self):
self.hooks = {}
def addHook(self, hookName, func):
if not self.hooks.get(hookName, None):
self.hooks[hookName] = []
if func not in self.hooks[hookName]:
self.hooks[hookName].append(func)
def removeHook(self, hookName, func):
hook = self.hooks.get(hookName, [])
if func in hook:
hook.remove(func)
def runHook(self, hookName, *args):
hook = self.hooks.get(hookName, None)
if hook:
for func in hook:
func(*args)
# Deck loading & saving: backend # Deck loading & saving: backend
########################################################################## ##########################################################################
@ -761,7 +746,7 @@ Error was:\n%(f1)s\n...\n%(f2)s""") % {'f1': fmt1, 'f2': fmt2})
def prepareForExit(self): def prepareForExit(self):
"Save config and window geometry." "Save config and window geometry."
self.runHook('quit') runHook("quit")
self.help.hide() self.help.hide()
self.config['mainWindowGeom'] = self.saveGeometry() self.config['mainWindowGeom'] = self.saveGeometry()
# save config # save config
@ -925,7 +910,7 @@ Error was:\n%(f1)s\n...\n%(f2)s""") % {'f1': fmt1, 'f2': fmt2})
self.help.showText(txt) self.help.showText(txt)
def onCardStats(self): def onCardStats(self):
self.addHook("showQuestion", self.onCardStats) addHook("showQuestion", self.onCardStats)
txt = "" txt = ""
if self.currentCard: if self.currentCard:
txt += _("<h1>Current card</h1>") txt += _("<h1>Current card</h1>")
@ -939,8 +924,7 @@ Error was:\n%(f1)s\n...\n%(f2)s""") % {'f1': fmt1, 'f2': fmt2})
def removeCardStatsHook(self): def removeCardStatsHook(self):
"Remove the update hook if the help menu was changed." "Remove the update hook if the help menu was changed."
print "rem" removeHook("showQuestion", self.onCardStats)
self.removeHook("showQuestion", self.onCardStats)
def onShowGraph(self): def onShowGraph(self):
self.setStatus(_("Loading graphs (may take time)...")) self.setStatus(_("Loading graphs (may take time)..."))
@ -1669,3 +1653,6 @@ tag or delete references to missing files?"""))
"%(b)d unused files removed.") % { "%(b)d unused files removed.") % {
'a': missing, 'a': missing,
'b': unused}) 'b': unused})
def addHook(self, *args):
addHook(*args)

View file

@ -2,6 +2,7 @@
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html # License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from anki.hooks import addHook
Qt = QtCore.Qt Qt = QtCore.Qt
class AnkiTrayIcon(QtCore.QObject): class AnkiTrayIcon(QtCore.QObject):
@ -21,7 +22,7 @@ class AnkiTrayIcon(QtCore.QObject):
self.ti.setObjectName("trayIcon") self.ti.setObjectName("trayIcon")
if self.ti: if self.ti:
QtGui.QApplication.setQuitOnLastWindowClosed(False) QtGui.QApplication.setQuitOnLastWindowClosed(False)
self.mw.addHook("quit", self.onQuit) addHook("quit", self.onQuit)
self.ti.setIcon(QtGui.QIcon(":/icons/anki.png")) self.ti.setIcon(QtGui.QIcon(":/icons/anki.png"))
self.ti.setToolTip("Anki") self.ti.setToolTip("Anki")
# hook signls, and Anki state changes # hook signls, and Anki state changes