Skip to content

Commit a88d83d

Browse files
committed
refactor: 废弃 zip 插件的 level 参数,打包粒度由所在钩子自动推导
- ZipPlugin: level 默认值改为 None,根据上下文自动推导(有 album 则合并,只有 photo 则单章) - Feature: export_zip 不再传递 level,_adapt_kwargs 移除 level 适配逻辑 - JmOption: compatible_with_old_versions 中新增 _migrate_zip_level,自动将旧 yaml 中的 level 配置等价迁移到正确的钩子位置 - 文档: 更新 option_file_syntax.md 和 13_export_and_feature.md 中 level 相关说明
1 parent db09b8b commit a88d83d

5 files changed

Lines changed: 68 additions & 20 deletions

File tree

assets/docs/sources/option_file_syntax.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,19 @@ plugins:
226226
after_album:
227227
- plugin: zip # 压缩文件插件
228228
kwargs:
229-
level: photo # 按照章节,一个章节一个压缩文件
230-
# level 也可以配成 album,表示一个本子对应一个压缩文件,该压缩文件会包含这个本子的所有章节
229+
# ⚠ level 参数已在 v2.6.19 废弃,打包粒度由插件所在的钩子自动推导:
230+
# 配置在 after_album 下 → 整本合并为一个压缩文件
231+
# 配置在 after_photo 下 → 每个章节各一个压缩文件
232+
# 旧配置会自动等价迁移,无需手动修改配置文件。
233+
# 迁移示例:
234+
# 旧:after_album + level: photo → 等价于:after_photo(不写 level)
235+
# 旧:after_album + level: album → 等价于:after_album(不写 level)
231236

232237
filename_rule: Ptitle # 压缩文件的命名规则
233238
# 请注意⚠ [https://github.com/hect0x7/JMComic-Crawler-Python/issues/223#issuecomment-2045227527]
234-
# filename_rule和level有对应关系
235-
# 如果level=[photo], filename_rule只能写Pxxx
236-
# 如果level=[album], filename_rule只能写Axxx
239+
# filename_rule和所在钩子有对应关系
240+
# 如果配置在 after_photo 下, filename_rule只能写Pxxx
241+
# 如果配置在 after_album 下, filename_rule只能写Axxx
237242

238243
zip_dir: D:/jmcomic/zip/ # 压缩文件存放的文件夹
239244

@@ -245,7 +250,7 @@ plugins:
245250
# dir_rule: # 新配置项,可取代旧的zip_dir和filename_rule
246251
# base_dir: D:/jmcomic-zip
247252
# rule: 'Bd / {Atitle} / [{Pid}]-{Ptitle}.zip' # 设置压缩文件夹规则,中间Atitle表示创建一层文件夹,名称是本子标题。[{Pid}]-{Ptitle}.zip 表示压缩文件的命名规则(需显式写出后缀名)
248-
# 使用此方法指定压缩包存储路径则无需和level对应
253+
# 使用此方法指定压缩包存储路径则无需和所在钩子对应
249254

250255
delete_original_file: true # 压缩成功后,删除所有原文件和文件夹
251256

assets/docs/sources/tutorial/13_export_and_feature.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ download_photo('456', option, extra=Feature.export_pdf)
8989
9090
### 2.5 智能适配规则
9191

92-
内置的导出 Feature 会根据调用的 API **自动适配**参数(命名规则、打包级别等):
92+
内置的导出 Feature 会根据调用的 API **自动适配**参数(命名规则、打包粒度等):
9393

9494
| 调用方式 | Feature.export_pdf | Feature.export_zip | Feature.export_long_img |
9595
|---------|-------------------|-------------------|----------------------|

src/jmcomic/jm_feature.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def __call__(self, **kwargs):
106106
def invoke(self, option, feature_from=None, **context):
107107
"""
108108
执行此 Feature 对应的插件。
109-
根据 feature_from 动态适配 filename_rule 和 level 等参数。
109+
根据 feature_from 动态适配 filename_rule 等参数。
110110
"""
111111
pclass = JmModuleConfig.REGISTRY_PLUGIN.get(self.plugin_key)
112112
if pclass is None:
@@ -127,29 +127,24 @@ def _adapt_kwargs(self, feature_from):
127127
"""
128128
根据 feature_from 动态适配参数:
129129
- filename_rule 前缀:download_album → A前缀,download_photo → P前缀
130-
- level:download_album → 'album',download_photo → 'photo'
131130
132131
注意:用户通过 __call__ 显式传入的参数(记录在 _user_keys 中)不会被适配。
133132
"""
134133
kwargs = self.kwargs.copy()
135134

136135
if feature_from == 'download_album':
137-
# album 模式:P前缀规则 → A前缀规则, level → album
136+
# album 模式:P前缀规则 → A前缀规则
138137
if 'filename_rule' not in self._user_keys and 'filename_rule' in kwargs:
139138
rule = kwargs['filename_rule']
140139
if rule and rule[0] == 'P':
141140
kwargs['filename_rule'] = 'A' + rule[1:]
142-
if 'level' not in self._user_keys and 'level' in kwargs and kwargs['level'] == 'photo':
143-
kwargs['level'] = 'album'
144141

145142
elif feature_from == 'download_photo':
146-
# photo 模式:A前缀规则 → P前缀规则, level → photo
143+
# photo 模式:A前缀规则 → P前缀规则
147144
if 'filename_rule' not in self._user_keys and 'filename_rule' in kwargs:
148145
rule = kwargs['filename_rule']
149146
if rule and rule[0] == 'A':
150147
kwargs['filename_rule'] = 'P' + rule[1:]
151-
if 'level' not in self._user_keys and 'level' in kwargs and kwargs['level'] == 'album':
152-
kwargs['level'] = 'photo'
153148

154149
return kwargs
155150

@@ -187,9 +182,8 @@ def __repr__(self):
187182

188183

189184
# 预定义特性(用插件类的 plugin_key 引用,附带默认参数)
190-
# filename_rule 和 level 会根据 feature_from 在 invoke 时动态适配:
191-
# download_album → A前缀 + level=album
192-
# download_photo → P前缀 + level=photo
185+
# filename_rule 会根据 feature_from 在 invoke 时动态适配 A/P 前缀
186+
# zip 的打包粒度由插件根据上下文(album/photo)自动推导,无需 level 参数
193187
Feature.export_pdf = PluginFeature(Img2pdfPlugin.plugin_key, pdf_dir='./', filename_rule='Atitle')
194-
Feature.export_zip = PluginFeature(ZipPlugin.plugin_key, level='photo', zip_dir='./', filename_rule='Ptitle')
188+
Feature.export_zip = PluginFeature(ZipPlugin.plugin_key, zip_dir='./', filename_rule='Ptitle')
195189
Feature.export_long_img = PluginFeature(LongImgPlugin.plugin_key, img_dir='./', filename_rule='Pid')

src/jmcomic/jm_option.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,52 @@ def compatible_with_old_versions(cls, dic):
330330
if 'plugin' in dic:
331331
dic['plugins'] = dic.pop('plugin')
332332

333+
# 3: zip 插件 level 参数迁移
334+
# level 已废弃,打包粒度由所在钩子上下文自动推导
335+
plugins = dic.get('plugins', {})
336+
if isinstance(plugins, dict):
337+
cls._migrate_zip_level(plugins)
338+
339+
@classmethod
340+
def _migrate_zip_level(cls, plugins: dict):
341+
"""
342+
zip 插件 level 参数迁移。
343+
344+
level 已废弃,打包粒度由所在钩子的上下文自动推导。
345+
迁移规则:level='album' → 确保在 after_album;其他 → 确保在 after_photo。
346+
"""
347+
for group in ['after_album', 'after_photo']:
348+
plugin_list = plugins.get(group)
349+
if not isinstance(plugin_list, list):
350+
continue
351+
i = 0
352+
while i < len(plugin_list):
353+
pinfo = plugin_list[i]
354+
if pinfo.get('plugin') != 'zip':
355+
i += 1
356+
continue
357+
kwargs = pinfo.get('kwargs') or {}
358+
if 'level' not in kwargs:
359+
# 旧版本默认值是 'photo'
360+
level = 'photo'
361+
else:
362+
level = kwargs.pop('level')
363+
364+
if group == 'after_album' and level != 'album':
365+
# after_album + level=photo → 等价迁移到 after_photo
366+
plugins.setdefault('after_photo', []).append(pinfo)
367+
plugin_list.pop(i)
368+
jm_log('option.migrate',
369+
f'[zip 插件迁移] level 参数已废弃,打包粒度由所在钩子自动推导。'
370+
f'已自动将 after_album 下的 zip(level={level!r}) 等价迁移到 after_photo。'
371+
f'等价写法:将 zip 插件从 after_album 移至 after_photo 并删除 level 配置项。')
372+
else:
373+
if level != 'photo':
374+
jm_log('option.migrate',
375+
f'[zip 插件迁移] level 参数已废弃,已自动移除。'
376+
f'打包粒度由所在钩子自动推导({group}{level})。')
377+
i += 1
378+
333379
def deconstruct(self) -> Dict:
334380
return {
335381
'version': JmModuleConfig.JM_OPTION_VER,

src/jmcomic/jm_plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def invoke(self,
321321
album: JmAlbumDetail = None,
322322
photo: JmPhotoDetail = None,
323323
delete_original_file=False,
324-
level='photo',
324+
level=None,
325325
filename_rule='Ptitle',
326326
suffix='zip',
327327
zip_dir='./',
@@ -332,6 +332,9 @@ def invoke(self,
332332
from .jm_downloader import JmDownloader
333333
downloader: JmDownloader
334334
self.downloader = downloader
335+
# level 自动推导:有 album 则合并打包,只有 photo 则单章打包
336+
if level is None:
337+
level = 'album' if album is not None else 'photo'
335338
self.level = level
336339
self.delete_original_file = delete_original_file
337340

0 commit comments

Comments
 (0)