Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change natlinkcore to use natlink.setMessageWindow() #93

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading