Skip to content

Commit

Permalink
Merge branch 'fix_unimacro_basics' of github.com:dougransom/unimacro …
Browse files Browse the repository at this point in the history
…into fix_unimacro_basics
  • Loading branch information
dougransom committed Oct 20, 2022
2 parents b7304eb + f3bd1ad commit 86c3463
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 58 deletions.
51 changes: 27 additions & 24 deletions src/unimacro/UnimacroGrammars/_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@
# 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
# reBracedActions = re.compile(r"\{")

ancestor = natbj.IniGrammar
class ThisGrammar(ancestor):
language = unimacroutils.getLanguage()


name = "latex"
gramSpec = """
Expand All @@ -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

Expand Down Expand Up @@ -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('[')
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
18 changes: 11 additions & 7 deletions src/unimacro/UnimacroGrammars/_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
48 changes: 23 additions & 25 deletions src/unimacro/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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()
2 changes: 1 addition & 1 deletion src/unimacro/sample_global_dictation/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_grammar_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def test_gototask(monkeypatch):
natlink.natDisconnect()

if __name__ == "__main__":
pytest.main(['test_gototask'])
pytest.main()

0 comments on commit 86c3463

Please sign in to comment.