Skip to content

Commit

Permalink
Third start oct (#46)
Browse files Browse the repository at this point in the history
* fixing switching on and off unimacro grammars with control grammar
* problem with spokenforms accented characters partly solved. In Dutch probably a Dragon error.
* updated documention on building and publishing.
* fixing an encoding and working on tests
Co-authored-by: Doug Ransom <doug.ransom@gmail.com>
  • Loading branch information
quintijn authored Jan 30, 2024
1 parent 98f7fae commit c98b32c
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 79 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ More about [Unimacro Grammars](https://qh.antenna.nl/unimacro/grammars/globalgra
If you want to install your local unimacro development environment as the working unimacro:
`pip install -e . `.

`flit build` to build the wheen and sdist with [flit](https://flit.pypa.io/).
`flit publish` to pubish them to the [Python Package Index](https://pypi.org/).
`py -m build` to build the Python package locally.

Publishing to PyPi is done through the [trusted publisher mechanism](https://docs.pypi.org/trusted-publishers/using-a-publisher/) when a release is created on github using github actions.


BEFORE YOU PUBLISH review the version number of dependencies in pyproject.toml, see if you require newer ones.


Expand Down
24 changes: 8 additions & 16 deletions src/unimacro/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,16 @@ def switch(self, gram, gname, switchOn):
gram.ini.write()
unimacroutils.Wait(0.1)

gram.unload()
# gram.unload()
gram.initialize()
return 1

# switch off:
gram.ini.set('general', 'initial on', 0)
gram.ini.writeIfChanged()
gram.cancelMode()
gram.unload()
gram.deactivateAll()
# gram.unload()
print('grammar "%s" switched off'% gram.getName())
return 1

Expand Down Expand Up @@ -430,8 +431,7 @@ def gotResults_show(self,words,fullResults):
self.BrowsePrepare(Start, All, Exclusive)
if All or Active:
#print 'collect and show active, non-active and non-Unimacro grammars'
Gmodulenames = self.getUnimacroGrammars()
G = {grammarobj.name: grammarobj for _objname, grammarobj in Gmodulenames.items()}
G = self.getUnimacroGrammars()
# print(f'allGrammars (Unimacro): {G}')
allGramNames = G.keys()
self.setList('gramnames', allGramNames)
Expand Down Expand Up @@ -570,7 +570,7 @@ def UnimacroControlPostLoad(self):
prevSet = set(self.Lists['gramnames'])
newSet = set(self.getRegisteredGrammarNames())
if prevSet != newSet:
# print(f'setting new grammar names list: {list(newSet)}')
# print(f'UnimacroControlPostLoad, setting new grammar names list: {list(newSet)}')
self.setList('gramnames', list(newSet))

def getUnimacroGrammarNamesPaths(self):
Expand All @@ -589,9 +589,10 @@ def getUnimacroGrammarNamesPaths(self):
natlink_modules = natlinkmain.get_loaded_modules()
natlink_modules_files = [str(f) for f in natlink_modules.keys()]
for name, gramobj in registered.items():
print(f'grammar name: {name}, gramobj: {gramobj}')
# print(f'grammar name: {name}, gramobj: {gramobj}, module_name: {gramobj.module_name}')
mod_name = gramobj.module_name
for try_file in natlink_modules_files:
if try_file.find(name) > 0:
if try_file.find(mod_name) > 0:
unimacro_modules[name] = try_file
break
else:
Expand All @@ -600,15 +601,6 @@ def getUnimacroGrammarNamesPaths(self):

return unimacro_modules

#
# def checkUnimacroGrammars(self):
# """see if there are any changed grammar files with respect to original file in release
#
# sync with ...
# """
# # print('checkUnimacroGrammars!!')
# check_unimacro_grammars.checkUnimacroGrammars()

# class MessageDictGrammar(natlinkutils.DictGramBase):
# def __init__(self):
# natlinkutils.DictGramBase.__init__(self)
Expand Down
37 changes: 18 additions & 19 deletions src/unimacro/natlinkutilsbj.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ class GrammarX(GrammarXAncestor):
#pylint:disable=R0904, C0116
__inherited=GrammarXAncestor
allGrammarXObjects = {}
GrammarsChanged = [] # take last item just True!
LoadedControlGrammars = []
GrammarsChanged = set() # take last item just True!
LoadedControlGrammars = set()


def __init__(self):
Expand All @@ -311,20 +311,20 @@ def getExclusiveGrammars(self):
return G

def getUnimacroGrammars(self):
"""duplicate functions
"""get from allGrammarXObjects the name: grammarobj dict of all Unimacro grammars
"""
return copy.copy(self.allGrammarXObjects)
return {grammarobj.getName(): grammarobj for _objname, grammarobj in self.allGrammarXObjects.items()}

def RegisterGrammarObject(self):
self.allGrammarXObjects[self.module_name] = self
self.GrammarsChanged.append(True)
self.allGrammarXObjects[self.getName()] = self
self.GrammarsChanged.add(True)

def UnregisterGrammarObject(self):
"""calling at unload time"""
if self.LoadedControlGrammars and self.LoadedControlGrammars[-1] is self:
if self.LoadedControlGrammars and self in self.LoadedControlGrammars:
self.LoadedControlGrammars.pop()
try:
del self.allGrammarXObjects[self.module_name]
del self.allGrammarXObjects[self.name]
except KeyError:
if self.name == self.module_name:
print(f'cannot unregister unimacro grammar {self.module_name}')
Expand All @@ -334,7 +334,7 @@ def UnregisterGrammarObject(self):
# self.GrammarsChanged.append(True)

def SetGrammarsChangedFlag(self):
self.GrammarsChanged.append(True)
self.GrammarsChanged.add(True)
print(f'GrammarsChanged: {self.GrammarsChanged}') ## seems not to work TODO QH:
def GetGrammarsChangedFlag(self):
if self.GrammarsChanged:
Expand Down Expand Up @@ -384,16 +384,13 @@ def ControlResetExclusiveGrammar(self):
if len(exclGr) > 1:
return
for gram in exclGr.values():
if gram is self.LoadedControlGrammars[-1]:
if gram in self.LoadedControlGrammars:
gram.setExclusive(0)

def RegisterControlObject(self, gramobj):
"""keep this (control grammar) in a special variable
"""
if not self.LoadedControlGrammars:
self.LoadedControlGrammars.append(gramobj)
elif not self.LoadedControlGrammars[0].name is gramobj.name:
print(f'RegisterControlObject, WARNING, wanting to set to {gramobj.name}, but already set to {self.LoadedControlGrammars[0].name}')
self.LoadedControlGrammars.add(gramobj)

def UnregisterControlObject(self):
"""clear this (control grammar) in a special variable
Expand All @@ -411,9 +408,8 @@ def ControlSetExclusiveGrammar(self, value):
return
if not self.LoadedControlGrammars:
return
if self.LoadedControlGrammars[0] is self:
return
self.LoadedControlGrammars[0].setExclusive(value)
for control_grammar in self.LoadedControlGrammars:
control_grammar.setExclusive(value)


def getRegisteredGrammarNames(self):
Expand Down Expand Up @@ -441,7 +437,8 @@ def __str__(self):

def unload(self):
self.UnregisterGrammarObject()
self.UnregisterControlObject()
if self in self.LoadedControlGrammars:
self.UnregisterControlObject()
self.__inherited.unload(self)

def beginCallback(self, moduleInfo):
Expand Down Expand Up @@ -539,8 +536,10 @@ def setExclusive(self,exclusive):
self.__inherited.setExclusive(self,exclusive)
self.exclusive = exclusive


## check this, QH TODO
if self.LoadedControlGrammars:
if self is self.LoadedControlGrammars[0]:
if self in self.LoadedControlGrammars:
# no extra setting if self is _control
return
self.ControlSetExclusiveGrammar(exclusive)
Expand Down
129 changes: 98 additions & 31 deletions tests/test_grammar_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
from pathlib import Path
import pytest
from unimacro._control import UtilGrammar
from unimacro.UnimacroGrammars._brackets import BracketsGrammar
from unimacro import natlinkutilsbj as natbj

thisDir = Path(__file__).parent

def do_nothing(*args, **kwargs):
return None

# test with _control.py, _brackets.py (a very simple grammar), and two grammars that are defined in this file, gramon and gramoff


class GramOn(natbj.IniGrammar):
"""simple grammar which is initially on
Expand Down Expand Up @@ -55,11 +57,10 @@ def test_getAllGrammars(unimacro_setup):
# utilGrammar.gotResults_show(words=['show', 'all', 'grammars'], fullResults={})
assert utilGrammar.isLoaded() is True
assert utilGrammar.isActive() is True
assert utilGrammar.LoadedControlGrammars[0] is utilGrammar
assert gramon.LoadedControlGrammars[-1] is utilGrammar
assert utilGrammar in utilGrammar.LoadedControlGrammars

al = utilGrammar.getUnimacroGrammars()
assert len(al) == 3
assert len(al) == 4

loaded = {g for g, gram in al.items() if gram.isLoaded()}
assert loaded == set(['control', 'grammaron'])
Expand Down Expand Up @@ -97,8 +98,82 @@ def test_getAllGrammars(unimacro_setup):
active = {g for g, gram in al.items() if gram.isActive()}
assert active == set(['control', 'grammaron'])

def test_getAllGrammarsSwitchingOnAlternative(unimacro_setup):
"""see if we can get all the grammars, as control switches on in a different way, should also work...
"""
gramon = GramOn(inifile_stem="_gramon")
gramon.initialize()
gramoff = GramOff(inifile_stem="_gramoff")
gramoff.initialize()
assert gramon.isLoaded() is True
assert gramon.isActive() is True
assert gramoff.isLoaded() is False
assert gramoff.isActive() is False
utilGrammar = UtilGrammar()
# monkeypatch.setattr(utilGrammar, 'switchOnOrOff', do_nothing)
utilGrammar.startInifile()
utilGrammar.initialize()
# utilGrammar.gotResults_show(words=['show', 'all', 'grammars'], fullResults={})
assert utilGrammar.isLoaded() is True
assert utilGrammar.isActive() is True
assert utilGrammar in utilGrammar.LoadedControlGrammars

al = utilGrammar.getUnimacroGrammars()
assert len(al) == 4

loaded = {g for g, gram in al.items() if gram.isLoaded()}
assert loaded == set(['control', 'grammaron'])
active = {g for g, gram in al.items() if gram.isActive()}
assert active == loaded

gramoff.switchOn()
loaded = {g for g, gram in al.items() if gram.isLoaded()}
assert loaded == set(['control', 'grammaron', 'grammaroff'])
active = {g for g, gram in al.items() if gram.isActive()}
assert active == loaded

gramoff.switchOff()
loaded = {g for g, gram in al.items() if gram.isLoaded()}
assert loaded == set(['control', 'grammaron']) # switchOff also unloads the grammar!
active = {g for g, gram in al.items() if gram.isActive()}
assert active == set(['control', 'grammaron'])

# should ignore this command:
utilGrammar.switchOff()
loaded = {g for g, gram in al.items() if gram.isLoaded()}
assert loaded == set(['control', 'grammaron'])
active = {g for g, gram in al.items() if gram.isActive()}
assert active == set(['control', 'grammaron'])

gramon.switchOn()
loaded = {g for g, gram in al.items() if gram.isLoaded()}
assert loaded == set(['control', 'grammaron'])
active = {g for g, gram in al.items() if gram.isActive()}
assert active == set(['control', 'grammaron'])

gramon.switchOn()
loaded = {g for g, gram in al.items() if gram.isLoaded()}
assert loaded == set(['control', 'grammaron'])
active = {g for g, gram in al.items() if gram.isActive()}
assert active == set(['control', 'grammaron'])


def test_switching_on_off_brackets(unimacro_setup):
"""try if brackets grammar switches on and off
"""
gram = BracketsGrammar()
gram.initialize()
assert gram.isLoaded() is True
assert gram.isActive() is True
gram.switchOff()
assert gram.isLoaded() is False
assert gram.isActive() is False
gram.switchOn()
assert gram.isLoaded() is True
assert gram.isActive() is True



def test_ExclusiveMode(unimacro_setup):
"""see if grammars can switch on and off exclusive mode, with _control following
Expand Down Expand Up @@ -133,8 +208,7 @@ def test_ExclusiveMode(unimacro_setup):
# utilGrammar.gotResults_show(words=['show', 'all', 'grammars'], fullResults={})
assert utilGrammar.isLoaded() is True
assert utilGrammar.isActive() is True
assert utilGrammar.LoadedControlGrammars[0] is utilGrammar
assert gramon.LoadedControlGrammars[-1] is utilGrammar
assert utilGrammar in utilGrammar.LoadedControlGrammars

exclGr = gramon.getExclusiveGrammars()
assert len(exclGr) == 0
Expand All @@ -148,26 +222,14 @@ def test_ExclusiveMode(unimacro_setup):
exclGr = gramon.getExclusiveGrammars()
assert len(exclGr) == 0





def test_show_all_grammars(unimacro_setup):
gramon = GramOn(inifile_stem="_gramon")
gramon.initialize()
gramoff = GramOff(inifile_stem="_gramoff")
gramoff.initialize()
assert gramon.isLoaded() is True
assert gramon.isActive() is True
assert gramoff.isLoaded() is False
assert gramoff.isActive() is False
utilGrammar = UtilGrammar()
# monkeypatch.setattr(utilGrammar, 'switchOnOrOff', do_nothing)
utilGrammar.startInifile()
utilGrammar.initialize()

def test_get_unimacro_grammars(unimacro_setup):
"""get modules from the active grammars
This test is only very partial, because here some test grammars are involved.
In the normal case, each unimacro grammar is in its own file, module,
which are registered at startup time (in allGrammarXObjects)
"""
gramon = GramOn(inifile_stem="_gramon")
gramon.initialize()
Expand All @@ -181,7 +243,10 @@ def test_get_unimacro_grammars(unimacro_setup):
# monkeypatch.setattr(utilGrammar, 'switchOnOrOff', do_nothing)
utilGrammar.startInifile()
utilGrammar.initialize()

unimacro_grammars = utilGrammar.getUnimacroGrammars()
# _brackets not loaded in this test
assert len(unimacro_grammars) == 3
# does not function here, as the grammars are not in separate files:
gramnames = utilGrammar.getUnimacroGrammarNamesPaths()
assert set(gramnames) == {'grammaron', 'grammaroff', 'control'}

Expand All @@ -195,14 +260,16 @@ def test_get_unimacro_grammars(unimacro_setup):
# Words = ['show', 'grammaroff']
# FR = {}
# utilGrammar.gotResults_show(Words, FR)
#
Words = ['switch', 'on', 'grammaroff']
FR = {}
utilGrammar.gotResults_switch(Words, FR)
assert gramoff.isLoaded() is True
assert gramoff.isActive() is True

# newSet = set(self.getRegisteredGrammarNames())

# this one does not work in this test, because at initialize time
# the grammar is hardcoded switched off....
# Words = ['switch', 'on', 'grammaroff']
# FR = {}
# utilGrammar.gotResults_switch(Words, FR)
# assert gramoff.isLoaded() is True
# assert gramoff.isActive() is True




if __name__ == "__main__":
Expand Down
Loading

0 comments on commit c98b32c

Please sign in to comment.