Skip to content

Commit

Permalink
fix: Handle cases where the program doesn't contain every field.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaVandaele committed May 26, 2023
1 parent 1c6c039 commit 6692674
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
4 changes: 2 additions & 2 deletions addon/appModules/captvty.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,13 @@ def get_program_info(

try:
program = Program(element.name)
program_info = f"{program.name} | Durée: {program.duration} | Sommaire: {program.summary}"
program_info = f"{program.name}{f' | Durée: {program.duration}' if program.duration else ''}{f' | Sommaire : {program.summary}' if program.summary else ''}"
return program_info
except (
AttributeError,
IndexError,
): # The element is not a program
log.info("Nuh uh, not a program")
log.debug(f"Element is not a program: {element.name}")
return None

def selected_program_callback(
Expand Down
29 changes: 28 additions & 1 deletion addon/appModules/modules/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import winUser
from logHandler import log
from NVDAObjects import NVDAObject
from NVDAObjects.IAccessible import IAccessible
from NVDAObjects.IAccessible import IAccessible, getNVDAObjectFromEvent


class AppModes(IntEnum):
Expand Down Expand Up @@ -379,3 +379,30 @@ def scroll_and_click_on_element(
bounds_offset=bounds_offset,
)
left_click_element_with_mouse(element, x_offset, y_offset)


def reacquire_element(
element: Union[NVDAObject, IAccessible]
) -> Union[IAccessible, None]:
"""Reacquires the element by index in group.
This is useful when the element is not accessible from the current thread.
Args:
element (Union[NVDAObject, IAccessible]): The element to reacquire.
Returns:
Union[IAccessible, None]: The reacquired element or None if not found.
"""
position_info = getattr(element, "positionInfo", None)

index_in_group = position_info.get("indexInGroup") if position_info else None

if index_in_group:
reacquired_element = getNVDAObjectFromEvent(
element.windowHandle,
winUser.OBJID_CLIENT,
index_in_group,
)

return reacquired_element
return None
20 changes: 5 additions & 15 deletions addon/appModules/modules/list_elements.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import threading
from typing import Any, Callable, List, Optional, Union

import winUser
import wx
from gui import guiHelper
from logHandler import log
from NVDAObjects import NVDAObject
from NVDAObjects.IAccessible import IAccessible, getNVDAObjectFromEvent
from NVDAObjects.IAccessible import IAccessible
from NVDAObjects.IAccessible.sysListView32 import ListItem

from .helper_functions import normalize_str
from .helper_functions import normalize_str, reacquire_element


class VirtualList(wx.ListCtrl):
Expand Down Expand Up @@ -321,18 +320,9 @@ def worker(elements):
new_element_names = []

for element in elements:
position_info = getattr(element, "positionInfo", None)
index_in_group = (
position_info.get("indexInGroup") if position_info else None
)

if index_in_group:
# get the element by index so we can access the element within this thread.
element = getNVDAObjectFromEvent(
element.windowHandle,
winUser.OBJID_CLIENT,
index_in_group,
)
if isinstance(element, (IAccessible, NVDAObject)):
element = reacquire_element(element)
# log.debug("Reacquired element: %s", element.name)

self.element_names.append(self.element_name_getter(element))

Expand Down
27 changes: 22 additions & 5 deletions addon/appModules/modules/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,28 @@ def __init__(self, unparsed_program: str) -> None:
unparsed_program (str): Unparsed string containing program information.
"""
parsed_program: List[str] = unparsed_program.split("; ")
self.name: str = parsed_program[0]
self.channel: str = parsed_program[1][8:] # Remove "Chaîne: " prefix
self.published_at: str = parsed_program[2][24:] # "Diffusée ou publiée le: "
self.duration: str = parsed_program[3][7:] # "Durée: "
self.summary: str = parsed_program[4][8:] # "Résumé: "

self.name: str = parsed_program.pop(0).strip()

if "Chaîne" in parsed_program[0]:
self.channel = parsed_program.pop(0).split(":")[1].strip()
else:
self.channel = None

if "Diffusée ou publiée le" in parsed_program[0]:
self.published_at = parsed_program.pop(0).split(":")[1].strip()
else:
self.published_at = None

if "Durée" in parsed_program[0]:
self.duration = parsed_program.pop(0).split(":")[1].strip()
else:
self.duration = None

if "Résumé" in parsed_program[0]:
self.summary = parsed_program.pop(0).split(":")[1].strip()
else:
self.summary = None

def __str__(self) -> str:
"""
Expand Down

0 comments on commit 6692674

Please sign in to comment.