From f743728c8ba662e50247a10394d139a6b2e81b07 Mon Sep 17 00:00:00 2001 From: Quintijn Hoogenboom Date: Mon, 17 Oct 2022 18:09:40 +0200 Subject: [PATCH] struggling with BrowsableGrammar details forward _tasks.py starting test_grammar_tasks.py (with little luck) --- src/unimacro/BrowseGrammarApp.py | 8 +++--- src/unimacro/UnimacroGrammars/__init__.py | 0 src/unimacro/UnimacroGrammars/_tasks.py | 31 ++++++-------------- src/unimacro/natlinkutilsbj.py | 2 +- tests/test_grammar_tasks.py | 35 +++++++++++++++++++++++ 5 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 src/unimacro/UnimacroGrammars/__init__.py create mode 100644 tests/test_grammar_tasks.py diff --git a/src/unimacro/BrowseGrammarApp.py b/src/unimacro/BrowseGrammarApp.py index 757629b..093a9c0 100644 --- a/src/unimacro/BrowseGrammarApp.py +++ b/src/unimacro/BrowseGrammarApp.py @@ -222,7 +222,7 @@ def on_size (self, params): self.LayoutControls(w, h) def LayoutControls(self, w, h): - d=w/4 + d=int(w/4) self.Tree.MoveWindow((0,0,d,h)) self.Syntax.MoveWindow((d,0,w,h)) @@ -314,7 +314,7 @@ def FillList(self): numCols = len(self.colHeadings) index = 0 for col in self.colHeadings: - ## change second variable to int, QH, 23092922:: + ## change second variable to int, QH, 23092022:: itemDetails = (LVCFMT_LEFT, int(width*colw[index]), col, 0) print(f'index: {index}, itemDetails: {itemDetails}') self.Syntax.InsertColumn(index, itemDetails) @@ -579,8 +579,8 @@ def CreateBrowseDialog(): if Exclusive: Name='Exclusive Grammars (Active Rules)' elif All: Name='All Grammars' else: Name='Active Grammars' - # dlg=GrammarDialog(Name,GramHierList(Grammars,Start)) - dlg=TrainGrammarDialog(Name,GramHierList(Grammars,Start)) + dlg=GrammarDialog(Name,GramHierList(Grammars,Start)) + # dlg=TrainGrammarDialog(Name,GramHierList(Grammars,Start)) return dlg diff --git a/src/unimacro/UnimacroGrammars/__init__.py b/src/unimacro/UnimacroGrammars/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/unimacro/UnimacroGrammars/_tasks.py b/src/unimacro/UnimacroGrammars/_tasks.py index 7b4db75..2906d0b 100644 --- a/src/unimacro/UnimacroGrammars/_tasks.py +++ b/src/unimacro/UnimacroGrammars/_tasks.py @@ -63,14 +63,14 @@ class ThisGrammar(ancestor): # include only if searchinothertask is True (via .ini file) (enable search commands = T) searchRule = " exported = search ({taskcount}|{application}) | search ({taskcount}|{application});" - def _prog_infonit__(self): + def __init__(self): """start the inifile and add to grammar if needed the searchRule """ + ancestor.__init__(self) self.startInifile() #print 'enableSearchCommands: %s'% self.enableSearchCommands if self.enableSearchCommands: - self.gramSpec.append(self.searchRule) - ancestor.__init__(self) + self.gramSpec += '\n' + self.searchRule _name = self.getName() @@ -1126,7 +1126,12 @@ def cancelMode(self): # 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 @@ -1142,21 +1147,3 @@ def changeCallback(Type,Args): if thisGrammar: thisGrammar.cancelMode() -if __name__ == "__main__": - natlink.natConnect() - try: - thisGrammar = ThisGrammar() - thisGrammar.startInifile() #modName = '_tasks') - thisGrammar.initialize() - Words = ['task', 'three'] - # print(f'natbj.loadedGrammars: {natbj.loadedGrammars}') - thisGrammar.rule_taskswitch(Words) - # natlink.recognitionMimic(["task", "three"]) - finally: - natlink.natDisconnect() -else: - thisGrammar = ThisGrammar() - if thisGrammar.gramSpec: - thisGrammar.initialize() - else: - thisGrammar = None diff --git a/src/unimacro/natlinkutilsbj.py b/src/unimacro/natlinkutilsbj.py index 3fd26a1..4cf4474 100644 --- a/src/unimacro/natlinkutilsbj.py +++ b/src/unimacro/natlinkutilsbj.py @@ -2239,7 +2239,7 @@ def startInifile(self, modName=None): self.checkForChanges = 1 self.openedInifile = 0 self.ignore = None - modName = modName or self.__module__ + modName = modName or self.__module__.rsplit('.', maxsplit=1)[-1] # baseDir = status.getUnimacroDirectory() userDir = status.getUnimacroUserDirectory() diff --git a/tests/test_grammar_tasks.py b/tests/test_grammar_tasks.py new file mode 100644 index 0000000..12fa54c --- /dev/null +++ b/tests/test_grammar_tasks.py @@ -0,0 +1,35 @@ +#pylint:disable=E1101 +from pathlib import Path +import pytest +import natlink +from unimacro.UnimacroGrammars._tasks import ThisGrammar +from natlinkcore import natlinkstatus +status = natlinkstatus.NatlinkStatus + +thisDir = Path(__file__).parent + +def mock_unimacro_user_dir(_self): + mock_folder= thisDir / "mock_unimacro_userdir" + if not mock_folder.is_dir(): + mock_folder.mkdir() + return str(mock_folder) + +def test_gototask(monkeypatch): + """see if (with debugging) coming into a grammar works + """ + monkeypatch.setattr(status, "getUnimacroUserDirectory", mock_unimacro_user_dir) + + natlink.natConnect() + try: + thisGrammar = ThisGrammar() + thisGrammar.startInifile() #modName = '_tasks') + thisGrammar.initialize() + Words = ['task', 'three'] + # print(f'natbj.loadedGrammars: {natbj.loadedGrammars}') + thisGrammar.rule_taskswitch(Words) + # natlink.recognitionMimic(["task", "three"]) + finally: + natlink.natDisconnect() + +if __name__ == "__main__": + pytest.main(['test_gototask'])