Spaces:
Sleeping
Sleeping
Upload captcha_solver/engines/base.py with huggingface_hub
Browse files
captcha_solver/engines/base.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Base engine class."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import abc
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class BaseEngine(abc.ABC):
|
| 10 |
+
"""Lifecycle: lazy -> load() -> ready -> unload()."""
|
| 11 |
+
|
| 12 |
+
name: str = "base"
|
| 13 |
+
|
| 14 |
+
def __init__(self) -> None:
|
| 15 |
+
self._loaded = False
|
| 16 |
+
self._lock_count = 0
|
| 17 |
+
self.last_error: str | None = None
|
| 18 |
+
|
| 19 |
+
@property
|
| 20 |
+
def loaded(self) -> bool:
|
| 21 |
+
return self._loaded
|
| 22 |
+
|
| 23 |
+
@property
|
| 24 |
+
def status(self) -> str:
|
| 25 |
+
if self._loaded:
|
| 26 |
+
return "loaded"
|
| 27 |
+
if self.last_error:
|
| 28 |
+
return f"error: {self.last_error}"
|
| 29 |
+
return "not_loaded"
|
| 30 |
+
|
| 31 |
+
def load(self) -> None:
|
| 32 |
+
if self._loaded:
|
| 33 |
+
return
|
| 34 |
+
try:
|
| 35 |
+
self._do_load()
|
| 36 |
+
self._loaded = True
|
| 37 |
+
self.last_error = None
|
| 38 |
+
except Exception as exc:
|
| 39 |
+
self.last_error = str(exc)
|
| 40 |
+
raise
|
| 41 |
+
|
| 42 |
+
def unload(self) -> None:
|
| 43 |
+
self._do_unload()
|
| 44 |
+
self._loaded = False
|
| 45 |
+
|
| 46 |
+
@abc.abstractmethod
|
| 47 |
+
def _do_load(self) -> None: ...
|
| 48 |
+
@abc.abstractmethod
|
| 49 |
+
def _do_unload(self) -> None: ...
|
| 50 |
+
|
| 51 |
+
def __repr__(self) -> str:
|
| 52 |
+
return f"<{self.__class__.__name__} loaded={self._loaded}>"
|