From 257f48b53508f278e6eead250da3a3a1c6852a9d Mon Sep 17 00:00:00 2001 From: joao-voltarelli Date: Fri, 13 Dec 2024 11:35:48 -0300 Subject: [PATCH] ENH: Reverting 'connect' method and improving return types --- botcity/core/application/functions.py | 25 ++++++------------------- botcity/core/bot.py | 5 ++--- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/botcity/core/application/functions.py b/botcity/core/application/functions.py index 591efb1..58738e8 100644 --- a/botcity/core/application/functions.py +++ b/botcity/core/application/functions.py @@ -1,15 +1,13 @@ import time from typing import Union -from pywinauto import Desktop from pywinauto.timings import TimeoutError -from pywinauto.findwindows import ElementNotFoundError, WindowNotFoundError +from pywinauto.findwindows import ElementNotFoundError from pywinauto.application import Application, WindowSpecification from .utils import Backend from .. import config -def connect(backend=Backend.WIN_32, timeout=60000, - **connection_selectors) -> Union[Application, WindowSpecification]: +def connect(backend=Backend.WIN_32, timeout=60000, **connection_selectors) -> Application: """ Connects to an instance of an open application. Use this method to be able to access application windows and elements. @@ -24,14 +22,16 @@ def connect(backend=Backend.WIN_32, timeout=60000, ](https://documentation.botcity.dev/frameworks/desktop/windows-apps/). Returns - app (Application | WindowSpecification): The Application/Window instance. + app (Application): The Application/Window instance. """ connect_exception = None start_time = time.time() while True: elapsed_time = (time.time() - start_time) * 1000 if elapsed_time > timeout: - break + if connect_exception: + raise connect_exception + return None try: app = Application(backend=backend).connect(**connection_selectors) return app @@ -39,19 +39,6 @@ def connect(backend=Backend.WIN_32, timeout=60000, connect_exception = e time.sleep(config.DEFAULT_SLEEP_AFTER_ACTION/1000.0) - if "path" in connection_selectors.keys(): - connection_selectors.pop("path") - - if not connection_selectors: - if connect_exception: - raise connect_exception - return None - - app = Desktop(backend=backend).window(**connection_selectors) - if not app.exists(): - raise WindowNotFoundError(f"Unable to find an app using these criteria: {connection_selectors}") - return app - def find_window(app: Union[Application, WindowSpecification], waiting_time=10000, **selectors) -> WindowSpecification: diff --git a/botcity/core/bot.py b/botcity/core/bot.py index c4690fe..a58976e 100644 --- a/botcity/core/bot.py +++ b/botcity/core/bot.py @@ -1608,8 +1608,7 @@ def sleep(self, interval): ############# @if_windows_os - def connect_to_app(self, backend=Backend.WIN_32, timeout=60000, - **connection_selectors) -> Union['Application', 'WindowSpecification']: + def connect_to_app(self, backend=Backend.WIN_32, timeout=60000, **connection_selectors) -> 'Application': """ Connects to an instance of an open application. Use this method to be able to access application windows and elements. @@ -1624,7 +1623,7 @@ def connect_to_app(self, backend=Backend.WIN_32, timeout=60000, ](https://documentation.botcity.dev/frameworks/desktop/windows-apps/). Returns - app (Application | WindowSpecification): The Application/Window instance. + app (Application): The Application/Window instance. """ self.app = connect(backend, timeout, **connection_selectors) return self.app