v2.4.12: 修复j2p插件的pdf_dir参数问题; 优化对插件异常的处理; 内置一个Downloader方便简单测试; (#185)

This commit is contained in:
hect0x7 2023-12-19 21:14:39 +08:00 committed by GitHub
parent 51c1db7057
commit d0f408203d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 13 deletions

View File

@ -3,7 +3,6 @@ name: 导出收藏夹数据
on:
# schedule:
# - cron: "0 0 * * *"
push:
workflow_dispatch:
inputs:
IN_JM_USERNAME:

View File

@ -2,7 +2,7 @@
# 被依赖方 <--- 使用方
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
__version__ = '2.4.11'
__version__ = '2.4.12'
from .api import *
from .jm_plugin import *

View File

@ -207,6 +207,13 @@ class JmDownloader(DownloadCallback):
f'{self.__class__.__name__} Exit with exception: {exc_type, exc_val}'
)
@classmethod
def use(cls, *args, **kwargs):
"""
让本类替换JmModuleConfig.CLASS_DOWNLOADER
"""
JmModuleConfig.CLASS_DOWNLOADER = cls
class DoNotDownloadImage(JmDownloader):
"""
@ -221,3 +228,37 @@ class DoNotDownloadImage(JmDownloader):
# ensure make dir
self.option.decide_image_filepath(image)
pass
class JustDownloadSpecificCountImage(JmDownloader):
from threading import Lock
count_lock = Lock()
count = 0
def __init__(self, option: JmOption) -> None:
super().__init__(option)
def download_by_image_detail(self, image: JmImageDetail, client: JmcomicClient):
# ensure make dir
self.option.decide_image_filepath(image)
if self.try_countdown():
return super().download_by_image_detail(image, client)
def try_countdown(self):
if self.count < 0:
return False
with self.count_lock:
if self.count < 0:
return False
self.count -= 1
return self.count >= 0
@classmethod
def use(cls, count):
cls.count = count
super().use()

View File

@ -187,7 +187,7 @@ class JmOption:
# 其他配置
self.filepath = filepath
self.call_all_plugin('after_init')
self.call_all_plugin('after_init', safe=True)
"""
下面是decide系列方法为了支持重写和增加程序动态性
@ -494,7 +494,7 @@ class JmOption:
# 下面的方法为调用插件提供支持
def call_all_plugin(self, group: str, **extra):
def call_all_plugin(self, group: str, safe=True, **extra):
plugin_list: List[dict] = self.plugins.get(group, [])
if plugin_list is None or len(plugin_list) == 0:
return
@ -509,7 +509,13 @@ class JmOption:
ExceptionTool.require_true(plugin_class is not None, f'[{group}] 未注册的plugin: {key}')
self.invoke_plugin(plugin_class, kwargs, extra, pinfo)
try:
self.invoke_plugin(plugin_class, kwargs, extra, pinfo)
except BaseException as e:
if safe is True:
traceback_print_exec()
else:
raise e
def invoke_plugin(self, plugin_class, kwargs: Any, extra: dict, pinfo: dict):
# 检查插件的参数类型
@ -542,7 +548,7 @@ class JmOption:
except JmcomicException as e:
# 模块内部异常通过不是插件抛出的而是插件调用了例如ClientClient请求失败抛出的
self.handle_plugin_exception(e, pinfo, kwargs, plugin)
self.handle_plugin_jmcomic_exception(e, pinfo, kwargs, plugin)
except BaseException as e:
# 为插件兜底,捕获其他所有异常
@ -575,11 +581,11 @@ class JmOption:
# noinspection PyMethodMayBeStatic,PyUnusedLocal
def handle_plugin_unexpected_error(self, e, pinfo: dict, kwargs: dict, plugin):
msg = str(e)
jm_log('plugin.error', f'插件 [{plugin.plugin_key}],运行遇到未捕获异常,异常信息: {msg}')
jm_log('plugin.error', f'插件 [{plugin.plugin_key}],运行遇到未捕获异常,异常信息: [{msg}]')
raise e
# noinspection PyMethodMayBeStatic,PyUnusedLocal
def handle_plugin_exception(self, e, pinfo: dict, kwargs: dict, plugin):
def handle_plugin_jmcomic_exception(self, e, pinfo: dict, kwargs: dict, plugin):
msg = str(e)
jm_log('plugin.exception', f'插件 [{plugin.plugin_key}] 调用失败,异常信息: [{msg}]')
raise e

View File

@ -43,14 +43,22 @@ class JmOptionPlugin:
msg=msg
)
def require_true(self, case: Any, msg: str):
def require_true(self, case: Any, msg: str, is_param_validation=True):
"""
独立于ExceptionTool的一套异常抛出体系
:param case: 条件
:param msg: 报错信息
:param is_param_validation: True 表示 调用本方法是用于校验参数则会抛出特定异常PluginValidationException
"""
if case:
return
raise PluginValidationException(self, msg)
if is_param_validation:
raise PluginValidationException(self, msg)
else:
ExceptionTool.raises(msg)
def warning_lib_not_install(self, lib: str):
msg = (f'插件`{self.plugin_key}`依赖库: {lib},请先安装{lib}再使用。'
@ -658,7 +666,6 @@ class ConvertJpgToPdfPlugin(JmOptionPlugin):
if pdf_dir is None:
pdf_dir = photo_dir
else:
pdf_dir = fix_windir_name(pdf_dir)
mkdir_if_not_exists(pdf_dir)
pdf_filepath = f'{pdf_dir}{filename}.pdf'
@ -673,7 +680,8 @@ class ConvertJpgToPdfPlugin(JmOptionPlugin):
self.require_true(
code == 0,
'jpg图片合并为pdf失败'
'请确认你是否安装了magick安装网站: [http://www.imagemagick.org/]'
'请确认你是否安装了magick安装网站: [http://www.imagemagick.org/]',
False,
)
self.log(f'Convert Successfully: JM{photo.id}{pdf_filepath}')

View File

@ -28,7 +28,7 @@ def prepare_actions_input_and_secrets():
def main():
prepare_actions_input_and_secrets()
option = create_option('../assets/option/option_workflow_export_favorites.yml')
option.call_all_plugin('main')
option.call_all_plugin('main', safe=False)
if __name__ == '__main__':