2012-12-21 08:51:59 +01:00
|
|
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2016-08-02 03:51:44 +02:00
|
|
|
import io
|
2012-12-22 01:17:10 +01:00
|
|
|
import sys, os, traceback
|
2016-05-12 06:45:35 +02:00
|
|
|
from io import StringIO
|
2015-01-05 02:47:05 +01:00
|
|
|
import zipfile
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.qt import *
|
2012-12-22 01:17:10 +01:00
|
|
|
from aqt.utils import showInfo, openFolder, isWin, openLink, \
|
2015-01-05 02:47:05 +01:00
|
|
|
askUser, restoreGeom, saveGeom, showWarning
|
2012-12-21 08:51:59 +01:00
|
|
|
from zipfile import ZipFile
|
|
|
|
import aqt.forms
|
|
|
|
import aqt
|
|
|
|
from aqt.downloader import download
|
2015-01-05 02:47:05 +01:00
|
|
|
from anki.lang import _
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# in the future, it would be nice to save the addon id and unzippped file list
|
|
|
|
# to the config so that we can clear up all files and check for updates
|
|
|
|
|
2017-02-06 23:21:33 +01:00
|
|
|
class AddonManager:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def __init__(self, mw):
|
|
|
|
self.mw = mw
|
2016-05-31 10:51:40 +02:00
|
|
|
f = self.mw.form
|
|
|
|
f.actionOpenPluginFolder.triggered.connect(self.onOpenAddonFolder)
|
|
|
|
f.actionDownloadSharedPlugin.triggered.connect(self.onGetAddons)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._menus = []
|
|
|
|
if isWin:
|
|
|
|
self.clearAddonCache()
|
|
|
|
sys.path.insert(0, self.addonsFolder())
|
2013-05-17 08:32:17 +02:00
|
|
|
if not self.mw.safeMode:
|
|
|
|
self.loadAddons()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def files(self):
|
|
|
|
return [f for f in os.listdir(self.addonsFolder())
|
|
|
|
if f.endswith(".py")]
|
|
|
|
|
2015-09-27 00:55:15 +02:00
|
|
|
def directories(self):
|
|
|
|
return [d for d in os.listdir(self.addonsFolder())
|
2016-07-08 05:10:19 +02:00
|
|
|
if not d.startswith('.') and
|
|
|
|
not d == "__pycache__" and
|
|
|
|
os.path.isdir(os.path.join(self.addonsFolder(), d))]
|
2015-09-27 00:55:15 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def loadAddons(self):
|
|
|
|
for file in self.files():
|
|
|
|
try:
|
|
|
|
__import__(file.replace(".py", ""))
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2015-09-27 00:55:15 +02:00
|
|
|
for directory in self.directories():
|
|
|
|
try:
|
|
|
|
__import__(directory)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.rebuildAddonsMenu()
|
|
|
|
|
|
|
|
# Menus
|
|
|
|
######################################################################
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
def onOpenAddonFolder(self, checked, path=None):
|
2012-12-21 08:51:59 +01:00
|
|
|
if path is None:
|
|
|
|
path = self.addonsFolder()
|
|
|
|
openFolder(path)
|
|
|
|
|
|
|
|
def rebuildAddonsMenu(self):
|
|
|
|
for m in self._menus:
|
|
|
|
self.mw.form.menuPlugins.removeAction(m.menuAction())
|
|
|
|
for file in self.files():
|
|
|
|
m = self.mw.form.menuPlugins.addMenu(
|
|
|
|
os.path.splitext(file)[0])
|
|
|
|
self._menus.append(m)
|
|
|
|
p = os.path.join(self.addonsFolder(), file)
|
2016-07-08 05:10:19 +02:00
|
|
|
a = QAction(_("Edit..."), self.mw, triggered=lambda x, y=p: self.onEdit(y))
|
2012-12-21 08:51:59 +01:00
|
|
|
m.addAction(a)
|
2016-07-08 05:10:19 +02:00
|
|
|
a = QAction(_("Delete..."), self.mw, triggered=lambda x, y=p: self.onRem(y))
|
2012-12-21 08:51:59 +01:00
|
|
|
m.addAction(a)
|
|
|
|
|
|
|
|
def onEdit(self, path):
|
|
|
|
d = QDialog(self.mw)
|
|
|
|
frm = aqt.forms.editaddon.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
|
|
|
d.setWindowTitle(os.path.basename(path))
|
2016-05-12 06:45:35 +02:00
|
|
|
frm.text.setPlainText(open(path).read())
|
2016-05-31 10:51:40 +02:00
|
|
|
frm.buttonBox.accepted.connect(lambda: self.onAcceptEdit(path, frm))
|
2012-12-21 08:51:59 +01:00
|
|
|
d.exec_()
|
|
|
|
|
|
|
|
def onAcceptEdit(self, path, frm):
|
2016-07-08 05:10:19 +02:00
|
|
|
open(path, "wb").write(frm.text.toPlainText().encode("utf8"))
|
2012-12-21 08:51:59 +01:00
|
|
|
showInfo(_("Edits saved. Please restart Anki."))
|
|
|
|
|
|
|
|
def onRem(self, path):
|
|
|
|
if not askUser(_("Delete %s?") % os.path.basename(path)):
|
|
|
|
return
|
|
|
|
os.unlink(path)
|
|
|
|
self.rebuildAddonsMenu()
|
|
|
|
showInfo(_("Deleted. Please restart Anki."))
|
|
|
|
|
|
|
|
# Tools
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def addonsFolder(self):
|
|
|
|
dir = self.mw.pm.addonFolder()
|
|
|
|
return dir
|
|
|
|
|
|
|
|
def clearAddonCache(self):
|
|
|
|
"Clear .pyc files which may cause crashes if Python version updated."
|
|
|
|
dir = self.addonsFolder()
|
|
|
|
for curdir, dirs, files in os.walk(dir):
|
|
|
|
for f in files:
|
|
|
|
if not f.endswith(".pyc"):
|
|
|
|
continue
|
|
|
|
os.unlink(os.path.join(curdir, f))
|
|
|
|
|
|
|
|
def registerAddon(self, name, updateId):
|
|
|
|
# not currently used
|
|
|
|
return
|
|
|
|
|
|
|
|
# Installing add-ons
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def onGetAddons(self):
|
2016-07-08 05:17:33 +02:00
|
|
|
showInfo("""\
|
|
|
|
Most add-ons built for Anki 2.0.x will not work on this version of Anki \
|
|
|
|
until they are updated. To avoid errors during startup, please only \
|
|
|
|
download add-ons that say they support Anki 2.1.x in the description.""")
|
|
|
|
GetAddons(self.mw)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def install(self, data, fname):
|
|
|
|
if fname.endswith(".py"):
|
|
|
|
# .py files go directly into the addon folder
|
|
|
|
path = os.path.join(self.addonsFolder(), fname)
|
2014-08-09 06:28:48 +02:00
|
|
|
open(path, "wb").write(data)
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
# .zip file
|
2015-01-05 02:47:05 +01:00
|
|
|
try:
|
2016-08-02 03:51:44 +02:00
|
|
|
z = ZipFile(io.BytesIO(data))
|
2016-02-18 09:49:44 +01:00
|
|
|
except zipfile.BadZipfile:
|
2015-01-05 02:47:05 +01:00
|
|
|
showWarning(_("The download was corrupt. Please try again."))
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
base = self.addonsFolder()
|
|
|
|
for n in z.namelist():
|
|
|
|
if n.endswith("/"):
|
|
|
|
# folder; ignore
|
|
|
|
continue
|
|
|
|
# write
|
|
|
|
z.extract(n, base)
|
|
|
|
|
|
|
|
class GetAddons(QDialog):
|
|
|
|
|
|
|
|
def __init__(self, mw):
|
|
|
|
QDialog.__init__(self, mw)
|
|
|
|
self.mw = mw
|
|
|
|
self.form = aqt.forms.getaddons.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
b = self.form.buttonBox.addButton(
|
|
|
|
_("Browse"), QDialogButtonBox.ActionRole)
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(self.onBrowse)
|
2014-06-18 20:47:45 +02:00
|
|
|
restoreGeom(self, "getaddons", adjustSize=True)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.exec_()
|
2014-06-18 20:47:45 +02:00
|
|
|
saveGeom(self, "getaddons")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onBrowse(self):
|
|
|
|
openLink(aqt.appShared + "addons/")
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
QDialog.accept(self)
|
2017-02-15 06:55:20 +01:00
|
|
|
|
|
|
|
# get codes
|
|
|
|
try:
|
|
|
|
ids = [int(n) for n in self.form.code.text().split()]
|
|
|
|
except ValueError:
|
|
|
|
showWarning(_("Invalid code."))
|
|
|
|
return
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
self.mw.progress.start(immediate=True)
|
|
|
|
for n in ids:
|
|
|
|
ret = download(self.mw, n)
|
|
|
|
if ret[0] == "error":
|
|
|
|
errors.append(_("Error downloading %(id)s: %(error)s") % dict(id=n, error=ret[1]))
|
|
|
|
continue
|
2017-02-14 05:48:46 +01:00
|
|
|
data, fname = ret
|
|
|
|
self.mw.addonManager.install(data, fname)
|
2017-02-15 06:55:20 +01:00
|
|
|
self.mw.progress.finish()
|
|
|
|
if not errors:
|
|
|
|
tooltip(_("Download successful. Please restart Anki."), period=3000)
|
|
|
|
else:
|
|
|
|
showWarning("\n".join(errors))
|