Skip to content

Commit 5cd0104

Browse files
committed
支持dir_rule使用python格式字符串语法 (#415)
1 parent ebf0ea6 commit 5cd0104

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

src/jmcomic/jm_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def new_postman(cls, session=False, **kwargs):
414414
'cache': None, # see CacheRegistry
415415
'domain': [],
416416
'postman': {
417-
'type': 'cffi',
417+
'type': 'curl_cffi',
418418
'meta_data': {
419419
'impersonate': 'chrome110',
420420
'headers': None,

src/jmcomic/jm_entity.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ def get_dirname(cls, detail: 'DetailEntity', ref: str) -> str:
164164

165165
return getattr(detail, ref)
166166

167+
def get_properties_dict(self):
168+
import inspect
169+
170+
prefix = self.__class__.__name__[2]
171+
result = {}
172+
173+
# field
174+
for k, v in self.__dict__.items():
175+
result[prefix + k] = v
176+
177+
# property
178+
for cls in inspect.getmro(type(self)):
179+
for name, attr in cls.__dict__.items():
180+
k = prefix + name
181+
if k not in result and isinstance(attr, property):
182+
v = attr.__get__(self, cls)
183+
result[k] = v
184+
185+
# advice
186+
advice_dict = JmModuleConfig.AFIELD_ADVICE if self.is_album() else JmModuleConfig.PFIELD_ADVICE
187+
for name, func in advice_dict:
188+
k = prefix + name
189+
result[k] = func(self)
190+
191+
return result
192+
167193

168194
class JmImageDetail(JmBaseEntity, Downloadable):
169195

src/jmcomic/jm_option.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DirRule:
7171
]
7272

7373
Detail = Union[JmAlbumDetail, JmPhotoDetail, None]
74-
RuleFunc = Callable[[Detail], str]
74+
RuleFunc = Callable
7575
RuleSolver = Tuple[str, RuleFunc, str]
7676
RuleSolverList = List[RuleSolver]
7777

@@ -154,6 +154,12 @@ def split_rule_dsl(self, rule_dsl: str) -> List[str]:
154154

155155
@classmethod
156156
def get_rule_solver(cls, rule: str) -> Optional[RuleSolver]:
157+
if '{' in rule:
158+
def format_path(album, photo):
159+
return fix_windir_name(rule.format(**album.get_properties_dict(), **photo.get_properties_dict())).strip()
160+
161+
return 'F', format_path, rule
162+
157163
# 检查dsl
158164
if not rule.startswith(('A', 'P')):
159165
return None
@@ -176,15 +182,17 @@ def apply_rule_solver(cls, album, photo, rule_solver: RuleSolver) -> str:
176182

177183
def choose_detail(key):
178184
if key == 'Bd':
179-
return None
185+
return None,
180186
if key == 'A':
181-
return album
187+
return album,
182188
if key == 'P':
183-
return photo
189+
return photo,
190+
if key == 'F':
191+
return album, photo
184192

185193
key, func, _ = rule_solver
186194
detail = choose_detail(key)
187-
return func(detail)
195+
return func(*detail)
188196

189197
@classmethod
190198
def apply_rule_directly(cls, album, photo, rule: str) -> str:

0 commit comments

Comments
 (0)