| 100 | |
| 101 | # WindowsACI Class |
| 102 | class WindowsACI(ACI): |
| 103 | def __init__(self, top_app_only: bool = True, ocr: bool = False): |
| 104 | super().__init__(top_app_only=top_app_only, ocr=ocr) |
| 105 | self.nodes = [] |
| 106 | self.all_apps = list_apps_in_directories() |
| 107 | |
| 108 | def get_active_apps(self, obs: Dict) -> List[str]: |
| 109 | return UIElement.get_current_applications(obs) |
| 110 | |
| 111 | def get_top_app(self, obs: Dict) -> str: |
| 112 | return UIElement.get_top_app(obs) |
| 113 | |
| 114 | def preserve_nodes(self, tree, exclude_roles=None): |
| 115 | if exclude_roles is None: |
| 116 | exclude_roles = set() |
| 117 | |
| 118 | preserved_nodes = [] |
| 119 | |
| 120 | def traverse_and_preserve(element): |
| 121 | role = element.role() |
| 122 | |
| 123 | if role not in exclude_roles: |
| 124 | position = element.position() |
| 125 | size = element.size() |
| 126 | if position and size: |
| 127 | x, y = position |
| 128 | w, h = size |
| 129 | |
| 130 | if x >= 0 and y >= 0 and w > 0 and h > 0: |
| 131 | preserved_nodes.append( |
| 132 | { |
| 133 | "position": (x, y), |
| 134 | "size": (w, h), |
| 135 | "title": element.title(), |
| 136 | "text": element.text(), |
| 137 | "role": role, |
| 138 | } |
| 139 | ) |
| 140 | |
| 141 | children = element.children() |
| 142 | if children: |
| 143 | for child_element in children: |
| 144 | traverse_and_preserve(child_element) |
| 145 | |
| 146 | traverse_and_preserve(tree) |
| 147 | return preserved_nodes |
| 148 | |
| 149 | def extract_elements_from_screenshot(self, screenshot: bytes) -> Dict[str, Any]: |
| 150 | url = os.environ.get("OCR_SERVER_ADDRESS") |
| 151 | if not url: |
| 152 | raise EnvironmentError("OCR SERVER ADDRESS NOT SET") |
| 153 | |
| 154 | encoded_screenshot = base64.b64encode(screenshot).decode("utf-8") |
| 155 | response = requests.post(url, json={"img_bytes": encoded_screenshot}) |
| 156 | |
| 157 | if response.status_code != 200: |
| 158 | return { |
| 159 | "error": f"Request failed with status code {response.status_code}", |
no outgoing calls
no test coverage detected