Skip to content

Commit 62a7c32

Browse files
committed
Move AC_mouse_* alias helpers into a shared module
Sonar flagged 39.8% duplication on the previous commit because the three single-button click adapters were defined identically in both action_executor.py and callback_function_executor.py. Generate them once in utils/executor/mouse_aliases.py via a small factory and have both executors import the same callables.
1 parent 8d7296a commit 62a7c32

3 files changed

Lines changed: 44 additions & 44 deletions

File tree

je_auto_control/utils/callback/callback_function_executor.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, Optional, Tuple
1+
from typing import Any, Callable
22

33
# utils cv2_utils
44
from je_auto_control.utils.cv2_utils.screenshot import pil_screenshot
@@ -7,6 +7,9 @@
77
from je_auto_control.utils.logging.logging_instance import autocontrol_logger
88
# executor
99
from je_auto_control.utils.executor.action_executor import execute_action, execute_files
10+
from je_auto_control.utils.executor.mouse_aliases import (
11+
click_mouse_left, click_mouse_middle, click_mouse_right,
12+
)
1013
# file process
1114
from je_auto_control.utils.file_process.get_dir_file_list import get_dir_files_as_list
1215
# html report
@@ -48,24 +51,6 @@
4851
from je_auto_control.wrapper.auto_control_screen import screen_size, screenshot
4952

5053

51-
def _click_mouse_left(x: Optional[int] = None,
52-
y: Optional[int] = None) -> Tuple[int, int, int]:
53-
"""Callback adapter: click left mouse button (button is hardcoded)."""
54-
return click_mouse("mouse_left", x, y)
55-
56-
57-
def _click_mouse_right(x: Optional[int] = None,
58-
y: Optional[int] = None) -> Tuple[int, int, int]:
59-
"""Callback adapter: click right mouse button (button is hardcoded)."""
60-
return click_mouse("mouse_right", x, y)
61-
62-
63-
def _click_mouse_middle(x: Optional[int] = None,
64-
y: Optional[int] = None) -> Tuple[int, int, int]:
65-
"""Callback adapter: click middle mouse button (button is hardcoded)."""
66-
return click_mouse("mouse_middle", x, y)
67-
68-
6954
class CallbackFunctionExecutor:
7055
"""
7156
CallbackFunctionExecutor
@@ -79,9 +64,9 @@ def __init__(self):
7964
# 事件字典,對應字串名稱到實際函式
8065
self.event_dict: dict = {
8166
# mouse 滑鼠相關
82-
"AC_mouse_left": _click_mouse_left,
83-
"AC_mouse_right": _click_mouse_right,
84-
"AC_mouse_middle": _click_mouse_middle,
67+
"AC_mouse_left": click_mouse_left,
68+
"AC_mouse_right": click_mouse_right,
69+
"AC_mouse_middle": click_mouse_middle,
8570
"AC_click_mouse": click_mouse,
8671
"AC_get_mouse_table": get_mouse_table,
8772
"AC_get_mouse_position": get_mouse_position,

je_auto_control/utils/executor/action_executor.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import types
2-
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
2+
from typing import Any, Callable, Dict, List, Optional, Union
33

44
from je_auto_control.utils.exception.exception_tags import (
55
action_is_null_error_message, add_command_exception_error_message,
@@ -22,6 +22,9 @@
2222
from je_auto_control.utils.executor.flow_control import (
2323
BLOCK_COMMANDS, LoopBreak, LoopContinue,
2424
)
25+
from je_auto_control.utils.executor.mouse_aliases import (
26+
click_mouse_left, click_mouse_middle, click_mouse_right,
27+
)
2528
from je_auto_control.utils.ocr.ocr_engine import (
2629
click_text as ocr_click_text,
2730
locate_text_center as ocr_locate_text_center,
@@ -91,24 +94,6 @@ def _vlm_locate_as_list(description: str,
9194
return None if coords is None else [coords[0], coords[1]]
9295

9396

94-
def _click_mouse_left(x: Optional[int] = None,
95-
y: Optional[int] = None) -> Tuple[int, int, int]:
96-
"""Executor adapter: click left mouse button (button is hardcoded)."""
97-
return click_mouse("mouse_left", x, y)
98-
99-
100-
def _click_mouse_right(x: Optional[int] = None,
101-
y: Optional[int] = None) -> Tuple[int, int, int]:
102-
"""Executor adapter: click right mouse button (button is hardcoded)."""
103-
return click_mouse("mouse_right", x, y)
104-
105-
106-
def _click_mouse_middle(x: Optional[int] = None,
107-
y: Optional[int] = None) -> Tuple[int, int, int]:
108-
"""Executor adapter: click middle mouse button (button is hardcoded)."""
109-
return click_mouse("mouse_middle", x, y)
110-
111-
11297
def _history_list_as_dicts(limit: int = 100,
11398
source_type: Optional[str] = None) -> List[dict]:
11499
"""Executor adapter: list run history as plain dicts (JSON-friendly)."""
@@ -142,9 +127,9 @@ def __init__(self):
142127
# 事件字典,對應字串名稱到函式
143128
self.event_dict: dict = {
144129
# Mouse 滑鼠相關
145-
"AC_mouse_left": _click_mouse_left,
146-
"AC_mouse_right": _click_mouse_right,
147-
"AC_mouse_middle": _click_mouse_middle,
130+
"AC_mouse_left": click_mouse_left,
131+
"AC_mouse_right": click_mouse_right,
132+
"AC_mouse_middle": click_mouse_middle,
148133
"AC_click_mouse": click_mouse,
149134
"AC_get_mouse_table": get_mouse_table,
150135
"AC_get_mouse_position": get_mouse_position,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Single-button mouse-click aliases shared by the action and callback executors.
2+
3+
Each ``AC_mouse_<button>`` command name needs a callable whose signature is
4+
``(x=None, y=None)`` so the button itself is implied by the command name and
5+
not threaded through ``mouse_keycode``. Generating the three callables from
6+
one factory keeps the registration sites identical across executors.
7+
"""
8+
from typing import Callable, Optional, Tuple
9+
10+
from je_auto_control.wrapper.auto_control_mouse import click_mouse
11+
12+
ClickFn = Callable[[Optional[int], Optional[int]], Tuple[int, int, int]]
13+
14+
15+
def _make_button_click(button: str) -> ClickFn:
16+
def click(x: Optional[int] = None,
17+
y: Optional[int] = None) -> Tuple[int, int, int]:
18+
return click_mouse(button, x, y)
19+
20+
click.__name__ = f"click_{button}"
21+
click.__qualname__ = click.__name__
22+
click.__doc__ = f"Click the {button.replace('_', ' ')} mouse button at (x, y)."
23+
return click
24+
25+
26+
click_mouse_left: ClickFn = _make_button_click("mouse_left")
27+
click_mouse_right: ClickFn = _make_button_click("mouse_right")
28+
click_mouse_middle: ClickFn = _make_button_click("mouse_middle")
29+
30+
__all__ = ["click_mouse_left", "click_mouse_right", "click_mouse_middle"]

0 commit comments

Comments
 (0)