Skip to content

Commit 2d23306

Browse files
authored
v2.2.6: 优化zip插件提高可扩展性,增加测试用例 (#129)
1 parent cd8952c commit 2d23306

File tree

4 files changed

+91
-28
lines changed

4 files changed

+91
-28
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
name: 跑测试
22

33
on:
4+
schedule:
5+
- cron: "0 8 * * *"
46
workflow_dispatch:
57
push:
68
branches: [ "dev" ]
79
paths:
810
- 'src/**/*.py'
9-
- 'test/**/*.py'
11+
- 'tests/**/*.py'
1012
- '.github/workflows/test.yml'
1113
- 'assets/config/option_test.yml'
1214

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.2.5'
5+
__version__ = '2.2.6'
66

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

src/jmcomic/jm_plugin.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ def filter_iter_objs(self, iter_objs):
186186
class ZipPlugin(JmOptionPlugin):
187187
plugin_key = 'zip'
188188

189+
# noinspection PyAttributeOutsideInit
189190
def invoke(self,
190191
album: JmAlbumDetail,
191192
downloader,
@@ -198,49 +199,39 @@ def invoke(self,
198199

199200
from .jm_downloader import JmDownloader
200201
downloader: JmDownloader
202+
self.downloader = downloader
203+
self.level = level
204+
self.delete_original_file = delete_original_file
201205

206+
# 确保压缩文件所在文件夹存在
202207
zip_dir = JmcomicText.parse_to_abspath(zip_dir)
203208
mkdir_if_not_exists(zip_dir)
209+
210+
# 原文件夹 -> zip文件
211+
dir_zip_dict = {}
204212
photo_dict = downloader.all_downloaded[album]
205-
original_file_dir_list = []
206213

207214
if level == 'album':
208215
zip_path = self.get_zip_path(album, None, filename_rule, suffix, zip_dir)
209-
original_file_dir_list.append(
210-
self.zip_album(album, photo_dict, zip_path)
211-
)
216+
dir_path = self.zip_album(album, photo_dict, zip_path)
217+
dir_zip_dict[dir_path] = zip_path
212218

213219
elif level == 'photo':
214-
# dst_file_list = []
215-
216220
for photo, image_list in photo_dict.items():
217221
zip_path = self.get_zip_path(None, photo, filename_rule, suffix, zip_dir)
218-
original_file_dir_list.append(
219-
self.zip_photo(photo, image_list, zip_path)
220-
)
221-
# dst_file_list.append(zip_path)
222+
dir_path = self.zip_photo(photo, image_list, zip_path)
223+
dir_zip_dict[dir_path] = zip_path
222224

223225
else:
224226
raise NotImplementedError(f'level: {level}')
225227

226-
if delete_original_file is True:
227-
self.delete_all_files(downloader.all_downloaded, original_file_dir_list)
228-
229-
# noinspection PyMethodMayBeStatic
230-
def delete_all_files(self, all_downloaded: dict, dir_list: List[str]):
231-
import os
232-
for album, photo_dict in all_downloaded.items():
233-
for photo, image_list in photo_dict.items():
234-
for f, image in image_list:
235-
os.remove(f)
236-
jm_debug('plugin.zip.remove', f'移除原文件: {f}')
237-
238-
for d in dir_list:
239-
if len(files_of_dir(d)) == 0:
240-
os.removedirs(d)
241-
jm_debug('plugin.zip.remove', f'移除文件夹: {d}')
228+
self.after_zip(dir_zip_dict)
242229

243230
def zip_photo(self, photo, image_list: list, zip_path: str):
231+
"""
232+
压缩photo文件夹
233+
@return: photo文件夹路径
234+
"""
244235
photo_dir = self.option.decide_image_save_dir(photo) \
245236
if len(image_list) == 0 \
246237
else os.path.dirname(image_list[0][0])
@@ -257,6 +248,10 @@ def zip_photo(self, photo, image_list: list, zip_path: str):
257248
return photo_dir
258249

259250
def zip_album(self, album, photo_dict: dict, zip_path):
251+
"""
252+
压缩album文件夹
253+
@return: album文件夹路径
254+
"""
260255
album_dir = self.option.decide_album_dir(album)
261256
all_filepath: Set[str] = set()
262257

@@ -279,15 +274,43 @@ def zip_album(self, album, photo_dict: dict, zip_path):
279274
jm_debug('plugin.zip.finish', f'压缩本子[{album.album_id}]成功 → {zip_path}')
280275
return album_dir
281276

277+
def after_zip(self, dir_zip_dict: Dict[str, str]):
278+
# 是否要删除所有原文件
279+
if self.delete_original_file is True:
280+
self.delete_all_files_and_empty_dir(
281+
all_downloaded=self.downloader.all_downloaded,
282+
dir_list=list(dir_zip_dict.keys())
283+
)
284+
282285
# noinspection PyMethodMayBeStatic
283286
def get_zip_path(self, album, photo, filename_rule, suffix, zip_dir):
287+
"""
288+
计算zip文件的路径
289+
"""
284290
filename = DirRule.apply_rule_directly(album, photo, filename_rule)
285291
from os.path import join
286292
return join(
287293
zip_dir,
288294
filename + fix_suffix(suffix),
289295
)
290296

297+
# noinspection PyMethodMayBeStatic
298+
def delete_all_files_and_empty_dir(self, all_downloaded: dict, dir_list: List[str]):
299+
"""
300+
删除所有文件和文件夹
301+
"""
302+
import os
303+
for album, photo_dict in all_downloaded.items():
304+
for photo, image_list in photo_dict.items():
305+
for f, image in image_list:
306+
os.remove(f)
307+
jm_debug('plugin.zip.remove', f'删除原文件: {f}')
308+
309+
for d in dir_list:
310+
if len(os.listdir(d)) == 0:
311+
os.removedirs(d)
312+
jm_debug('plugin.zip.remove', f'删除文件夹: {d}')
313+
291314

292315
JmModuleConfig.register_plugin(JmLoginPlugin)
293316
JmModuleConfig.register_plugin(UsageLogPlugin)

tests/test_jmcomic/test_jm_custom.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from test_jmcomic import *
2+
3+
4+
class Test_Custom(JmTestConfigurable):
5+
6+
def test_custom_entity(self):
7+
"""
8+
测试自定义属性
9+
"""
10+
dic = {1: 'd', 2: 'e'}
11+
12+
class MyAlbum(JmAlbumDetail):
13+
14+
@property
15+
def aname(self):
16+
return dic[int(self.album_id)]
17+
18+
class MyPhoto(JmPhotoDetail):
19+
20+
@property
21+
def pname(self):
22+
return dic[int(self.photo_id)]
23+
24+
JmModuleConfig.CLASS_ALBUM = MyAlbum
25+
JmModuleConfig.CLASS_PHOTO = MyPhoto
26+
27+
base_dir: str = workspace()
28+
dir_rule = DirRule('Bd_Aaname_Ppname', base_dir)
29+
# noinspection PyTypeChecker
30+
save_dir = dir_rule.deside_image_save_dir(
31+
MyAlbum('1', *['0'] * 13),
32+
MyPhoto('2', *['0'] * 7)
33+
)
34+
35+
self.assertEqual(
36+
os.path.abspath(save_dir),
37+
os.path.abspath(base_dir + dic[1] + '/' + dic[2]),
38+
)

0 commit comments

Comments
 (0)