2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2020-01-19 01:37:15 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-08-02 03:51:44 +02:00
|
|
|
import io
|
2017-08-26 07:14:20 +02:00
|
|
|
import json
|
2020-01-03 16:32:20 +01:00
|
|
|
import os
|
2017-08-26 07:14:20 +02:00
|
|
|
import re
|
2015-01-05 02:47:05 +01:00
|
|
|
import zipfile
|
2019-02-22 21:14:42 +01:00
|
|
|
from collections import defaultdict
|
2020-01-19 01:37:15 +01:00
|
|
|
from concurrent.futures import Future
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import IO, Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
|
2020-01-24 08:25:52 +01:00
|
|
|
from urllib.parse import parse_qs, urlparse
|
2019-12-20 10:19:03 +01:00
|
|
|
from zipfile import ZipFile
|
2019-12-15 23:51:38 +01:00
|
|
|
|
2019-04-24 21:44:11 +02:00
|
|
|
import jsonschema
|
2019-12-20 10:19:03 +01:00
|
|
|
import markdown
|
2019-04-24 21:44:11 +02:00
|
|
|
from jsonschema.exceptions import ValidationError
|
2019-12-20 10:19:03 +01:00
|
|
|
from send2trash import send2trash
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 04:37:55 +01:00
|
|
|
import anki
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2019-12-20 10:19:03 +01:00
|
|
|
import aqt.forms
|
2020-01-19 02:33:27 +01:00
|
|
|
from anki.httpclient import HttpClient
|
2020-11-18 02:53:33 +01:00
|
|
|
from anki.lang import without_unicode_isolation
|
2020-03-02 00:54:58 +01:00
|
|
|
from aqt import gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import (
|
2020-02-27 11:32:57 +01:00
|
|
|
TR,
|
2019-12-23 01:34:10 +01:00
|
|
|
askUser,
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button,
|
2019-12-23 01:34:10 +01:00
|
|
|
getFile,
|
|
|
|
isWin,
|
|
|
|
openFolder,
|
|
|
|
openLink,
|
|
|
|
restoreGeom,
|
|
|
|
restoreSplitter,
|
|
|
|
saveGeom,
|
|
|
|
saveSplitter,
|
|
|
|
showInfo,
|
|
|
|
showWarning,
|
|
|
|
tooltip,
|
2020-02-27 11:32:57 +01:00
|
|
|
tr,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
@dataclass
|
|
|
|
class InstallOk:
|
|
|
|
name: str
|
|
|
|
conflicts: List[str]
|
2020-01-24 08:25:52 +01:00
|
|
|
compatible: bool
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class InstallError:
|
|
|
|
errmsg: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DownloadOk:
|
|
|
|
data: bytes
|
|
|
|
filename: str
|
2020-01-27 08:01:09 +01:00
|
|
|
mod_time: int
|
|
|
|
min_point_version: int
|
|
|
|
max_point_version: int
|
|
|
|
branch_index: int
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DownloadError:
|
|
|
|
# set if result was not 200
|
|
|
|
status_code: Optional[int] = None
|
|
|
|
# set if an exception occurred
|
|
|
|
exception: Optional[Exception] = None
|
2020-01-03 16:32:20 +01:00
|
|
|
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
# first arg is add-on id
|
|
|
|
DownloadLogEntry = Tuple[int, Union[DownloadError, InstallError, InstallOk]]
|
|
|
|
|
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
@dataclass
|
|
|
|
class UpdateInfo:
|
|
|
|
id: int
|
2020-01-27 08:01:09 +01:00
|
|
|
suitable_branch_last_modified: int
|
|
|
|
current_branch_last_modified: int
|
|
|
|
current_branch_min_point_ver: int
|
|
|
|
current_branch_max_point_ver: int
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
ANKIWEB_ID_RE = re.compile(r"^\d+$")
|
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
current_point_version = anki.utils.pointVersion()
|
2020-01-19 04:37:55 +01:00
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
@dataclass
|
|
|
|
class AddonMeta:
|
|
|
|
dir_name: str
|
2020-01-19 04:06:53 +01:00
|
|
|
provided_name: Optional[str]
|
2020-01-19 03:44:53 +01:00
|
|
|
enabled: bool
|
|
|
|
installed_at: int
|
|
|
|
conflicts: List[str]
|
2020-01-27 08:01:09 +01:00
|
|
|
min_point_version: int
|
|
|
|
max_point_version: int
|
|
|
|
branch_index: int
|
2020-02-03 02:14:55 +01:00
|
|
|
human_version: Optional[str]
|
2020-01-19 03:44:53 +01:00
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
def human_name(self) -> str:
|
|
|
|
return self.provided_name or self.dir_name
|
2020-01-19 03:44:53 +01:00
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
def ankiweb_id(self) -> Optional[int]:
|
|
|
|
m = ANKIWEB_ID_RE.match(self.dir_name)
|
|
|
|
if m:
|
|
|
|
return int(m.group(0))
|
|
|
|
else:
|
|
|
|
return None
|
2020-01-19 03:44:53 +01:00
|
|
|
|
2020-01-19 04:37:55 +01:00
|
|
|
def compatible(self) -> bool:
|
2020-01-24 08:25:52 +01:00
|
|
|
min = self.min_point_version
|
|
|
|
if min is not None and current_point_version < min:
|
|
|
|
return False
|
|
|
|
max = self.max_point_version
|
2020-01-27 07:58:12 +01:00
|
|
|
if max is not None and max < 0 and current_point_version > abs(max):
|
2020-01-24 08:25:52 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-04-15 10:47:04 +02:00
|
|
|
def is_latest(self, server_update_time: int) -> bool:
|
|
|
|
return self.installed_at >= server_update_time
|
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
@staticmethod
|
|
|
|
def from_json_meta(dir_name: str, json_meta: Dict[str, Any]) -> AddonMeta:
|
|
|
|
return AddonMeta(
|
|
|
|
dir_name=dir_name,
|
|
|
|
provided_name=json_meta.get("name"),
|
|
|
|
enabled=not json_meta.get("disabled"),
|
|
|
|
installed_at=json_meta.get("mod", 0),
|
|
|
|
conflicts=json_meta.get("conflicts", []),
|
2020-01-27 08:01:09 +01:00
|
|
|
min_point_version=json_meta.get("min_point_version", 0) or 0,
|
|
|
|
max_point_version=json_meta.get("max_point_version", 0) or 0,
|
|
|
|
branch_index=json_meta.get("branch_index", 0) or 0,
|
2020-02-04 08:03:21 +01:00
|
|
|
human_version=json_meta.get("human_version"),
|
2020-01-24 08:25:52 +01:00
|
|
|
)
|
2020-01-19 03:44:53 +01:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
# fixme: this class should not have any GUI code in it
|
2017-02-06 23:21:33 +01:00
|
|
|
class AddonManager:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-03 18:23:28 +01:00
|
|
|
ext: str = ".ankiaddon"
|
|
|
|
_manifest_schema: dict = {
|
2019-04-24 21:44:11 +02:00
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2020-01-27 08:01:09 +01:00
|
|
|
# the name of the folder
|
2019-04-24 21:44:11 +02:00
|
|
|
"package": {"type": "string", "meta": False},
|
2020-01-27 08:01:09 +01:00
|
|
|
# the displayed name to the user
|
2019-04-24 21:44:11 +02:00
|
|
|
"name": {"type": "string", "meta": True},
|
2020-01-27 08:01:09 +01:00
|
|
|
# the time the add-on was last modified
|
2019-04-24 21:44:11 +02:00
|
|
|
"mod": {"type": "number", "meta": True},
|
2020-01-27 08:01:09 +01:00
|
|
|
# a list of other packages that conflict
|
2019-12-23 01:34:10 +01:00
|
|
|
"conflicts": {"type": "array", "items": {"type": "string"}, "meta": True},
|
2020-01-27 08:01:09 +01:00
|
|
|
# the minimum 2.1.x version this add-on supports
|
2020-01-24 08:25:52 +01:00
|
|
|
"min_point_version": {"type": "number", "meta": True},
|
2020-01-27 08:01:09 +01:00
|
|
|
# if negative, abs(n) is the maximum 2.1.x version this add-on supports
|
|
|
|
# if positive, indicates version tested on, and is ignored
|
2020-01-24 08:25:52 +01:00
|
|
|
"max_point_version": {"type": "number", "meta": True},
|
2020-01-27 08:01:09 +01:00
|
|
|
# AnkiWeb sends this to indicate which branch the user downloaded.
|
|
|
|
"branch_index": {"type": "number", "meta": True},
|
2020-02-03 02:14:55 +01:00
|
|
|
# version string set by the add-on creator
|
|
|
|
"human_version": {"type": "string", "meta": True},
|
2019-04-24 21:44:11 +02:00
|
|
|
},
|
2019-12-23 01:34:10 +01:00
|
|
|
"required": ["package", "name"],
|
2019-02-22 17:04:07 +01:00
|
|
|
}
|
2019-02-22 10:17:56 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def __init__(self, mw: aqt.main.AnkiQt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
2017-08-26 07:14:20 +02:00
|
|
|
self.dirty = False
|
2016-05-31 10:51:40 +02:00
|
|
|
f = self.mw.form
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionAdd_ons.triggered, self.onAddonsDialog)
|
2012-12-21 08:51:59 +01:00
|
|
|
sys.path.insert(0, self.addonsFolder())
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
# in new code, you may want all_addon_meta() instead
|
2020-08-01 09:50:37 +02:00
|
|
|
def allAddons(self) -> List[str]:
|
2017-08-26 07:14:20 +02:00
|
|
|
l = []
|
|
|
|
for d in os.listdir(self.addonsFolder()):
|
|
|
|
path = self.addonsFolder(d)
|
|
|
|
if not os.path.exists(os.path.join(path, "__init__.py")):
|
|
|
|
continue
|
|
|
|
l.append(d)
|
2018-02-24 03:23:15 +01:00
|
|
|
l.sort()
|
|
|
|
if os.getenv("ANKIREVADDONS", ""):
|
2020-08-01 09:50:37 +02:00
|
|
|
l = list(reversed(l))
|
|
|
|
return l
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def all_addon_meta(self) -> Iterable[AddonMeta]:
|
|
|
|
return map(self.addon_meta, self.allAddons())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def addonsFolder(self, dir: Optional[str] = None) -> str:
|
2017-08-26 07:14:20 +02:00
|
|
|
root = self.mw.pm.addonFolder()
|
|
|
|
if not dir:
|
|
|
|
return root
|
|
|
|
return os.path.join(root, dir)
|
2015-09-27 00:55:15 +02:00
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def loadAddons(self) -> None:
|
|
|
|
for addon in self.all_addon_meta():
|
|
|
|
if not addon.enabled:
|
2017-08-26 07:14:20 +02:00
|
|
|
continue
|
2020-01-19 04:37:55 +01:00
|
|
|
if not addon.compatible():
|
|
|
|
continue
|
2017-08-26 07:14:20 +02:00
|
|
|
self.dirty = True
|
2015-09-27 00:55:15 +02:00
|
|
|
try:
|
2020-01-19 03:44:53 +01:00
|
|
|
__import__(addon.dir_name)
|
2015-09-27 00:55:15 +02:00
|
|
|
except:
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
2020-02-27 11:32:57 +01:00
|
|
|
tr(
|
|
|
|
TR.ADDONS_FAILED_TO_LOAD,
|
|
|
|
name=addon.human_name(),
|
|
|
|
traceback=traceback.format_exc(),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onAddonsDialog(self) -> None:
|
2020-12-26 18:07:37 +01:00
|
|
|
aqt.dialogs.open("AddonsDialog", self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-26 07:14:20 +02:00
|
|
|
# Metadata
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def addon_meta(self, dir_name: str) -> AddonMeta:
|
|
|
|
"""Get info about an installed add-on."""
|
|
|
|
json_obj = self.addonMeta(dir_name)
|
2020-01-24 08:25:52 +01:00
|
|
|
return AddonMeta.from_json_meta(dir_name, json_obj)
|
2020-01-19 03:44:53 +01:00
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
def write_addon_meta(self, addon: AddonMeta) -> None:
|
|
|
|
# preserve any unknown attributes
|
|
|
|
json_obj = self.addonMeta(addon.dir_name)
|
|
|
|
|
|
|
|
if addon.provided_name is not None:
|
|
|
|
json_obj["name"] = addon.provided_name
|
|
|
|
json_obj["disabled"] = not addon.enabled
|
|
|
|
json_obj["mod"] = addon.installed_at
|
|
|
|
json_obj["conflicts"] = addon.conflicts
|
|
|
|
json_obj["max_point_version"] = addon.max_point_version
|
2020-01-24 08:25:52 +01:00
|
|
|
json_obj["min_point_version"] = addon.min_point_version
|
2020-01-27 08:01:09 +01:00
|
|
|
json_obj["branch_index"] = addon.branch_index
|
2020-02-03 02:14:55 +01:00
|
|
|
if addon.human_version is not None:
|
|
|
|
json_obj["human_version"] = addon.human_version
|
2020-01-19 04:06:53 +01:00
|
|
|
|
|
|
|
self.writeAddonMeta(addon.dir_name, json_obj)
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _addonMetaPath(self, dir: str) -> str:
|
2017-08-26 07:14:20 +02:00
|
|
|
return os.path.join(self.addonsFolder(dir), "meta.json")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
# in new code, use self.addon_meta() instead
|
2020-01-19 01:37:15 +01:00
|
|
|
def addonMeta(self, dir: str) -> Dict[str, Any]:
|
2017-08-26 07:14:20 +02:00
|
|
|
path = self._addonMetaPath(dir)
|
|
|
|
try:
|
2017-12-11 08:25:51 +01:00
|
|
|
with open(path, encoding="utf8") as f:
|
|
|
|
return json.load(f)
|
2020-03-04 15:29:48 +01:00
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
print(f"json error in add-on {dir}:\n{e}")
|
2017-08-26 07:14:20 +02:00
|
|
|
return dict()
|
2020-03-05 00:24:26 +01:00
|
|
|
except:
|
|
|
|
# missing meta file, etc
|
|
|
|
return dict()
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
# in new code, use write_addon_meta() instead
|
2020-08-01 07:50:27 +02:00
|
|
|
def writeAddonMeta(self, dir: str, meta: Dict[str, Any]) -> None:
|
2017-08-26 07:14:20 +02:00
|
|
|
path = self._addonMetaPath(dir)
|
2017-12-11 08:25:51 +01:00
|
|
|
with open(path, "w", encoding="utf8") as f:
|
2018-12-15 00:13:10 +01:00
|
|
|
json.dump(meta, f)
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
def toggleEnabled(self, dir: str, enable: Optional[bool] = None) -> None:
|
|
|
|
addon = self.addon_meta(dir)
|
|
|
|
should_enable = enable if enable is not None else not addon.enabled
|
|
|
|
if should_enable is True:
|
2019-04-16 09:43:02 +02:00
|
|
|
conflicting = self._disableConflicting(dir)
|
|
|
|
if conflicting:
|
|
|
|
addons = ", ".join(self.addonName(f) for f in conflicting)
|
|
|
|
showInfo(
|
2020-11-22 05:57:53 +01:00
|
|
|
tr(
|
|
|
|
TR.ADDONS_THE_FOLLOWING_ADDONS_ARE_INCOMPATIBLE_WITH,
|
|
|
|
name=addon.human_name(),
|
|
|
|
found=addons,
|
|
|
|
),
|
2019-12-23 01:34:10 +01:00
|
|
|
textFormat="plain",
|
|
|
|
)
|
|
|
|
|
2020-01-19 04:06:53 +01:00
|
|
|
addon.enabled = should_enable
|
|
|
|
self.write_addon_meta(addon)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-19 05:04:57 +01:00
|
|
|
def ankiweb_addons(self) -> List[int]:
|
2020-01-19 01:37:15 +01:00
|
|
|
ids = []
|
2020-01-19 03:44:53 +01:00
|
|
|
for meta in self.all_addon_meta():
|
2020-01-19 05:04:57 +01:00
|
|
|
if meta.ankiweb_id() is not None:
|
2020-01-19 04:06:53 +01:00
|
|
|
ids.append(meta.ankiweb_id())
|
2020-01-19 01:37:15 +01:00
|
|
|
return ids
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
# Legacy helpers
|
2019-02-22 21:14:42 +01:00
|
|
|
######################################################################
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def isEnabled(self, dir: str) -> bool:
|
|
|
|
return self.addon_meta(dir).enabled
|
|
|
|
|
|
|
|
def addonName(self, dir: str) -> str:
|
2020-01-19 04:06:53 +01:00
|
|
|
return self.addon_meta(dir).human_name()
|
2020-01-19 03:44:53 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def addonConflicts(self, dir: str) -> List[str]:
|
2020-01-19 03:44:53 +01:00
|
|
|
return self.addon_meta(dir).conflicts
|
|
|
|
|
|
|
|
def annotatedName(self, dir: str) -> str:
|
|
|
|
meta = self.addon_meta(dir)
|
2020-01-19 04:06:53 +01:00
|
|
|
name = meta.human_name()
|
2020-01-19 03:44:53 +01:00
|
|
|
if not meta.enabled:
|
2020-11-22 05:57:53 +01:00
|
|
|
name += " " + tr(TR.ADDONS_DISABLED)
|
2020-01-19 03:44:53 +01:00
|
|
|
return name
|
2019-02-22 21:14:42 +01:00
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
# Conflict resolution
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def allAddonConflicts(self) -> Dict[str, List[str]]:
|
|
|
|
all_conflicts: Dict[str, List[str]] = defaultdict(list)
|
|
|
|
for addon in self.all_addon_meta():
|
|
|
|
if not addon.enabled:
|
2019-02-22 21:14:42 +01:00
|
|
|
continue
|
2020-01-19 03:44:53 +01:00
|
|
|
for other_dir in addon.conflicts:
|
|
|
|
all_conflicts[other_dir].append(addon.dir_name)
|
2019-02-22 21:14:42 +01:00
|
|
|
return all_conflicts
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _disableConflicting(self, dir: str, conflicts: List[str] = None) -> List[str]:
|
2019-02-22 21:14:42 +01:00
|
|
|
conflicts = conflicts or self.addonConflicts(dir)
|
|
|
|
|
|
|
|
installed = self.allAddons()
|
|
|
|
found = [d for d in conflicts if d in installed and self.isEnabled(d)]
|
|
|
|
found.extend(self.allAddonConflicts().get(dir, []))
|
|
|
|
if not found:
|
2019-04-16 09:43:02 +02:00
|
|
|
return []
|
|
|
|
|
2019-02-22 21:14:42 +01:00
|
|
|
for package in found:
|
|
|
|
self.toggleEnabled(package, enable=False)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2019-04-16 09:43:02 +02:00
|
|
|
return found
|
2019-02-22 21:14:42 +01:00
|
|
|
|
2017-08-26 07:14:20 +02:00
|
|
|
# Installing and deleting add-ons
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def readManifestFile(self, zfile: ZipFile) -> Dict[Any, Any]:
|
2015-01-05 02:47:05 +01:00
|
|
|
try:
|
2019-02-22 17:04:07 +01:00
|
|
|
with zfile.open("manifest.json") as f:
|
|
|
|
data = json.loads(f.read())
|
2019-04-24 21:44:11 +02:00
|
|
|
jsonschema.validate(data, self._manifest_schema)
|
|
|
|
# build new manifest from recognized keys
|
|
|
|
schema = self._manifest_schema["properties"]
|
|
|
|
manifest = {key: data[key] for key in data.keys() & schema.keys()}
|
|
|
|
except (KeyError, json.decoder.JSONDecodeError, ValidationError):
|
2019-02-22 17:04:07 +01:00
|
|
|
# raised for missing manifest, invalid json, missing/invalid keys
|
|
|
|
return {}
|
|
|
|
return manifest
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-03 17:57:33 +01:00
|
|
|
def install(
|
2020-08-01 07:50:27 +02:00
|
|
|
self, file: Union[IO, str], manifest: Dict[str, Any] = None
|
2020-01-19 01:37:15 +01:00
|
|
|
) -> Union[InstallOk, InstallError]:
|
2019-02-22 17:04:07 +01:00
|
|
|
"""Install add-on from path or file-like object. Metadata is read
|
2019-04-16 09:44:00 +02:00
|
|
|
from the manifest file, with keys overriden by supplying a 'manifest'
|
|
|
|
dictionary"""
|
2019-02-18 07:17:14 +01:00
|
|
|
try:
|
2019-02-22 17:04:07 +01:00
|
|
|
zfile = ZipFile(file)
|
2019-02-18 07:17:14 +01:00
|
|
|
except zipfile.BadZipfile:
|
2020-01-19 01:37:15 +01:00
|
|
|
return InstallError(errmsg="zip")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2019-02-22 17:04:07 +01:00
|
|
|
with zfile:
|
2019-04-24 21:44:11 +02:00
|
|
|
file_manifest = self.readManifestFile(zfile)
|
2019-04-16 09:44:00 +02:00
|
|
|
if manifest:
|
|
|
|
file_manifest.update(manifest)
|
|
|
|
manifest = file_manifest
|
2019-02-22 17:04:07 +01:00
|
|
|
if not manifest:
|
2020-01-19 01:37:15 +01:00
|
|
|
return InstallError(errmsg="manifest")
|
2019-02-22 17:04:07 +01:00
|
|
|
package = manifest["package"]
|
2019-02-22 21:14:42 +01:00
|
|
|
conflicts = manifest.get("conflicts", [])
|
2019-12-23 01:34:10 +01:00
|
|
|
found_conflicts = self._disableConflicting(package, conflicts)
|
2019-02-22 17:04:07 +01:00
|
|
|
meta = self.addonMeta(package)
|
|
|
|
self._install(package, zfile)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2019-04-24 21:44:11 +02:00
|
|
|
schema = self._manifest_schema["properties"]
|
2019-12-23 01:34:10 +01:00
|
|
|
manifest_meta = {
|
|
|
|
k: v for k, v in manifest.items() if k in schema and schema[k]["meta"]
|
|
|
|
}
|
2019-02-22 17:04:07 +01:00
|
|
|
meta.update(manifest_meta)
|
|
|
|
self.writeAddonMeta(package, meta)
|
2019-02-18 07:17:14 +01:00
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
meta2 = self.addon_meta(package)
|
|
|
|
|
|
|
|
return InstallOk(
|
|
|
|
name=meta["name"], conflicts=found_conflicts, compatible=meta2.compatible()
|
|
|
|
)
|
2019-02-18 07:17:14 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _install(self, dir: str, zfile: ZipFile) -> None:
|
2017-08-28 12:51:43 +02:00
|
|
|
# previously installed?
|
2019-02-18 07:17:14 +01:00
|
|
|
base = self.addonsFolder(dir)
|
2017-08-26 07:14:20 +02:00
|
|
|
if os.path.exists(base):
|
2019-02-18 07:17:14 +01:00
|
|
|
self.backupUserFiles(dir)
|
2019-04-29 10:43:10 +02:00
|
|
|
if not self.deleteAddon(dir):
|
|
|
|
self.restoreUserFiles(dir)
|
|
|
|
return
|
2017-08-26 07:14:20 +02:00
|
|
|
|
|
|
|
os.mkdir(base)
|
2019-02-18 07:17:14 +01:00
|
|
|
self.restoreUserFiles(dir)
|
2017-09-10 10:53:47 +02:00
|
|
|
|
|
|
|
# extract
|
2019-02-18 07:17:14 +01:00
|
|
|
for n in zfile.namelist():
|
2012-12-21 08:51:59 +01:00
|
|
|
if n.endswith("/"):
|
|
|
|
# folder; ignore
|
|
|
|
continue
|
2017-09-10 10:53:47 +02:00
|
|
|
|
|
|
|
path = os.path.join(base, n)
|
|
|
|
# skip existing user files
|
|
|
|
if os.path.exists(path) and n.startswith("user_files/"):
|
|
|
|
continue
|
2019-02-18 07:17:14 +01:00
|
|
|
zfile.extract(n, base)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-04-29 10:43:10 +02:00
|
|
|
# true on success
|
2020-08-01 07:50:27 +02:00
|
|
|
def deleteAddon(self, dir: str) -> bool:
|
2019-04-29 10:43:10 +02:00
|
|
|
try:
|
|
|
|
send2trash(self.addonsFolder(dir))
|
|
|
|
return True
|
|
|
|
except OSError as e:
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
2020-11-17 12:47:47 +01:00
|
|
|
tr(TR.ADDONS_UNABLE_TO_UPDATE_OR_DELETE_ADDON, val=str(e)),
|
2019-12-23 01:34:10 +01:00
|
|
|
textFormat="plain",
|
|
|
|
)
|
2019-04-29 10:43:10 +02:00
|
|
|
return False
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2019-02-18 07:17:14 +01:00
|
|
|
# Processing local add-on files
|
|
|
|
######################################################################
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-01-03 17:57:33 +01:00
|
|
|
def processPackages(
|
|
|
|
self, paths: List[str], parent: QWidget = None
|
|
|
|
) -> Tuple[List[str], List[str]]:
|
|
|
|
|
2019-02-18 07:17:14 +01:00
|
|
|
log = []
|
|
|
|
errs = []
|
2020-01-03 16:32:20 +01:00
|
|
|
|
2020-05-31 03:24:33 +02:00
|
|
|
self.mw.progress.start(parent=parent)
|
2019-02-27 05:08:20 +01:00
|
|
|
try:
|
|
|
|
for path in paths:
|
|
|
|
base = os.path.basename(path)
|
2020-01-03 16:32:20 +01:00
|
|
|
result = self.install(path)
|
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
if isinstance(result, InstallError):
|
2020-01-03 16:32:20 +01:00
|
|
|
errs.extend(
|
|
|
|
self._installationErrorReport(result, base, mode="local")
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2019-02-27 05:08:20 +01:00
|
|
|
else:
|
2020-01-03 16:32:20 +01:00
|
|
|
log.extend(
|
|
|
|
self._installationSuccessReport(result, base, mode="local")
|
|
|
|
)
|
2019-02-27 05:08:20 +01:00
|
|
|
finally:
|
|
|
|
self.mw.progress.finish()
|
2020-01-03 16:32:20 +01:00
|
|
|
|
2019-02-18 07:17:14 +01:00
|
|
|
return log, errs
|
|
|
|
|
2020-01-03 16:32:20 +01:00
|
|
|
# Installation messaging
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _installationErrorReport(
|
2020-01-19 01:37:15 +01:00
|
|
|
self, result: InstallError, base: str, mode: str = "download"
|
2020-01-03 16:32:20 +01:00
|
|
|
) -> List[str]:
|
|
|
|
|
|
|
|
messages = {
|
2020-11-17 08:42:43 +01:00
|
|
|
"zip": tr(TR.ADDONS_CORRUPT_ADDON_FILE),
|
|
|
|
"manifest": tr(TR.ADDONS_INVALID_ADDON_MANIFEST),
|
2020-01-03 16:32:20 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 14:24:19 +01:00
|
|
|
msg = messages.get(
|
|
|
|
result.errmsg, tr(TR.ADDONS_UNKNOWN_ERROR, val=result.errmsg)
|
|
|
|
)
|
2020-01-03 16:32:20 +01:00
|
|
|
|
2020-11-22 05:57:53 +01:00
|
|
|
if mode == "download":
|
|
|
|
template = tr(TR.ADDONS_ERROR_DOWNLOADING_IDS_ERRORS, id=base, error=msg)
|
2020-01-03 16:32:20 +01:00
|
|
|
else:
|
2020-11-22 05:57:53 +01:00
|
|
|
template = tr(TR.ADDONS_ERROR_INSTALLING_BASES_ERRORS, base=base, error=msg)
|
2020-01-03 16:32:20 +01:00
|
|
|
|
2020-11-22 05:57:53 +01:00
|
|
|
return [template]
|
2020-01-03 16:32:20 +01:00
|
|
|
|
|
|
|
def _installationSuccessReport(
|
2020-01-19 01:37:15 +01:00
|
|
|
self, result: InstallOk, base: str, mode: str = "download"
|
2020-01-03 16:32:20 +01:00
|
|
|
) -> List[str]:
|
|
|
|
|
2020-11-22 05:57:53 +01:00
|
|
|
name = result.name or base
|
|
|
|
if mode == "download":
|
|
|
|
template = tr(TR.ADDONS_DOWNLOADED_FNAMES, fname=name)
|
2020-01-03 16:32:20 +01:00
|
|
|
else:
|
2020-11-22 05:57:53 +01:00
|
|
|
template = tr(TR.ADDONS_INSTALLED_NAMES, name=name)
|
2020-01-03 16:32:20 +01:00
|
|
|
|
2020-11-22 05:57:53 +01:00
|
|
|
strings = [template]
|
2020-01-03 16:32:20 +01:00
|
|
|
|
|
|
|
if result.conflicts:
|
|
|
|
strings.append(
|
2020-11-17 08:42:43 +01:00
|
|
|
tr(TR.ADDONS_THE_FOLLOWING_CONFLICTING_ADDONS_WERE_DISABLED)
|
2020-01-03 16:32:20 +01:00
|
|
|
+ " "
|
2020-01-04 04:49:36 +01:00
|
|
|
+ ", ".join(self.addonName(f) for f in result.conflicts)
|
2020-01-03 16:32:20 +01:00
|
|
|
)
|
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
if not result.compatible:
|
2020-11-17 08:42:43 +01:00
|
|
|
strings.append(tr(TR.ADDONS_THIS_ADDON_IS_NOT_COMPATIBLE_WITH))
|
2020-01-24 08:25:52 +01:00
|
|
|
|
2020-01-03 16:32:20 +01:00
|
|
|
return strings
|
|
|
|
|
2017-08-26 07:14:20 +02:00
|
|
|
# Updating
|
|
|
|
######################################################################
|
|
|
|
|
2020-01-27 08:01:09 +01:00
|
|
|
def extract_update_info(self, items: List[Dict]) -> List[UpdateInfo]:
|
|
|
|
def extract_one(item: Dict) -> UpdateInfo:
|
|
|
|
id = item["id"]
|
|
|
|
meta = self.addon_meta(str(id))
|
|
|
|
branch_idx = meta.branch_index
|
|
|
|
return extract_update_info(current_point_version, branch_idx, item)
|
|
|
|
|
|
|
|
return list(map(extract_one, items))
|
|
|
|
|
|
|
|
def update_supported_versions(self, items: List[UpdateInfo]) -> None:
|
2020-01-19 04:37:55 +01:00
|
|
|
for item in items:
|
2020-01-27 08:01:09 +01:00
|
|
|
self.update_supported_version(item)
|
2020-01-19 04:37:55 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def update_supported_version(self, item: UpdateInfo) -> None:
|
2020-01-19 04:37:55 +01:00
|
|
|
addon = self.addon_meta(str(item.id))
|
2020-01-27 08:01:09 +01:00
|
|
|
updated = False
|
2020-04-15 10:47:04 +02:00
|
|
|
is_latest = addon.is_latest(item.current_branch_last_modified)
|
2020-01-27 08:01:09 +01:00
|
|
|
|
|
|
|
# if max different to the stored value
|
|
|
|
cur_max = item.current_branch_max_point_ver
|
|
|
|
if addon.max_point_version != cur_max:
|
|
|
|
if is_latest:
|
|
|
|
addon.max_point_version = cur_max
|
|
|
|
updated = True
|
|
|
|
else:
|
|
|
|
# user is not up to date; only update if new version is stricter
|
|
|
|
if cur_max is not None and cur_max < addon.max_point_version:
|
|
|
|
addon.max_point_version = cur_max
|
|
|
|
updated = True
|
|
|
|
|
|
|
|
# if min different to the stored value
|
|
|
|
cur_min = item.current_branch_min_point_ver
|
|
|
|
if addon.min_point_version != cur_min:
|
|
|
|
if is_latest:
|
|
|
|
addon.min_point_version = cur_min
|
|
|
|
updated = True
|
2020-01-19 04:37:55 +01:00
|
|
|
else:
|
2020-01-27 08:01:09 +01:00
|
|
|
# user is not up to date; only update if new version is stricter
|
|
|
|
if cur_min is not None and cur_min > addon.min_point_version:
|
|
|
|
addon.min_point_version = cur_min
|
|
|
|
updated = True
|
|
|
|
|
|
|
|
if updated:
|
|
|
|
self.write_addon_meta(addon)
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
def updates_required(self, items: List[UpdateInfo]) -> List[int]:
|
|
|
|
"""Return ids of add-ons requiring an update."""
|
|
|
|
need_update = []
|
|
|
|
for item in items:
|
2020-04-15 10:47:04 +02:00
|
|
|
addon = self.addon_meta(str(item.id))
|
|
|
|
# update if server mtime is newer
|
|
|
|
if not addon.is_latest(item.suitable_branch_last_modified):
|
|
|
|
need_update.append(item.id)
|
|
|
|
elif not addon.compatible() and item.suitable_branch_last_modified > 0:
|
|
|
|
# Addon is currently disabled, and a suitable branch was found on the
|
|
|
|
# server. Ignore our stored mtime (which may have been set incorrectly
|
|
|
|
# in the past) and require an update.
|
2020-01-19 01:37:15 +01:00
|
|
|
need_update.append(item.id)
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
return need_update
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2017-08-28 12:51:43 +02:00
|
|
|
# Add-on Config
|
|
|
|
######################################################################
|
|
|
|
|
2019-12-21 16:48:05 +01:00
|
|
|
_configButtonActions: Dict[str, Callable[[], Optional[bool]]] = {}
|
2019-12-15 23:51:38 +01:00
|
|
|
_configUpdatedActions: Dict[str, Callable[[Any], None]] = {}
|
2017-08-28 12:51:43 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def addonConfigDefaults(self, dir: str) -> Optional[Dict[str, Any]]:
|
2017-08-28 12:51:43 +02:00
|
|
|
path = os.path.join(self.addonsFolder(dir), "config.json")
|
|
|
|
try:
|
2017-12-11 08:25:51 +01:00
|
|
|
with open(path, encoding="utf8") as f:
|
|
|
|
return json.load(f)
|
2017-08-28 12:51:43 +02:00
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def addonConfigHelp(self, dir: str) -> str:
|
2017-08-28 12:51:43 +02:00
|
|
|
path = os.path.join(self.addonsFolder(dir), "config.md")
|
|
|
|
if os.path.exists(path):
|
2018-03-01 03:54:54 +01:00
|
|
|
with open(path, encoding="utf-8") as f:
|
2017-12-11 08:25:51 +01:00
|
|
|
return markdown.markdown(f.read())
|
2017-08-28 12:51:43 +02:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def addonFromModule(self, module: str) -> str:
|
2017-08-31 06:41:00 +02:00
|
|
|
return module.split(".")[0]
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def configAction(self, addon: str) -> Callable[[], Optional[bool]]:
|
2017-08-31 06:41:00 +02:00
|
|
|
return self._configButtonActions.get(addon)
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def configUpdatedAction(self, addon: str) -> Callable[[Any], None]:
|
2018-07-28 09:00:49 +02:00
|
|
|
return self._configUpdatedActions.get(addon)
|
|
|
|
|
2020-03-05 00:19:09 +01:00
|
|
|
# Schema
|
|
|
|
######################################################################
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _addon_schema_path(self, dir: str) -> str:
|
2020-03-05 00:19:09 +01:00
|
|
|
return os.path.join(self.addonsFolder(dir), "config.schema.json")
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def _addon_schema(self, dir: str) -> Any:
|
2020-03-11 00:56:14 +01:00
|
|
|
path = self._addon_schema_path(dir)
|
2020-03-05 00:19:09 +01:00
|
|
|
try:
|
|
|
|
if not os.path.exists(path):
|
|
|
|
# True is a schema accepting everything
|
|
|
|
return True
|
|
|
|
with open(path, encoding="utf-8") as f:
|
|
|
|
return json.load(f)
|
|
|
|
except json.decoder.JSONDecodeError as e:
|
|
|
|
print("The schema is not valid:")
|
|
|
|
print(e)
|
|
|
|
|
2017-08-31 06:41:00 +02:00
|
|
|
# Add-on Config API
|
|
|
|
######################################################################
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def getConfig(self, module: str) -> Optional[Dict[str, Any]]:
|
2017-08-31 06:41:00 +02:00
|
|
|
addon = self.addonFromModule(module)
|
2017-08-30 07:31:03 +02:00
|
|
|
# get default config
|
|
|
|
config = self.addonConfigDefaults(addon)
|
|
|
|
if config is None:
|
|
|
|
return None
|
|
|
|
# merge in user's keys
|
2017-08-28 12:51:43 +02:00
|
|
|
meta = self.addonMeta(addon)
|
2017-08-30 07:31:03 +02:00
|
|
|
userConf = meta.get("config", {})
|
|
|
|
config.update(userConf)
|
|
|
|
return config
|
2017-08-28 12:51:43 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def setConfigAction(self, module: str, fn: Callable[[], Optional[bool]]) -> None:
|
2017-08-31 06:41:00 +02:00
|
|
|
addon = self.addonFromModule(module)
|
2017-08-28 12:51:43 +02:00
|
|
|
self._configButtonActions[addon] = fn
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def setConfigUpdatedAction(self, module: str, fn: Callable[[Any], None]) -> None:
|
2018-07-28 09:00:49 +02:00
|
|
|
addon = self.addonFromModule(module)
|
|
|
|
self._configUpdatedActions[addon] = fn
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def writeConfig(self, module: str, conf: dict) -> None:
|
2017-08-31 06:41:00 +02:00
|
|
|
addon = self.addonFromModule(module)
|
2017-08-28 12:51:43 +02:00
|
|
|
meta = self.addonMeta(addon)
|
2019-12-23 01:34:10 +01:00
|
|
|
meta["config"] = conf
|
2017-08-28 12:51:43 +02:00
|
|
|
self.writeAddonMeta(addon, meta)
|
|
|
|
|
2017-09-10 10:53:47 +02:00
|
|
|
# user_files
|
|
|
|
######################################################################
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _userFilesPath(self, sid: str) -> str:
|
2017-09-10 10:53:47 +02:00
|
|
|
return os.path.join(self.addonsFolder(sid), "user_files")
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _userFilesBackupPath(self) -> str:
|
2017-09-10 10:53:47 +02:00
|
|
|
return os.path.join(self.addonsFolder(), "files_backup")
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def backupUserFiles(self, sid: str) -> None:
|
2017-09-10 10:53:47 +02:00
|
|
|
p = self._userFilesPath(sid)
|
|
|
|
if os.path.exists(p):
|
|
|
|
os.rename(p, self._userFilesBackupPath())
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def restoreUserFiles(self, sid: str) -> None:
|
2017-09-10 10:53:47 +02:00
|
|
|
p = self._userFilesPath(sid)
|
|
|
|
bp = self._userFilesBackupPath()
|
|
|
|
# did we back up userFiles?
|
|
|
|
if not os.path.exists(bp):
|
|
|
|
return
|
|
|
|
os.rename(bp, p)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2019-03-02 18:57:51 +01:00
|
|
|
# Web Exports
|
|
|
|
######################################################################
|
|
|
|
|
2019-12-15 23:51:38 +01:00
|
|
|
_webExports: Dict[str, str] = {}
|
2019-03-02 18:57:51 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def setWebExports(self, module: str, pattern: str) -> None:
|
2019-03-02 18:57:51 +01:00
|
|
|
addon = self.addonFromModule(module)
|
|
|
|
self._webExports[addon] = pattern
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def getWebExports(self, addon: str) -> str:
|
2019-03-02 18:57:51 +01:00
|
|
|
return self._webExports.get(addon)
|
|
|
|
|
2017-09-10 10:53:47 +02:00
|
|
|
|
2017-08-26 07:14:20 +02:00
|
|
|
# Add-ons Dialog
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class AddonsDialog(QDialog):
|
2020-08-01 07:50:27 +02:00
|
|
|
def __init__(self, addonsManager: AddonManager) -> None:
|
2017-08-26 07:14:20 +02:00
|
|
|
self.mgr = addonsManager
|
|
|
|
self.mw = addonsManager.mw
|
|
|
|
|
|
|
|
super().__init__(self.mw)
|
|
|
|
|
|
|
|
f = self.form = aqt.forms.addons.Ui_Dialog()
|
|
|
|
f.setupUi(self)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.getAddons.clicked, self.onGetAddons)
|
|
|
|
qconnect(f.installFromFile.clicked, self.onInstallFiles)
|
|
|
|
qconnect(f.checkForUpdates.clicked, self.check_for_updates)
|
|
|
|
qconnect(f.toggleEnabled.clicked, self.onToggleEnabled)
|
|
|
|
qconnect(f.viewPage.clicked, self.onViewPage)
|
|
|
|
qconnect(f.viewFiles.clicked, self.onViewFiles)
|
|
|
|
qconnect(f.delete_2.clicked, self.onDelete)
|
|
|
|
qconnect(f.config.clicked, self.onConfig)
|
|
|
|
qconnect(self.form.addonList.itemDoubleClicked, self.onConfig)
|
|
|
|
qconnect(self.form.addonList.currentRowChanged, self._onAddonItemSelected)
|
2020-07-19 01:49:36 +02:00
|
|
|
self.setWindowTitle(tr(TR.ADDONS_WINDOW_TITLE))
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2019-02-18 07:17:53 +01:00
|
|
|
self.setAcceptDrops(True)
|
2017-08-26 07:14:20 +02:00
|
|
|
self.redrawAddons()
|
2019-02-20 05:38:22 +01:00
|
|
|
restoreGeom(self, "addons")
|
2020-03-06 21:04:51 +01:00
|
|
|
gui_hooks.addons_dialog_will_show(self)
|
2017-08-26 07:14:20 +02:00
|
|
|
self.show()
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def dragEnterEvent(self, event: QEvent) -> None:
|
2019-02-18 07:17:53 +01:00
|
|
|
mime = event.mimeData()
|
|
|
|
if not mime.hasUrls():
|
|
|
|
return None
|
|
|
|
urls = mime.urls()
|
2019-02-22 10:17:56 +01:00
|
|
|
ext = self.mgr.ext
|
|
|
|
if all(url.toLocalFile().endswith(ext) for url in urls):
|
2019-02-18 07:17:53 +01:00
|
|
|
event.acceptProposedAction()
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def dropEvent(self, event: QEvent) -> None:
|
2019-02-18 07:17:53 +01:00
|
|
|
mime = event.mimeData()
|
|
|
|
paths = []
|
|
|
|
for url in mime.urls():
|
|
|
|
path = url.toLocalFile()
|
|
|
|
if os.path.exists(path):
|
|
|
|
paths.append(path)
|
|
|
|
self.onInstallFiles(paths)
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def reject(self) -> None:
|
2019-02-20 05:38:22 +01:00
|
|
|
saveGeom(self, "addons")
|
2020-12-26 18:07:37 +01:00
|
|
|
aqt.dialogs.markClosed("AddonsDialog")
|
|
|
|
|
2019-02-20 05:38:22 +01:00
|
|
|
return QDialog.reject(self)
|
|
|
|
|
2021-01-03 23:57:45 +01:00
|
|
|
silentlyClose = True
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def name_for_addon_list(self, addon: AddonMeta) -> str:
|
2020-01-19 04:06:53 +01:00
|
|
|
name = addon.human_name()
|
2020-01-19 03:44:53 +01:00
|
|
|
|
|
|
|
if not addon.enabled:
|
2020-11-17 08:42:43 +01:00
|
|
|
return name + " " + tr(TR.ADDONS_DISABLED2)
|
2020-01-19 04:37:55 +01:00
|
|
|
elif not addon.compatible():
|
2020-11-17 08:42:43 +01:00
|
|
|
return (
|
2020-11-17 12:47:47 +01:00
|
|
|
name + " " + tr(TR.ADDONS_REQUIRES, val=self.compatible_string(addon))
|
2020-11-17 08:42:43 +01:00
|
|
|
)
|
2020-01-19 03:44:53 +01:00
|
|
|
|
|
|
|
return name
|
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
def compatible_string(self, addon: AddonMeta) -> str:
|
|
|
|
min = addon.min_point_version
|
|
|
|
if min is not None and min > current_point_version:
|
|
|
|
return f"Anki >= 2.1.{min}"
|
|
|
|
else:
|
2020-02-17 23:12:14 +01:00
|
|
|
max = abs(addon.max_point_version)
|
2020-01-24 08:25:52 +01:00
|
|
|
return f"Anki <= 2.1.{max}"
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def should_grey(self, addon: AddonMeta) -> bool:
|
2020-01-19 04:37:55 +01:00
|
|
|
return not addon.enabled or not addon.compatible()
|
|
|
|
|
2020-08-31 05:29:28 +02:00
|
|
|
def redrawAddons(
|
|
|
|
self,
|
|
|
|
) -> None:
|
2019-02-23 10:04:45 +01:00
|
|
|
addonList = self.form.addonList
|
|
|
|
mgr = self.mgr
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
self.addons = list(mgr.all_addon_meta())
|
2020-01-19 04:06:53 +01:00
|
|
|
self.addons.sort(key=lambda a: a.human_name().lower())
|
2020-01-19 04:37:55 +01:00
|
|
|
self.addons.sort(key=self.should_grey)
|
2019-02-23 10:10:05 +01:00
|
|
|
|
|
|
|
selected = set(self.selectedAddons())
|
2019-02-23 10:04:45 +01:00
|
|
|
addonList.clear()
|
2020-01-19 03:44:53 +01:00
|
|
|
for addon in self.addons:
|
|
|
|
name = self.name_for_addon_list(addon)
|
2019-02-23 10:04:45 +01:00
|
|
|
item = QListWidgetItem(name, addonList)
|
2020-01-19 04:37:55 +01:00
|
|
|
if self.should_grey(addon):
|
2019-02-23 10:04:45 +01:00
|
|
|
item.setForeground(Qt.gray)
|
2020-01-19 03:44:53 +01:00
|
|
|
if addon.dir_name in selected:
|
2019-02-23 10:10:05 +01:00
|
|
|
item.setSelected(True)
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 01:33:51 +01:00
|
|
|
addonList.reset()
|
2019-02-06 00:19:20 +01:00
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def _onAddonItemSelected(self, row_int: int) -> None:
|
2018-08-31 08:56:16 +02:00
|
|
|
try:
|
2020-01-19 03:44:53 +01:00
|
|
|
addon = self.addons[row_int]
|
2018-08-31 09:13:06 +02:00
|
|
|
except IndexError:
|
2020-01-19 03:44:53 +01:00
|
|
|
return
|
2020-01-19 04:06:53 +01:00
|
|
|
self.form.viewPage.setEnabled(addon.ankiweb_id() is not None)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.config.setEnabled(
|
2020-01-19 03:44:53 +01:00
|
|
|
bool(
|
|
|
|
self.mgr.getConfig(addon.dir_name)
|
|
|
|
or self.mgr.configAction(addon.dir_name)
|
|
|
|
)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-03-06 22:21:42 +01:00
|
|
|
gui_hooks.addons_dialog_did_change_selected_addon(self, addon)
|
2020-01-19 03:44:53 +01:00
|
|
|
return
|
2018-08-31 08:56:16 +02:00
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
def selectedAddons(self) -> List[str]:
|
2017-08-26 07:14:20 +02:00
|
|
|
idxs = [x.row() for x in self.form.addonList.selectedIndexes()]
|
2020-01-19 03:44:53 +01:00
|
|
|
return [self.addons[idx].dir_name for idx in idxs]
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onlyOneSelected(self) -> Optional[str]:
|
2017-08-26 07:14:20 +02:00
|
|
|
dirs = self.selectedAddons()
|
|
|
|
if len(dirs) != 1:
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.ADDONS_PLEASE_SELECT_A_SINGLE_ADDON_FIRST))
|
2020-08-01 07:50:27 +02:00
|
|
|
return None
|
2017-08-26 07:14:20 +02:00
|
|
|
return dirs[0]
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onToggleEnabled(self) -> None:
|
2017-08-26 07:14:20 +02:00
|
|
|
for dir in self.selectedAddons():
|
|
|
|
self.mgr.toggleEnabled(dir)
|
|
|
|
self.redrawAddons()
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onViewPage(self) -> None:
|
2017-08-26 07:14:20 +02:00
|
|
|
addon = self.onlyOneSelected()
|
|
|
|
if not addon:
|
|
|
|
return
|
2017-12-11 08:25:51 +01:00
|
|
|
if re.match(r"^\d+$", addon):
|
2018-01-16 07:07:30 +01:00
|
|
|
openLink(aqt.appShared + "info/{}".format(addon))
|
2017-08-26 07:14:20 +02:00
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.ADDONS_ADDON_WAS_NOT_DOWNLOADED_FROM_ANKIWEB))
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onViewFiles(self) -> None:
|
2017-08-26 07:14:20 +02:00
|
|
|
# if nothing selected, open top level folder
|
|
|
|
selected = self.selectedAddons()
|
|
|
|
if not selected:
|
|
|
|
openFolder(self.mgr.addonsFolder())
|
|
|
|
return
|
|
|
|
|
|
|
|
# otherwise require a single selection
|
|
|
|
addon = self.onlyOneSelected()
|
|
|
|
if not addon:
|
|
|
|
return
|
|
|
|
path = self.mgr.addonsFolder(addon)
|
|
|
|
openFolder(path)
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onDelete(self) -> None:
|
2017-08-26 07:14:20 +02:00
|
|
|
selected = self.selectedAddons()
|
|
|
|
if not selected:
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
if not askUser(
|
2020-11-18 02:32:22 +01:00
|
|
|
tr(TR.ADDONS_DELETE_THE_NUMD_SELECTED_ADDON, count=len(selected))
|
2019-12-23 01:34:10 +01:00
|
|
|
):
|
2017-08-26 07:14:20 +02:00
|
|
|
return
|
|
|
|
for dir in selected:
|
2019-04-29 10:43:10 +02:00
|
|
|
if not self.mgr.deleteAddon(dir):
|
|
|
|
break
|
2019-02-24 06:24:31 +01:00
|
|
|
self.form.addonList.clearSelection()
|
2017-08-26 07:14:20 +02:00
|
|
|
self.redrawAddons()
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onGetAddons(self) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
obj = GetAddons(self)
|
|
|
|
if obj.ids:
|
|
|
|
download_addons(self, self.mgr, obj.ids, self.after_downloading)
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def after_downloading(self, log: List[DownloadLogEntry]) -> None:
|
2020-01-19 04:37:55 +01:00
|
|
|
self.redrawAddons()
|
2020-01-19 01:37:15 +01:00
|
|
|
if log:
|
|
|
|
show_log_to_user(self, log)
|
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
tooltip(tr(TR.ADDONS_NO_UPDATES_AVAILABLE))
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onInstallFiles(self, paths: Optional[List[str]] = None) -> Optional[bool]:
|
2019-02-18 07:17:14 +01:00
|
|
|
if not paths:
|
2020-11-17 08:42:43 +01:00
|
|
|
key = tr(TR.ADDONS_PACKAGED_ANKI_ADDON) + " (*{})".format(self.mgr.ext)
|
2021-02-01 11:23:48 +01:00
|
|
|
paths_ = getFile(
|
2020-11-17 08:42:43 +01:00
|
|
|
self, tr(TR.ADDONS_INSTALL_ADDONS), None, key, key="addons", multi=True
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2021-02-01 11:23:48 +01:00
|
|
|
paths = paths_ # type: ignore
|
2019-02-18 07:17:14 +01:00
|
|
|
if not paths:
|
|
|
|
return False
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-01-03 17:57:33 +01:00
|
|
|
installAddonPackages(self.mgr, paths, parent=self)
|
2019-02-18 07:17:14 +01:00
|
|
|
|
|
|
|
self.redrawAddons()
|
2020-08-01 07:50:27 +02:00
|
|
|
return None
|
2019-02-18 07:17:14 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def check_for_updates(self) -> None:
|
2020-11-17 08:42:43 +01:00
|
|
|
tooltip(tr(TR.ADDONS_CHECKING))
|
2020-01-19 01:37:15 +01:00
|
|
|
check_and_prompt_for_updates(self, self.mgr, self.after_downloading)
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onConfig(self) -> None:
|
2017-08-28 12:51:43 +02:00
|
|
|
addon = self.onlyOneSelected()
|
|
|
|
if not addon:
|
|
|
|
return
|
|
|
|
|
|
|
|
# does add-on manage its own config?
|
|
|
|
act = self.mgr.configAction(addon)
|
|
|
|
if act:
|
2019-12-21 16:48:05 +01:00
|
|
|
ret = act()
|
|
|
|
if ret is not False:
|
|
|
|
return
|
2017-08-28 12:51:43 +02:00
|
|
|
|
|
|
|
conf = self.mgr.getConfig(addon)
|
|
|
|
if conf is None:
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.ADDONS_ADDON_HAS_NO_CONFIGURATION))
|
2017-08-28 12:51:43 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
ConfigEditor(self, addon, conf)
|
|
|
|
|
|
|
|
|
2017-08-26 07:14:20 +02:00
|
|
|
# Fetching Add-ons
|
|
|
|
######################################################################
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class GetAddons(QDialog):
|
2021-02-02 15:00:29 +01:00
|
|
|
def __init__(self, dlg: QDialog) -> None:
|
2017-08-26 07:14:20 +02:00
|
|
|
QDialog.__init__(self, dlg)
|
|
|
|
self.addonsDlg = dlg
|
|
|
|
self.mgr = dlg.mgr
|
|
|
|
self.mw = self.mgr.mw
|
2020-01-19 01:37:15 +01:00
|
|
|
self.ids: List[int] = []
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form = aqt.forms.getaddons.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
b = self.form.buttonBox.addButton(
|
2020-11-17 08:42:43 +01:00
|
|
|
tr(TR.ADDONS_BROWSE_ADDONS), QDialogButtonBox.ActionRole
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(b.clicked, self.onBrowse)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
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
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onBrowse(self) -> None:
|
2017-08-26 14:40:11 +02:00
|
|
|
openLink(aqt.appShared + "addons/2.1")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def accept(self) -> None:
|
2017-02-15 06:55:20 +01:00
|
|
|
# get codes
|
|
|
|
try:
|
|
|
|
ids = [int(n) for n in self.form.code.text().split()]
|
|
|
|
except ValueError:
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.ADDONS_INVALID_CODE))
|
2017-02-15 06:55:20 +01:00
|
|
|
return
|
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
self.ids = ids
|
|
|
|
QDialog.accept(self)
|
|
|
|
|
2017-02-15 06:55:20 +01:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
# Downloading
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2020-01-19 02:33:27 +01:00
|
|
|
def download_addon(client: HttpClient, id: int) -> Union[DownloadOk, DownloadError]:
|
2020-01-19 01:37:15 +01:00
|
|
|
"Fetch a single add-on from AnkiWeb."
|
|
|
|
try:
|
2020-01-24 08:25:52 +01:00
|
|
|
resp = client.get(
|
|
|
|
aqt.appShared + f"download/{id}?v=2.1&p={current_point_version}"
|
|
|
|
)
|
2020-01-19 01:37:15 +01:00
|
|
|
if resp.status_code != 200:
|
|
|
|
return DownloadError(status_code=resp.status_code)
|
|
|
|
|
|
|
|
data = client.streamContent(resp)
|
|
|
|
|
|
|
|
fname = re.match(
|
|
|
|
"attachment; filename=(.+)", resp.headers["content-disposition"]
|
|
|
|
).group(1)
|
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
meta = extract_meta_from_download_url(resp.url)
|
|
|
|
|
|
|
|
return DownloadOk(
|
|
|
|
data=data,
|
|
|
|
filename=fname,
|
2020-01-27 08:01:09 +01:00
|
|
|
mod_time=meta.mod_time,
|
2020-01-24 08:25:52 +01:00
|
|
|
min_point_version=meta.min_point_version,
|
|
|
|
max_point_version=meta.max_point_version,
|
2020-01-27 08:01:09 +01:00
|
|
|
branch_index=meta.branch_index,
|
2020-01-24 08:25:52 +01:00
|
|
|
)
|
2020-01-19 01:37:15 +01:00
|
|
|
except Exception as e:
|
|
|
|
return DownloadError(exception=e)
|
|
|
|
|
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
@dataclass
|
|
|
|
class ExtractedDownloadMeta:
|
2020-01-27 08:01:09 +01:00
|
|
|
mod_time: int
|
|
|
|
min_point_version: int
|
|
|
|
max_point_version: int
|
|
|
|
branch_index: int
|
2020-01-24 08:25:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
def extract_meta_from_download_url(url: str) -> ExtractedDownloadMeta:
|
|
|
|
urlobj = urlparse(url)
|
|
|
|
query = parse_qs(urlobj.query)
|
|
|
|
|
2020-01-27 08:01:09 +01:00
|
|
|
meta = ExtractedDownloadMeta(
|
|
|
|
mod_time=int(query.get("t")[0]),
|
|
|
|
min_point_version=int(query.get("minpt")[0]),
|
|
|
|
max_point_version=int(query.get("maxpt")[0]),
|
|
|
|
branch_index=int(query.get("bidx")[0]),
|
|
|
|
)
|
2020-01-24 08:25:52 +01:00
|
|
|
|
|
|
|
return meta
|
|
|
|
|
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
def download_log_to_html(log: List[DownloadLogEntry]) -> str:
|
2020-01-19 07:46:24 +01:00
|
|
|
return "<br>".join(map(describe_log_entry, log))
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def describe_log_entry(id_and_entry: DownloadLogEntry) -> str:
|
|
|
|
(id, entry) = id_and_entry
|
|
|
|
buf = f"{id}: "
|
|
|
|
|
|
|
|
if isinstance(entry, DownloadError):
|
|
|
|
if entry.status_code is not None:
|
|
|
|
if entry.status_code in (403, 404):
|
2020-11-17 08:42:43 +01:00
|
|
|
buf += tr(TR.ADDONS_INVALID_CODE_OR_ADDON_NOT_AVAILABLE)
|
2019-04-16 09:38:38 +02:00
|
|
|
else:
|
2020-11-17 12:47:47 +01:00
|
|
|
buf += tr(TR.QT_MISC_UNEXPECTED_RESPONSE_CODE, val=entry.status_code)
|
2020-01-19 01:37:15 +01:00
|
|
|
else:
|
|
|
|
buf += (
|
2020-11-17 08:42:43 +01:00
|
|
|
tr(TR.ADDONS_PLEASE_CHECK_YOUR_INTERNET_CONNECTION)
|
2020-01-19 01:37:15 +01:00
|
|
|
+ "\n\n"
|
|
|
|
+ str(entry.exception)
|
|
|
|
)
|
|
|
|
elif isinstance(entry, InstallError):
|
|
|
|
buf += entry.errmsg
|
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
buf += tr(TR.ADDONS_INSTALLED_SUCCESSFULLY)
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
return buf
|
|
|
|
|
|
|
|
|
|
|
|
def download_encountered_problem(log: List[DownloadLogEntry]) -> bool:
|
|
|
|
return any(not isinstance(e[1], InstallOk) for e in log)
|
|
|
|
|
|
|
|
|
|
|
|
def download_and_install_addon(
|
2020-01-19 02:33:27 +01:00
|
|
|
mgr: AddonManager, client: HttpClient, id: int
|
2020-01-19 01:37:15 +01:00
|
|
|
) -> DownloadLogEntry:
|
|
|
|
"Download and install a single add-on."
|
|
|
|
result = download_addon(client, id)
|
|
|
|
if isinstance(result, DownloadError):
|
|
|
|
return (id, result)
|
|
|
|
|
|
|
|
fname = result.filename.replace("_", " ")
|
|
|
|
name = os.path.splitext(fname)[0]
|
|
|
|
|
2020-01-27 08:01:09 +01:00
|
|
|
manifest = dict(
|
|
|
|
package=str(id),
|
|
|
|
name=name,
|
|
|
|
mod=result.mod_time,
|
|
|
|
min_point_version=result.min_point_version,
|
|
|
|
max_point_version=result.max_point_version,
|
|
|
|
branch_index=result.branch_index,
|
|
|
|
)
|
2020-01-24 08:25:52 +01:00
|
|
|
|
2020-01-27 08:01:09 +01:00
|
|
|
result2 = mgr.install(io.BytesIO(result.data), manifest=manifest)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
return (id, result2)
|
|
|
|
|
|
|
|
|
|
|
|
class DownloaderInstaller(QObject):
|
|
|
|
progressSignal = pyqtSignal(int, int)
|
|
|
|
|
2020-01-19 02:33:27 +01:00
|
|
|
def __init__(self, parent: QWidget, mgr: AddonManager, client: HttpClient) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
QObject.__init__(self, parent)
|
|
|
|
self.mgr = mgr
|
|
|
|
self.client = client
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.progressSignal, self._progress_callback)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def bg_thread_progress(up: int, down: int) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
self.progressSignal.emit(up, down) # type: ignore
|
|
|
|
|
|
|
|
self.client.progress_hook = bg_thread_progress
|
|
|
|
|
|
|
|
def download(
|
|
|
|
self, ids: List[int], on_done: Callable[[List[DownloadLogEntry]], None]
|
|
|
|
) -> None:
|
|
|
|
self.ids = ids
|
|
|
|
self.log: List[DownloadLogEntry] = []
|
|
|
|
|
|
|
|
self.dl_bytes = 0
|
|
|
|
self.last_tooltip = 0
|
|
|
|
|
|
|
|
self.on_done = on_done
|
|
|
|
|
|
|
|
self.mgr.mw.progress.start(immediate=True, parent=self.parent())
|
2020-01-22 05:09:51 +01:00
|
|
|
self.mgr.mw.taskman.run_in_background(self._download_all, self._download_done)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
def _progress_callback(self, up: int, down: int) -> None:
|
|
|
|
self.dl_bytes += down
|
|
|
|
self.mgr.mw.progress.update(
|
2020-11-22 05:57:53 +01:00
|
|
|
label=tr(
|
|
|
|
TR.ADDONS_DOWNLOADING_ADBD_KB02FKB,
|
|
|
|
part=len(self.log) + 1,
|
|
|
|
total=len(self.ids),
|
|
|
|
kilobytes=self.dl_bytes / 1024,
|
|
|
|
)
|
2020-01-19 01:37:15 +01:00
|
|
|
)
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _download_all(self) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
for id in self.ids:
|
|
|
|
self.log.append(download_and_install_addon(self.mgr, self.client, id))
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def _download_done(self, future: Future) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
self.mgr.mw.progress.finish()
|
|
|
|
# qt gets confused if on_done() opens new windows while the progress
|
|
|
|
# modal is still cleaning up
|
|
|
|
self.mgr.mw.progress.timer(50, lambda: self.on_done(self.log), False)
|
|
|
|
|
|
|
|
|
|
|
|
def show_log_to_user(parent: QWidget, log: List[DownloadLogEntry]) -> None:
|
|
|
|
have_problem = download_encountered_problem(log)
|
|
|
|
|
|
|
|
if have_problem:
|
2020-11-17 08:42:43 +01:00
|
|
|
text = tr(TR.ADDONS_ONE_OR_MORE_ERRORS_OCCURRED)
|
2020-01-19 01:37:15 +01:00
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
text = tr(TR.ADDONS_DOWNLOAD_COMPLETE_PLEASE_RESTART_ANKI_TO)
|
2020-01-19 01:37:15 +01:00
|
|
|
text += "<br><br>" + download_log_to_html(log)
|
|
|
|
|
|
|
|
if have_problem:
|
|
|
|
showWarning(text, textFormat="rich", parent=parent)
|
|
|
|
else:
|
|
|
|
showInfo(text, parent=parent)
|
|
|
|
|
|
|
|
|
|
|
|
def download_addons(
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
|
|
|
ids: List[int],
|
|
|
|
on_done: Callable[[List[DownloadLogEntry]], None],
|
2020-01-19 02:33:27 +01:00
|
|
|
client: Optional[HttpClient] = None,
|
2020-01-19 01:37:15 +01:00
|
|
|
) -> None:
|
|
|
|
if client is None:
|
2020-01-19 02:33:27 +01:00
|
|
|
client = HttpClient()
|
2020-01-19 01:37:15 +01:00
|
|
|
downloader = DownloaderInstaller(parent, mgr, client)
|
|
|
|
downloader.download(ids, on_done=on_done)
|
|
|
|
|
|
|
|
|
|
|
|
# Update checking
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2020-01-27 08:01:09 +01:00
|
|
|
def fetch_update_info(client: HttpClient, ids: List[int]) -> List[Dict]:
|
2020-01-19 01:37:15 +01:00
|
|
|
"""Fetch update info from AnkiWeb in one or more batches."""
|
2020-01-27 08:01:09 +01:00
|
|
|
all_info: List[Dict] = []
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
while ids:
|
|
|
|
# get another chunk
|
|
|
|
chunk = ids[:25]
|
|
|
|
del ids[:25]
|
|
|
|
|
|
|
|
batch_results = _fetch_update_info_batch(client, map(str, chunk))
|
|
|
|
all_info.extend(batch_results)
|
|
|
|
|
|
|
|
return all_info
|
|
|
|
|
|
|
|
|
|
|
|
def _fetch_update_info_batch(
|
2020-01-19 02:33:27 +01:00
|
|
|
client: HttpClient, chunk: Iterable[str]
|
2020-01-27 08:01:09 +01:00
|
|
|
) -> Iterable[Dict]:
|
2020-01-19 01:37:15 +01:00
|
|
|
"""Get update info from AnkiWeb.
|
|
|
|
|
|
|
|
Chunk must not contain more than 25 ids."""
|
2020-01-27 08:01:09 +01:00
|
|
|
resp = client.get(aqt.appShared + "updates/" + ",".join(chunk) + "?v=3")
|
2020-01-19 01:37:15 +01:00
|
|
|
if resp.status_code == 200:
|
2020-01-27 08:01:09 +01:00
|
|
|
return resp.json()
|
2020-01-19 01:37:15 +01:00
|
|
|
else:
|
|
|
|
raise Exception(
|
|
|
|
"Unexpected response code from AnkiWeb: {}".format(resp.status_code)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def check_and_prompt_for_updates(
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
|
|
|
on_done: Callable[[List[DownloadLogEntry]], None],
|
2020-08-01 07:50:27 +02:00
|
|
|
) -> None:
|
|
|
|
def on_updates_received(client: HttpClient, items: List[Dict]) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
handle_update_info(parent, mgr, client, items, on_done)
|
|
|
|
|
|
|
|
check_for_updates(mgr, on_updates_received)
|
|
|
|
|
|
|
|
|
|
|
|
def check_for_updates(
|
2020-01-27 08:01:09 +01:00
|
|
|
mgr: AddonManager, on_done: Callable[[HttpClient, List[Dict]], None]
|
2020-08-01 07:50:27 +02:00
|
|
|
) -> None:
|
2020-01-19 02:33:27 +01:00
|
|
|
client = HttpClient()
|
2020-01-19 01:37:15 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def check() -> List[Dict]:
|
2020-01-19 05:04:57 +01:00
|
|
|
return fetch_update_info(client, mgr.ankiweb_addons())
|
2020-01-19 01:37:15 +01:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def update_info_received(future: Future) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
# if syncing/in profile screen, defer message delivery
|
|
|
|
if not mgr.mw.col:
|
|
|
|
mgr.mw.progress.timer(
|
|
|
|
1000,
|
|
|
|
lambda: update_info_received(future),
|
|
|
|
False,
|
|
|
|
requiresCollection=False,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
if future.exception():
|
|
|
|
# swallow network errors
|
|
|
|
print(str(future.exception()))
|
|
|
|
result = []
|
|
|
|
else:
|
|
|
|
result = future.result()
|
|
|
|
|
|
|
|
on_done(client, result)
|
|
|
|
|
2020-01-22 05:09:51 +01:00
|
|
|
mgr.mw.taskman.run_in_background(check, update_info_received)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
|
2020-01-27 08:01:09 +01:00
|
|
|
def extract_update_info(
|
|
|
|
current_point_version: int, current_branch_idx: int, info_json: Dict
|
|
|
|
) -> UpdateInfo:
|
|
|
|
"Process branches to determine the updated mod time and min/max versions."
|
|
|
|
branches = info_json["branches"]
|
2020-01-27 08:59:40 +01:00
|
|
|
try:
|
|
|
|
current = branches[current_branch_idx]
|
|
|
|
except IndexError:
|
|
|
|
current = branches[0]
|
2020-01-27 08:01:09 +01:00
|
|
|
|
|
|
|
last_mod = 0
|
|
|
|
for branch in branches:
|
|
|
|
if branch["minpt"] > current_point_version:
|
|
|
|
continue
|
|
|
|
if branch["maxpt"] < 0 and abs(branch["maxpt"]) < current_point_version:
|
|
|
|
continue
|
|
|
|
last_mod = branch["fmod"]
|
|
|
|
|
|
|
|
return UpdateInfo(
|
|
|
|
id=info_json["id"],
|
|
|
|
suitable_branch_last_modified=last_mod,
|
|
|
|
current_branch_last_modified=current["fmod"],
|
|
|
|
current_branch_min_point_ver=current["minpt"],
|
|
|
|
current_branch_max_point_ver=current["maxpt"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
def handle_update_info(
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
2020-01-19 02:33:27 +01:00
|
|
|
client: HttpClient,
|
2020-01-27 08:01:09 +01:00
|
|
|
items: List[Dict],
|
2020-01-19 01:37:15 +01:00
|
|
|
on_done: Callable[[List[DownloadLogEntry]], None],
|
|
|
|
) -> None:
|
2020-01-27 08:01:09 +01:00
|
|
|
update_info = mgr.extract_update_info(items)
|
|
|
|
mgr.update_supported_versions(update_info)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
2020-01-27 08:01:09 +01:00
|
|
|
updated_ids = mgr.updates_required(update_info)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
if not updated_ids:
|
|
|
|
on_done([])
|
|
|
|
return
|
|
|
|
|
|
|
|
prompt_to_update(parent, mgr, client, updated_ids, on_done)
|
|
|
|
|
|
|
|
|
|
|
|
def prompt_to_update(
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
2020-01-19 02:33:27 +01:00
|
|
|
client: HttpClient,
|
2020-01-19 01:37:15 +01:00
|
|
|
ids: List[int],
|
|
|
|
on_done: Callable[[List[DownloadLogEntry]], None],
|
|
|
|
) -> None:
|
|
|
|
names = map(lambda x: mgr.addonName(str(x)), ids)
|
|
|
|
if not askUser(
|
2020-11-17 08:42:43 +01:00
|
|
|
tr(TR.ADDONS_THE_FOLLOWING_ADDONS_HAVE_UPDATES_AVAILABLE)
|
2020-01-19 01:37:15 +01:00
|
|
|
+ "\n\n"
|
|
|
|
+ "\n".join(names)
|
|
|
|
):
|
|
|
|
# on_done is not called if the user cancels
|
|
|
|
return
|
|
|
|
|
|
|
|
download_addons(parent, mgr, ids, on_done, client)
|
2017-08-28 12:51:43 +02:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-28 12:51:43 +02:00
|
|
|
# Editing config
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class ConfigEditor(QDialog):
|
2021-02-02 15:00:29 +01:00
|
|
|
def __init__(self, dlg: QDialog, addon: str, conf: Dict) -> None:
|
2017-08-28 12:51:43 +02:00
|
|
|
super().__init__(dlg)
|
|
|
|
self.addon = addon
|
|
|
|
self.conf = conf
|
|
|
|
self.mgr = dlg.mgr
|
|
|
|
self.form = aqt.forms.addonconf.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
|
|
|
restore = self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(restore.clicked, self.onRestoreDefaults)
|
2019-02-15 14:15:54 +01:00
|
|
|
self.setupFonts()
|
2017-08-28 12:51:43 +02:00
|
|
|
self.updateHelp()
|
2018-07-28 09:09:17 +02:00
|
|
|
self.updateText(self.conf)
|
2019-02-23 09:02:06 +01:00
|
|
|
restoreGeom(self, "addonconf")
|
|
|
|
restoreSplitter(self.form.splitter, "addonconf")
|
2020-02-29 20:15:23 +01:00
|
|
|
self.setWindowTitle(
|
2020-08-30 23:35:17 +02:00
|
|
|
without_unicode_isolation(
|
|
|
|
tr(
|
|
|
|
TR.ADDONS_CONFIG_WINDOW_TITLE,
|
|
|
|
name=self.mgr.addon_meta(addon).human_name(),
|
|
|
|
)
|
2020-02-29 20:15:23 +01:00
|
|
|
)
|
|
|
|
)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2017-08-28 12:51:43 +02:00
|
|
|
self.show()
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onRestoreDefaults(self) -> None:
|
2018-07-28 09:09:17 +02:00
|
|
|
default_conf = self.mgr.addonConfigDefaults(self.addon)
|
|
|
|
self.updateText(default_conf)
|
2020-11-17 08:42:43 +01:00
|
|
|
tooltip(tr(TR.ADDONS_RESTORED_DEFAULTS), parent=self)
|
2017-08-28 12:51:43 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def setupFonts(self) -> None:
|
2019-02-15 14:15:54 +01:00
|
|
|
font_mono = QFontDatabase.systemFont(QFontDatabase.FixedFont)
|
2019-02-23 09:02:06 +01:00
|
|
|
font_mono.setPointSize(font_mono.pointSize() + 1)
|
2019-02-15 14:15:54 +01:00
|
|
|
self.form.editor.setFont(font_mono)
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def updateHelp(self) -> None:
|
2017-08-28 12:51:43 +02:00
|
|
|
txt = self.mgr.addonConfigHelp(self.addon)
|
|
|
|
if txt:
|
|
|
|
self.form.label.setText(txt)
|
|
|
|
else:
|
|
|
|
self.form.scrollArea.setVisible(False)
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def updateText(self, conf: Dict[str, Any]) -> None:
|
2020-03-02 00:54:58 +01:00
|
|
|
text = json.dumps(
|
2020-08-31 05:29:28 +02:00
|
|
|
conf,
|
|
|
|
ensure_ascii=False,
|
|
|
|
sort_keys=True,
|
|
|
|
indent=4,
|
|
|
|
separators=(",", ": "),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-03-02 00:54:58 +01:00
|
|
|
text = gui_hooks.addon_config_editor_will_display_json(text)
|
|
|
|
self.form.editor.setPlainText(text)
|
2020-06-24 03:12:45 +02:00
|
|
|
if isMac:
|
|
|
|
self.form.editor.repaint()
|
2017-08-28 12:51:43 +02:00
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def onClose(self) -> None:
|
2019-02-23 09:02:06 +01:00
|
|
|
saveGeom(self, "addonconf")
|
|
|
|
saveSplitter(self.form.splitter, "addonconf")
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def reject(self) -> None:
|
2019-02-23 09:02:06 +01:00
|
|
|
self.onClose()
|
|
|
|
super().reject()
|
|
|
|
|
2020-08-01 07:50:27 +02:00
|
|
|
def accept(self) -> None:
|
2017-08-28 12:51:43 +02:00
|
|
|
txt = self.form.editor.toPlainText()
|
2020-03-02 01:09:52 +01:00
|
|
|
txt = gui_hooks.addon_config_editor_will_save_json(txt)
|
2017-08-28 12:51:43 +02:00
|
|
|
try:
|
2018-07-28 09:09:17 +02:00
|
|
|
new_conf = json.loads(txt)
|
2020-03-11 00:56:14 +01:00
|
|
|
jsonschema.validate(new_conf, self.parent().mgr._addon_schema(self.addon))
|
2020-03-05 00:19:09 +01:00
|
|
|
except ValidationError as e:
|
|
|
|
# The user did edit the configuration and entered a value
|
|
|
|
# which can not be interpreted.
|
2020-03-11 01:03:52 +01:00
|
|
|
schema = e.schema
|
|
|
|
erroneous_conf = new_conf
|
|
|
|
for link in e.path:
|
|
|
|
erroneous_conf = erroneous_conf[link]
|
|
|
|
path = "/".join(str(path) for path in e.path)
|
|
|
|
if "error_msg" in schema:
|
|
|
|
msg = schema["error_msg"].format(
|
|
|
|
problem=e.message,
|
|
|
|
path=path,
|
|
|
|
schema=str(schema),
|
|
|
|
erroneous_conf=erroneous_conf,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
msg = tr(
|
2020-03-08 16:27:53 +01:00
|
|
|
TR.ADDONS_CONFIG_VALIDATION_ERROR,
|
|
|
|
problem=e.message,
|
2020-03-11 01:03:52 +01:00
|
|
|
path=path,
|
|
|
|
schema=str(schema),
|
2020-03-08 16:27:53 +01:00
|
|
|
)
|
2020-03-11 01:03:52 +01:00
|
|
|
showInfo(msg)
|
2020-03-05 00:19:09 +01:00
|
|
|
return
|
2017-08-28 12:51:43 +02:00
|
|
|
except Exception as e:
|
2020-11-22 05:57:53 +01:00
|
|
|
showInfo(tr(TR.ADDONS_INVALID_CONFIGURATION) + " " + repr(e))
|
2017-08-28 12:51:43 +02:00
|
|
|
return
|
|
|
|
|
2018-11-18 06:22:31 +01:00
|
|
|
if not isinstance(new_conf, dict):
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.ADDONS_INVALID_CONFIGURATION_TOP_LEVEL_OBJECT_MUST))
|
2018-11-18 06:22:31 +01:00
|
|
|
return
|
|
|
|
|
2018-07-28 09:09:17 +02:00
|
|
|
if new_conf != self.conf:
|
|
|
|
self.mgr.writeConfig(self.addon, new_conf)
|
2018-07-28 09:25:38 +02:00
|
|
|
# does the add-on define an action to be fired?
|
2018-07-28 09:09:17 +02:00
|
|
|
act = self.mgr.configUpdatedAction(self.addon)
|
|
|
|
if act:
|
2018-07-28 09:25:38 +02:00
|
|
|
act(new_conf)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2019-02-23 09:02:06 +01:00
|
|
|
self.onClose()
|
2017-08-28 12:51:43 +02:00
|
|
|
super().accept()
|
2020-01-03 17:57:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
# .ankiaddon installation wizard
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
|
|
|
def installAddonPackages(
|
|
|
|
addonsManager: AddonManager,
|
|
|
|
paths: List[str],
|
2020-01-04 04:34:16 +01:00
|
|
|
parent: Optional[QWidget] = None,
|
|
|
|
warn: bool = False,
|
2020-01-04 04:45:43 +01:00
|
|
|
strictly_modal: bool = False,
|
|
|
|
advise_restart: bool = False,
|
2020-01-03 17:57:33 +01:00
|
|
|
) -> bool:
|
|
|
|
|
2020-01-04 04:34:16 +01:00
|
|
|
if warn:
|
2020-01-04 01:30:20 +01:00
|
|
|
names = ",<br>".join(f"<b>{os.path.basename(p)}</b>" for p in paths)
|
2020-11-17 14:24:19 +01:00
|
|
|
q = tr(TR.ADDONS_IMPORTANT_AS_ADDONS_ARE_PROGRAMS_DOWNLOADED) % dict(
|
|
|
|
names=names
|
|
|
|
)
|
2020-01-03 17:57:33 +01:00
|
|
|
if (
|
|
|
|
not showInfo(
|
|
|
|
q,
|
|
|
|
parent=parent,
|
2020-11-17 08:42:43 +01:00
|
|
|
title=tr(TR.ADDONS_INSTALL_ANKI_ADDON),
|
2020-01-03 17:57:33 +01:00
|
|
|
type="warning",
|
|
|
|
customBtns=[QMessageBox.No, QMessageBox.Yes],
|
|
|
|
)
|
|
|
|
== QMessageBox.Yes
|
|
|
|
):
|
|
|
|
return False
|
|
|
|
|
|
|
|
log, errs = addonsManager.processPackages(paths, parent=parent)
|
|
|
|
|
|
|
|
if log:
|
|
|
|
log_html = "<br>".join(log)
|
2020-01-04 04:45:43 +01:00
|
|
|
if advise_restart:
|
2020-11-17 08:42:43 +01:00
|
|
|
log_html += "<br><br>" + tr(TR.ADDONS_PLEASE_RESTART_ANKI_TO_COMPLETE_THE)
|
2020-01-04 04:34:16 +01:00
|
|
|
if len(log) == 1 and not strictly_modal:
|
2020-01-03 17:57:33 +01:00
|
|
|
tooltip(log_html, parent=parent)
|
|
|
|
else:
|
|
|
|
showInfo(
|
|
|
|
log_html,
|
|
|
|
parent=parent,
|
|
|
|
textFormat="rich",
|
2020-11-17 08:42:43 +01:00
|
|
|
title=tr(TR.ADDONS_INSTALLATION_COMPLETE),
|
2020-01-03 17:57:33 +01:00
|
|
|
)
|
|
|
|
if errs:
|
2020-11-17 08:42:43 +01:00
|
|
|
msg = tr(TR.ADDONS_PLEASE_REPORT_THIS_TO_THE_RESPECTIVE)
|
2020-01-03 17:57:33 +01:00
|
|
|
showWarning(
|
|
|
|
"<br><br>".join(errs + [msg]),
|
|
|
|
parent=parent,
|
|
|
|
textFormat="rich",
|
2020-11-17 08:42:43 +01:00
|
|
|
title=tr(TR.ADDONS_ADDON_INSTALLATION_ERROR),
|
2020-01-03 17:57:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return not errs
|