Skip to content

Commit

Permalink
feat(python): add decorator api of custom_recognition and custom_acti…
Browse files Browse the repository at this point in the history
…on (#522)
  • Loading branch information
YUASDS authored Jan 23, 2025
1 parent 0f6c68d commit 4dc1818
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
52 changes: 43 additions & 9 deletions sample/python/__main__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# python -m pip install maafw
from maa.resource import Resource
from maa.controller import AdbController
import os
from maa.tasker import Tasker
from maa.toolkit import Toolkit

from maa.custom_recognition import CustomRecognition
from maa.context import Context
from maa.resource import Resource
from maa.controller import AdbController
from maa.custom_action import CustomAction
from maa.custom_recognition import CustomRecognition
from maa.notification_handler import NotificationHandler, NotificationType


# for register decorator
resource = Resource()


def main():
user_path = "./"
resource_path = "sample/resource"

Toolkit.init_option(user_path)

resource = Resource()
res_job = resource.post_bundle("sample/resource")
res_job = resource.post_bundle(resource_path)
res_job.wait()


# If not found on Windows, try running as administrator
adb_devices = Toolkit.find_adb_devices()
if not adb_devices:
print("No ADB device found.")
Expand All @@ -41,12 +48,21 @@ def main():
print("Failed to init MAA.")
exit()

resource.register_custom_recognition("MyRec", MyRecongition())
# just an example, use it in json
pipeline_override = {
"MyCustomEntry": {"action": "custom", "custom_action": "MyCustomAction"},
}

# another way to register
# resource.register_custom_recognition("My_Recongition", MyRecongition())
# resource.register_custom_action("My_CustomAction", MyCustomAction())

task_detail = tasker.post_task("StartUpAndClickButton").wait().get()
task_detail = tasker.post_task("MyCustomEntry", pipeline_override).wait().get()
# do something with task_detail


# auto register by decorator, can also call `resource.register_custom_recognition` manually
@resource.custom_recognition("MyRecongition")
class MyRecongition(CustomRecognition):

def analyze(
Expand Down Expand Up @@ -119,5 +135,23 @@ def on_node_action(
print(f"on_node_action: {noti_type}, {detail}")


# auto register by decorator, can also call `resource.register_custom_action` manually
@resource.custom_action("MyCustomAction")
class MyCustomAction(CustomAction):

def run(
self,
context: Context,
argv: CustomAction.RunArg,
) -> bool:
"""
:param argv:
:param context: 运行上下文
:return: 是否执行成功。-参考流水线协议 `on_error`
"""
print("MyCustomAction is running!")
return True


if __name__ == "__main__":
main()
16 changes: 16 additions & 0 deletions source/binding/Python/maa/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def set_auto_device(self) -> bool:
"""
return self.use_auto_ep()

def custom_recognition(self, name: str):

def wrapper_recognition(recognition):
self.register_custom_recognition(name=name, recognition=recognition())
return recognition

return wrapper_recognition

def register_custom_recognition(
self, name: str, recognition: "CustomRecognition" # type: ignore
) -> bool:
Expand Down Expand Up @@ -131,6 +139,14 @@ def clear_custom_recognition(self) -> bool:
)
)

def custom_action(self, name: str):

def wrapper_action(action):
self.register_custom_action(name=name, action=action())
return action

return wrapper_action

def register_custom_action(self, name: str, action: "CustomAction") -> bool: # type: ignore
# avoid gc
self._custom_action_holder[name] = action
Expand Down

0 comments on commit 4dc1818

Please sign in to comment.