Skip to content

Commit 012a8cf

Browse files
committed
polish code
1 parent 3fbcd31 commit 012a8cf

4 files changed

Lines changed: 10 additions & 21 deletions

File tree

assets/option/option_test_api.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# GitHub Actions 测试用
22
# 移动端配置
33
dir_rule:
4+
normalize_zh: zh-cn
45
rule: Bd_Aauthor_Aid_Pindextitle
56

67
client:

src/jmcomic/jm_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def new_postman(cls, session=False, **kwargs):
411411

412412
DEFAULT_OPTION_DICT: dict = {
413413
'log': None,
414-
'dir_rule': {'rule': 'Bd_Pname', 'base_dir': None, 'normalize_zh': 'zh-cn'},
414+
'dir_rule': {'rule': 'Bd_Pname', 'base_dir': None, 'normalize_zh': 'zh-cn'},
415415
'download': {
416416
'cache': True,
417417
'image': {'decode': True, 'suffix': None},

src/jmcomic/jm_option.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ def enable_client_cache_on_condition(cls,
6161
class DirRule:
6262
RULE_BASE_DIR = 'Bd'
6363

64-
def __init__(self, rule: str, base_dir=None, normalize_zh='zh-cn'):
64+
def __init__(self, rule: str, base_dir=None, normalize_zh=None):
6565
"""
6666
:param rule: DSL rule
6767
:param base_dir: base directory
68-
:param normalize_zh: 'zh-cn'|'zh-tw'|'none' or None. 控制是否以及如何进行繁简体归一化,默认 'zh-cn'
68+
:param normalize_zh: 'zh-cn'|'zh-tw'| or None. 控制是否以及如何进行繁简体归一化,默认 None
6969
"""
7070
base_dir = JmcomicText.parse_to_abspath(base_dir)
7171
self.base_dir = base_dir
@@ -95,17 +95,8 @@ def apply_rule_to_path(self, album, photo, only_album_rules=False) -> str:
9595
jm_log('dir_rule', f'路径规则"{rule}"的解析出错: {e}, album={album}, photo={photo}')
9696
raise e
9797
if parser != self.parse_bd_rule:
98-
# 根据配置 normalize_zh 进行繁简体统一或跳过
99-
try:
100-
target = getattr(self, 'normalize_zh', None)
101-
if target is None:
102-
# 默认为不转换
103-
conv_path = str(path)
104-
else:
105-
conv_path = JmcomicText.to_zh(str(path), target)
106-
except Exception:
107-
conv_path = str(path)
108-
98+
# 根据配置 normalize_zh 进行繁简体统一
99+
conv_path = JmcomicText.to_zh(str(path), self.normalize_zh)
109100
path = fix_windir_name(conv_path).strip()
110101

111102
path_ls.append(path)

src/jmcomic/jm_toolkit.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def to_zh_cn(cls, s):
333333
return cls.to_zh(s, 'zh-cn')
334334

335335
@classmethod
336-
def to_zh(cls, s, target='zh-cn'):
336+
def to_zh(cls, s, target=None):
337337
"""
338338
通用的繁简体转换接口。
339339
@@ -347,15 +347,12 @@ def to_zh(cls, s, target='zh-cn'):
347347
if target is None:
348348
return s
349349

350-
t = str(target).strip().lower()
351-
if t in ('none', ''):
352-
return s
353-
354350
try:
355351
import zhconv
356-
return zhconv.convert(s, t)
357-
except Exception:
352+
return zhconv.convert(s, target)
353+
except Exception as e:
358354
# 如果 zhconv 不可用或转换失败,则回退原字符串
355+
jm_log('zhconv.error', f'error: [{e}], s: [{s}]')
359356
return s
360357

361358
@classmethod

0 commit comments

Comments
 (0)