Skip to content

Commit

Permalink
Natlinksettingsdir (#56)
Browse files Browse the repository at this point in the history
* print message for loader.py improved
* natlinkstatus.py, removed UnimacroGrammarsDirectory (not needed any more)
* improving and testing with pytest nsformat.py
* added unload_all_loaded_modules method in loader. Used when changing user, on_change_callback( user)
* bump version number to 5.3.6
  • Loading branch information
quintijn committed Oct 8, 2023
1 parent e3f4178 commit 1357189
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/natlinkcore/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'''Python portion of Natlink, a compatibility module for Dragon Naturally Speaking
The python stuff including test modules'''
__version__="5.3.5"
__version__="5.3.6"
#pylint:disable=
from pathlib import Path

Expand Down
21 changes: 16 additions & 5 deletions src/natlinkcore/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,12 @@ def _call_and_catch_all_exceptions(self, fn: Callable[[], None]) -> None:

def unload_module(self, module: ModuleType) -> None:
unload = getattr(module, 'unload', None)
if unload is not None:
self.logger.debug(f'unloading module: {module.__name__}')
self._call_and_catch_all_exceptions(unload)
if unload is None:
self.logger.info(f'cannot unload module {module.__name__}')
return
self.logger.debug(f'unloading module: {module.__name__}')
self._call_and_catch_all_exceptions(unload)


@staticmethod
def _import_module_from_path(mod_path: Path) -> ModuleType:
Expand Down Expand Up @@ -357,6 +360,13 @@ def load_or_reload_modules(self, mod_paths: Iterable[Path], force_load: bool = N
for mod_path in mod_paths:
self.load_or_reload_module(mod_path, force_load=force_load)
self.seen.add(mod_path)

def unload_all_loaded_modules(self):
"""unload the modules that are loaded, and empty the bad modules list
"""
for module in self.loaded_modules.values():
self.unload_module(module)
self.bad_modules.clear()

def remove_modules_that_no_longer_exist(self) -> None:
mod_paths = self.module_paths_for_user
Expand Down Expand Up @@ -401,6 +411,8 @@ def on_change_callback(self, change_type: str, args: Any) -> None:
self.set_user_language(args)
self.logger.debug(f'on_change_callback, user "{self.user}", profile: "{self.profile}", language: "{self.language}"')
if self.config.load_on_user_changed:
# added line, QH, 2023-10-08
self.unload_all_loaded_modules()
self.trigger_load(force_load=True)
elif change_type == 'mic' and args == 'on':
self.logger.debug('on_change_callback called with: "mic", "on"')
Expand Down Expand Up @@ -487,10 +499,9 @@ def set_user_language(self, args: Any = None):
self.logger.debug(f'set_user_language, user: "{self.user}", profile: "{self.profile}", language: "{self.language}"')
else:
self.user, self.profile = '', ''
self.logger.warning('set_user_language, cannot get input for get_user_language, set to "enx",\n\tprobably Dragon is not running')
self.logger.warning('set_user_language, cannot get input for get_user_language, set to "enx",\n\tprobably Dragon is not running or you are preforming pytests')
self.language = 'enx'


def start(self) -> None:
self.logger.info(f'Starting natlink loader from config file:\n\t"{self.config.config_path}"')
natlink.active_loader = self
Expand Down
62 changes: 31 additions & 31 deletions src/natlinkcore/natlinkstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class NatlinkStatus(metaclass=singleton.Singleton):
"""
known_directory_options = ['userdirectory', 'dragonflyuserdirectory',
'unimacrodirectory', 'unimacrogrammarsdirectory',
'unimacrodirectory',
'vocoladirectory', 'vocolagrammarsdirectory']

def __init__(self):
Expand All @@ -152,7 +152,7 @@ def __init__(self):
## Unimacro:
self.UnimacroDirectory = None
self.UnimacroUserDirectory = None
self.UnimacroGrammarsDirectory = None
# self.UnimacroGrammarsDirectory = None
self.UnimacroDataDirectory = None
## Vocola:
self.VocolaUserDirectory = None
Expand Down Expand Up @@ -426,34 +426,34 @@ def getUnimacroDataDirectory(self):

return um_data_dir

def getUnimacroGrammarsDirectory(self):
"""return the path to the directory where (part of) the ActiveGrammars of Unimacro are located.
By default in the UnimacroGrammars subdirectory of site-packages/unimacro, but look in natlink.ini file...
"""
isdir, abspath = os.path.isdir, os.path.abspath
if self.UnimacroGrammarsDirectory is not None:
return self.UnimacroGrammarsDirectory
key = 'unimacrogrammarsdirectory'
value = self.natlinkmain.getconfigsetting(section="directories", option=key)
if not value:
self.UnimacroGrammarsDirectory = ''
return ''
if isdir(value):
self.UnimacroGrammarDirectory = value
return abspath(value)

expanded = config.expand_path(value)
if expanded and isdir(expanded):
self.UnimacroGrammarDirectory = abspath(expanded)
return self.UnimacroGrammarDirectory

# check_natlinkini =
self.UnimacroGrammarsDirectory = ''

return ''

# def getUnimacroGrammarsDirectory(self):
# """return the path to the directory where (part of) the ActiveGrammars of Unimacro are located.
#
# By default in the UnimacroGrammars subdirectory of site-packages/unimacro, but look in natlink.ini file...
#
# """
# isdir, abspath = os.path.isdir, os.path.abspath
# if self.UnimacroGrammarsDirectory is not None:
# return self.UnimacroGrammarsDirectory
# key = 'unimacrogrammarsdirectory'
# value = self.natlinkmain.getconfigsetting(section="directories", option=key)
# if not value:
# self.UnimacroGrammarsDirectory = ''
# return ''
# if isdir(value):
# self.UnimacroGrammarDirectory = value
# return abspath(value)
#
# expanded = config.expand_path(value)
# if expanded and isdir(expanded):
# self.UnimacroGrammarDirectory = abspath(expanded)
# return self.UnimacroGrammarDirectory
#
# # check_natlinkini =
# self.UnimacroGrammarsDirectory = ''
#
# return ''
#
def getNatlinkDirectory(self):
"""return the path of the NatlinkDirectory, where the _natlink_core.pyd package (C++ code) is
"""
Expand Down Expand Up @@ -807,7 +807,7 @@ def getNatlinkStatusString(self):
## Unimacro:
if D['unimacroIsEnabled']:
self.appendAndRemove(L, D, 'unimacroIsEnabled', "---Unimacro is enabled")
for key in ('UnimacroUserDirectory', 'UnimacroDirectory', 'UnimacroGrammarsDirectory', 'UnimacroDataDirectory'):
for key in ('UnimacroUserDirectory', 'UnimacroDirectory', 'UnimacroDataDirectory'):
self.appendAndRemove(L, D, key)
else:
self.appendAndRemove(L, D, 'unimacroIsEnabled', "---Unimacro is disabled")
Expand Down
91 changes: 64 additions & 27 deletions src/natlinkcore/nsformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
removed pre 11 things,
now for python3 version, with (normally) DNSVersion 15 (QH, June 2020)/Febr 2022
"""
#pylint:disable=C0116, C0123, R0911, R0912, R0915, R0916
#pylint:disable=C0116, C0123, R0911, R0912, R0915, R0916, R1735, R1728
import copy
import natlink

Expand Down Expand Up @@ -51,37 +51,37 @@
if name.startswith('flag_') and isinstance(globals()[name], int) and 0 < globals()[name] < 32:
flagNames[globals()[name]] = name
#
flags_like_period = (9, 4, 21, 17) # flag_two_spaces_next = 9, flag_passive_cap_next = 4, flag_no_space_before = 21
flags_like_comma = (21, ) # flag_no_space_before = 21 (flag_nodelete = 3 we just ignore here, so leave out)
flags_like_number = (10,)
flags_like_point = (8, 10, 21) # no spacing (combination with numbers seems
flags_like_period = {9, 4, 21, 17} # flag_two_spaces_next = 9, flag_passive_cap_next = 4, flag_no_space_before = 21
flags_like_comma = {21} # flag_no_space_before = 21 (flag_nodelete = 3 we just ignore here, so leave out)
flags_like_number = {10}
flags_like_point = {8, 10, 21} # no spacing (combination with numbers seems
# obsolete (cond_no_space = 10)
flags_like_hyphen = (8, 21) # no spacing before and after
flags_like_open_quote = (8, 20) # no space next and no cap change
flags_like_close_quote = (21, 20, 19) # no space before, no cap change and no space change (??)
flags_like_hyphen = {8, 21} # no spacing before and after
flags_like_open_quote = {8, 20} # no space next and no cap change
flags_like_close_quote = {21, 20, 19} # no space before, no cap change and no space change (??)

# word flags from properties part of the word:
# Dragon 11...
propDict = {}
propDict['space-bar'] = (flag_space_bar, flag_no_space_next, flag_no_formatting,
flag_no_cap_change, flag_no_space_before) # (8, 18, 20, 21, 27)
propDict['space-bar'] = {flag_space_bar, flag_no_space_next, flag_no_formatting,
flag_no_cap_change, flag_no_space_before} # {8, 18, 20, 21, 27}

propDict['period'] = flags_like_period
propDict['point'] = flags_like_point
propDict['dot'] = flags_like_point
propDict['comma'] = flags_like_comma
propDict['cap'] = (19, 18, flag_active_cap_next)
propDict['caps-on'] = (19, 18, flag_cap_all)
propDict['caps-off'] = (19, 18, flag_reset_uc_lc_caps)
propDict['all-caps'] = (19, 18, flag_uppercase_next)
propDict['all-caps-on'] = (19, 18, flag_uppercase_all)
propDict['all-caps-off'] = (19, 18, flag_reset_uc_lc_caps)
propDict['no-caps'] = (19, 18, flag_lowercase_next)
propDict['no-caps-on'] = (19, 18, flag_lowercase_all)
propDict['no-caps-off'] = (19, 18, flag_reset_uc_lc_caps)
propDict['no-space'] = (18, 20, flag_no_space_next)
propDict['no-space-on'] = (18, 20, flag_no_space_all)
propDict['no-space-off'] = (18, 20, flag_reset_no_space)
propDict['caps-on'] = {19, 18, flag_cap_all}
propDict['caps-off'] = {19, 18, flag_reset_uc_lc_caps}
propDict['all-caps'] = {19, 18, flag_uppercase_next}
propDict['all-caps-on'] = {19, 18, flag_uppercase_all}
propDict['all-caps-off'] = {19, 18, flag_reset_uc_lc_caps}
propDict['no-caps'] = {19, 18, flag_lowercase_next}
propDict['no-caps-on'] = {19, 18, flag_lowercase_all}
propDict['no-caps-off'] = {19, 18, flag_reset_uc_lc_caps}
propDict['no-space'] = {18, 20, flag_no_space_next}
propDict['no-space-on'] = {18, 20, flag_no_space_all}
propDict['no-space-off'] = {18, 20, flag_reset_no_space}
propDict['left-double-quote'] = flags_like_open_quote
propDict['right-double-quote'] = flags_like_close_quote
# left- as left-double-quote
Expand Down Expand Up @@ -112,15 +112,17 @@
#
# If you already have the wordInfo for each word, you can pass in a list of
# tuples of (wordName,wordInfo) instead of just the list of words.

def formatWords(wordList,state=None):
def formatWords(wordList, state=None):
"""return the formatted words and the state at end.
when passing this state in the next call, the spacing and capitalization
will be maintained.
"""
#pylint:disable=W0603
global flags_like_period
assert isinstance(wordList, list)

language = 'enx'
if language != 'enx':
flags_like_period = (4, 21, 17) # one space after period.
Expand Down Expand Up @@ -164,6 +166,41 @@ def formatWords(wordList,state=None):

return output, state


def formatString(text, state=None):
r"""pass in a string, and optionally the result of previous call
Do NOT require the .\period\period specifications, so you can call this one from a plain sentence.
For frequent punctuation, like "." these are replaced by the counterparts.
"""
like_period = ".?!"
like_comma = ",;:"
like_hyphen = "-"

all_special = like_period + like_comma + like_hyphen
for char in all_special:
text = text.replace(char, f' {char} ')

input_list = text.split()
for i, w in enumerate(input_list):
if w not in all_special:
continue
if w in like_period:
input_list[i] = (w, set(flags_like_period))
if w in like_comma:
input_list[i] = (w, set(flags_like_comma))
if w in like_hyphen:
input_list[i] = (w, set(flags_like_hyphen))



result, state = formatWords(input_list, state=state)
return result, state



countDict= dict(one=1, two=2, three=3, four=4, five=5, six=6, seven=7, eight=8, nine=9,
een=1, twee=2, drie=3, vier=4, vijf=5, zes=6, zeven=7, acht=8, negen=9)

Expand Down Expand Up @@ -236,7 +273,7 @@ def formatWord(wordName,wordInfo=None,stateFlags=None, gwi=None):
elif isinstance(state, (list, tuple)):
state = set(state)
else:
raise ValueError("formatWord, invalid stateFlags: %s"% repr(stateFlags))
raise ValueError(f'formatWord, invalid stateFlags: {repr(stateFlags)}')
stateFlags = copy.copy(state)


Expand Down Expand Up @@ -392,7 +429,7 @@ def getWordInfo(word):
return set(propDict['left-double-quote'])
if prop.startswith('right-'):
return set(propDict['right-double-quote'])
print('getWordInfo11, unknown word property: "%s" ("%s")'% (prop, word))
print(f'getWordInfo, unknown word property: {prop} {"word"}')
return set() # empty tuple
# should not come here
return set()
Expand Down Expand Up @@ -445,8 +482,8 @@ def testSubroutine(state, Input, Output):
words[i] = words[i].replace('_', ' ')
actual,state = formatWords(words,state)
if actual != Output:
print('Expected "%s"'%Output)
print('Actually "%s"'%actual)
print(f'Expected {"Output"}')
print(f'Actually {"actual"}')
raise ValueError("test error")
return state

Expand Down
Loading

0 comments on commit 1357189

Please sign in to comment.