Skip to content

Commit

Permalink
Merge pull request #93 from drmfinlay/feat/decouple-message-window
Browse files Browse the repository at this point in the history
Change natlinkcore to use natlink.setMessageWindow()
  • Loading branch information
dougransom authored Nov 25, 2024
2 parents 3327ba8 + 1851c85 commit 4b02fdd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/natlinkcore/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import natlink
from natlinkcore.config import LogLevel, NatlinkConfig, expand_path
from natlinkcore.natlinkutils import idd_reload, idd_exit
from natlinkcore.readwritefile import ReadWriteFile
from natlinkcore.callbackhandler import CallbackHandler
from natlinkcore.singleton import Singleton
Expand Down Expand Up @@ -257,6 +258,13 @@ def _add_dirs_to_path(directories: Iterable[str]) -> None:
if d_expanded not in sys.path:
sys.path.insert(0, d_expanded)

@staticmethod
def _del_dirs_from_path(directories: Iterable[str]) -> None:
for d in directories:
d_expanded = expand_path(d)
if d_expanded in sys.path:
sys.path.remove(d_expanded)

def _call_and_catch_all_exceptions(self, fn: Callable[[], None]) -> None:
try:
fn()
Expand Down Expand Up @@ -455,6 +463,14 @@ def on_begin_callback(self, module_info: Tuple[str, str, int]) -> None:
value -= 1
self.load_on_begin_utterance = value
self.trigger_load()

def on_message_window_callback(self, event):
if event == idd_reload:
self.trigger_load(force_load=True)
natlink.setBeginCallback(self.on_begin_callback)
natlink.setChangeCallback(self.on_change_callback)
elif event == idd_exit:
self.finish()

def get_user_language(self, DNSuserDirectory):
"""return the user language (default "enx") from Dragon inifiles
Expand Down Expand Up @@ -545,6 +561,17 @@ def start(self) -> None:
self.trigger_load()
natlink.setBeginCallback(self.on_begin_callback)
natlink.setChangeCallback(self.on_change_callback)
natlink.setMessageWindow(self.on_message_window_callback)

def finish(self) -> None:
# reverse changes made by start()
natlink.setMessageWindow(None)
natlink.setChangeCallback(None)
natlink.setBeginCallback(None)
self.unload_all_loaded_modules()
self._del_dirs_from_path(self.config.directories)
natlink.active_loader = None
self.logger.info('Stopping natlink loader')

def setup_logger(self) -> None:
for handler in list(self.logger.handlers):
Expand Down
5 changes: 5 additions & 0 deletions src/natlinkcore/natlinkutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
dgnwordflag_space_bar = 0x08000000
dgnwordflag_topicadded = 0x40000000
dgnwordflag_DNS8newwrdProp = 0x20000000

# The following constants define the window message codes which are passed
# to the message window callback on menu command activation.
idd_reload = 32768
idd_exit = 32769

## temporary fix, remove as soon as natlinkmain is more secure QH (Febr 2021)

Expand Down

0 comments on commit 4b02fdd

Please sign in to comment.