diff --git a/src/unimacro/UnimacroGrammars/_latex.py b/src/unimacro/UnimacroGrammars/_latex.py index 72e53ef..4748aa1 100644 --- a/src/unimacro/UnimacroGrammars/_latex.py +++ b/src/unimacro/UnimacroGrammars/_latex.py @@ -11,17 +11,17 @@ # written by: Frank Olaf Sem-Jacobsen # March 2011 # - +#pylint:disable = R0912, C0209, E1101 import natlink from natlinkcore import nsformat +from natlinkcore import natlinkstatus from dtactions.unimacro import unimacroutils -from natlinkcore import natlinkutils as natut -from dtactions.unimacro import unimacroutils -import unimacro.natlinkutilsbj as natbj -from dtactions.unimacro.unimacroactions import doAction as action from dtactions.unimacro.unimacroactions import doAction as action +from dtactions.sendkeys import sendkeys as keystroke +import unimacro.natlinkutilsbj as natbj -language = unimacroutils.getLanguage() +status = natlinkstatus.NatlinkStatus() +language = status.language ICAlphabet = natbj.getICAlphabet(language=language) # import re @@ -29,8 +29,6 @@ ancestor = natbj.IniGrammar class ThisGrammar(ancestor): - language = unimacroutils.getLanguage() - name = "latex" gramSpec = """ @@ -45,7 +43,7 @@ class ThisGrammar(ancestor): """ def initialize(self): - if not self.language: + if not language: print("no valid language in grammar "+__name__+" grammar not initialized") return @@ -133,7 +131,7 @@ def gotResults_options(self, words, fullResults): keystroke('{end}') else: keystroke('{home}') - for i in range(0, squared): + for _ in range(0, squared): keystroke('{right}') if present == 0: keystroke('[') @@ -180,13 +178,13 @@ def gotResults(self, words, fullResults): keystroke('{up}') print('floating: %s'% self.floating) if self.reference: - stringpaste ('\\ref{%s}' % (self.makes_label(self.reference, self.dictation))) + stringpaste('\\ref{%s}' % (self.makes_label(self.reference, self.dictation))) if self.namereference: - stringpaste ('\\nameref{%s}' % (self.makes_label(self.namereference, self.dictation))) + stringpaste('\\nameref{%s}' % (self.makes_label(self.namereference, self.dictation))) if self.label_text: - stringpaste ('\\label{%s}' % (self.makes_label(self.label_text, self.dictation))) + stringpaste('\\label{%s}' % (self.makes_label(self.label_text, self.dictation))) if self.replace_text: - stringpaste (self.dictation) + stringpaste(self.dictation) def gotResults_dgndictation(self, words, fullResults): @@ -231,26 +229,31 @@ def view_selection_current_line(self): - def makes_label(self, type, text): - return type + ':' + ''.join(text.split()).lower() + def makes_label(self, Type, Text): + return Type + ':' + ''.join(Text.split()).lower() def stringpaste(t): """paste via clipboard, to circumvent German keyboard issues """ action('SCLIP "%s"'%t) - - -# standard stuff Joel (QH, Unimacro) -thisGrammar = ThisGrammar() -if thisGrammar.gramSpec: - thisGrammar.initialize() -else: +# standard stuff Joel (QH, Unimacro, python3): +try: + thisGrammar +except NameError: thisGrammar = None def unload(): #pylint:disable=W0603 global thisGrammar - if thisGrammar: thisGrammar.unload() + if thisGrammar: + thisGrammar.unload() thisGrammar = None +if __name__ == "__main__": + # here code to interactive run this module + pass +elif __name__.find('.') == -1: + # called from the loader, when starting Dragon/Natlink: + thisGrammar = ThisGrammar() + thisGrammar.initialize() diff --git a/src/unimacro/UnimacroGrammars/_tasks.py b/src/unimacro/UnimacroGrammars/_tasks.py index 2906d0b..463d549 100644 --- a/src/unimacro/UnimacroGrammars/_tasks.py +++ b/src/unimacro/UnimacroGrammars/_tasks.py @@ -1125,13 +1125,6 @@ def cancelMode(self): self.winkeyDown = 0 -# standard stuff Joel (adapted for possible empty gramSpec, QH, unimacro) -# print(f'_task grammar, __name__: {__name__}') -thisGrammar = ThisGrammar() -if thisGrammar.gramSpec: - thisGrammar.initialize() -else: - thisGrammar = None def unload(): #pylint:disable = W0603 @@ -1147,3 +1140,14 @@ def changeCallback(Type,Args): if thisGrammar: thisGrammar.cancelMode() +# standard stuff Joel +print(f'_task grammar, __name__: {__name__}') +# -- when testing, the __name__ parameter is qualified, like unimacro.UnimacroGrammars._tasks +# -- when called from Dragon/Natlink/loader, the name is "_tasks" +if __name__ == "__main__": + # here code to interactive run this module + pass +elif __name__.find('.') == -1: + # this is caught when this module is imported by the loader (when Dragon/Natlink starts) + thisGrammar = ThisGrammar() + thisGrammar.initialize() diff --git a/src/unimacro/_control.py b/src/unimacro/_control.py index a5f8541..c7f7244 100644 --- a/src/unimacro/_control.py +++ b/src/unimacro/_control.py @@ -508,43 +508,42 @@ def gotResults_show(self,words,fullResults): Start=() # fix state at this moment (in case of Active grammars popup) self.BrowsePrepare(Start, All, Exclusive) - if Active: + if All or Active: #print 'collect and show active, non-active and non-Unimacro grammars' activeGrammars, inactiveGrammars, switchedOffGrammars = [], [], [] - otherGrammars = natlinkmain.loadedFiles.keys() + otherGrammars = [] # natlinkmain.loadedFiles.keys() - print(f'loadedGrammars (natbj): {natbj.loadedGrammars}') - print(f'all active grammars (natlinkmain): {natlinkmain.loadedFiles}') + print(f'loadedGrammars (Unimacro): {natbj.loadedGrammars}') + # print(f'all active grammars (natlinkmain): {natlinkmain.loadedFiles}') allGramNames = self.getUnimacroGrammarNames() self.setList('gramnames', allGramNames) print(f'for being sure, set all active grammars in list "gramnames": "{allGramNames}"') - - for g in natbj.loadedGrammars.items(): - gram = natbj.loadedGrammars[g] + + for grammar_name, gram in natbj.loadedGrammars.items(): + # gram = natbj.loadedGrammars[g] result = getattr(gram, 'isActive') - modName = gram.__module__ - #print 'gram: %s, modName: %s, result: %s'% (gram, modName, result) + mod_name = gram.__module__ + print(f'gram: {grammar_name}, module: {mod_name}') if result: - activeGrammars.append(g) - if modName in otherGrammars: - otherGrammars.remove(modName) + activeGrammars.append(grammar_name) + if mod_name in otherGrammars: + otherGrammars.remove(mod_name) else: - print('cannot remove from otherGrammars: %s'% modName) + print(f'cannot remove from otherGrammars: {mod_name}') elif result == 0: maySwitchOn = gram.mayBeSwitchedOn if maySwitchOn: - inactiveGrammars.append(g) + inactiveGrammars.append(grammar_name) else: - switchedOffGrammars.append(g) - #print 'gram: %s, name: %s'% (gram, modName) - if modName in otherGrammars: - otherGrammars.remove(modName) + switchedOffGrammars.append(grammar_name) + if mod_name in otherGrammars: + otherGrammars.remove(mod_name) else: - print('cannot remove from otherGrammars: %s'% modName) + print(f'cannot remove from otherGrammars: {mod_name}') if not activeGrammars: msg = 'No Unimacro grammars are active' elif activeGrammars == [self.name]: - msg = 'No grammars are active (apart from "%s")'% self.name + msg = f'No grammars are active (apart from "{self.name}")' elif inactiveGrammars or switchedOffGrammars: msg = 'Active Unimacro grammars:\n' + ', '.join(activeGrammars) else: @@ -601,7 +600,7 @@ def gotResults_edit(self,words,fullResults): return print(f'open for edit file: "{filepath}"') self.openFileDefault(filepath, mode="edit", name=f'edit grammar {gramName}') - unimacroutils.setCheckForGrammarChanges(1) + # unimacroutils.setCheckForGrammarChanges(1) else: # edit the inifile try: @@ -755,8 +754,7 @@ def checkOriginalFileWithActualTxtPy(name, org_path, txt_path, py_path): return # new - -# standard stuff Joel (adapted for possible empty gramSpec, QH, unimacro) +# standard stuff Joel (adapted for python3, QH, unimacro): if __name__ == "__main__": ## interactive use, for debugging: natlink.natConnect() @@ -770,10 +768,10 @@ def checkOriginalFileWithActualTxtPy(name, org_path, txt_path, py_path): utilGrammar.gotResults_show(Words, FR) finally: natlink.natDisconnect() -else: +elif __name__.find('.') == -1: # standard startup when Dragon starts: - print('control, standard startup') utilGrammar = UtilGrammar() utilGrammar.initialize() + # set special function as a callback... natlinkmain.set_post_load_callback(utilGrammar.UnimacroControlPostLoad) utilGrammar.checkUnimacroGrammars() diff --git a/src/unimacro/sample_global_dictation/_control.py b/src/unimacro/sample_global_dictation/_control.py index 322b952..8fb96a5 100644 --- a/src/unimacro/sample_global_dictation/_control.py +++ b/src/unimacro/sample_global_dictation/_control.py @@ -401,7 +401,7 @@ def gotResults_edit(self,words,fullResults): filename = unimacroutils.getModuleFilename(module) #print 'open for edit file: %s'% filename self.openFileDefault(filename, mode="edit", name='edit grammar %s'% gramName) - unimacroutils.setCheckForGrammarChanges(1) + # unimacroutils.setCheckForGrammarChanges(1) else: # edit the inifile try: diff --git a/tests/test_grammar_tasks.py b/tests/test_grammar_tasks.py index d7ce4ce..888090b 100644 --- a/tests/test_grammar_tasks.py +++ b/tests/test_grammar_tasks.py @@ -36,4 +36,4 @@ def test_gototask(monkeypatch): natlink.natDisconnect() if __name__ == "__main__": - pytest.main(['test_gototask']) + pytest.main()