Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate to python3 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions kbd-tst.py → kbd-tst_3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/python3

# Simple Keyboard Test Program - inspired by old DOS CheckIt / QA Plus
#
Expand Down Expand Up @@ -206,11 +206,11 @@ def cursor_on(self):

def print_(self, str):
""" print and stay on line """
print str,
print(str, end=' ')

def print_ln(self, str=''):
""" print with newline """
print str
print(str)
self.atrow += 1

def set_map(self, map):
Expand Down Expand Up @@ -254,19 +254,19 @@ def key_action(self, keydict, action):

def banner(self, txt, bg='cyan', above=1, bellow=1):
for i in range(above):
print
print()
#
self.color(fg='black', bg=bg)
print txt
print(txt)
self.color_reset()
self.flush()
#
for i in range(bellow):
print
print()

def dbg(self, txt):
self.write_at(25,1)
print txt
print(txt)

class Xinput:
""" executing xinput as subprocess """
Expand All @@ -285,12 +285,12 @@ def version(self):
# stdout
for line in stdout.splitlines():
pat = 'xinput version '
if line.startswith(pat):
return line.replace(pat, '')
if line.startswith(pat.encode()):
return line.replace(pat.encode(), ''.encode())
#
return '?'

def list(self, filter='keyboard', trim=True):
def list(self, filter='keyboard'.encode(), trim=True):
""" list xinput devices with optional filter """
args = [self.exe, 'list']
xinput = subprocess.Popen(args, stdout=subprocess.PIPE)
Expand All @@ -299,7 +299,7 @@ def list(self, filter='keyboard', trim=True):

def name_by_id(self, id):
# get also ascii name for id (strip junk)
name = [ (dev.split('\t')[0]).strip('\xe2\x86\xb3 ') for dev in self.list() if "id=%d" % id in dev ]
name = [ dev.decode().strip() for dev in self.list() if "id="+str(id) in dev.decode() ]
return name[0] if name else '?'

def start(self, id=8):
Expand Down Expand Up @@ -646,12 +646,10 @@ def show_err(self, err):
if len(err) == 0: return
for e in err:
self.gui.banner(" ERR: %s " % e, bg='red', above=0, bellow=0)
print
print "This is caused either by:"
print "\t - problems in layout file: %s (incorrect [ key_labels ] etc )" % self.gmapfname
print "\t - missing dictionary entries in rev_xmodmap = {...} ( Layout class )"
print
_ = raw_input('Press ENTER to continue ...')
print("This is caused either by:")
print("\t - problems in layout file: %s (incorrect [ key_labels ] etc )" % self.gmapfname)
print("\t - missing dictionary entries in rev_xmodmap = {...} ( Layout class )")
_ = input('Press ENTER to continue ...')

def kut_id(self, id=None):
""" returns either specific keyboard unde test id or guide user with autodetection """
Expand All @@ -664,7 +662,7 @@ def kut_id(self, id=None):
def autodetect_id(self):
""" checking changes in xinput list when connecting unknown kbd reveals its id """
self.gui.banner(" = Autodetection process started ... = ")
print "Connect or Reconnect keyboard you want to test ",
print("Connect or Reconnect keyboard you want to test ", end=' ')
ref = self.xinput.list()
while True:
act = self.xinput.list()
Expand All @@ -676,19 +674,19 @@ def autodetect_id(self):
# something disconnected - take new reference nad loop again
if len(act) < len(ref):
removed = list(set(ref) - set(act))
print
print "Device disconnected:", ' / '.join(removed)
removed_str = list(map(lambda x: x.decode(), removed))
print("Device disconnected:", ' / '.join(removed_str))
ref = act
continue
# something connected - find out id
if len(act) > len(ref):
added = list(set(act) - set(ref))
print
print "Device connected :", ' / '.join(added)
added_str = list(map(lambda x: x.decode(), added))
print("Device connected :", ' / '.join(added_str))
break
# extract minimal id = first of sorted list
# Mitsumi Electric Apple Extended USB Keyboard id=8 [slave keyboard (3)]
id = sorted([int(part.replace('id=', '')) for item in added for part in item.split() if part.startswith('id=')])[0]
id = sorted([int(part.replace('id='.encode(), ''.encode())) for item in added for part in item.split() if part.startswith('id='.encode())])[0]
self.gui.banner(" = Autodetection done = detected xinput id:%d [ %s ] = " % (id, self.xinput.name_by_id(id)))
time.sleep(1)
return id
Expand Down Expand Up @@ -802,7 +800,7 @@ def key_tested(self, action, keydict):

def update_stats(self):
""" update footer stats """
tested = len([ k for k,v in self.layout.layout.items() if v['tested'] ])
tested = len([ k for k,v in list(self.layout.layout.items()) if v['tested'] ])
total = len(self.layout.layout) + self.key_missing
stats = {
'total': total,
Expand All @@ -817,14 +815,14 @@ def update_stats(self):

def all_tested(self):
""" are we doone = all keys has been tested """
return all([ v['tested'] for k,v in self.layout.layout.items() ])
return all([ v['tested'] for k,v in list(self.layout.layout.items()) ])

def keypress(self):
""" xinput key event press/release and keycode"""
# wait for key
line = self.xinput.readline()
# key press 128
m = re.search('key (press|release)\s+(\d+)', line)
m = re.search('key (press|release)\s+(\d+)', line.decode())
# this should not happen: return if not key press|release
if not m: return '',0
action = m.group(1)
Expand All @@ -835,7 +833,7 @@ def keypress(self):
def report(self):
""" mini report - right now only timestamp """
now = datetime.datetime.now()
tested = len([k for k, v in self.layout.layout.items() if v['tested']])
tested = len([k for k, v in list(self.layout.layout.items()) if v['tested']])
total = len(self.layout.layout) + self.key_missing
untested = total - tested
if self.all_tested():
Expand All @@ -855,8 +853,8 @@ def parse_argv(argv):
id, layout = None, None
for par in argv:
if par in ['-h', '--help']:
print "=",__about__,"version",__version__,"=",__copyright__,"="
print __usage__
print("=",__about__,"version",__version__,"=",__copyright__,"=")
print(__usage__)
sys.exit()
if par.isdigit():
id = int(par)
Expand Down