2020-01-13 05:38:05 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
"""
|
2020-11-01 05:26:58 +01:00
|
|
|
Code for generating hooks.
|
2020-01-13 05:38:05 +01:00
|
|
|
"""
|
|
|
|
|
2021-05-13 12:15:51 +02:00
|
|
|
import os
|
2020-11-01 05:26:58 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-01-13 05:38:05 +01:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from operator import attrgetter
|
|
|
|
from typing import List, Optional
|
|
|
|
|
2020-01-13 23:54:07 +01:00
|
|
|
import stringcase
|
|
|
|
|
2020-01-13 05:38:05 +01:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Hook:
|
|
|
|
# the name of the hook. _filter or _hook is appending automatically.
|
|
|
|
name: str
|
|
|
|
# string of the typed arguments passed to the callback, eg
|
2020-01-13 08:59:52 +01:00
|
|
|
# ["kind: str", "val: int"]
|
|
|
|
args: List[str] = None
|
2020-01-13 05:38:05 +01:00
|
|
|
# string of the return type. if set, hook is a filter.
|
|
|
|
return_type: Optional[str] = None
|
|
|
|
# if add-ons may be relying on the legacy hook name, add it here
|
|
|
|
legacy_hook: Optional[str] = None
|
2020-01-13 09:37:08 +01:00
|
|
|
# if legacy hook takes no arguments but the new hook does, set this
|
|
|
|
legacy_no_args: bool = False
|
2020-01-15 03:14:32 +01:00
|
|
|
# docstring to add to hook class
|
|
|
|
doc: Optional[str] = None
|
2020-01-13 05:38:05 +01:00
|
|
|
|
|
|
|
def callable(self) -> str:
|
|
|
|
"Convert args into a Callable."
|
|
|
|
types = []
|
2020-01-13 08:59:52 +01:00
|
|
|
for arg in self.args or []:
|
2020-01-13 05:38:05 +01:00
|
|
|
(name, type) = arg.split(":")
|
2021-01-31 23:36:33 +01:00
|
|
|
type = '"' + type.strip() + '"'
|
2020-01-14 23:53:57 +01:00
|
|
|
types.append(type)
|
2020-01-13 05:38:05 +01:00
|
|
|
types_str = ", ".join(types)
|
|
|
|
return f"Callable[[{types_str}], {self.return_type or 'None'}]"
|
|
|
|
|
|
|
|
def arg_names(self) -> List[str]:
|
|
|
|
names = []
|
2020-01-13 08:59:52 +01:00
|
|
|
for arg in self.args or []:
|
2020-01-13 05:38:05 +01:00
|
|
|
if not arg:
|
|
|
|
continue
|
|
|
|
(name, type) = arg.split(":")
|
|
|
|
names.append(name.strip())
|
|
|
|
return names
|
|
|
|
|
|
|
|
def full_name(self) -> str:
|
|
|
|
return f"{self.name}_{self.kind()}"
|
|
|
|
|
|
|
|
def kind(self) -> str:
|
|
|
|
if self.return_type is not None:
|
|
|
|
return "filter"
|
|
|
|
else:
|
|
|
|
return "hook"
|
|
|
|
|
2020-01-13 23:54:07 +01:00
|
|
|
def classname(self) -> str:
|
2020-01-14 23:58:27 +01:00
|
|
|
return "_" + stringcase.pascalcase(self.full_name())
|
2020-01-13 23:54:07 +01:00
|
|
|
|
2020-01-13 05:38:05 +01:00
|
|
|
def list_code(self) -> str:
|
|
|
|
return f"""\
|
2020-01-15 03:14:32 +01:00
|
|
|
_hooks: List[{self.callable()}] = []
|
2020-01-13 05:38:05 +01:00
|
|
|
"""
|
|
|
|
|
2020-01-13 23:54:07 +01:00
|
|
|
def code(self) -> str:
|
2020-01-15 03:14:32 +01:00
|
|
|
appenddoc = f"({', '.join(self.args or [])})"
|
|
|
|
if self.doc:
|
|
|
|
classdoc = f" '''{self.doc}'''\n"
|
|
|
|
else:
|
|
|
|
classdoc = ""
|
2020-01-13 23:54:07 +01:00
|
|
|
code = f"""\
|
|
|
|
class {self.classname()}:
|
2020-01-15 03:14:32 +01:00
|
|
|
{classdoc}{self.list_code()}
|
2020-01-13 23:54:07 +01:00
|
|
|
|
2021-06-27 05:49:58 +02:00
|
|
|
def append(self, callback: {self.callable()}) -> None:
|
2020-01-15 03:14:32 +01:00
|
|
|
'''{appenddoc}'''
|
2021-06-27 05:49:58 +02:00
|
|
|
self._hooks.append(callback)
|
2020-01-13 23:54:07 +01:00
|
|
|
|
2021-06-27 05:49:58 +02:00
|
|
|
def remove(self, callback: {self.callable()}) -> None:
|
|
|
|
if callback in self._hooks:
|
|
|
|
self._hooks.remove(callback)
|
2020-06-04 10:20:03 +02:00
|
|
|
|
|
|
|
def count(self) -> int:
|
|
|
|
return len(self._hooks)
|
|
|
|
|
2020-01-13 23:54:07 +01:00
|
|
|
{self.fire_code()}
|
2020-01-15 07:53:24 +01:00
|
|
|
{self.name} = {self.classname()}()
|
2020-01-13 23:54:07 +01:00
|
|
|
"""
|
|
|
|
return code
|
|
|
|
|
2020-01-13 05:38:05 +01:00
|
|
|
def fire_code(self) -> str:
|
|
|
|
if self.return_type is not None:
|
|
|
|
# filter
|
|
|
|
return self.filter_fire_code()
|
|
|
|
else:
|
|
|
|
# hook
|
|
|
|
return self.hook_fire_code()
|
|
|
|
|
2020-01-13 09:37:08 +01:00
|
|
|
def legacy_args(self) -> str:
|
|
|
|
if self.legacy_no_args:
|
|
|
|
# hook name only
|
|
|
|
return f'"{self.legacy_hook}"'
|
|
|
|
else:
|
|
|
|
return ", ".join([f'"{self.legacy_hook}"'] + self.arg_names())
|
|
|
|
|
2020-01-13 05:38:05 +01:00
|
|
|
def hook_fire_code(self) -> str:
|
|
|
|
arg_names = self.arg_names()
|
2020-01-13 23:54:07 +01:00
|
|
|
args_including_self = ["self"] + (self.args or [])
|
2020-01-13 05:38:05 +01:00
|
|
|
out = f"""\
|
2020-01-13 23:54:07 +01:00
|
|
|
def __call__({", ".join(args_including_self)}) -> None:
|
|
|
|
for hook in self._hooks:
|
|
|
|
try:
|
|
|
|
hook({", ".join(arg_names)})
|
|
|
|
except:
|
|
|
|
# if the hook fails, remove it
|
|
|
|
self._hooks.remove(hook)
|
|
|
|
raise
|
2020-01-13 05:38:05 +01:00
|
|
|
"""
|
|
|
|
if self.legacy_hook:
|
|
|
|
out += f"""\
|
2020-01-13 23:54:07 +01:00
|
|
|
# legacy support
|
2020-11-01 05:26:58 +01:00
|
|
|
anki.hooks.runHook({self.legacy_args()})
|
2020-01-13 05:38:05 +01:00
|
|
|
"""
|
|
|
|
return out + "\n\n"
|
|
|
|
|
|
|
|
def filter_fire_code(self) -> str:
|
|
|
|
arg_names = self.arg_names()
|
2020-01-13 23:54:07 +01:00
|
|
|
args_including_self = ["self"] + (self.args or [])
|
2020-01-13 05:38:05 +01:00
|
|
|
out = f"""\
|
2020-01-13 23:54:07 +01:00
|
|
|
def __call__({", ".join(args_including_self)}) -> {self.return_type}:
|
|
|
|
for filter in self._hooks:
|
|
|
|
try:
|
|
|
|
{arg_names[0]} = filter({", ".join(arg_names)})
|
|
|
|
except:
|
|
|
|
# if the hook fails, remove it
|
|
|
|
self._hooks.remove(filter)
|
|
|
|
raise
|
2020-01-13 05:38:05 +01:00
|
|
|
"""
|
|
|
|
if self.legacy_hook:
|
|
|
|
out += f"""\
|
2020-01-13 23:54:07 +01:00
|
|
|
# legacy support
|
2020-11-01 05:26:58 +01:00
|
|
|
{arg_names[0]} = anki.hooks.runFilter({self.legacy_args()})
|
2020-01-13 05:38:05 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
out += f"""\
|
2020-01-13 23:54:07 +01:00
|
|
|
return {arg_names[0]}
|
2020-01-13 05:38:05 +01:00
|
|
|
"""
|
|
|
|
return out + "\n\n"
|
|
|
|
|
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
def write_file(path: str, hooks: List[Hook], prefix: str, suffix: str):
|
2020-01-13 05:38:05 +01:00
|
|
|
hooks.sort(key=attrgetter("name"))
|
2020-11-01 05:26:58 +01:00
|
|
|
code = prefix + "\n"
|
2020-01-13 05:38:05 +01:00
|
|
|
for hook in hooks:
|
2020-01-13 23:54:07 +01:00
|
|
|
code += hook.code()
|
2020-01-13 05:38:05 +01:00
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
code += "\n" + suffix
|
2020-01-13 05:38:05 +01:00
|
|
|
|
2021-05-13 12:15:51 +02:00
|
|
|
# work around issue with latest black
|
2021-05-18 22:19:09 +02:00
|
|
|
if sys.platform == "win32" and "HOME" in os.environ:
|
2021-05-13 12:15:51 +02:00
|
|
|
os.environ["USERPROFILE"] = os.environ["HOME"]
|
2020-05-10 02:58:42 +02:00
|
|
|
with open(path, "wb") as file:
|
2020-11-01 05:26:58 +01:00
|
|
|
file.write(code.encode("utf8"))
|
|
|
|
subprocess.run([sys.executable, "-m", "black", "-q", path], check=True)
|