""" flow_engine.py - Declarative annotated flow engine (based on DrissionPage native API) """ import time from typing import Optional, Callable, Dict, Any, Type from core_types import BizLogicError def on(url: Optional[str] = None, title: Optional[str] = None, text: Optional[str] = None, ele: Optional[str] = None, not_ele: Optional[str] = None, predicate: Optional[Callable[['BaseFlow'], bool]] = None, priority: int = 100): """Main matching decorator: triggers method when page satisfies conditions (supports stacking for OR relationships)""" def decorator(func: Callable): rule_spec = { "url": url, "title": title, "text": text, "ele": ele, "not_ele": not_ele, "predicate": predicate, "priority": priority, "is_done": getattr(func, "_is_done", False), "fatal_msg": getattr(func, "_fatal_msg", None), "exception_cls": getattr(func, "_exception_cls", BizLogicError) } rules = getattr(func, "_flow_rules", None) if rules is None: rules = [] func._flow_rules = rules rules.append(rule_spec) func._flow_rule = rule_spec return func return decorator def done(func: Callable): """Done state decorator: exits and marks flow successful upon execution""" func._is_done = True for r in getattr(func, "_flow_rules", []): r["is_done"] = True return func def fatal(error_msg: str, exception_cls: Type[Exception] = BizLogicError): """Fatal error decorator: aborts flow with an exception when matched""" def decorator(func: Callable): func._fatal_msg = error_msg func._exception_cls = exception_cls for r in getattr(func, "_flow_rules", []): r["fatal_msg"] = error_msg r["exception_cls"] = exception_cls return func return decorator def guard(url: Optional[str] = None, title: Optional[str] = None, text: Optional[str] = None, ele: Optional[str] = None, not_ele: Optional[str] = None, predicate: Optional[Callable[['BaseFlow'], bool]] = None, priority: int = 10): """High-priority exception guard decorator""" return on(url=url, title=title, text=text, ele=ele, not_ele=not_ele, predicate=predicate, priority=priority) class BaseFlow: """ Lightweight annotated state machine driver engine. """ def __init__(self, page, config: Optional[Dict[str, Any]] = None, context: Optional[Dict[str, Any]] = None, interval: float = 1.0, logger: Optional[Callable[[str], None]] = None, cf_bypasser=None, max_stuck_cycles: Optional[int] = 30): self.page = page self.config = config or {} self.context = context if context is not None else {} self.result = None self.interval = interval self.cf_bypasser = cf_bypasser self._logger = logger self.last_url = "" self.stuck_count = 0 self.max_stuck_cycles = max_stuck_cycles # Collect annotated methods and sort by priority ascending self._rules = [] for attr_name in dir(self): try: method = getattr(self, attr_name) if callable(method): for r in getattr(method, "_flow_rules", []): rule = r.copy() rule["is_done"] = getattr(method, "_is_done", rule.get("is_done", False)) rule["fatal_msg"] = getattr(method, "_fatal_msg", rule.get("fatal_msg", None)) rule["exception_cls"] = getattr(method, "_exception_cls", rule.get("exception_cls", BizLogicError)) rule["method"] = method rule["name"] = attr_name self._rules.append(rule) except Exception: continue self._rules.sort(key=lambda r: r["priority"]) def log(self, msg: str, level: str = "info"): if self._logger is not None: log_fn = getattr(self._logger, level.lower(), getattr(self._logger, "info", None)) if callable(log_fn): log_fn(f"[{self.__class__.__name__}] {msg}") elif callable(self._logger): self._logger(f"[{self.__class__.__name__}] {msg}") else: print(f"[{self.__class__.__name__}] [{level.upper()}] {msg}") else: print(f"[{self.__class__.__name__}] [{level.upper()}] {msg}") def _matches(self, rule: Dict[str, Any]) -> bool: """Determines whether a rule is matched based on URL, Title, Text, Ele and custom Predicate""" # Guard: if no matching condition is specified, treat as unmatched if not any([ rule.get("url"), rule.get("title"), rule.get("text"), rule.get("ele"), rule.get("not_ele"), rule.get("predicate"), ]): return False curr_url = getattr(self.page, "url", "") or "" curr_title = getattr(self.page, "title", "") or "" if rule["url"] and rule["url"] not in curr_url: return False if rule["title"] and rule["title"].lower() not in curr_title.lower(): return False if rule["text"]: try: curr_html = getattr(self.page, "html", "") or "" if rule["text"] not in curr_html: return False except Exception: return False if rule["ele"]: try: if not self.page.ele(rule["ele"], timeout=0.2): return False except Exception: return False if rule["not_ele"]: try: if self.page.ele(rule["not_ele"], timeout=0.2): return False except Exception: pass if rule.get("predicate"): try: if not rule["predicate"](self): return False except Exception as e: self.log(f"[WARN] Rule @{rule.get('name', 'unknown')} predicate evaluation exception: {e}", level="warning") return False return True def run(self, max_steps: Optional[int] = None, timeout: Optional[float] = None, max_stuck_cycles: Optional[int] = None) -> Any: """ Executes scanning dispatch loop, supporting both step count and timeout limits. :param max_steps: Maximum loop steps limit; None means unlimited. :param timeout: Maximum execution timeout (seconds); None means unlimited. :param max_stuck_cycles: Stuck cycles before refresh self-healing; None to disable. """ if max_stuck_cycles is not None: self.max_stuck_cycles = max_stuck_cycles start_time = time.time() step = 0 # Construct display info limits_desc = [] if max_steps is not None: limits_desc.append(f"max_steps={max_steps}") if timeout is not None: limits_desc.append(f"timeout={timeout}s") limits_str = f" (Limits: {', '.join(limits_desc)})" if limits_desc else " (Unlimited mode)" self.log(f"🚀 Flow state machine started with {len(self._rules)} rule(s){limits_str}") while True: step += 1 elapsed = time.time() - start_time # 1. Check step limit if max_steps is not None and step > max_steps: raise BizLogicError( f"Flow steps exhausted (Executed {step - 1} steps / limit {max_steps}, elapsed {elapsed:.1f}s), last URL: {getattr(self.page, 'url', '')}" ) # 2. Check timeout limit if timeout is not None and elapsed > timeout: raise BizLogicError( f"Flow execution timed out (Elapsed {elapsed:.1f}s / limit {timeout:.1f}s, executed {step - 1} steps), last URL: {getattr(self.page, 'url', '')}" ) # Format step prefix if max_steps is not None and timeout is not None: step_prefix = f"[{step}/{max_steps} | {elapsed:.1f}s/{timeout:.1f}s]" elif max_steps is not None: step_prefix = f"[{step}/{max_steps} | {elapsed:.1f}s]" elif timeout is not None: step_prefix = f"[Step {step} | {elapsed:.1f}s/{timeout:.1f}s]" else: step_prefix = f"[Step {step} | {elapsed:.1f}s]" try: self.page.wait.doc_loaded(timeout=5) except Exception: pass time.sleep(self.interval) curr_url = getattr(self.page, "url", "") # Stuck detection and refresh self-healing if self.max_stuck_cycles is not None and self.max_stuck_cycles > 0: if curr_url and curr_url == self.last_url: self.stuck_count += 1 if self.stuck_count >= self.max_stuck_cycles: self.log(f"{step_prefix} [WARN] Page stuck for {self.stuck_count} cycles, executing refresh...", level="warning") try: self.page.refresh() self.page.wait.load_start(timeout=5) except Exception as e: self.log(f"[ERROR] Failed to refresh page: {e}", level="error") self.stuck_count = 0 continue else: self.last_url, self.stuck_count = curr_url, 0 # Match rules by priority matched_rule = None for rule in self._rules: if self._matches(rule): matched_rule = rule self.log(f"{step_prefix} 🎯 Matched rule: @{rule['name']} (priority={rule['priority']})") if rule["fatal_msg"]: self.log(f"💥 Fatal error interruption triggered: {rule['fatal_msg']}", level="error") exc_cls = rule.get("exception_cls", BizLogicError) raise exc_cls(rule["fatal_msg"]) rule["method"]() if rule["is_done"]: self.log(f"✨ Goal achieved, flow complete! (Elapsed {time.time() - start_time:.1f}s, executed {step} steps)") return self.result if self.result is not None else True break if not matched_rule: self.log(f"{step_prefix} ⏳ No rules matched (URL: {curr_url}), waiting for next cycle...")