diff --git a/addon/brailleDisplayDrivers/remote.py b/addon/brailleDisplayDrivers/remote.py index b7f7bc1..e94f142 100644 --- a/addon/brailleDisplayDrivers/remote.py +++ b/addon/brailleDisplayDrivers/remote.py @@ -54,12 +54,7 @@ def _incoming_numCells(self, payload: bytes) -> int: def _get_numCells(self) -> int: if (value := self.numRows * self.numCols) == 0: - attribute = protocol.BrailleAttribute.NUM_CELLS - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) + value = self._getRemoteAttributeValueWithFallback(protocol.BrailleAttribute.NUM_CELLS) return value @protocol.attributeReceiver(protocol.BrailleAttribute.NUM_ROWS, defaultValue=1) @@ -68,13 +63,7 @@ def _incoming_numRows(self, payload: bytes) -> int: return ord(payload) def _get_numRows(self) -> int: - attribute = protocol.BrailleAttribute.NUM_ROWS - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) - return value + return self._getRemoteAttributeValueWithFallback(protocol.BrailleAttribute.NUM_ROWS) @protocol.attributeReceiver(protocol.BrailleAttribute.NUM_COLS, defaultValue=0) def _incoming_numCols(self, payload: bytes) -> int: @@ -82,13 +71,7 @@ def _incoming_numCols(self, payload: bytes) -> int: return ord(payload) def _get_numCols(self) -> int: - attribute = protocol.BrailleAttribute.NUM_COLS - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) - return value + return self._getRemoteAttributeValueWithFallback(protocol.BrailleAttribute.NUM_COLS) @protocol.attributeReceiver(protocol.BrailleAttribute.GESTURE_MAP) def _incoming_gestureMapUpdate(self, payload: bytes) -> inputCore.GlobalGestureMap: @@ -100,13 +83,7 @@ def _default_gestureMap(self, _attribute: protocol.AttributeT): return inputCore.GlobalGestureMap() def _get_gestureMap(self) -> inputCore.GlobalGestureMap: - attribute = protocol.BrailleAttribute.GESTURE_MAP - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) - return value + return self._getRemoteAttributeValueWithFallback(protocol.BrailleAttribute.GESTURE_MAP) @protocol.commandHandler(protocol.BrailleCommand.EXECUTE_GESTURE) def _command_executeGesture(self, payload: bytes): diff --git a/addon/lib/protocol/__init__.py b/addon/lib/protocol/__init__.py index c099b29..085aad8 100644 --- a/addon/lib/protocol/__init__.py +++ b/addon/lib/protocol/__init__.py @@ -174,6 +174,13 @@ def updateCallback(self, func: AttributeValueUpdateCallbackT): return func +def _constantDefaultValueGetter(defaultValue: Any) -> DefaultValueGetterT: + def _defaultValueGetter(_self: RemoteProtocolHandler, _attribute: AttributeT): + return defaultValue + + return _defaultValueGetter + + def attributeReceiver( attribute: AttributeT, defaultValue: Any = None, @@ -183,11 +190,7 @@ def attributeReceiver( if defaultValue is not None and defaultValueGetter is not None: raise ValueError("Either defaultValue or defaultValueGetter is required, but not both") if defaultValueGetter is None: - - def _defaultValueGetter(_self: RemoteProtocolHandler, _attribute: AttributeT): - return defaultValue - - defaultValueGetter = _defaultValueGetter + defaultValueGetter = _constantDefaultValueGetter(defaultValue) return partial( AttributeReceiver, attribute, @@ -442,6 +445,14 @@ def _safeWait(self, predicate: Callable[[], bool], timeout: float | None = None) timeout -= time.perf_counter() - curTime return predicate() + def _getRemoteAttributeValueWithFallback(self, attribute: AttributeT): + try: + return self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) + except KeyError: + value = self._attributeValueProcessor._getDefaultValue(attribute) + self.requestRemoteAttribute(attribute) + return value + def getRemoteAttribute( self, attribute: AttributeT, @@ -510,13 +521,7 @@ def _incoming_nvdaVersion(self, payload: bytes) -> str: return payload.decode() def _get_nvdaVersion(self) -> str: - attribute = GenericAttribute.NVDA_VERSION - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) - return value + return self._getRemoteAttributeValueWithFallback(GenericAttribute.NVDA_VERSION) @attributeSender(GenericAttribute.RD_ACCESS_VERSION) def _outgoing_rdAccessVersion(self) -> bytes: @@ -527,10 +532,4 @@ def _incoming_rdAccessVersion(self, payload: bytes) -> str: return payload.decode() def _get_rdAccessVersion(self) -> str: - attribute = GenericAttribute.RD_ACCESS_VERSION - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) - return value + return self._getRemoteAttributeValueWithFallback(GenericAttribute.RD_ACCESS_VERSION) diff --git a/addon/synthDrivers/remote.py b/addon/synthDrivers/remote.py index 72b615c..45abf86 100644 --- a/addon/synthDrivers/remote.py +++ b/addon/synthDrivers/remote.py @@ -114,13 +114,7 @@ def _incoming_supportedCommands(self, payLoad: bytes) -> frozenset: return self._unpickle(payLoad) def _get_supportedCommands(self): - attribute = protocol.SpeechAttribute.SUPPORTED_COMMANDS - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) - return value + return self._getRemoteAttributeValueWithFallback(protocol.SpeechAttribute.SUPPORTED_COMMANDS) @protocol.attributeReceiver(protocol.SpeechAttribute.LANGUAGE, defaultValue=getLanguage()) def _incoming_language(self, payload: bytes) -> str | None: @@ -128,13 +122,7 @@ def _incoming_language(self, payload: bytes) -> str | None: return self._unpickle(payload) def _get_language(self): - attribute = protocol.SpeechAttribute.LANGUAGE - try: - value = self._attributeValueProcessor.getValue(attribute, fallBackToDefault=False) - except KeyError: - value = self._attributeValueProcessor._getDefaultValue(attribute) - self.requestRemoteAttribute(attribute) - return value + return self._getRemoteAttributeValueWithFallback(protocol.SpeechAttribute.LANGUAGE) @protocol.commandHandler(protocol.SpeechCommand.INDEX_REACHED) def _command_indexReached(self, incomingPayload: bytes):