add ServerState enum to give is_running return values meaning.

fail faster if nvda isn't running
This commit is contained in:
onyx-and-iris 2026-03-20 00:29:39 +00:00
parent aae62fa136
commit 2a86c05bea
2 changed files with 16 additions and 2 deletions

View File

@ -1,7 +1,14 @@
from enum import IntEnum
from .cdll import libc from .cdll import libc
from .errors import NVDAVMCAPIError from .errors import NVDAVMCAPIError
class ServerState(IntEnum):
RUNNING = 0
UNAVAILABLE = 1722
class CBindings: class CBindings:
bind_test_if_running = libc.nvdaController_testIfRunning bind_test_if_running = libc.nvdaController_testIfRunning
bind_speak_text = libc.nvdaController_speakText bind_speak_text = libc.nvdaController_speakText
@ -18,7 +25,10 @@ class CBindings:
class Nvda(CBindings): class Nvda(CBindings):
@property @property
def is_running(self): def is_running(self):
return self.call(self.bind_test_if_running) == 0 return (
self.call(self.bind_test_if_running, ok=(ServerState.RUNNING, ServerState.UNAVAILABLE))
== ServerState.RUNNING
)
def speak(self, text): def speak(self, text):
self.call(self.bind_speak_text, text) self.call(self.bind_speak_text, text)

View File

@ -6,6 +6,7 @@ import FreeSimpleGUI as psg
from . import configuration, models, util from . import configuration, models, util
from .builder import Builder from .builder import Builder
from .errors import NVDAVMError
from .nvda import Nvda from .nvda import Nvda
from .parser import Parser from .parser import Parser
from .popup import Popup from .popup import Popup
@ -25,6 +26,10 @@ class NVDAVMWindow(psg.Window):
self.kind = self.vm.kind self.kind = self.vm.kind
self.logger = logger.getChild(type(self).__name__) self.logger = logger.getChild(type(self).__name__)
self.logger.debug(f'loaded with theme: {psg.theme()}') self.logger.debug(f'loaded with theme: {psg.theme()}')
self.nvda = Nvda()
if not self.nvda.is_running:
self.logger.error('NVDA is not running. Exiting...')
raise NVDAVMError('NVDA is not running')
self.cache = { self.cache = {
'hw_ins': models._make_hardware_ins_cache(self.vm), 'hw_ins': models._make_hardware_ins_cache(self.vm),
'hw_outs': models._make_hardware_outs_cache(self.vm), 'hw_outs': models._make_hardware_outs_cache(self.vm),
@ -34,7 +39,6 @@ class NVDAVMWindow(psg.Window):
'asio': models._make_patch_asio_cache(self.vm), 'asio': models._make_patch_asio_cache(self.vm),
'insert': models._make_patch_insert_cache(self.vm), 'insert': models._make_patch_insert_cache(self.vm),
} }
self.nvda = Nvda()
self.parser = Parser() self.parser = Parser()
self.popup = Popup(self) self.popup = Popup(self)
self.builder = Builder(self) self.builder = Builder(self)