Skip to content

Commit

Permalink
ENH: Reverting 'connect' method and improving return types
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-voltarelli committed Dec 13, 2024
1 parent 0584825 commit 257f48b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
25 changes: 6 additions & 19 deletions botcity/core/application/functions.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,34 +22,23 @@ 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
except Exception as e:
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:
Expand Down
5 changes: 2 additions & 3 deletions botcity/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 257f48b

Please sign in to comment.