Skip to content

Commit

Permalink
a big stroke towards all the unimacro grammars working
Browse files Browse the repository at this point in the history
  • Loading branch information
quintijn committed Dec 1, 2022
1 parent 19fc76e commit 74d22fb
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 403 deletions.
2 changes: 1 addition & 1 deletion src/unimacro/UnimacroGrammars/_debug_natlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pylint:disable=W0611, W0613, C0115, C0116

import natlink
import natlinkpydebug as pd
from natlinkcore import natlinkpydebug as pd
from natlinkcore import natlinkutils
from natlinkcore import gramparser as gp
from natlinkcore import nsformat
Expand Down
208 changes: 75 additions & 133 deletions src/unimacro/UnimacroGrammars/_folders.py

Large diffs are not rendered by default.

61 changes: 20 additions & 41 deletions src/unimacro/UnimacroGrammars/_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def gotBegin(self,moduleInfo):
self.checkInifile() # refills grammar lists and instance variables
# if something changed.
self.gotPassword = 0
self.progInfo = unimacroutils.getProgInfo(moduleInfo)

def gotResults_wrongrule(self,words,fullResults):
natut.playString("%s\n"% fullResults)
Expand All @@ -211,6 +212,8 @@ def gotResultsInit(self,words,fullResults):
print('Here from _general...')
natut.buttonClick()
unimacroutils.Wait()
self.progInfo = unimacroutils.getProgInfo()
print(f'progInfo _general: {self.progInfo.prog}, {self.progInfo.title}')

def gotResults_password(self,words,fullResults):
"""interpret password as dictate
Expand Down Expand Up @@ -762,10 +765,8 @@ def gotResults_info(self,words,fullResults):
T = []
extra = []
if self.hasCommon(words,'window') or self.hasCommon(words,'prog'):
m = natlink.getCurrentModule()
hndle = m[2]
p = unimacroutils.getProgInfo(m)
assert hndle == p.hndle
p = unimacroutils.getProgInfo()
hndle = p.hndle
T.append('---from unimacroutils.getProgInfo:')
# (progpath, prog, title, toporchild, classname, hndle)
if self.hasCommon(words, 'prog'):
Expand All @@ -777,7 +778,7 @@ def gotResults_info(self,words,fullResults):
T.append(f' .classname\t{p.classname}')
T.append(f' .hndle:\t{hndle}')
childClass = "#32770"
overruleIsTop = self.getTopOrChild(m, childClass=childClass)
overruleIsTop = self.getTopOrChild(self.progInfo, childClass=childClass)

if p.toporchild != overruleIsTop:
T.append('')
Expand Down Expand Up @@ -890,15 +891,14 @@ def gotResults_comment(self,words,fullResults):
else:
ts = time.strftime("%d%m%y_", time.localtime(time.time()))

m = natlink.getCurrentModule()
if unimacroutils.matchModule('pythonwin', modInfo=m):
progInfo = unimacroutils.getProgInfo()
if unimacroutils.matchModule('pythonwin', progInfo=progInfo):
com = "#" + name + ts
elif unimacroutils.matchModule('textpad', 'html', modInfo=m):
elif unimacroutils.matchModule('textpad', 'html', progInfo=progInfo):
com = "<!--" + name + ts + "-->"
elif unimacroutils.matchModule(m,'textpad', '.c'):
elif unimacroutils.matchModule('textpad', '.c', progInfo=progInfo):
com = "$$$$" + name + ts + "$$$$"
#TODO QH figure this out (parameters)
elif unimacroutils.matchModule(m,'textpad', '.py', modInfo=m):
elif unimacroutils.matchModule('textpad', '.py', progInfo=progInfo):
com = "#" + name + ts
else:
com = name + ts
Expand Down Expand Up @@ -1200,40 +1200,19 @@ def getIdleWindowsWithText(hwnd, th):
wTitle = win32gui.GetWindowText(hwnd).strip().lower()
TH.append((wTitle, hwnd))


# standard stuff Joel (adapted for possible empty gramSpec, QH, unimacro)
thisGrammar = ThisGrammar()
if thisGrammar.gramSpec:
thisGrammar.initialize()
else:
thisGrammar = None

def unload():
#pylint:disable=W0603
global thisGrammar
if thisGrammar: thisGrammar.unload()
if thisGrammar:
thisGrammar.unload()
thisGrammar = None

# forgot about this, possibly delete (QH 2010):
#def processLine(line):
# """proces a line"""
# if line.find("Bind") == -1:
# return line
# obj, options = line.split('.Bind')
# lenstart = len(obj) - len(obj.lstrip())
# obj = obj.strip()
# options = options[1:-1]
# pars = tuple(map(string.strip, options.split(',')))
# if len(pars) == 2:
# oldMethod, method = pars
# return '%s%s(%s, %s)'% (' '*lenstart, oldMethod, obj, method)
# elif len(pars) == 3:
# oldMethod, method, id = pars
# return '%s%s(%s, %s, %s)'% (' '*lenstart, oldMethod, obj, id, method)
# else:
# return 'invalid pars: %s %s'% (len(pars), line)
#


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()


76 changes: 35 additions & 41 deletions src/unimacro/UnimacroGrammars/_keystrokes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
__version__ = "$Rev: 606 $ on $Date: 2019-04-23 14:30:57 +0200 (di, 23 apr 2019) $ by $Author: quintijn $"
# This file is part of a SourceForge project called "unimacro" see
# http://unimacro.SourceForge.net and http://qh.antenna.nl/unimacro
# (c) copyright 2003 see http://qh.antenna.nl/unimacro/aboutunimacro.html
Expand All @@ -17,21 +16,16 @@
others continuously.
"""
import time
import os
#pylint:disable=R0904, R0913, R0912

import sys
from natlinkcore import inivars
import types
import copy
import natlink
from natlinkcore import nsformat
from dtactions.unimacro.unimacroactions import doAction as action
from dtactions.unimacro import inivars
from dtactions.unimacro.unimacroactions import doKeystroke as keystroke
from dtactions.unimacro.unimacroactions import doAction as action

from natlinkcore import natlinkutils as natut
from dtactions.unimacro import unimacroutils
from dtactions.unimacro import unimacroutils
import unimacro.natlinkutilsbj as natbj
from unimacro import natlinkutilsbj as natbj
from natlinkcore import nsformat

language = unimacroutils.getLanguage()

Expand Down Expand Up @@ -114,7 +108,7 @@ def initialize(self):
self.resetAllVars()
self.resetVars()
self.isIgnored = 0
self.title = 'Unimacro grammar %s (%s) language: %s'% (self.name, __name__, language)
self.title = 'Unimacro grammar {self.name} (__name__) language: {language}'
#self.activateAll()
self.fillGrammarLists()
self.dictateOutputState = -1 # start state for dictate <dgndictation> rule (nsformat)
Expand Down Expand Up @@ -146,12 +140,12 @@ def gotBegin(self, moduleInfo):
ini = self.ini
#print 'do keystrokes for mode: %s'% repr(mode)
if mode == 'inactive':
print('%s: deactivate: %s'% (self.GetName(), mode))
print(f'{self.GetName()}: deactivate: {mode}')
self.deactivateAll()
self.cancelMode()
self.resetAllVars()
return
elif mode == 'active':
if mode == 'active':
repkeySections = ['repkey']
norepkeySections = ['norepkey']
# print 'activate "default mode" keystrokes'
Expand All @@ -162,7 +156,7 @@ def gotBegin(self, moduleInfo):
# self.modeSet is set of modestrings being active:
wantExclusive = self.modeSet & self.exclusiveModes # both a set
if wantExclusive:
print('make keystokes mode exclusive: %s'% wantExclusive)
print(f'make keystokes mode exclusive: {wantExclusive}')
self.setExclusive(1)
#if
repkeySections = self.ini.getSectionsWithPrefix('repkey', mode)
Expand Down Expand Up @@ -234,7 +228,7 @@ def subrule_contextmenu(self, words):
# else: #
#
def gotResults_dgndictation(self, words, fullResults):
print('words of dgndictation: %s'% words)
print(f'words of dgndictation: {words}')
formattedOutput, self.dictateOutputState = nsformat.formatWords(words, state=self.dictateOutputState)
self.key = formattedOutput
self.flush()
Expand All @@ -258,12 +252,12 @@ def gotResults_repkey(self, words, fullResults):
if self.repkeySections:
res = self.ini.get(self.repkeySections, w)
else:
print('_keystrokes, no repkeySections for this mode: %s'% repr(self.prevMode))
print(f'_keystrokes, no repkeySections for this mode: {self.prevMode}')
return
if res:
self.key = res
else:
print('rep, found character or something else: %s'% w)
print(f'rep, found character or something else: {w}')
posSlash = w.find('\\')
if posSlash > 0:
self.key = w[:posSlash]
Expand Down Expand Up @@ -300,11 +294,11 @@ def gotResults_norepkey(self, words, fullResults):
if self.repkeySections:
res = self.ini.get(self.norepkeySections, w)
else:
print('_keystrokes, no norepkeySections for this mode: %s'% self.prevMode)
print(f'_keystrokes, no norepkeySections for this mode: {self.prevMode}')
return
res = self.ini.get(self.norepkeySections, w)
if not res:
raise ValueError("_keystrokes, norepkey: no code found for %s (%s)"% w, (self.fullResults))
raise ValueError(f'_keystrokes, norepkey: no code found for {w} ({self.fullResults})')
self.key = res
#print 'norepkey: %s'% self.key
self.flush()
Expand Down Expand Up @@ -361,14 +355,14 @@ def gotResults_click(self, words, fullResults):
if len(parts) > 1:

if parts[1] not in possibleClicks:
print('number of clicks (%s) should be one of %s'% (parts[1], possibleClicks))
print(f'number of clicks ({parts[1]}) should be one of {possibleClicks}')
return
nClick = int(parts[1])
else:
nClick = 1

if len(parts) > 2:
print('currently only (button, clicks) allowed in clickrule, not: %s'% clickrules)
print(f'currently only (button, clicks) allowed in clickrule, not: {clickrules}')
return

if self.nextRule == 'contextmenu' and nClick == 1:
Expand All @@ -385,10 +379,10 @@ def doMouseMoveStopClick(self):
action("ALERT")
if not action("WAITMOUSEMOVE"):
action("ALERT 2")
return
return None
if not action("WAITMOUSESTOP"):
action("ALERT 2")
return
return None
action("ALERT")
return 1

Expand Down Expand Up @@ -436,8 +430,8 @@ def flush(self):
if self.count != 1:
self.key = unimacroutils.doCount(self.key, self.count)

if type(self.key) == list:
print('_keystrokes, flush: warning, self.key is list: %s'% self.key)
if isinstance(self.key, list):
print(f'_keystrokes, flush: warning, self.key is list: {self.key}')
self.buf.extend(self.key)
else:
self.buf.append(self.key)
Expand All @@ -448,12 +442,12 @@ def flushAll(self):
try:
buf = ''.join(self.buf)
except TypeError:
print("---TypeError in flushAll of _keystrokes: %s"% repr(self.buf))
print(f'---TypeError in flushAll of _keystrokes: {self.buf}')
raise

#print 'flushAll: %s'% buf
if self.codes:
action("SCLIP %s"% buf)
action(f'SCLIP {buf}')
elif buf.find("<<") >= 0:
action(buf)
else:
Expand Down Expand Up @@ -495,14 +489,14 @@ def windowPolicy(self, modInfo=None, progInfo=None):
if unimacroutils.matchWindow(self.modes[mode], progInfo=progInfo):
modeSet.append(mode)
self.modeSet = set(modeSet)
if modeSet: return tuple(modeSet)
if modeSet:
return tuple(modeSet)

if unimacroutils.matchWindow(self.activateRules, progInfo=progInfo):
if unimacroutils.matchWindow(self.deactivateRules, progInfo=progInfo):
return 'inactive'
return 'active'
else:
return 'inactive'
return 'inactive'


def showInifile(self, body=None, grammarLists=None, ini=None,
Expand All @@ -514,24 +508,24 @@ def showInifile(self, body=None, grammarLists=None, ini=None,
modesString = ', '.join(self.prevMode)
else:
modesString = str(self.prevMode)
body.append('current mode/active/inactive: %s'% modesString)
body.append(f'current mode/active/inactive: {modesString}')
if not self.prevMode:
body.append("no current mode (yet): %s"% self.prevMode)
body.append(f'no current mode (yet): {self.prevMode}')
elif self.prevMode == 'active':
pass
elif self.prevMode == 'inactive':
# probably never comes here, because grammar is inactive at this moment
body.append('inactive in %s'% self.deactivateRules)
body.append(f'inactive in {self.deactivateRules}')
else:
# give extra moohdes information:
body.append('possible modes:')
for mode in self.modes:
if self.modes[mode] == {mode: None}:
body.append('\t%s: (all windows)'% mode)
body.append(f'\t{mode}: (all windows)')
else:
body.append('\t%s: %s'% (mode, self.modes[mode]))
body.append(f'\t{mode}: {self.modes[mode]}')
else:
body.append('active/inactive: %s'% self.prevMode)
body.append('active/inactive: {self.prevMode}')

if self.doKeystrokesExtended:
body.append('do keystrokes extended (with Here and mouse clicking)')
Expand All @@ -545,7 +539,6 @@ def fillDefaultInifile(self, ini):
"""initialize as a starting example the ini file obsolete
"""
pass

def fillInstanceVariables(self):
"""fills instance variables with data from inifile
Expand Down Expand Up @@ -587,7 +580,7 @@ def fillInstanceVariables(self):
self.exclusiveModes = set(self.ini.getList('general', 'exclusive modes', []))
for m in self.exclusiveModes:
if m not in self.modes:
print('warning, exclusive mode "%s" not in defined modes: %s'% (repr(m), list(self.modes.keys())))
print(f'warning, exclusive mode "{m}" not in defined modes: {self.modes.keys()}')


except inivars.IniError:
Expand Down Expand Up @@ -621,7 +614,8 @@ def cancelMode(self):
def unload():
#pylint:disable=W0603
global thisGrammar
if thisGrammar: thisGrammar.unload()
if thisGrammar:
thisGrammar.unload()
thisGrammar = None

def changeCallback(type,args):
Expand Down
Loading

0 comments on commit 74d22fb

Please sign in to comment.