2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# 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
|
2021-03-03 02:34:43 +01:00
|
|
|
from datetime import datetime
|
2021-10-03 10:59:42 +02:00
|
|
|
from typing import IO, Any, Callable, Iterable, 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 (
|
|
|
|
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
|
|
|
|
2021-03-08 03:40:15 +01:00
|
|
|
class AbortAddonImport(Exception):
|
2021-03-23 10:31:24 +01:00
|
|
|
"""If raised during add-on import, Anki will silently ignore this exception.
|
|
|
|
This allows you to terminate loading without an error being shown."""
|
2021-03-08 03:40:15 +01:00
|
|
|
|
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
@dataclass
|
|
|
|
class InstallOk:
|
|
|
|
name: str
|
2021-11-14 02:36:32 +01:00
|
|
|
conflicts: set[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
|
2021-10-03 10:59:42 +02:00
|
|
|
status_code: int | None = None
|
2020-01-19 01:37:15 +01:00
|
|
|
# set if an exception occurred
|
2021-10-03 10:59:42 +02:00
|
|
|
exception: Exception | None = None
|
2020-01-03 16:32:20 +01:00
|
|
|
|
|
|
|
|
2020-01-19 03:44:53 +01:00
|
|
|
# first arg is add-on id
|
2021-10-03 10:59:42 +02:00
|
|
|
DownloadLogEntry = tuple[int, Union[DownloadError, InstallError, InstallOk]]
|
2020-01-19 03:44:53 +01:00
|
|
|
|
|
|
|
|
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+$")
|
|
|
|
|
2021-10-25 06:50:13 +02:00
|
|
|
current_point_version = anki.utils.point_version()
|
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
|
2021-10-03 10:59:42 +02:00
|
|
|
provided_name: str | None
|
2020-01-19 03:44:53 +01:00
|
|
|
enabled: bool
|
|
|
|
installed_at: int
|
2021-10-03 10:59:42 +02:00
|
|
|
conflicts: list[str]
|
2020-01-27 08:01:09 +01:00
|
|
|
min_point_version: int
|
|
|
|
max_point_version: int
|
|
|
|
branch_index: int
|
2021-10-03 10:59:42 +02:00
|
|
|
human_version: str | None
|
2021-03-03 03:23:59 +01:00
|
|
|
update_enabled: bool
|
2021-10-03 10:59:42 +02:00
|
|
|
homepage: str | None
|
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
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def ankiweb_id(self) -> int | None:
|
2020-01-19 04:06:53 +01:00
|
|
|
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
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def page(self) -> str | None:
|
2021-09-30 16:21:15 +02:00
|
|
|
if self.ankiweb_id():
|
|
|
|
return f"{aqt.appShared}info/{self.dir_name}"
|
|
|
|
return self.homepage
|
|
|
|
|
2020-01-24 08:25:52 +01:00
|
|
|
@staticmethod
|
2021-10-03 10:59:42 +02:00
|
|
|
def from_json_meta(dir_name: str, json_meta: dict[str, Any]) -> AddonMeta:
|
2020-01-24 08:25:52 +01:00
|
|
|
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"),
|
2021-03-03 03:23:59 +01:00
|
|
|
update_enabled=json_meta.get("update_enabled", True),
|
2021-09-30 16:21:15 +02:00
|
|
|
homepage=json_meta.get("homepage"),
|
2020-01-24 08:25:52 +01:00
|
|
|
)
|
2020-01-19 03:44:53 +01:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
|
2021-08-29 03:23:47 +02:00
|
|
|
def package_name_valid(name: str) -> bool:
|
|
|
|
# embedded /?
|
|
|
|
base = os.path.basename(name)
|
|
|
|
if base != name:
|
|
|
|
return False
|
|
|
|
# tries to escape to parent?
|
|
|
|
root = os.getcwd()
|
|
|
|
subfolder = os.path.abspath(os.path.join(root, name))
|
|
|
|
if root.startswith(subfolder):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
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
|
2021-08-27 14:49:55 +02:00
|
|
|
"package": {"type": "string", "minLength": 1, "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},
|
2021-09-30 16:21:15 +02:00
|
|
|
# add-on page on AnkiWeb or some other webpage
|
|
|
|
"homepage": {"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
|
2021-10-03 10:59:42 +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-10-03 10:59:42 +02:00
|
|
|
def addonsFolder(self, dir: str | None = None) -> str:
|
2017-08-26 07:14:20 +02:00
|
|
|
root = self.mw.pm.addonFolder()
|
2021-08-29 03:23:47 +02:00
|
|
|
if dir is None:
|
2017-08-26 07:14:20 +02:00
|
|
|
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)
|
2021-03-08 03:40:15 +01:00
|
|
|
except AbortAddonImport:
|
|
|
|
pass
|
2015-09-27 00:55:15 +02:00
|
|
|
except:
|
2019-12-23 01:34:10 +01:00
|
|
|
showWarning(
|
2021-03-26 05:38:15 +01:00
|
|
|
tr.addons_failed_to_load(
|
2020-02-27 11:32:57 +01:00
|
|
|
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
|
2021-03-03 03:23:59 +01:00
|
|
|
json_obj["update_enabled"] = addon.update_enabled
|
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
|
2021-10-03 10:59:42 +02: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
|
2021-10-03 10:59:42 +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
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def toggleEnabled(self, dir: str, enable: bool | None = None) -> None:
|
2020-01-19 04:06:53 +01:00
|
|
|
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(
|
2021-03-26 05:38:15 +01:00
|
|
|
tr.addons_the_following_addons_are_incompatible_with(
|
2020-11-22 05:57:53 +01:00
|
|
|
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
|
|
|
|
2021-10-03 10:59:42 +02: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
|
|
|
|
2021-10-03 10:59:42 +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:
|
2021-03-26 04:48:26 +01:00
|
|
|
name += f" {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
|
|
|
|
######################################################################
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def allAddonConflicts(self) -> dict[str, list[str]]:
|
|
|
|
all_conflicts: dict[str, list[str]] = defaultdict(list)
|
2020-01-19 03:44:53 +01:00
|
|
|
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
|
|
|
|
|
2021-11-14 02:36:32 +01:00
|
|
|
def _disableConflicting(self, dir: str, conflicts: list[str] = None) -> set[str]:
|
2019-02-22 21:14:42 +01:00
|
|
|
conflicts = conflicts or self.addonConflicts(dir)
|
|
|
|
|
|
|
|
installed = self.allAddons()
|
2021-11-14 02:36:32 +01:00
|
|
|
found = {d for d in conflicts if d in installed and self.isEnabled(d)}
|
|
|
|
found.update(self.allAddonConflicts().get(dir, []))
|
2019-04-16 09:43:02 +02:00
|
|
|
|
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
|
|
|
######################################################################
|
|
|
|
|
2021-10-03 10:59:42 +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(
|
2021-10-03 10:59:42 +02:00
|
|
|
self, file: IO | str, manifest: dict[str, Any] = None
|
|
|
|
) -> 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"]
|
2021-08-29 03:23:47 +02:00
|
|
|
if not package_name_valid(package):
|
|
|
|
return InstallError(errmsg="invalid 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(
|
2021-03-26 05:21:04 +01:00
|
|
|
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(
|
2021-10-03 10:59:42 +02:00
|
|
|
self, paths: list[str], parent: QWidget = None
|
|
|
|
) -> tuple[list[str], list[str]]:
|
2020-01-03 17:57:33 +01:00
|
|
|
|
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"
|
2021-10-03 10:59:42 +02:00
|
|
|
) -> list[str]:
|
2020-01-03 16:32:20 +01:00
|
|
|
|
|
|
|
messages = {
|
2021-03-26 04:48:26 +01:00
|
|
|
"zip": tr.addons_corrupt_addon_file(),
|
|
|
|
"manifest": tr.addons_invalid_addon_manifest(),
|
2020-01-03 16:32:20 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 05:21:04 +01:00
|
|
|
msg = messages.get(result.errmsg, 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":
|
2021-03-26 05:21:04 +01:00
|
|
|
template = tr.addons_error_downloading_ids_errors(id=base, error=msg)
|
2020-01-03 16:32:20 +01:00
|
|
|
else:
|
2021-03-26 05:21:04 +01:00
|
|
|
template = 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"
|
2021-10-03 10:59:42 +02:00
|
|
|
) -> list[str]:
|
2020-01-03 16:32:20 +01:00
|
|
|
|
2020-11-22 05:57:53 +01:00
|
|
|
name = result.name or base
|
|
|
|
if mode == "download":
|
2021-03-26 05:21:04 +01:00
|
|
|
template = tr.addons_downloaded_fnames(fname=name)
|
2020-01-03 16:32:20 +01:00
|
|
|
else:
|
2021-03-26 05:21:04 +01:00
|
|
|
template = 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(
|
2021-03-26 04:48:26 +01:00
|
|
|
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:
|
2021-03-26 04:48:26 +01:00
|
|
|
strings.append(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
|
|
|
|
######################################################################
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def extract_update_info(self, items: list[dict]) -> list[UpdateInfo]:
|
|
|
|
def extract_one(item: dict) -> UpdateInfo:
|
2020-01-27 08:01:09 +01:00
|
|
|
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))
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
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
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def updates_required(self, items: list[UpdateInfo]) -> list[UpdateInfo]:
|
2020-01-19 01:37:15 +01:00
|
|
|
"""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):
|
2021-03-03 02:34:43 +01:00
|
|
|
need_update.append(item)
|
2020-04-15 10:47:04 +02:00
|
|
|
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.
|
2021-03-03 02:34:43 +01:00
|
|
|
need_update.append(item)
|
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
|
|
|
|
######################################################################
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
_configButtonActions: dict[str, Callable[[], bool | None]] = {}
|
|
|
|
_configUpdatedActions: dict[str, Callable[[Any], None]] = {}
|
2017-08-28 12:51:43 +02:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def addonConfigDefaults(self, dir: str) -> dict[str, Any] | None:
|
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]
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def configAction(self, addon: str) -> Callable[[], bool | None]:
|
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
|
|
|
|
######################################################################
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def getConfig(self, module: str) -> dict[str, Any] | None:
|
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
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def setConfigAction(self, module: str, fn: Callable[[], bool | None]) -> 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
|
|
|
|
######################################################################
|
|
|
|
|
2021-10-03 10:59:42 +02: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)
|
2021-03-26 04:48:26 +01:00
|
|
|
self.setWindowTitle(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()
|
|
|
|
|
2021-03-17 05:51:59 +01:00
|
|
|
def dragEnterEvent(self, event: QDragEnterEvent) -> 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()
|
|
|
|
|
2021-03-17 05:51:59 +01:00
|
|
|
def dropEvent(self, event: QDropEvent) -> 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:
|
2021-03-26 04:48:26 +01:00
|
|
|
return f"{name} {tr.addons_disabled2()}"
|
2020-01-19 04:37:55 +01:00
|
|
|
elif not addon.compatible():
|
2021-03-26 05:21:04 +01:00
|
|
|
return f"{name} {tr.addons_requires(val=self.compatible_string(addon))}"
|
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):
|
2021-10-05 05:53:01 +02:00
|
|
|
item.setForeground(Qt.GlobalColor.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
|
2021-09-30 16:21:15 +02:00
|
|
|
self.form.viewPage.setEnabled(addon.page() 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
|
|
|
|
2021-10-03 10:59:42 +02: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
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def onlyOneSelected(self) -> str | None:
|
2017-08-26 07:14:20 +02:00
|
|
|
dirs = self.selectedAddons()
|
|
|
|
if len(dirs) != 1:
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(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]
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def selected_addon_meta(self) -> AddonMeta | None:
|
2021-09-30 16:21:15 +02:00
|
|
|
idxs = [x.row() for x in self.form.addonList.selectedIndexes()]
|
|
|
|
if len(idxs) != 1:
|
|
|
|
showInfo(tr.addons_please_select_a_single_addon_first())
|
|
|
|
return None
|
|
|
|
return self.addons[idxs[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:
|
2021-09-30 16:21:15 +02:00
|
|
|
addon = self.selected_addon_meta()
|
2017-08-26 07:14:20 +02:00
|
|
|
if not addon:
|
|
|
|
return
|
2021-09-30 16:21:15 +02:00
|
|
|
if page := addon.page():
|
|
|
|
openLink(page)
|
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
|
2021-03-26 05:21:04 +01:00
|
|
|
if not askUser(tr.addons_delete_the_numd_selected_addon(count=len(selected))):
|
2017-08-26 07:14:20 +02:00
|
|
|
return
|
2021-06-15 02:01:29 +02:00
|
|
|
gui_hooks.addons_dialog_will_delete_addons(self, selected)
|
2017-08-26 07:14:20 +02:00
|
|
|
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)
|
|
|
|
|
2021-10-03 10:59:42 +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:
|
2021-03-26 04:48:26 +01:00
|
|
|
tooltip(tr.addons_no_updates_available())
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def onInstallFiles(self, paths: list[str] | None = None) -> bool | None:
|
2019-02-18 07:17:14 +01:00
|
|
|
if not paths:
|
2021-03-26 04:48:26 +01:00
|
|
|
key = f"{tr.addons_packaged_anki_addon()} (*{self.mgr.ext})"
|
2021-02-01 11:23:48 +01:00
|
|
|
paths_ = getFile(
|
2021-03-26 04:48:26 +01:00
|
|
|
self, 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:
|
2021-03-26 04:48:26 +01:00
|
|
|
tooltip(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:
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(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-03-17 05:51:59 +01:00
|
|
|
def __init__(self, dlg: AddonsDialog) -> 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
|
2021-10-03 10:59:42 +02: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(
|
2021-10-05 05:53:01 +02:00
|
|
|
tr.addons_browse_addons(), QDialogButtonBox.ButtonRole.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)
|
2021-10-05 02:01:45 +02: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:
|
2021-02-11 01:09:06 +01:00
|
|
|
openLink(f"{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:
|
2021-03-26 04:48:26 +01:00
|
|
|
showWarning(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
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def download_addon(client: HttpClient, id: int) -> 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(
|
2021-02-11 01:09:06 +01:00
|
|
|
f"{aqt.appShared}download/{id}?v=2.1&p={current_point_version}"
|
2020-01-24 08:25:52 +01:00
|
|
|
)
|
2020-01-19 01:37:15 +01:00
|
|
|
if resp.status_code != 200:
|
|
|
|
return DownloadError(status_code=resp.status_code)
|
|
|
|
|
2021-10-25 06:50:13 +02:00
|
|
|
data = client.stream_content(resp)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-10-03 10:59:42 +02: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):
|
2021-03-26 04:48:26 +01:00
|
|
|
buf += tr.addons_invalid_code_or_addon_not_available()
|
2019-04-16 09:38:38 +02:00
|
|
|
else:
|
2021-03-26 05:21:04 +01:00
|
|
|
buf += tr.qt_misc_unexpected_response_code(val=entry.status_code)
|
2020-01-19 01:37:15 +01:00
|
|
|
else:
|
|
|
|
buf += (
|
2021-03-26 04:48:26 +01:00
|
|
|
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:
|
2021-03-26 04:48:26 +01:00
|
|
|
buf += tr.addons_installed_successfully()
|
2017-08-26 07:14:20 +02:00
|
|
|
|
2020-01-19 01:37:15 +01:00
|
|
|
return buf
|
|
|
|
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def download_encountered_problem(log: list[DownloadLogEntry]) -> bool:
|
2020-01-19 01:37:15 +01:00
|
|
|
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("_", " ")
|
2021-02-16 02:12:05 +01:00
|
|
|
name = os.path.splitext(fname)[0].strip()
|
|
|
|
if not name:
|
|
|
|
name = str(id)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
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(
|
2021-10-03 10:59:42 +02:00
|
|
|
self, ids: list[int], on_done: Callable[[list[DownloadLogEntry]], None]
|
2020-01-19 01:37:15 +01:00
|
|
|
) -> None:
|
|
|
|
self.ids = ids
|
2021-10-03 10:59:42 +02:00
|
|
|
self.log: list[DownloadLogEntry] = []
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
self.dl_bytes = 0
|
|
|
|
self.last_tooltip = 0
|
|
|
|
|
|
|
|
self.on_done = on_done
|
|
|
|
|
2021-03-17 05:51:59 +01:00
|
|
|
parent = self.parent()
|
|
|
|
assert isinstance(parent, QWidget)
|
|
|
|
self.mgr.mw.progress.start(immediate=True, parent=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(
|
2021-03-26 05:38:15 +01:00
|
|
|
label=tr.addons_downloading_adbd_kb02fkb(
|
2020-11-22 05:57:53 +01:00
|
|
|
part=len(self.log) + 1,
|
|
|
|
total=len(self.ids),
|
2021-03-26 07:52:54 +01:00
|
|
|
kilobytes=self.dl_bytes // 1024,
|
2020-11-22 05:57:53 +01:00
|
|
|
)
|
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)
|
|
|
|
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def show_log_to_user(parent: QWidget, log: list[DownloadLogEntry]) -> None:
|
2020-01-19 01:37:15 +01:00
|
|
|
have_problem = download_encountered_problem(log)
|
|
|
|
|
|
|
|
if have_problem:
|
2021-03-26 04:48:26 +01:00
|
|
|
text = tr.addons_one_or_more_errors_occurred()
|
2020-01-19 01:37:15 +01:00
|
|
|
else:
|
2021-03-26 04:48:26 +01:00
|
|
|
text = tr.addons_download_complete_please_restart_anki_to()
|
2021-02-11 01:09:06 +01:00
|
|
|
text += f"<br><br>{download_log_to_html(log)}"
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
if have_problem:
|
|
|
|
showWarning(text, textFormat="rich", parent=parent)
|
|
|
|
else:
|
|
|
|
showInfo(text, parent=parent)
|
|
|
|
|
|
|
|
|
|
|
|
def download_addons(
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
2021-10-03 10:59:42 +02:00
|
|
|
ids: list[int],
|
|
|
|
on_done: Callable[[list[DownloadLogEntry]], None],
|
|
|
|
client: HttpClient | None = 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
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2021-03-03 02:34:43 +01:00
|
|
|
class ChooseAddonsToUpdateList(QListWidget):
|
|
|
|
ADDON_ID_ROLE = 101
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
2021-10-03 10:59:42 +02:00
|
|
|
updated_addons: list[UpdateInfo],
|
2021-03-03 02:34:43 +01:00
|
|
|
) -> None:
|
|
|
|
QListWidget.__init__(self, parent)
|
|
|
|
self.mgr = mgr
|
|
|
|
self.updated_addons = sorted(
|
|
|
|
updated_addons, key=lambda addon: addon.suitable_branch_last_modified
|
|
|
|
)
|
2021-03-03 03:54:37 +01:00
|
|
|
self.ignore_check_evt = False
|
2021-03-03 02:34:43 +01:00
|
|
|
self.setup()
|
2021-10-05 05:53:01 +02:00
|
|
|
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
2021-03-03 03:54:37 +01:00
|
|
|
qconnect(self.itemClicked, self.on_click)
|
|
|
|
qconnect(self.itemChanged, self.on_check)
|
|
|
|
qconnect(self.itemDoubleClicked, self.on_double_click)
|
2021-03-03 04:17:56 +01:00
|
|
|
qconnect(self.customContextMenuRequested, self.on_context_menu)
|
2021-03-03 02:34:43 +01:00
|
|
|
|
|
|
|
def setup(self) -> None:
|
2021-05-28 10:33:28 +02:00
|
|
|
header_item = QListWidgetItem(tr.addons_choose_update_update_all(), self)
|
2021-10-05 05:53:01 +02:00
|
|
|
header_item.setFlags(
|
|
|
|
Qt.ItemFlag(Qt.ItemFlag.ItemIsUserCheckable | Qt.ItemFlag.ItemIsEnabled)
|
|
|
|
)
|
2021-03-03 02:34:43 +01:00
|
|
|
self.header_item = header_item
|
|
|
|
for update_info in self.updated_addons:
|
|
|
|
addon_id = update_info.id
|
2021-03-03 03:23:59 +01:00
|
|
|
addon_meta = self.mgr.addon_meta(str(addon_id))
|
|
|
|
update_enabled = addon_meta.update_enabled
|
|
|
|
addon_name = addon_meta.human_name()
|
2021-03-03 02:34:43 +01:00
|
|
|
update_timestamp = update_info.suitable_branch_last_modified
|
|
|
|
update_time = datetime.fromtimestamp(update_timestamp)
|
|
|
|
|
|
|
|
addon_label = f"{update_time:%Y-%m-%d} {addon_name}"
|
|
|
|
item = QListWidgetItem(addon_label, self)
|
|
|
|
# Not user checkable because it overlaps with itemClicked signal
|
2021-10-05 05:53:01 +02:00
|
|
|
item.setFlags(Qt.ItemFlag(Qt.ItemFlag.ItemIsEnabled))
|
2021-03-03 03:23:59 +01:00
|
|
|
if update_enabled:
|
2021-10-05 05:53:01 +02:00
|
|
|
item.setCheckState(Qt.CheckState.Checked)
|
2021-03-03 03:23:59 +01:00
|
|
|
else:
|
2021-10-05 05:53:01 +02:00
|
|
|
item.setCheckState(Qt.CheckState.Unchecked)
|
2021-03-03 02:34:43 +01:00
|
|
|
item.setData(self.ADDON_ID_ROLE, addon_id)
|
2021-03-03 03:23:59 +01:00
|
|
|
self.refresh_header_check_state()
|
2021-03-03 02:34:43 +01:00
|
|
|
|
2021-03-03 04:05:24 +01:00
|
|
|
def bool_to_check(self, check_bool: bool) -> Qt.CheckState:
|
|
|
|
if check_bool:
|
2021-10-05 05:53:01 +02:00
|
|
|
return Qt.CheckState.Checked
|
2021-03-03 04:05:24 +01:00
|
|
|
else:
|
2021-10-05 05:53:01 +02:00
|
|
|
return Qt.CheckState.Unchecked
|
2021-03-03 04:05:24 +01:00
|
|
|
|
|
|
|
def checked(self, item: QListWidgetItem) -> bool:
|
2021-10-05 05:53:01 +02:00
|
|
|
return item.checkState() == Qt.CheckState.Checked
|
2021-03-03 04:05:24 +01:00
|
|
|
|
2021-03-03 03:54:37 +01:00
|
|
|
def on_click(self, item: QListWidgetItem) -> None:
|
2021-03-03 02:34:43 +01:00
|
|
|
if item == self.header_item:
|
|
|
|
return
|
2021-03-03 04:05:24 +01:00
|
|
|
checked = self.checked(item)
|
|
|
|
self.check_item(item, self.bool_to_check(not checked))
|
2021-03-03 03:23:59 +01:00
|
|
|
self.refresh_header_check_state()
|
2021-03-03 02:34:43 +01:00
|
|
|
|
2021-03-03 03:54:37 +01:00
|
|
|
def on_check(self, item: QListWidgetItem) -> None:
|
|
|
|
if self.ignore_check_evt:
|
|
|
|
return
|
|
|
|
if item == self.header_item:
|
2021-03-03 04:05:24 +01:00
|
|
|
self.header_checked(item.checkState())
|
2021-03-03 03:54:37 +01:00
|
|
|
|
|
|
|
def on_double_click(self, item: QListWidgetItem) -> None:
|
2021-03-03 02:34:43 +01:00
|
|
|
if item == self.header_item:
|
2021-03-03 04:05:24 +01:00
|
|
|
checked = self.checked(item)
|
|
|
|
self.check_item(self.header_item, self.bool_to_check(not checked))
|
|
|
|
self.header_checked(self.bool_to_check(not checked))
|
2021-03-03 03:54:37 +01:00
|
|
|
|
2021-03-03 04:17:56 +01:00
|
|
|
def on_context_menu(self, point: QPoint) -> None:
|
|
|
|
item = self.itemAt(point)
|
|
|
|
addon_id = item.data(self.ADDON_ID_ROLE)
|
|
|
|
m = QMenu()
|
2021-03-26 04:48:26 +01:00
|
|
|
a = m.addAction(tr.addons_view_addon_page())
|
2021-03-03 04:17:56 +01:00
|
|
|
qconnect(a.triggered, lambda _: openLink(f"{aqt.appShared}info/{addon_id}"))
|
2021-10-05 02:01:45 +02:00
|
|
|
m.exec(QCursor.pos())
|
2021-03-03 04:17:56 +01:00
|
|
|
|
2021-03-03 03:54:37 +01:00
|
|
|
def check_item(self, item: QListWidgetItem, check: Qt.CheckState) -> None:
|
|
|
|
"call item.setCheckState without triggering on_check"
|
|
|
|
self.ignore_check_evt = True
|
|
|
|
item.setCheckState(check)
|
|
|
|
self.ignore_check_evt = False
|
2021-03-03 02:34:43 +01:00
|
|
|
|
2021-03-03 03:54:37 +01:00
|
|
|
def header_checked(self, check: Qt.CheckState) -> None:
|
2021-03-03 02:34:43 +01:00
|
|
|
for i in range(1, self.count()):
|
2021-03-03 03:54:37 +01:00
|
|
|
self.check_item(self.item(i), check)
|
2021-03-03 02:34:43 +01:00
|
|
|
|
2021-03-03 03:23:59 +01:00
|
|
|
def refresh_header_check_state(self) -> None:
|
2021-03-03 02:34:43 +01:00
|
|
|
for i in range(1, self.count()):
|
|
|
|
item = self.item(i)
|
2021-03-03 04:05:24 +01:00
|
|
|
if not self.checked(item):
|
2021-10-05 05:53:01 +02:00
|
|
|
self.check_item(self.header_item, Qt.CheckState.Unchecked)
|
2021-03-03 03:23:59 +01:00
|
|
|
return
|
2021-10-05 05:53:01 +02:00
|
|
|
self.check_item(self.header_item, Qt.CheckState.Checked)
|
2021-03-03 02:34:43 +01:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def get_selected_addon_ids(self) -> list[int]:
|
2021-03-03 02:34:43 +01:00
|
|
|
addon_ids = []
|
|
|
|
for i in range(1, self.count()):
|
|
|
|
item = self.item(i)
|
2021-03-03 04:05:24 +01:00
|
|
|
if self.checked(item):
|
2021-03-03 03:23:59 +01:00
|
|
|
addon_id = item.data(self.ADDON_ID_ROLE)
|
|
|
|
addon_ids.append(addon_id)
|
2021-03-03 02:34:43 +01:00
|
|
|
return addon_ids
|
|
|
|
|
2021-03-03 03:23:59 +01:00
|
|
|
def save_check_state(self) -> None:
|
|
|
|
for i in range(1, self.count()):
|
|
|
|
item = self.item(i)
|
|
|
|
addon_id = item.data(self.ADDON_ID_ROLE)
|
|
|
|
addon_meta = self.mgr.addon_meta(str(addon_id))
|
2021-03-03 04:05:24 +01:00
|
|
|
addon_meta.update_enabled = self.checked(item)
|
2021-03-03 03:23:59 +01:00
|
|
|
self.mgr.write_addon_meta(addon_meta)
|
|
|
|
|
2021-03-03 02:34:43 +01:00
|
|
|
|
|
|
|
class ChooseAddonsToUpdateDialog(QDialog):
|
|
|
|
def __init__(
|
2021-10-03 10:59:42 +02:00
|
|
|
self, parent: QWidget, mgr: AddonManager, updated_addons: list[UpdateInfo]
|
2021-03-03 02:34:43 +01:00
|
|
|
) -> None:
|
|
|
|
QDialog.__init__(self, parent)
|
2021-03-26 04:48:26 +01:00
|
|
|
self.setWindowTitle(tr.addons_choose_update_window_title())
|
2021-10-05 05:53:01 +02:00
|
|
|
self.setWindowModality(Qt.WindowModality.WindowModal)
|
2021-03-03 02:34:43 +01:00
|
|
|
self.mgr = mgr
|
|
|
|
self.updated_addons = updated_addons
|
|
|
|
self.setup()
|
|
|
|
restoreGeom(self, "addonsChooseUpdate")
|
|
|
|
|
|
|
|
def setup(self) -> None:
|
|
|
|
layout = QVBoxLayout()
|
2021-03-26 04:48:26 +01:00
|
|
|
label = QLabel(tr.addons_the_following_addons_have_updates_available())
|
2021-03-03 02:34:43 +01:00
|
|
|
layout.addWidget(label)
|
|
|
|
addons_list_widget = ChooseAddonsToUpdateList(
|
|
|
|
self, self.mgr, self.updated_addons
|
|
|
|
)
|
|
|
|
layout.addWidget(addons_list_widget)
|
|
|
|
self.addons_list_widget = addons_list_widget
|
|
|
|
|
2021-10-05 05:53:01 +02:00
|
|
|
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) # type: ignore
|
|
|
|
qconnect(
|
|
|
|
button_box.button(QDialogButtonBox.StandardButton.Ok).clicked, self.accept
|
|
|
|
)
|
|
|
|
qconnect(
|
|
|
|
button_box.button(QDialogButtonBox.StandardButton.Cancel).clicked,
|
|
|
|
self.reject,
|
|
|
|
)
|
2021-03-03 02:34:43 +01:00
|
|
|
layout.addWidget(button_box)
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def ask(self) -> list[int]:
|
2021-03-03 02:34:43 +01:00
|
|
|
"Returns a list of selected addons' ids"
|
2021-10-05 02:01:45 +02:00
|
|
|
ret = self.exec()
|
2021-03-03 02:34:43 +01:00
|
|
|
saveGeom(self, "addonsChooseUpdate")
|
2021-03-03 03:23:59 +01:00
|
|
|
self.addons_list_widget.save_check_state()
|
2021-10-05 05:53:01 +02:00
|
|
|
if ret == QDialog.DialogCode.Accepted:
|
2021-03-03 02:34:43 +01:00
|
|
|
return self.addons_list_widget.get_selected_addon_ids()
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2021-10-03 10:59:42 +02: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."""
|
2021-10-03 10:59:42 +02: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]
|
2021-10-03 10:59:42 +02:00
|
|
|
) -> Iterable[dict]:
|
2020-01-19 01:37:15 +01:00
|
|
|
"""Get update info from AnkiWeb.
|
|
|
|
|
|
|
|
Chunk must not contain more than 25 ids."""
|
2021-02-11 01:09:06 +01:00
|
|
|
resp = client.get(f"{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:
|
2021-02-11 00:37:38 +01:00
|
|
|
raise Exception(f"Unexpected response code from AnkiWeb: {resp.status_code}")
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def check_and_prompt_for_updates(
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
2021-10-03 10:59:42 +02:00
|
|
|
on_done: Callable[[list[DownloadLogEntry]], None],
|
2021-03-09 14:27:28 +01:00
|
|
|
requested_by_user: bool = True,
|
2020-08-01 07:50:27 +02:00
|
|
|
) -> None:
|
2021-10-03 10:59:42 +02:00
|
|
|
def on_updates_received(client: HttpClient, items: list[dict]) -> None:
|
2021-03-09 14:27:28 +01:00
|
|
|
handle_update_info(parent, mgr, client, items, on_done, requested_by_user)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
check_for_updates(mgr, on_updates_received)
|
|
|
|
|
|
|
|
|
|
|
|
def check_for_updates(
|
2021-10-03 10:59:42 +02: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
|
|
|
|
2021-10-03 10:59:42 +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(
|
2021-10-03 10:59:42 +02:00
|
|
|
current_point_version: int, current_branch_idx: int, info_json: dict
|
2020-01-27 08:01:09 +01:00
|
|
|
) -> 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,
|
2021-10-03 10:59:42 +02:00
|
|
|
items: list[dict],
|
|
|
|
on_done: Callable[[list[DownloadLogEntry]], None],
|
2021-03-09 14:27:28 +01:00
|
|
|
requested_by_user: bool = True,
|
2020-01-19 01:37:15 +01:00
|
|
|
) -> 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
|
|
|
|
2021-03-03 02:34:43 +01:00
|
|
|
updated_addons = mgr.updates_required(update_info)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
2021-03-03 02:34:43 +01:00
|
|
|
if not updated_addons:
|
2020-01-19 01:37:15 +01:00
|
|
|
on_done([])
|
|
|
|
return
|
|
|
|
|
2021-03-09 14:27:28 +01:00
|
|
|
prompt_to_update(parent, mgr, client, updated_addons, on_done, requested_by_user)
|
2020-01-19 01:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def prompt_to_update(
|
|
|
|
parent: QWidget,
|
|
|
|
mgr: AddonManager,
|
2020-01-19 02:33:27 +01:00
|
|
|
client: HttpClient,
|
2021-10-03 10:59:42 +02:00
|
|
|
updated_addons: list[UpdateInfo],
|
|
|
|
on_done: Callable[[list[DownloadLogEntry]], None],
|
2021-03-09 14:27:28 +01:00
|
|
|
requested_by_user: bool = True,
|
2020-01-19 01:37:15 +01:00
|
|
|
) -> None:
|
2021-03-09 14:27:28 +01:00
|
|
|
if not requested_by_user:
|
|
|
|
prompt_update = False
|
|
|
|
for addon in updated_addons:
|
|
|
|
if mgr.addon_meta(str(addon.id)).update_enabled:
|
|
|
|
prompt_update = True
|
|
|
|
if not prompt_update:
|
|
|
|
return
|
|
|
|
|
2021-03-03 02:34:43 +01:00
|
|
|
ids = ChooseAddonsToUpdateDialog(parent, mgr, updated_addons).ask()
|
|
|
|
if not ids:
|
2020-01-19 01:37:15 +01:00
|
|
|
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-10-03 10:59:42 +02:00
|
|
|
def __init__(self, dlg: AddonsDialog, 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)
|
2021-10-05 05:53:01 +02:00
|
|
|
restore = self.form.buttonBox.button(
|
|
|
|
QDialogButtonBox.StandardButton.RestoreDefaults
|
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(restore.clicked, self.onRestoreDefaults)
|
2021-11-16 22:18:48 +01:00
|
|
|
ok = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Ok)
|
|
|
|
ok.setShortcut(QKeySequence("Ctrl+Return"))
|
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(
|
2021-03-26 05:38:15 +01:00
|
|
|
tr.addons_config_window_title(
|
2020-08-30 23:35:17 +02:00
|
|
|
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)
|
2021-03-26 04:48:26 +01:00
|
|
|
tooltip(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:
|
2021-10-05 05:53:01 +02:00
|
|
|
font_mono = QFontDatabase.systemFont(QFontDatabase.SystemFont.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)
|
|
|
|
|
2021-10-03 10:59:42 +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)
|
2021-03-17 05:51:59 +01:00
|
|
|
jsonschema.validate(new_conf, self.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:
|
2021-03-26 05:38:15 +01:00
|
|
|
msg = tr.addons_config_validation_error(
|
2020-03-08 16:27:53 +01:00
|
|
|
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:
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(f"{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):
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(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,
|
2021-10-03 10:59:42 +02:00
|
|
|
paths: list[str],
|
|
|
|
parent: QWidget | None = None,
|
2020-01-04 04:34:16 +01:00
|
|
|
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)
|
2021-03-26 04:48:26 +01:00
|
|
|
q = 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,
|
2021-03-26 04:48:26 +01:00
|
|
|
title=tr.addons_install_anki_addon(),
|
2020-01-03 17:57:33 +01:00
|
|
|
type="warning",
|
2021-10-05 05:53:01 +02:00
|
|
|
customBtns=[
|
|
|
|
QMessageBox.StandardButton.No,
|
|
|
|
QMessageBox.StandardButton.Yes,
|
|
|
|
],
|
2020-01-03 17:57:33 +01:00
|
|
|
)
|
2021-10-05 05:53:01 +02:00
|
|
|
== QMessageBox.StandardButton.Yes
|
2020-01-03 17:57:33 +01:00
|
|
|
):
|
|
|
|
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:
|
2021-03-26 04:48:26 +01:00
|
|
|
log_html += f"<br><br>{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",
|
2021-03-26 04:48:26 +01:00
|
|
|
title=tr.addons_installation_complete(),
|
2020-01-03 17:57:33 +01:00
|
|
|
)
|
|
|
|
if errs:
|
2021-03-26 04:48:26 +01:00
|
|
|
msg = 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",
|
2021-03-26 04:48:26 +01:00
|
|
|
title=tr.addons_addon_installation_error(),
|
2020-01-03 17:57:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return not errs
|