robotframework-platynui 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. PlatynUI/ButtonKeywords.py +9 -0
  2. PlatynUI/TextKeywords.py +7 -0
  3. PlatynUI/__about__.py +4 -0
  4. PlatynUI/__init__.py +34 -0
  5. PlatynUI/core/__init__.py +13 -0
  6. PlatynUI/core/adapter.py +152 -0
  7. PlatynUI/core/adapterfactory.py +51 -0
  8. PlatynUI/core/adapterproxy.py +168 -0
  9. PlatynUI/core/contextbase.py +586 -0
  10. PlatynUI/core/exceptions.py +77 -0
  11. PlatynUI/core/internal/__init__.py +0 -0
  12. PlatynUI/core/internal/decorator.py +148 -0
  13. PlatynUI/core/locatorbase.py +197 -0
  14. PlatynUI/core/locatorscope.py +17 -0
  15. PlatynUI/core/predicate.py +15 -0
  16. PlatynUI/core/settings.py +63 -0
  17. PlatynUI/core/strategies/__init__.py +1 -0
  18. PlatynUI/core/strategies/strategies.py +26 -0
  19. PlatynUI/core/strategybase.py +8 -0
  20. PlatynUI/core/strategyimpl.py +102 -0
  21. PlatynUI/core/technology.py +25 -0
  22. PlatynUI/core/types.py +421 -0
  23. PlatynUI/core/wait_for.py +47 -0
  24. PlatynUI/core/weight_calculator.py +110 -0
  25. PlatynUI/py.typed +0 -0
  26. PlatynUI/robot/__init__.py +0 -0
  27. PlatynUI/robot/reporting.py +74 -0
  28. PlatynUI/technology/__init__.py +0 -0
  29. PlatynUI/technology/uiautomation/.gitignore +1 -0
  30. PlatynUI/technology/uiautomation/__init__.py +4 -0
  31. PlatynUI/technology/uiautomation/core/adapterfactory.py +88 -0
  32. PlatynUI/technology/uiautomation/core/displaydevice.py +23 -0
  33. PlatynUI/technology/uiautomation/core/impls/__init__.py +2 -0
  34. PlatynUI/technology/uiautomation/core/impls/control.py +31 -0
  35. PlatynUI/technology/uiautomation/core/impls/window.py +83 -0
  36. PlatynUI/technology/uiautomation/core/keyboarddevice.py +21 -0
  37. PlatynUI/technology/uiautomation/core/loader.py +117 -0
  38. PlatynUI/technology/uiautomation/core/mousedevice.py +29 -0
  39. PlatynUI/technology/uiautomation/core/technology.py +61 -0
  40. PlatynUI/technology/uiautomation/core/uiaadapter.py +197 -0
  41. PlatynUI/technology/uiautomation/core/uiabase.py +19 -0
  42. PlatynUI/technology/uiautomation/desktop.py +11 -0
  43. PlatynUI/technology/uiautomation/locator.py +435 -0
  44. PlatynUI/technology/uiautomation/runtime/Caliburn.Micro.Core.dll +0 -0
  45. PlatynUI/technology/uiautomation/runtime/Caliburn.Micro.Platform.Core.dll +0 -0
  46. PlatynUI/technology/uiautomation/runtime/Caliburn.Micro.Platform.dll +0 -0
  47. PlatynUI/technology/uiautomation/runtime/Microsoft.Xaml.Behaviors.dll +0 -0
  48. PlatynUI/technology/uiautomation/runtime/PlatynUI.Technology.UiAutomation.Spy.deps.json +266 -0
  49. PlatynUI/technology/uiautomation/runtime/PlatynUI.Technology.UiAutomation.Spy.dll +0 -0
  50. PlatynUI/technology/uiautomation/runtime/PlatynUI.Technology.UiAutomation.Spy.exe +0 -0
  51. PlatynUI/technology/uiautomation/runtime/PlatynUI.Technology.UiAutomation.Spy.runtimeconfig.json +19 -0
  52. PlatynUI/technology/uiautomation/runtime/PlatynUI.Technology.UiAutomation.dll +0 -0
  53. PlatynUI/technology/uiautomation/runtime/System.ComponentModel.Composition.dll +0 -0
  54. PlatynUI/technology/uiautomation/spy/__init__.py +0 -0
  55. PlatynUI/technology/uiautomation/spy/__main__.py +24 -0
  56. PlatynUI/ui/__init__.py +65 -0
  57. PlatynUI/ui/application.py +27 -0
  58. PlatynUI/ui/buttons.py +82 -0
  59. PlatynUI/ui/combobox.py +149 -0
  60. PlatynUI/ui/container.py +9 -0
  61. PlatynUI/ui/control.py +49 -0
  62. PlatynUI/ui/core/__init__.py +3 -0
  63. PlatynUI/ui/core/devices/__init__.py +6 -0
  64. PlatynUI/ui/core/devices/basedisplaydevice.py +25 -0
  65. PlatynUI/ui/core/devices/basekeyboarddevice.py +64 -0
  66. PlatynUI/ui/core/devices/basemousedevice.py +30 -0
  67. PlatynUI/ui/core/devices/basescreendevice.py +16 -0
  68. PlatynUI/ui/core/devices/displaydevice.py +113 -0
  69. PlatynUI/ui/core/devices/inputdevice.py +24 -0
  70. PlatynUI/ui/core/devices/keyboarddevice.py +279 -0
  71. PlatynUI/ui/core/devices/keyboardproxy.py +69 -0
  72. PlatynUI/ui/core/devices/mousebutton.py +11 -0
  73. PlatynUI/ui/core/devices/mousedevice.py +279 -0
  74. PlatynUI/ui/core/devices/mouseproxy.py +178 -0
  75. PlatynUI/ui/core/ui_technology.py +31 -0
  76. PlatynUI/ui/core/window_manager.py +59 -0
  77. PlatynUI/ui/desktopbase.py +56 -0
  78. PlatynUI/ui/dialog.py +10 -0
  79. PlatynUI/ui/edit.py +48 -0
  80. PlatynUI/ui/element.py +265 -0
  81. PlatynUI/ui/graphic.py +14 -0
  82. PlatynUI/ui/header.py +33 -0
  83. PlatynUI/ui/item.py +100 -0
  84. PlatynUI/ui/lists.py +37 -0
  85. PlatynUI/ui/menus.py +42 -0
  86. PlatynUI/ui/orientation.py +9 -0
  87. PlatynUI/ui/pane.py +14 -0
  88. PlatynUI/ui/proxies/__init__.py +4 -0
  89. PlatynUI/ui/proxies/standardproxies.py +391 -0
  90. PlatynUI/ui/scrollbar.py +9 -0
  91. PlatynUI/ui/strategies/__init__.py +19 -0
  92. PlatynUI/ui/strategies/activatable.py +27 -0
  93. PlatynUI/ui/strategies/application.py +28 -0
  94. PlatynUI/ui/strategies/closeable.py +22 -0
  95. PlatynUI/ui/strategies/control.py +16 -0
  96. PlatynUI/ui/strategies/editable.py +13 -0
  97. PlatynUI/ui/strategies/element.py +65 -0
  98. PlatynUI/ui/strategies/expandable.py +27 -0
  99. PlatynUI/ui/strategies/hasnativewindowhandle.py +14 -0
  100. PlatynUI/ui/strategies/hasorientation.py +15 -0
  101. PlatynUI/ui/strategies/maximizeable.py +27 -0
  102. PlatynUI/ui/strategies/minimizeable.py +27 -0
  103. PlatynUI/ui/strategies/movable.py +14 -0
  104. PlatynUI/ui/strategies/resizable.py +13 -0
  105. PlatynUI/ui/strategies/restorable.py +13 -0
  106. PlatynUI/ui/strategies/selectable.py +27 -0
  107. PlatynUI/ui/strategies/text.py +55 -0
  108. PlatynUI/ui/strategies/title.py +14 -0
  109. PlatynUI/ui/strategies/toggleable.py +23 -0
  110. PlatynUI/ui/strategies/value.py +15 -0
  111. PlatynUI/ui/table.py +56 -0
  112. PlatynUI/ui/tabs.py +28 -0
  113. PlatynUI/ui/text.py +14 -0
  114. PlatynUI/ui/togglestate.py +12 -0
  115. PlatynUI/ui/tree.py +99 -0
  116. PlatynUI/ui/unknown.py +9 -0
  117. PlatynUI/ui/window.py +177 -0
  118. robotframework_platynui-0.0.1.dist-info/METADATA +49 -0
  119. robotframework_platynui-0.0.1.dist-info/RECORD +122 -0
  120. robotframework_platynui-0.0.1.dist-info/WHEEL +4 -0
  121. robotframework_platynui-0.0.1.dist-info/entry_points.txt +2 -0
  122. robotframework_platynui-0.0.1.dist-info/licenses/LICENSE.txt +73 -0
PlatynUI/ui/control.py ADDED
@@ -0,0 +1,49 @@
1
+ from ..core import context
2
+ from ..core.predicate import predicate
3
+ from . import strategies
4
+ from .core.devices.keyboarddevice import KeyboardDevice
5
+ from .core.devices.keyboardproxy import KeyboardProxy
6
+ from .element import Element
7
+
8
+ __all__ = ["Control", "CustomControl"]
9
+
10
+
11
+ @context
12
+ class Control(Element):
13
+ default_role = "Control"
14
+
15
+ @property
16
+ def has_focus(self) -> bool:
17
+ self.ensure_that(self._application_is_ready)
18
+
19
+ return self.adapter.get_strategy(strategies.Control).has_focus
20
+
21
+ @predicate("control {0} has focus")
22
+ def _control_has_focus(self) -> bool:
23
+ self.ensure_that(self._toplevel_parent_is_active, self._element_is_in_view)
24
+
25
+ return self.adapter.get_strategy(strategies.Control).try_ensure_focused()
26
+
27
+ def focus(self) -> bool:
28
+ return self.ensure_that(self._control_has_focus)
29
+
30
+ class _ControlKeyboardProxy(KeyboardProxy):
31
+ def __init__(self, control: "Control", keyboard_device: KeyboardDevice):
32
+ super().__init__(keyboard_device)
33
+ self._control = control
34
+
35
+ def before_action(self, action: KeyboardProxy.Action) -> None:
36
+ self._control.ensure_that(self._control._control_has_focus)
37
+ self.keyboard_device.add_context(self._control)
38
+
39
+ def after_action(self, action: KeyboardProxy.Action) -> None:
40
+ self._control.ensure_that(self._control._application_is_ready, raise_exception=False)
41
+ self.keyboard_device.remove_context(self._control)
42
+
43
+ def _create_keyboard_proxy(self, keyboard_device: KeyboardDevice) -> KeyboardProxy:
44
+ return self._ControlKeyboardProxy(self, keyboard_device)
45
+
46
+
47
+ @context
48
+ class CustomControl(Element):
49
+ default_role = "*"
@@ -0,0 +1,3 @@
1
+ from . import devices
2
+ from .ui_technology import *
3
+ from .window_manager import *
@@ -0,0 +1,6 @@
1
+ from .displaydevice import *
2
+ from .keyboarddevice import *
3
+ from .keyboardproxy import *
4
+ from .mousebutton import *
5
+ from .mousedevice import *
6
+ from .mouseproxy import *
@@ -0,0 +1,25 @@
1
+ from abc import abstractmethod
2
+ from typing import ClassVar, Optional
3
+
4
+ from ....core.strategybase import StrategyBase
5
+ from ....core.types import Rect
6
+
7
+ __all__ = ["BaseDisplayDevice"]
8
+
9
+
10
+ class BaseDisplayDevice(StrategyBase):
11
+ strategy_name: ClassVar[str] = "org.platynui.devices.Display"
12
+
13
+ @property
14
+ @abstractmethod
15
+ def name(self) -> str: ...
16
+
17
+ @property
18
+ @abstractmethod
19
+ def bounding_rectangle(self) -> Rect: ...
20
+
21
+ @abstractmethod
22
+ def highlight_rect(self, rect: Rect, time: float) -> None: ...
23
+
24
+ @abstractmethod
25
+ def get_screenshot(self, rect: Rect, format: str, quality: Optional[int]) -> bytearray: ...
@@ -0,0 +1,64 @@
1
+ import time
2
+ from abc import abstractmethod
3
+ from enum import Enum
4
+ from typing import Any, Optional, Union
5
+
6
+ from ....core.strategybase import StrategyBase
7
+
8
+ __all__ = ["BaseKeyboardDevice", "BaseKeyCode", "InputType"]
9
+
10
+ Key = Union[str, Any]
11
+
12
+
13
+ class BaseKeyCode:
14
+ def __init__(self, key: Any, code: Any, valid: bool, error_text: Optional[str] = None):
15
+ self.key = key
16
+ self.code = code
17
+ self.valid = valid
18
+ self.error_text = error_text
19
+
20
+ def __repr__(self) -> str:
21
+ return (
22
+ f"{self.__class__.__name__}(key={self.key!r}, "
23
+ f"code={self.code!r}, "
24
+ f"valid={self.valid!r}, "
25
+ f"error_text={self.error_text!r})"
26
+ )
27
+
28
+ def __str__(self) -> str:
29
+ return repr(self)
30
+
31
+ def __eq__(self, value: object) -> bool:
32
+ if isinstance(value, BaseKeyCode):
33
+ return bool(self.key == value.key and self.code == value.code)
34
+ return False
35
+
36
+ def __hash__(self) -> int:
37
+ return hash((self.key, self.code))
38
+
39
+
40
+ class InputType(Enum):
41
+ TYPE = 1
42
+ PRESS = 2
43
+ RELEASE = 3
44
+
45
+
46
+ class BaseKeyboardDevice(StrategyBase):
47
+ strategy_name = "org.platynui.devices.Keyboard"
48
+
49
+ def start_input(self, input_type: InputType) -> None:
50
+ pass
51
+
52
+ def end_input(self) -> None:
53
+ pass
54
+
55
+ def delay(self, seconds: float) -> None:
56
+ time.sleep(seconds)
57
+
58
+ @abstractmethod
59
+ def key_to_keycode(self, key: Key) -> BaseKeyCode:
60
+ pass
61
+
62
+ @abstractmethod
63
+ def send_keycode(self, keycode: BaseKeyCode, pressed: bool) -> bool:
64
+ pass
@@ -0,0 +1,30 @@
1
+ from abc import abstractmethod
2
+
3
+ from ....core.strategybase import StrategyBase
4
+ from ....core.types import Point, Size
5
+ from .mousebutton import MouseButton
6
+
7
+ __all__ = ["BaseMouseDevice"]
8
+
9
+
10
+ class BaseMouseDevice(StrategyBase):
11
+ strategy_name = "org.platynui.devices.Mouse"
12
+
13
+ @property
14
+ @abstractmethod
15
+ def double_click_time(self) -> float: ...
16
+
17
+ @property
18
+ @abstractmethod
19
+ def double_click_size(self) -> Size: ...
20
+
21
+ @abstractmethod
22
+ def get_position(self) -> Point: ...
23
+ @abstractmethod
24
+ def move_to(self, pos: Point) -> None: ...
25
+
26
+ @abstractmethod
27
+ def press(self, button: MouseButton) -> None: ...
28
+
29
+ @abstractmethod
30
+ def release(self, button: MouseButton) -> None: ...
@@ -0,0 +1,16 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from ....core.types import Rect
4
+
5
+ __all__ = ["BaseScreenDevice"]
6
+
7
+
8
+ class BaseScreenDevice(ABC):
9
+ @property
10
+ @abstractmethod
11
+ def rectangle(self) -> Rect:
12
+ pass
13
+
14
+ @abstractmethod
15
+ def take_screen_shot(self, rect: Rect, filename: str, image_type: str) -> bool:
16
+ pass
@@ -0,0 +1,113 @@
1
+ import datetime
2
+ import os
3
+ from abc import ABCMeta, abstractmethod
4
+ from typing import Optional
5
+
6
+ from ....core.settings import Settings
7
+ from ....core.types import Rect
8
+ from .basedisplaydevice import BaseDisplayDevice
9
+
10
+ __all__ = ["DisplayDevice", "DefaultDisplayDevice"]
11
+
12
+
13
+ class DisplayDevice(metaclass=ABCMeta):
14
+ @property
15
+ @abstractmethod
16
+ def name(self) -> str:
17
+ pass
18
+
19
+ @property
20
+ @abstractmethod
21
+ def bounding_rectangle(self) -> Rect:
22
+ pass
23
+
24
+ @abstractmethod
25
+ def highlight_rect(self, rect: Rect, time: float) -> None:
26
+ pass
27
+
28
+ @abstractmethod
29
+ def get_screenshot(self, rect: Rect, format: Optional[str] = None, quality: Optional[int] = None) -> bytearray:
30
+ pass
31
+
32
+ @abstractmethod
33
+ def save_screenshot(
34
+ self,
35
+ filename: Optional[str] = None,
36
+ rect: Rect = None,
37
+ format: Optional[str] = None,
38
+ quality: Optional[int] = None,
39
+ ) -> str:
40
+ pass
41
+
42
+
43
+ class DefaultDisplayDevice(DisplayDevice):
44
+ # TODO implement something that gets the single display sizes and orientations to check if the mouse pointer movements are in range
45
+
46
+ def __init__(self, base_display_device: BaseDisplayDevice):
47
+ self.__base_display_device = base_display_device
48
+
49
+ @property
50
+ def name(self) -> str:
51
+ return self.__base_display_device.name
52
+
53
+ @property
54
+ def bounding_rectangle(self) -> Rect:
55
+ return self.__base_display_device.bounding_rectangle
56
+
57
+ def highlight_rect(self, rect: Rect, time: float) -> None:
58
+ self.__base_display_device.highlight_rect(rect, time)
59
+
60
+ @staticmethod
61
+ def __timestamp() -> str:
62
+ return "_" + datetime.datetime.now().isoformat() # noqa: DTZ005
63
+
64
+ def get_screenshot(self, rect: Rect, format: Optional[str] = None, quality: Optional[int] = None) -> bytearray:
65
+ return self.__base_display_device.get_screenshot(
66
+ rect,
67
+ format if format is not None else Settings.current().display_screenshot_format,
68
+ quality if quality is not None else Settings.current().display_screenshot_quality,
69
+ )
70
+
71
+ def save_screenshot(
72
+ self,
73
+ filename: Optional[str] = None,
74
+ rect: Optional[Rect] = None,
75
+ format: Optional[str] = None,
76
+ quality: Optional[int] = None,
77
+ ) -> str:
78
+ directory = "."
79
+ if filename is not None and os.path.isdir(filename):
80
+ directory = filename
81
+ filename = None
82
+ if filename is None:
83
+ while True:
84
+ filename = os.path.join(
85
+ directory,
86
+ Settings.current().display_screenshot_basename
87
+ + self.__timestamp()
88
+ + "."
89
+ + Settings.current().display_screenshot_format,
90
+ )
91
+
92
+ if os.path.exists(filename):
93
+ continue
94
+ break
95
+
96
+ if format is None:
97
+ filename, file_extension = os.path.splitext(filename)
98
+ if file_extension is not None and file_extension != "":
99
+ format = file_extension[1:]
100
+ if format is None or format == "":
101
+ format = None
102
+ if format is None or format == "":
103
+ format = Settings.current().display_screenshot_format
104
+
105
+ filename = "%s.%s" % (filename, format)
106
+
107
+ if rect is None:
108
+ rect = Rect()
109
+
110
+ with open(filename, "wb") as f:
111
+ f.write(self.get_screenshot(rect, format, quality))
112
+
113
+ return filename
@@ -0,0 +1,24 @@
1
+ import abc
2
+ import time
3
+ from typing import Optional
4
+
5
+ from ....core.settings import Settings
6
+
7
+ __all__ = ["InputDevice"]
8
+
9
+
10
+ class InputDevice(metaclass=abc.ABCMeta):
11
+ __after_input_delay: Optional[float] = None
12
+
13
+ @property
14
+ def after_input_delay(self) -> float:
15
+ if self.__after_input_delay is None:
16
+ return Settings.current().input_after_input_delay
17
+ return self.__after_input_delay
18
+
19
+ @after_input_delay.setter
20
+ def after_input_delay(self, value: float) -> None:
21
+ self.__after_input_delay = value
22
+
23
+ def delay(self, seconds: float) -> None:
24
+ time.sleep(seconds)
@@ -0,0 +1,279 @@
1
+ import abc
2
+ import logging
3
+ from typing import Any, Iterable, Iterator, List, Optional, Union
4
+
5
+ from ....core.exceptions import PlatyUiError
6
+ from ....core.settings import Settings
7
+ from .basekeyboarddevice import BaseKeyboardDevice, BaseKeyCode, InputType, Key
8
+ from .inputdevice import InputDevice
9
+
10
+ __all__ = [
11
+ "KeyboardDevice",
12
+ "DefaultKeyboardDevice",
13
+ "InvalidKeySequenceError",
14
+ "InvalidKeyCodeError",
15
+ "InvalidKeyError",
16
+ ]
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ class KeyboardDevice(InputDevice):
22
+ __after_press_release_delay: Optional[float] = None
23
+ __after_release_key_delay: Optional[float] = None
24
+ __after_press_key_delay: Optional[float] = None
25
+
26
+ @property
27
+ def after_press_key_delay(self) -> float:
28
+ if self.__after_press_key_delay is None:
29
+ return Settings.current().keyboard_after_press_key_delay
30
+ return self.__after_press_key_delay
31
+
32
+ @after_press_key_delay.setter
33
+ def after_press_key_delay(self, value: float) -> None:
34
+ self.__after_press_key_delay = value
35
+
36
+ @property
37
+ def after_release_key_delay(self) -> float:
38
+ if self.__after_release_key_delay is None:
39
+ return Settings.current().keyboard_after_release_key_delay
40
+ return self.__after_release_key_delay
41
+
42
+ @after_release_key_delay.setter
43
+ def after_release_key_delay(self, value: float) -> None:
44
+ self.__after_release_key_delay = value
45
+
46
+ @property
47
+ def after_press_release_delay(self) -> float:
48
+ if self.__after_press_release_delay is None:
49
+ return Settings.current().keyboard_after_press_release_delay
50
+ return self.__after_press_release_delay
51
+
52
+ @after_press_release_delay.setter
53
+ def after_press_release_delay(self, value: float) -> None:
54
+ self.__after_press_release_delay = value
55
+
56
+ def add_context(self, context: Any) -> None:
57
+ pass
58
+
59
+ def remove_context(self, context: Any) -> None:
60
+ pass
61
+
62
+ @abc.abstractmethod
63
+ def escape_text(self, value: str) -> str:
64
+ pass
65
+
66
+ @abc.abstractmethod
67
+ def type_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None: ...
68
+
69
+ @abc.abstractmethod
70
+ def press_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None: ...
71
+
72
+ @abc.abstractmethod
73
+ def release_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None: ...
74
+
75
+
76
+ class KeyEvent:
77
+ def __init__(self, key_code: BaseKeyCode, press: bool):
78
+ self.key_code = key_code
79
+ self.press = press
80
+
81
+ def __repr__(self) -> str:
82
+ return "KeyEvent(key=%s, press=%s)" % (repr(self.key_code), repr(self.press))
83
+
84
+ def __str__(self) -> str:
85
+ return self.__repr__()
86
+
87
+
88
+ class KeyboardSendError(PlatyUiError):
89
+ pass
90
+
91
+
92
+ class InvalidKeyError(PlatyUiError):
93
+ pass
94
+
95
+
96
+ class InvalidKeySequenceError(InvalidKeyError):
97
+ pass
98
+
99
+
100
+ class InvalidKeyCodeError(InvalidKeyError):
101
+ pass
102
+
103
+
104
+ class KeyConverter:
105
+ def __init__(
106
+ self,
107
+ base_keyboard_device: BaseKeyboardDevice,
108
+ *keys: Union[str, Any, Iterable[Any]],
109
+ down: bool = True,
110
+ up: bool = True,
111
+ ):
112
+ self.base_keyboard_device = base_keyboard_device
113
+ self.input = keys
114
+ self._current_index = 0
115
+ self._down = down
116
+ self._up = up
117
+
118
+ def __next_key(self, keys: str) -> Iterator[str]:
119
+ for i in keys:
120
+ yield i
121
+ self._current_index += 1
122
+
123
+ def convert(self) -> Iterator[KeyEvent]:
124
+ for i in self.input:
125
+ for j in self.__convert_single(i):
126
+ yield j
127
+
128
+ def __key_to_keycode(self, key: Key) -> BaseKeyCode:
129
+ result = self.base_keyboard_device.key_to_keycode(key)
130
+
131
+ if not result.valid:
132
+ raise InvalidKeyCodeError(
133
+ "can't get a valid keycode from device for key '%s'%s"
134
+ % (key, "" if result.error_text is None else ": %s" % result.error_text)
135
+ )
136
+ return result
137
+
138
+ def __convert_single(self, keys: Union[str, Any, Iterable[Any]]) -> Iterable[KeyEvent]:
139
+ if isinstance(keys, str):
140
+ g = self.__next_key(keys)
141
+ for i in g:
142
+ if i == "<":
143
+ current = ""
144
+ first = 0
145
+ escape = False
146
+ sequence = []
147
+ for j in g:
148
+ first += 1
149
+ if j == "<":
150
+ if first == 1:
151
+ if self._down:
152
+ yield KeyEvent(self.__key_to_keycode(j), press=True)
153
+ if self._up:
154
+ yield KeyEvent(self.__key_to_keycode(j), press=False)
155
+ escape = True
156
+ break
157
+ else:
158
+ raise InvalidKeySequenceError("invalid key sequence at index %s" % self._current_index)
159
+
160
+ if str.isspace(j):
161
+ continue
162
+ elif j == "+":
163
+ sequence.append(current)
164
+ current = ""
165
+ elif j == ">":
166
+ break
167
+ else:
168
+ current += j
169
+
170
+ if not escape:
171
+ if current:
172
+ sequence.append(current)
173
+
174
+ if len(sequence) == 0:
175
+ raise InvalidKeySequenceError("empty key sequence at index %s" % self._current_index)
176
+
177
+ if self._down:
178
+ for k in [KeyEvent(self.__key_to_keycode(k), press=True) for k in sequence]:
179
+ yield k
180
+ if self._up:
181
+ for k in [
182
+ KeyEvent(self.__key_to_keycode(k), press=False)
183
+ for k in (reversed(sequence) if self._down else sequence)
184
+ ]:
185
+ yield k
186
+ continue
187
+
188
+ if self._down:
189
+ yield KeyEvent(self.__key_to_keycode(i), press=True)
190
+ if self._up:
191
+ yield KeyEvent(self.__key_to_keycode(i), press=False)
192
+ elif isinstance(keys, Iterable):
193
+ if self._down:
194
+ for i in keys:
195
+ yield KeyEvent(self.__key_to_keycode(i), press=True)
196
+ if self._up:
197
+ for i in list(keys)[::-1] if self._down else keys:
198
+ yield KeyEvent(self.__key_to_keycode(i), press=False)
199
+ else:
200
+ if self._down:
201
+ yield KeyEvent(self.__key_to_keycode(keys), press=True)
202
+ if self._up:
203
+ yield KeyEvent(self.__key_to_keycode(keys), press=False)
204
+
205
+
206
+ class DefaultKeyboardDevice(KeyboardDevice):
207
+ __base_keyboard_device = None # type: BaseKeyboardDevice
208
+
209
+ def __init__(self, base_keyboard_device: BaseKeyboardDevice):
210
+ self.__base_keyboard_device = base_keyboard_device
211
+
212
+ self._pressed_keys: List[BaseKeyCode] = []
213
+
214
+ def escape_text(self, value: str) -> str:
215
+ return str.replace(value, "<", "<<")
216
+
217
+ @property
218
+ def base_keyboard_device(self) -> BaseKeyboardDevice:
219
+ return self.__base_keyboard_device
220
+
221
+ def __del__(self) -> None:
222
+ if len(self._pressed_keys) > 0:
223
+ logger.warning("there are pressed keys (%s), try to send key release", self._pressed_keys, exc_info=True)
224
+ for k in self._pressed_keys:
225
+ self.base_keyboard_device.send_keycode(k, False)
226
+
227
+ def __send_key_event(self, key_event: KeyEvent, delay: Optional[float]) -> None:
228
+ if key_event.key_code.valid:
229
+ if not self.base_keyboard_device.send_keycode(key_event.key_code, key_event.press):
230
+ raise KeyboardSendError(
231
+ "can't send {} {}".format("press" if key_event.press else "release", key_event.key_code)
232
+ )
233
+ else:
234
+ logger.warning(
235
+ "invalid keycode %s, don't send it to device%s",
236
+ key_event.key_code,
237
+ "" if key_event.key_code.error_text is None else ": %s" % key_event.key_code.error_text,
238
+ )
239
+
240
+ if key_event.press:
241
+ self.base_keyboard_device.delay(self.after_press_key_delay)
242
+ self._pressed_keys.insert(0, key_event.key_code)
243
+ else:
244
+ self.base_keyboard_device.delay(delay if delay is not None else self.after_release_key_delay)
245
+ if key_event.key_code in self._pressed_keys:
246
+ self._pressed_keys.remove(key_event.key_code)
247
+
248
+ def type_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None:
249
+ self.base_keyboard_device.start_input(InputType.TYPE)
250
+ try:
251
+ for c in KeyConverter(self.base_keyboard_device, *keys, down=True, up=True).convert():
252
+ self.__send_key_event(c, delay)
253
+
254
+ self.delay(self.after_press_release_delay)
255
+ self.delay(self.after_input_delay)
256
+ finally:
257
+ self.base_keyboard_device.end_input()
258
+
259
+ def press_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None:
260
+ self.base_keyboard_device.start_input(InputType.PRESS)
261
+ try:
262
+ for c in KeyConverter(self.base_keyboard_device, *keys, down=True, up=False).convert():
263
+ self.__send_key_event(c, delay)
264
+
265
+ self.delay(self.after_press_release_delay)
266
+ self.delay(self.after_input_delay)
267
+ finally:
268
+ self.base_keyboard_device.end_input()
269
+
270
+ def release_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None:
271
+ self.base_keyboard_device.start_input(InputType.RELEASE)
272
+ try:
273
+ for c in KeyConverter(self.base_keyboard_device, *keys, down=False, up=True).convert():
274
+ self.__send_key_event(c, delay)
275
+
276
+ self.delay(self.after_press_release_delay)
277
+ self.delay(self.after_input_delay)
278
+ finally:
279
+ self.base_keyboard_device.end_input()
@@ -0,0 +1,69 @@
1
+ import enum
2
+ from abc import ABCMeta
3
+ from typing import Any, Iterable, Optional, Union, cast
4
+
5
+ from typing_extensions import Self
6
+
7
+ from ....core import Adapter
8
+ from ...core.ui_technology import UiTechnology
9
+ from .keyboarddevice import KeyboardDevice
10
+
11
+ __all__ = ["KeyboardProxy", "AdapterKeyboardProxy"]
12
+
13
+
14
+ class KeyboardProxy(metaclass=ABCMeta):
15
+ def __init__(self, keyboard_device: KeyboardDevice):
16
+ self.keyboard_device = keyboard_device
17
+
18
+ def __enter__(self) -> "Self":
19
+ return self
20
+
21
+ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
22
+ pass
23
+
24
+ class Action(enum.Enum):
25
+ TYPE = 1
26
+ PRESS = 2
27
+ RELEASE = 3
28
+
29
+ def before_action(self, action: Action) -> None:
30
+ pass
31
+
32
+ def after_action(self, action: Action) -> None:
33
+ pass
34
+
35
+ def type_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None:
36
+ self.before_action(KeyboardProxy.Action.TYPE)
37
+
38
+ self.keyboard_device.type_keys(*keys, delay=delay)
39
+
40
+ self.after_action(KeyboardProxy.Action.TYPE)
41
+
42
+ def press_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None:
43
+ self.before_action(KeyboardProxy.Action.PRESS)
44
+
45
+ self.keyboard_device.press_keys(*keys, delay=delay)
46
+
47
+ self.after_action(KeyboardProxy.Action.PRESS)
48
+
49
+ def release_keys(self, *keys: Union[str, Any, Iterable[Any]], delay: Optional[float] = None) -> None:
50
+ self.before_action(KeyboardProxy.Action.RELEASE)
51
+
52
+ self.keyboard_device.release_keys(*keys, delay=delay)
53
+
54
+ self.after_action(KeyboardProxy.Action.RELEASE)
55
+
56
+ def escape_text(self, value: str) -> str:
57
+ return self.keyboard_device.escape_text(value)
58
+
59
+
60
+ class AdapterKeyboardProxy(KeyboardProxy):
61
+ def __init__(self, adapter: Adapter, keyboard_device: KeyboardDevice = None):
62
+ super().__init__(keyboard_device or cast(UiTechnology, adapter.technology).keyboard_device)
63
+ self._adapter = adapter
64
+
65
+ def before_action(self, action: KeyboardProxy.Action) -> None:
66
+ self.keyboard_device.add_context(self._adapter)
67
+
68
+ def after_action(self, action: KeyboardProxy.Action) -> None:
69
+ self.keyboard_device.remove_context(self._adapter)