keytests: Move shortcut handling into own class

This commit is contained in:
Kornel Benko 2017-05-22 08:48:16 +02:00
parent 958814ca28
commit 176670fc56

View File

@ -25,7 +25,6 @@ print('Beginning keytest.py')
FNULL = open('/dev/null', 'w') FNULL = open('/dev/null', 'w')
key_delay = '' key_delay = ''
bindings = {}
# Ignore status == "dead" if this is set. Used at the last commands after "\Cq" # Ignore status == "dead" if this is set. Used at the last commands after "\Cq"
dead_expected = False dead_expected = False
@ -472,59 +471,74 @@ def RaiseWindow():
intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1") intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'") system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"') class Shortcuts:
def UseShortcut(c):
m = shortcut_entry.match(c)
if m:
sh = m.group(1)
fkt = m.group(2)
bindings[sh] = fkt
else:
die(1, "cad shortcut spec(" + c + ")")
def PrepareShortcuts(): def __init__(self):
bind = re.compile(r'^\s*\\bind\s+"([^"]+)"') self.shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"')
if lyx_userdir_ver is None: self.bindings = {}
dir = lyx_userdir self.bind = re.compile(r'^\s*\\bind\s+"([^"]+)"')
else: if lyx_userdir_ver is None:
dir = lyx_userdir_ver self.dir = lyx_userdir
if not dir is None:
tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
try:
old = open(dir + '/bind/user.bind', 'r')
except IOError as e:
old = None
if not old is None:
lines = old.read().split("\n")
old.close()
bindfound = False
for line in lines:
m = bind.match(line)
if m:
bindfound = True
val = m.group(1)
if val in bindings:
if bindings[val] != "":
tmp.write("\\bind \"" + val + "\" \"" + bindings[val] + "\"\n")
bindings[val] = ""
else:
tmp.write(line + '\n')
elif not bindfound:
tmp.write(line + '\n')
else: else:
tmp.writelines( self.dir = lyx_userdir_ver
'## This file is used for keytests only\n\n' +
'Format 4\n\n' def __UseShortcut(self, c):
) m = self.shortcut_entry.match(c)
for val in bindings: if m:
if not bindings[val] is None: sh = m.group(1)
if bindings[val] != "": fkt = m.group(2)
tmp.write("\\bind \"" + val + "\" \"" + bindings[val] + "\"\n") self.bindings[sh] = fkt
bindings[val] = "" else:
tmp.close() die(1, "cad shortcut spec(" + c + ")")
shutil.move(tmp.name, dir + '/bind/user.bind')
else: def __PrepareShortcuts(self):
print("User dir not specified") if not self.dir is None:
tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
try:
old = open(self.dir + '/bind/user.bind', 'r')
except IOError as e:
old = None
if not old is None:
lines = old.read().split("\n")
old.close()
bindfound = False
for line in lines:
m = self.bind.match(line)
if m:
bindfound = True
val = m.group(1)
if val in self.bindings:
if self.bindings[val] != "":
tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
self.bindings[val] = ""
else:
tmp.write(line + '\n')
elif not bindfound:
tmp.write(line + '\n')
else:
tmp.writelines(
'## This file is used for keytests only\n\n' +
'Format 4\n\n'
)
for val in self.bindings:
if not self.bindings[val] is None:
if self.bindings[val] != "":
tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
self.bindings[val] = ""
tmp.close()
shutil.move(tmp.name, self.dir + '/bind/user.bind')
else:
print("User dir not specified")
def dispatch(self, c):
if c[0:12] == 'UseShortcut ':
self.__UseShortcut(c[12:])
elif c == 'PrepareShortcuts':
print('Preparing usefull sortcuts for tests')
self.__PrepareShortcuts()
else:
return False
return True
lyx_pid = os.environ.get('LYX_PID') lyx_pid = os.environ.get('LYX_PID')
print('lyx_pid: ' + str(lyx_pid) + '\n') print('lyx_pid: ' + str(lyx_pid) + '\n')
@ -625,6 +639,7 @@ write_commands = True
failed = False failed = False
lineempty = re.compile(r'^\s*$') lineempty = re.compile(r'^\s*$')
marked = ControlFile() marked = ControlFile()
shortcuts = Shortcuts()
while not failed: while not failed:
#intr_system('echo -n LOADAVG:; cat /proc/loadavg') #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
c = x.getCommand() c = x.getCommand()
@ -638,6 +653,8 @@ while not failed:
outfile.flush() outfile.flush()
if marked.dispatch(c): if marked.dispatch(c):
continue continue
elif shortcuts.dispatch(c):
continue
if c[0] == '#': if c[0] == '#':
print("Ignoring comment line: " + c) print("Ignoring comment line: " + c)
elif c[0:9] == 'TestBegin': elif c[0:9] == 'TestBegin':
@ -696,11 +713,6 @@ while not failed:
elif c == 'RaiseLyx': elif c == 'RaiseLyx':
print('Raising Lyx') print('Raising Lyx')
RaiseWindow() RaiseWindow()
elif c == 'PrepareShortcuts':
print('Preparing usefull sortcuts for tests')
PrepareShortcuts()
elif c[0:12] == 'UseShortcut ':
UseShortcut(c[12:])
elif c[0:4] == 'KK: ': elif c[0:4] == 'KK: ':
if lyx_exists(): if lyx_exists():
sendKeystringRT(c[4:], lyx_pid) sendKeystringRT(c[4:], lyx_pid)
@ -834,7 +846,7 @@ while not failed:
print("Unrecognised Command '" + c + "'\n") print("Unrecognised Command '" + c + "'\n")
failed = True failed = True
print("Test case terminated: ") print("Test case terminated: ", end = '')
if failed: if failed:
die(1,"FAIL") die(1,"FAIL")
else: else: