Skip to content

Commit

Permalink
working a bit on _control and the testing, (test_grammar_control.py)
Browse files Browse the repository at this point in the history
  • Loading branch information
quintijn committed Jan 13, 2023
1 parent 0b981fa commit 1224471
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 510 deletions.
10 changes: 0 additions & 10 deletions src/unimacro/UnimacroGrammars/_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,6 @@ def cancelMode(self):
def onTimer(self):
## if self.lastClock:
## diff = int((time.clock() - self.lastClock) * 1000 )
if natbj.IsDisplayingMessage:
debugPrint('in timer, displaying message, returning')
return
moduleInfo = natlink.getCurrentModule()
if natlink.getMicState() == 'on' and moduleInfo[2] == self.moduleInfo[2]:
if self.inside or self.insideCommand:
Expand Down Expand Up @@ -430,17 +427,10 @@ def doAction(self):
return 1 # good exit

def gotBegin(self,moduleInfo):
if natbj.IsDisplayingMessage:
debugPrint('displaying message, ignoring generic movement')
return
if self.inTimer:
self.inside = 1

def gotResultsObject(self,recogType,resObj):
if natbj.IsDisplayingMessage:
debugPrint('displaying message, ignoring generic movement')
return

if recogType == 'reject':
return
handle = natlink.getCurrentModule()[2]
Expand Down
161 changes: 50 additions & 111 deletions src/unimacro/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def initialize(self):
# temp set allResults to 0, disabling the messages trick:
if not self.load(self.gramSpec, allResults=showAll):
return
natbj.RegisterControlObject(self)
self.RegisterControlObject(self)
self.emptyList('message')
allGramNames = self.getUnimacroGrammarNames()
self.setList('gramnames', allGramNames)
Expand All @@ -141,14 +141,15 @@ def initialize(self):
## self.setExclusive(1)
print('---now starting other Unimacro grammars:')


def unload(self):
natbj.UnRegisterControlObject(self)
self.UnregisterControlObject()
ancestor.unload(self)

def gotBegin(self, moduleInfo):
#Now is the time to get the names of the grammar objects and
# activate the list for the <ShowTrainGrammar> rule
if self.GetGrammarsChanged():
if self.GetGrammarsChangedFlag():
prevSet = set(self.Lists['gramnames'])
newSet = set(self.getUnimacroGrammars().keys())
if prevSet != newSet:
Expand All @@ -164,16 +165,6 @@ def gotBegin(self, moduleInfo):

self.startExclusive = self.exclusive # exclusive state at start of recognition!

def gotResultsInit(self,words,fullResults):
if self.mayBeSwitchedOn == 'exclusive':
print('recog controle, switch off mic: %s'% words)
natbj.SetMic('off')
if self.exclusive:
try:
self.DisplayMessage('<%s>'% ' '.join(words))
except natlink.NatError:
print('DisplayMessage failed: "%s"'% ' '.join(words))

def resetExclusiveMode(self):
"""no activateAll, do nothing, this grammar follows the last unexclusive grammar
"""
Expand All @@ -185,36 +176,9 @@ def setExclusiveMode(self):
pass

def gotResultsObject(self,recogType,resObj):
if natbj.IsDisplayingMessage:
return
mes = natbj.GetPendingMessage()
if mes:
mes = '\n\n'.join(mes)
natbj.ClearPendingMessage()
self.DisplayMessage(mes)
return
unimacroutils.doPendingBringUps() # if there were
unimacroutils.doPendingExecScripts() # if there were

if self.startExclusive and self.exclusive and showAll:
# display recognition results for exclusive grammars
# with showAll = None (in top of this file), you can disable this feature
if recogType == 'reject':
URName = ''
exclGram = natbj.exclusiveGrammars
exclGramKeys = list(exclGram.keys())
if len(exclGramKeys) > 1 and self.name in exclGramKeys:
exclGramKeys.remove(self.name)
if len(exclGramKeys) > 1:
URName = ', '.join(exclGramKeys)
k = exclGramKeys[0]
v = exclGram[k]
# UE stands for unrecognised exclusive:
# if more exclusive grammars,
# the last one is taken
URName = URName or v[2] # combined name or last rule of excl grammar
msg = '<'+URName+': ???>'
self.DisplayMessage(msg, alsoPrint=0)
"""probably obsolete, mechanism was too complicated
"""
return

#Utilities for Filter Modes and other special modes
def setMode(self,NewMode):
Expand Down Expand Up @@ -304,78 +268,59 @@ def gotResults_trace(self,words,fullResults):
def gotResults_switch(self,words,fullResults):
#print 'control, switch: %s'% words
if self.hasCommon(words, 'on'):
funcName = 'switchOn'
switchOn = True
elif self.hasCommon(words, 'off'):
funcName = 'switchOff'
switchOn = False
else:
try:
t = {'nld': '<%s: ongeldig schakel-commando>'% self.GetName()}[self.language]
except:
t = '<%s: invalid switch command>'% self.GetName()
self.DisplayMessage(t)
return
G = self.getUnimacroGrammars()
Gkeys = list(G.keys())
if self.hasCommon(words, 'all grammars'):
#print '%s all grammars:'% funcName
notLoaded = {}
for impg, gram in natbj.importedGrammars.items():
if impg not in natbj.allUnimacroGrammars:
notLoaded[impg] = gram

for g in natbj.allUnimacroGrammars.items():
gram = natbj.allUnimacroGrammars[g]
for gname, gram in G.items():
if gram == self:
print('no need to switch on _control (should always be on...)')
else:
self.switch(gram, g, funcName)
continue
self.switch(gram, gname, switchOn)
else:
grammars = self.getUnimacroGrammars()
gramNames = list(grammars.keys())
gramname = self.hasCommon(words, gramNames)
if gramname:
gram = grammars[gramname]
gname = self.hasCommon(words, Gkeys)
if gname:
gram = G[gname]
if gram != self:
self.switch(gram, gramname, funcName)
self.switch(gram, gname, switchOn)
# self never needs switching on
else:
print('_control switch, no valid grammar found, command: %s'% words)

def switch(self, gram, gramName, funcName):
def switch(self, gram, gname, switchOn):
"""switch on or off grammar, and set in inifile,
gram is the grammar object
gname is the grammar name
switchOn is True or Fals
"""
func = getattr(gram, funcName)
# in case manual changes were done:
#gram.checkInifile()
if funcName == 'switchOn':
if gram == self:
print(f'should not be here, do not switch on of off _control {gram}')
return
if switchOn:
self.checkInifile()
if not gram.mayBeSwitchedOn:
gram.ini.set('general', 'initial on', 1)
#gram.mayBeSwitchedOn = 1
gram.ini.write()
unimacroutils.Wait(0.1)
else:
print('reload grammar "%s"'% gram.getName())
gram.ini.set('general', 'initial on', 1)
gram.ini.write()
unimacroutils.Wait(0.1)

gram.unload()
gram.initialize()

# modName = gram.__module__
# modPath = gram.__module__.__file__
# natlinkmain.unload_module(modName)
# natlinkmain.load_or_reload_module(modPath, force_load=True)
#print 'reloaded "%s"'% modName
return 1
if funcName == 'switchOff':
if gram.mayBeSwitchedOn:
gram.ini.set('general', 'initial on', 0)
gram.ini.writeIfChanged()
gram.mayBeSwitchedOn = 0
gram.unload()
print('grammar "%s" switched off'% gram.getName())
return 1
print('grammar "%s" switched off (again)'% gram.getName())
return 1
raise ValueError('switching on/off should have as function "switchOn" or "switchOff", not: %s'% func)

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

def gotResults_showexclusive(self,words,fullResults):

Expand All @@ -386,6 +331,7 @@ def gotResults_showexclusive(self,words,fullResults):
else:
Start=()
# fix state at this moment (in case of Active grammars popup)

if natbj.exclusiveGrammars:
Exclusive = 1
self.BrowsePrepare(Start, All, Exclusive)
Expand All @@ -410,15 +356,15 @@ def gotResults_showexclusive(self,words,fullResults):

def gotResults_resetexclusive(self,words,fullResults):
print('reset exclusive')
if natbj.exclusiveGrammars:
exclGrammars = natbj.getExclusiveGrammars()
if exclGrammars:
T = ['exclusive grammars:']
for e in natbj.exclusiveGrammars:
T.append(e)
for name, gram in exclGrammars.items():
T.append(name)
gram.cancelMode()
T.append(self.name)
T.append('... reset exclusive mode')
self.DisplayMessage(' '.join(T))
natbj.GlobalResetExclusiveMode()
unimacroutils.Wait(1)
self.DisplayMessage('reset exclusive mode OK')
else:
self.DisplayMessage('no exclusive grammars')
Expand All @@ -445,9 +391,10 @@ def gotResults_show(self,words,fullResults):
if self.hasCommon(words, 'spoken forms'):
spokenforms.showSpokenForms(comingFrom=self, name="show spoken forms", language=self.language)
return
Exclusive = 0
if natbj.exclusiveGrammars:
print('exclusive grammars (+ control) are: %s'% ' '.join(list(natbj.exclusiveGrammars.keys())))
if self.hasCommon(words, 'exclusive'):
G = self.getExclusiveGrammars()
exclNames = [g for g in G if G[g].isExclusive()]
print('exclusive grammars (+ control) are: {exclNames}')
self.gotResults_showexclusive(words, fullResults)
return

Expand Down Expand Up @@ -514,11 +461,7 @@ def gotResults_show(self,words,fullResults):
# else:
# print(f'cannot remove from otherGrammars: {mod_name}')
# elif result == 0:
# maySwitchOn = gram.mayBeSwitchedOn
# if maySwitchOn:
# inactiveGrammars.append(grammar_name)
# else:
# switchedOffGrammars.append(grammar_name)
# inactiveGrammars.append(grammar_name)
# if mod_name in otherGrammars:
# otherGrammars.remove(mod_name)
# else:
Expand Down Expand Up @@ -620,16 +563,12 @@ def offInfo(self, grammar):
try:
t = {'nld': ['Grammatica "%s" is uitgeschakeld'% name,
'',
'"Schakel in %s" om te activeren'% name]}[self.language]
'Zeg: "schakel in [grammatica] %s" om te activeren'% name]}[self.language]
# title = {'nld': 'Grammatica %s'% name}[self.language]
except KeyError:
t = ['Grammar "%s" is switched off'% name,
'"Switch On Grammar %s" to activate'% name]
'Say: "switch on [grammar] %s" to activate'% name]
# title = 'Grammar %s'% name
if natbj.loadedMessageGrammar:
t = '; '.join(t)
self.DisplayMessage(t)
else:
t = '; '.join(t)
t = t.replace('; ', '\n')
actions.Message(t)
Expand Down
Loading

0 comments on commit 1224471

Please sign in to comment.