Skip to content

Commit 530e74c

Browse files
committed
refactor(feature): 响应 CodeReview 意见,优化 FeatureChain 封装与动态参数适配,补充新手文档
1 parent 6b47ecb commit 530e74c

7 files changed

Lines changed: 20 additions & 20 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ download_album('123', option, extra=Feature.export_pdf)
4444
├── [JM123]本子标题.pdf ← 整本合并为 1 个 PDF,注意pdf文件名的格式,默认包含本子禁漫车号+本子标题
4545
```
4646

47+
> 💡 **小白提示:当前工作目录在哪?**
48+
>
49+
> 默认情况下,文件会直接出现在你**运行 Python 脚本的那个文件夹里**。如果你不知道具体是哪,可以看后面的【自定义参数】章节,手动指定你想保存到的文件夹。
50+
4751
### 2.2 需要多种导出格式(PDF、ZIP等)——直接组合 Feature
4852

4953
`+` 号组合,同时导出多种格式:

src/jmcomic/jm_client_impl.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,6 @@ def get_photo_detail(self,
280280
photo = self.fetch_detail_entity(photo_id, 'photo')
281281

282282
# 一并获取该章节的所处本子
283-
# todo: 可优化,获取章节所在本子,其实不需要等待章节获取完毕后。
284-
# 可以直接调用 self.get_album_detail(photo_id),会重定向返回本子的HTML
285-
# (had polished by FutureClientProxy)
286283
if fetch_album is True:
287284
photo.from_album = self.get_album_detail(photo.album_id)
288285

@@ -1205,4 +1202,4 @@ def get_photo_detail(self, photo_id, fetch_album=True, fetch_scramble_id=True) -
12051202
if scramble_id != '':
12061203
photo.scramble_id = scramble_id
12071204

1208-
return photo
1205+
return photo

src/jmcomic/jm_config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ def format(self, record):
591591
def enable_pretty_log():
592592
"""开启带颜色的美化日志"""
593593
import sys
594-
import os as _os
595594

596595
# Windows 需要启用 VT100 ANSI 支持
597596
if sys.platform == 'win32':

src/jmcomic/jm_downloader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(self, option: JmOption) -> None:
8282
self.download_failed_image: List[Tuple[JmImageDetail, BaseException]] = []
8383
self.download_failed_photo: List[Tuple[JmPhotoDetail, BaseException]] = []
8484
# Feature 特性列表: [(feature, feature_from), ...]
85-
self._feature_list: list = []
85+
self._feature_list: List[Tuple] = []
8686

8787
def download_album(self, album_id):
8888
album = self.client.get_album_detail(album_id)
@@ -292,7 +292,7 @@ def add_features(self, features, feature_from: str):
292292
for f in features:
293293
self.add_features(f, feature_from)
294294
elif isinstance(features, FeatureChain):
295-
for f in features._features:
295+
for f in features.to_list():
296296
self._feature_list.append((f, feature_from))
297297
elif isinstance(features, Feature):
298298
self._feature_list.append((features, feature_from))

src/jmcomic/jm_exception.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def new(msg, context=None, _etype=None):
183183

184184
@classmethod
185185
def notify_all_listeners(cls, e):
186-
registry: Dict[Type, Callable[Type]] = JmModuleConfig.REGISTRY_EXCEPTION_LISTENER
186+
registry: Dict[Type, Callable] = JmModuleConfig.REGISTRY_EXCEPTION_LISTENER
187187
if not registry:
188188
return None
189189

src/jmcomic/jm_feature.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ def invoke(self, option: JmOption, feature_from: str, when: str, **kwargs):
5858
# ---- 组合运算符,统一返回 FeatureChain ----
5959

6060
def __add__(self, other):
61-
return FeatureChain._combine(self, other)
61+
return FeatureChain.combine(self, other)
6262

6363
def __or__(self, other):
64-
return FeatureChain._combine(self, other)
64+
return FeatureChain.combine(self, other)
6565

6666
def __and__(self, other):
67-
return FeatureChain._combine(self, other)
67+
return FeatureChain.combine(self, other)
6868

69-
def _to_list(self):
69+
def to_list(self):
7070
return [self]
7171

7272

@@ -122,7 +122,7 @@ def _adapt_plugin_kwargs(self, feature_from: str, when: str) -> dict:
122122
filename_rule
123123
"""
124124
kwargs = self.kwargs.copy()
125-
kwargs.setdefault('filename_rule', '[JM{Aid}]{Atitle}' if feature_from == 'download_album' else '[JM{Pid}]{Ptitle}')
125+
kwargs.setdefault('filename_rule', '[JM{Aid}]{Atitle}' if when == 'after_album' else '[JM{Pid}]{Ptitle}')
126126
return kwargs
127127

128128
def __repr__(self):
@@ -139,19 +139,19 @@ def __init__(self, features):
139139
self._features = features
140140

141141
@classmethod
142-
def _combine(cls, left, right):
143-
return cls(left._to_list() + right._to_list())
142+
def combine(cls, left, right):
143+
return cls(left.to_list() + right.to_list())
144144

145145
def __add__(self, other):
146-
return FeatureChain._combine(self, other)
146+
return FeatureChain.combine(self, other)
147147

148148
def __or__(self, other):
149-
return FeatureChain._combine(self, other)
149+
return FeatureChain.combine(self, other)
150150

151151
def __and__(self, other):
152-
return FeatureChain._combine(self, other)
152+
return FeatureChain.combine(self, other)
153153

154-
def _to_list(self):
154+
def to_list(self):
155155
return list(self._features)
156156

157157
def __repr__(self):

src/jmcomic/jm_toolkit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def to_zh(cls, s, target=None):
350350
try:
351351
import zhconv
352352
return zhconv.convert(s, target)
353-
except ImportError as e:
353+
except ImportError:
354354
jm_log('zhconv.error', '繁简转换失败,未安装zhconv,请先使用命令安装: [pip install zhconv]')
355355
return s
356356
except Exception as e:

0 commit comments

Comments
 (0)