|
9 | 9 | import os |
10 | 10 | import threading |
11 | 11 | import time |
12 | | -from typing import Optional, cast, Callable, Union, List, Tuple, Iterable |
| 12 | +from typing import Optional, cast, Callable, Union, List, Tuple |
13 | 13 |
|
14 | 14 | import Xlib.display |
15 | 15 | import Xlib.protocol |
@@ -258,28 +258,37 @@ def getPropertyValue(prop: Optional[Xlib.protocol.request.GetProperty], text: bo |
258 | 258 | Extract data from retrieved window/root property |
259 | 259 |
|
260 | 260 | :param prop: Xlib.protocol.request.GetProperty struct from which extract data |
261 | | - :param text: set to ''True'' to convert the atoms (int) to their names (str) |
| 261 | + :param text: set to ''True'' to convert the atoms (int) to their names (str), if possible |
262 | 262 | :param display: display to which window belongs to (defaults to default display) |
263 | 263 | :return: extracted property data (as a list of integers or strings) or None |
264 | 264 | """ |
265 | 265 | if prop and hasattr(prop, "value"): |
266 | 266 | # Value is either str, bytes (separated by '\x00' when multiple values) or array.array of integers. |
267 | 267 | # The type of array values is stored in array.typecode ('I' in this case). |
268 | | - valueData: Union[array.array[int], bytes] = prop.value |
269 | | - if isinstance(valueData, str) or isinstance(valueData, int): |
| 268 | + valueData: Union[str, int, bytes, array.array[int], List[str], List[int]] = prop.value |
| 269 | + if isinstance(valueData, str): |
270 | 270 | return [valueData] |
271 | | - if isinstance(valueData, bytes): |
| 271 | + elif isinstance(valueData, int): |
| 272 | + result = valueData |
| 273 | + if text and valueData != 0: |
| 274 | + try: |
| 275 | + result = display.get_atom_name(valueData) |
| 276 | + except: |
| 277 | + pass |
| 278 | + return [result] |
| 279 | + elif isinstance(valueData, bytes): |
272 | 280 | resultStr: List[str] = [a for a in valueData.decode().split("\x00") if a] |
273 | 281 | return resultStr |
274 | | - elif isinstance(valueData, array.array): |
| 282 | + elif isinstance(valueData, array.array) or isinstance(valueData, list): |
275 | 283 | if text: |
276 | | - resultStr = [display.get_atom_name(a) for a in valueData if isinstance(a, int) and a != 0] |
277 | | - return resultStr |
278 | | - else: |
| 284 | + try: |
| 285 | + resultStr = [display.get_atom_name(a) for a in valueData if isinstance(a, int) and a != 0] |
| 286 | + return resultStr |
| 287 | + except: |
| 288 | + text = False |
| 289 | + if not text: |
279 | 290 | resultInt: List[int] = [a for a in valueData if isinstance(a, int)] |
280 | 291 | return resultInt |
281 | | - # Leaving this to detect if data has an unexpected type |
282 | | - return [a for a in valueData] if isinstance(valueData, Iterable) else [valueData] |
283 | 292 | return None |
284 | 293 |
|
285 | 294 |
|
|
0 commit comments