Skip to content

Commit d0f4082

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

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

.github/workflows/export_favorites.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: 导出收藏夹数据
33
on:
44
# schedule:
55
# - cron: "0 0 * * *"
6-
push:
76
workflow_dispatch:
87
inputs:
98
IN_JM_USERNAME:

src/jmcomic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# 被依赖方 <--- 使用方
33
# config <--- entity <--- toolkit <--- client <--- option <--- downloader
44

5-
__version__ = '2.4.11'
5+
__version__ = '2.4.12'
66

77
from .api import *
88
from .jm_plugin import *

src/jmcomic/jm_downloader.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):
207207
f'{self.__class__.__name__} Exit with exception: {exc_type, exc_val}'
208208
)
209209

210+
@classmethod
211+
def use(cls, *args, **kwargs):
212+
"""
213+
让本类替换JmModuleConfig.CLASS_DOWNLOADER
214+
"""
215+
JmModuleConfig.CLASS_DOWNLOADER = cls
216+
210217

211218
class DoNotDownloadImage(JmDownloader):
212219
"""
@@ -221,3 +228,37 @@ def download_by_image_detail(self, image: JmImageDetail, client: JmcomicClient):
221228
# ensure make dir
222229
self.option.decide_image_filepath(image)
223230
pass
231+
232+
233+
class JustDownloadSpecificCountImage(JmDownloader):
234+
from threading import Lock
235+
236+
count_lock = Lock()
237+
count = 0
238+
239+
def __init__(self, option: JmOption) -> None:
240+
super().__init__(option)
241+
242+
def download_by_image_detail(self, image: JmImageDetail, client: JmcomicClient):
243+
# ensure make dir
244+
self.option.decide_image_filepath(image)
245+
246+
if self.try_countdown():
247+
return super().download_by_image_detail(image, client)
248+
249+
def try_countdown(self):
250+
if self.count < 0:
251+
return False
252+
253+
with self.count_lock:
254+
if self.count < 0:
255+
return False
256+
257+
self.count -= 1
258+
259+
return self.count >= 0
260+
261+
@classmethod
262+
def use(cls, count):
263+
cls.count = count
264+
super().use()

src/jmcomic/jm_option.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def __init__(self,
187187
# 其他配置
188188
self.filepath = filepath
189189

190-
self.call_all_plugin('after_init')
190+
self.call_all_plugin('after_init', safe=True)
191191

192192
"""
193193
下面是decide系列方法,为了支持重写和增加程序动态性。
@@ -494,7 +494,7 @@ def download_photo(self, photo_id):
494494

495495
# 下面的方法为调用插件提供支持
496496

497-
def call_all_plugin(self, group: str, **extra):
497+
def call_all_plugin(self, group: str, safe=True, **extra):
498498
plugin_list: List[dict] = self.plugins.get(group, [])
499499
if plugin_list is None or len(plugin_list) == 0:
500500
return
@@ -509,7 +509,13 @@ def call_all_plugin(self, group: str, **extra):
509509

510510
ExceptionTool.require_true(plugin_class is not None, f'[{group}] 未注册的plugin: {key}')
511511

512-
self.invoke_plugin(plugin_class, kwargs, extra, pinfo)
512+
try:
513+
self.invoke_plugin(plugin_class, kwargs, extra, pinfo)
514+
except BaseException as e:
515+
if safe is True:
516+
traceback_print_exec()
517+
else:
518+
raise e
513519

514520
def invoke_plugin(self, plugin_class, kwargs: Any, extra: dict, pinfo: dict):
515521
# 检查插件的参数类型
@@ -542,7 +548,7 @@ def invoke_plugin(self, plugin_class, kwargs: Any, extra: dict, pinfo: dict):
542548

543549
except JmcomicException as e:
544550
# 模块内部异常,通过不是插件抛出的,而是插件调用了例如Client,Client请求失败抛出的
545-
self.handle_plugin_exception(e, pinfo, kwargs, plugin)
551+
self.handle_plugin_jmcomic_exception(e, pinfo, kwargs, plugin)
546552

547553
except BaseException as e:
548554
# 为插件兜底,捕获其他所有异常
@@ -575,11 +581,11 @@ def handle_plugin_valid_exception(self, e, pinfo: dict, kwargs: dict, plugin):
575581
# noinspection PyMethodMayBeStatic,PyUnusedLocal
576582
def handle_plugin_unexpected_error(self, e, pinfo: dict, kwargs: dict, plugin):
577583
msg = str(e)
578-
jm_log('plugin.error', f'插件 [{plugin.plugin_key}],运行遇到未捕获异常,异常信息: {msg}')
584+
jm_log('plugin.error', f'插件 [{plugin.plugin_key}],运行遇到未捕获异常,异常信息: [{msg}]')
579585
raise e
580586

581587
# noinspection PyMethodMayBeStatic,PyUnusedLocal
582-
def handle_plugin_exception(self, e, pinfo: dict, kwargs: dict, plugin):
588+
def handle_plugin_jmcomic_exception(self, e, pinfo: dict, kwargs: dict, plugin):
583589
msg = str(e)
584590
jm_log('plugin.exception', f'插件 [{plugin.plugin_key}] 调用失败,异常信息: [{msg}]')
585591
raise e

src/jmcomic/jm_plugin.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,22 @@ def log(self, msg, topic=None):
4343
msg=msg
4444
)
4545

46-
def require_true(self, case: Any, msg: str):
46+
def require_true(self, case: Any, msg: str, is_param_validation=True):
4747
"""
4848
独立于ExceptionTool的一套异常抛出体系
49+
50+
51+
:param case: 条件
52+
:param msg: 报错信息
53+
:param is_param_validation: True 表示 调用本方法是用于校验参数,则会抛出特定异常(PluginValidationException)
4954
"""
5055
if case:
5156
return
5257

53-
raise PluginValidationException(self, msg)
58+
if is_param_validation:
59+
raise PluginValidationException(self, msg)
60+
else:
61+
ExceptionTool.raises(msg)
5462

5563
def warning_lib_not_install(self, lib: str):
5664
msg = (f'插件`{self.plugin_key}`依赖库: {lib},请先安装{lib}再使用。'
@@ -658,7 +666,6 @@ def invoke(self,
658666
if pdf_dir is None:
659667
pdf_dir = photo_dir
660668
else:
661-
pdf_dir = fix_windir_name(pdf_dir)
662669
mkdir_if_not_exists(pdf_dir)
663670

664671
pdf_filepath = f'{pdf_dir}{filename}.pdf'
@@ -673,7 +680,8 @@ def get_cmd(suffix='.jpg'):
673680
self.require_true(
674681
code == 0,
675682
'jpg图片合并为pdf失败!'
676-
'请确认你是否安装了magick,安装网站: [http://www.imagemagick.org/]'
683+
'请确认你是否安装了magick,安装网站: [http://www.imagemagick.org/]',
684+
False,
677685
)
678686

679687
self.log(f'Convert Successfully: JM{photo.id}{pdf_filepath}')

usage/workflow_export_favorites.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def env(match: Match) -> str:
2828
def main():
2929
prepare_actions_input_and_secrets()
3030
option = create_option('../assets/option/option_workflow_export_favorites.yml')
31-
option.call_all_plugin('main')
31+
option.call_all_plugin('main', safe=False)
3232

3333

3434
if __name__ == '__main__':

0 commit comments

Comments
 (0)