Skip to content

Commit 257f48b

Browse files
ENH: Reverting 'connect' method and improving return types
1 parent 0584825 commit 257f48b

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

botcity/core/application/functions.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import time
22
from typing import Union
3-
from pywinauto import Desktop
43
from pywinauto.timings import TimeoutError
5-
from pywinauto.findwindows import ElementNotFoundError, WindowNotFoundError
4+
from pywinauto.findwindows import ElementNotFoundError
65
from pywinauto.application import Application, WindowSpecification
76
from .utils import Backend
87
from .. import config
98

109

11-
def connect(backend=Backend.WIN_32, timeout=60000,
12-
**connection_selectors) -> Union[Application, WindowSpecification]:
10+
def connect(backend=Backend.WIN_32, timeout=60000, **connection_selectors) -> Application:
1311
"""
1412
Connects to an instance of an open application.
1513
Use this method to be able to access application windows and elements.
@@ -24,34 +22,23 @@ def connect(backend=Backend.WIN_32, timeout=60000,
2422
](https://documentation.botcity.dev/frameworks/desktop/windows-apps/).
2523
2624
Returns
27-
app (Application | WindowSpecification): The Application/Window instance.
25+
app (Application): The Application/Window instance.
2826
"""
2927
connect_exception = None
3028
start_time = time.time()
3129
while True:
3230
elapsed_time = (time.time() - start_time) * 1000
3331
if elapsed_time > timeout:
34-
break
32+
if connect_exception:
33+
raise connect_exception
34+
return None
3535
try:
3636
app = Application(backend=backend).connect(**connection_selectors)
3737
return app
3838
except Exception as e:
3939
connect_exception = e
4040
time.sleep(config.DEFAULT_SLEEP_AFTER_ACTION/1000.0)
4141

42-
if "path" in connection_selectors.keys():
43-
connection_selectors.pop("path")
44-
45-
if not connection_selectors:
46-
if connect_exception:
47-
raise connect_exception
48-
return None
49-
50-
app = Desktop(backend=backend).window(**connection_selectors)
51-
if not app.exists():
52-
raise WindowNotFoundError(f"Unable to find an app using these criteria: {connection_selectors}")
53-
return app
54-
5542

5643
def find_window(app: Union[Application, WindowSpecification],
5744
waiting_time=10000, **selectors) -> WindowSpecification:

botcity/core/bot.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,8 +1608,7 @@ def sleep(self, interval):
16081608
#############
16091609

16101610
@if_windows_os
1611-
def connect_to_app(self, backend=Backend.WIN_32, timeout=60000,
1612-
**connection_selectors) -> Union['Application', 'WindowSpecification']:
1611+
def connect_to_app(self, backend=Backend.WIN_32, timeout=60000, **connection_selectors) -> 'Application':
16131612
"""
16141613
Connects to an instance of an open application.
16151614
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,
16241623
](https://documentation.botcity.dev/frameworks/desktop/windows-apps/).
16251624
16261625
Returns
1627-
app (Application | WindowSpecification): The Application/Window instance.
1626+
app (Application): The Application/Window instance.
16281627
"""
16291628
self.app = connect(backend, timeout, **connection_selectors)
16301629
return self.app

0 commit comments

Comments
 (0)