Skip to content

Commit

Permalink
test: Convert test scripts from Python2 to Python3
Browse files Browse the repository at this point in the history
Python2 is deprecated so convert the neard test scripts in the 'test'
and 'se/test' directories to Python3.

Signed-off-by: Mark Greer <[email protected]>
Signed-off-by: Luca Weiss <[email protected]>
  • Loading branch information
mgreeraz authored and krzk committed Jun 24, 2024
1 parent 9fcc972 commit b8a4aee
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 157 deletions.
19 changes: 10 additions & 9 deletions se/test/test-channel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import sys
import dbus
Expand All @@ -11,10 +11,10 @@ def extract_byte_array(byte_array):


def usage():
print "Usage: %s <command>" % (sys.argv[0])
print ""
print " list"
print " send_apdu se/nfcX_<se_type>_seX channelX <apdu>"
print("Usage: %s <command>" % (sys.argv[0]))
print("")
print(" list")
print(" send_apdu se/nfcX_<se_type>_seX channelX <apdu>")
sys.exit(1)


Expand All @@ -32,7 +32,7 @@ if (sys.argv[1] == "list"):
properties = seel.GetProperties()

for path in properties["Channels"]:
print "[ %s ]" % (path)
print("[ %s ]" % path)

channel = dbus.Interface(bus.get_object("org.neard.se", path),
"org.neard.se.Channel")
Expand All @@ -44,10 +44,11 @@ if (sys.argv[1] == "list"):
val = "true"
else:
val = "false"
print " %s = %s" % (key, val)
print(" %s = %s" % (key, val))

if key in ["AID"]:
print " %s = %s" % (key, extract_byte_array(properties[key]))
print(" %s = %s" %
(key, extract_byte_array(properties[key])))
sys.exit(0)

if (sys.argv[1] == "send_apdu"):
Expand All @@ -61,7 +62,7 @@ if (sys.argv[1] == "send_apdu"):

response = channel.SendAPDU(sys.argv[4].decode("hex"))

print "--> %s" % extract_byte_array(response)
print("--> %s" % extract_byte_array(response))
sys.exit(0)

usage()
32 changes: 16 additions & 16 deletions se/test/test-seel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import sys
import dbus
Expand All @@ -15,12 +15,12 @@ def extract_list(list):


def usage():
print "Usage: %s <command>" % (sys.argv[0])
print ""
print " list"
print " enabled se/nfcX_<se_type>_seX [on/off]"
print " open_channel se/nfcX_<se_type>_seX <AID>"
print " close_channel se/nfcX_<se_type>_seX channelX"
print("Usage: %s <command>" % (sys.argv[0]))
print("")
print(" list")
print(" enabled se/nfcX_<se_type>_seX [on/off]")
print(" open_channel se/nfcX_<se_type>_seX <AID>")
print(" close_channel se/nfcX_<se_type>_seX channelX")
sys.exit(1)


Expand All @@ -36,7 +36,7 @@ if (sys.argv[1] == "list"):
properties = manager.GetProperties()

for path in properties["SecureElements"]:
print "[ %s ]" % (path)
print("[ %s ]" % path)

seel = dbus.Interface(bus.get_object("org.neard.se", path),
"org.neard.se.SecureElement")
Expand All @@ -48,13 +48,13 @@ if (sys.argv[1] == "list"):
val = "true"
else:
val = "false"
print " %s = %s" % (key, val)
print(" %s = %s" % (key, val))

if key in ["Type"]:
print " %s = %s" % (key, str(properties[key]))
print(" %s = %s" % (key, str(properties[key])))

if key in ["Channels"]:
print " %s = %s" % (key, extract_list(properties[key]))
print(" %s = %s" % (key, extract_list(properties[key])))
sys.exit(0)

if (sys.argv[1] == "enabled"):
Expand All @@ -72,10 +72,10 @@ if (sys.argv[1] == "enabled"):
elif (sys.argv[3] == "off"):
val = seel.SetProperty("Enabled", dbus.Boolean(0), timeout=10)
else:
print "Invalid option %s" % sys.argv[3]
print("Invalid option %s" % sys.argv[3])

except dbus.DBusException as error:
print "%s: %s" % (error._dbus_error_name, error.message)
print("%s: %s" % (error._dbus_error_name, error.message))
sys.exit(0)

if (sys.argv[1] == "open_channel"):
Expand All @@ -90,9 +90,9 @@ if (sys.argv[1] == "open_channel"):
try:
channel = seel.OpenChannel(sys.argv[3].decode("hex"))

print "Opened channel: %s" % str(channel)
print("Opened channel: %s" % str(channel))
except dbus.DBusException as error:
print "%s: %s" % (error._dbus_error_name, error.message)
print("%s: %s" % (error._dbus_error_name, error.message))
sys.exit(0)

if (sys.argv[1] == "close_channel"):
Expand All @@ -111,7 +111,7 @@ if (sys.argv[1] == "close_channel"):
seel.CloseChannel(channel)

except dbus.DBusException as error:
print "%s: %s" % (error._dbus_error_name, error.message)
print("%s: %s" % (error._dbus_error_name, error.message))
sys.exit(0)

usage()
8 changes: 4 additions & 4 deletions test/bt-handover
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import os
import sys
Expand All @@ -15,7 +15,7 @@ mainloop = gobject.MainLoop()


def device_added(path, interfaces):
for iface, props in interfaces.iteritems():
for iface, props in interfaces.items():
if "org.neard.Device" in interfaces:
print("Pairing with %s" % (path))
device = dbus.Interface(bus.get_object("org.neard", path),
Expand All @@ -37,7 +37,7 @@ def remove_paired_devices(bt_adapter):
"org.freedesktop.DBus.ObjectManager")
objects = manager.GetManagedObjects()

all_adapters = (path for path, interfaces in objects.iteritems() if
all_adapters = (path for path, interfaces in objects.items() if
"org.bluez.Adapter1" in interfaces.keys()
and path.endswith(bt_adapter))

Expand All @@ -63,7 +63,7 @@ def remove_paired_devices(bt_adapter):
print("Bluetooth adapter %s is not powered" % adapter_path)
exit()

all_devices = (path for path, interfaces in objects.iteritems() if
all_devices = (path for path, interfaces in objects.items() if
("org.bluez.Device1" in interfaces.keys()
and path.startswith(bluez_adapter.object_path)))

Expand Down
34 changes: 17 additions & 17 deletions test/handover-agent
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import gobject

Expand Down Expand Up @@ -66,18 +66,18 @@ power_state = None
def print_fields(fields):
if 'EIR' in fields:
s = ' '.join('{:#02x}'.format(i) for i in fields['EIR'])
print ' EIR: %s' % s
print(' EIR: %s' % s)

if 'nokia.com:bt' in fields:
s = ' '.join('{:#02x}'.format(i) for i in fields['nokia.com:bt'])
print ' nokia.com:bt: %s' % s
print(' nokia.com:bt: %s' % s)

if 'State' in fields:
print ' State: %s' % fields['State']
print(' State: %s' % fields['State'])

if 'WSC' in fields:
s = ' '.join('{:#02x}'.format(i) for i in fields['WSC'])
print ' WSC: %s' % s
print(' WSC: %s' % s)


class BTHOAgent(dbus.service.Object):
Expand All @@ -86,29 +86,29 @@ class BTHOAgent(dbus.service.Object):
in_signature='',
out_signature='')
def Release(self):
print 'Release'
print('Release')
mainloop.quit()

@dbus.service.method('org.neard.HandoverAgent',
in_signature='a{sv}',
out_signature='')
def PushOOB(self, fields):
print 'PushOOB'
print('PushOOB')
print_fields(fields)

@dbus.service.method('org.neard.HandoverAgent',
in_signature='a{sv}',
out_signature='a{sv}')
def RequestOOB(self, fields):
print 'RequestOOB'
print('RequestOOB')
print_fields(fields)

print ' Replying with'
print(' Replying with')
s = ' '.join('{:#02x}'.format(i) for i in eir_test_data)
print ' EIR: %s' % s
print(' EIR: %s' % s)

if power_state != 'unknown':
print ' State: %s' % power_state
print(' State: %s' % power_state)
return {'EIR': eir_test_data, 'State': power_state}
else:
return {'EIR': eir_test_data}
Expand All @@ -120,29 +120,29 @@ class WiFiHOAgent(dbus.service.Object):
in_signature='',
out_signature='')
def Release(self):
print 'Release'
print('Release')
mainloop.quit()

@dbus.service.method('org.neard.HandoverAgent',
in_signature='a{sv}',
out_signature='')
def PushOOB(self, fields):
print 'PushOOB'
print('PushOOB')
print_fields(fields)

@dbus.service.method('org.neard.HandoverAgent',
in_signature='a{sv}',
out_signature='a{sv}')
def RequestOOB(self, fields):
print 'RequestOOB'
print('RequestOOB')
print_fields(fields)

print ' Replying with'
print(' Replying with')
s = ' '.join('{:#02x}'.format(i) for i in wsc_test_data)
print ' WSC: %s' % s
print(' WSC: %s' % s)

if power_state != 'unknown':
print ' State: %s' % power_state
print(' State: %s' % power_state)
return {'WSC': wsc_test_data, 'State': power_state}
else:
return {'WSC': wsc_test_data}
Expand Down
10 changes: 4 additions & 6 deletions test/monitor-near
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/python

from __future__ import absolute_import, print_function, unicode_literals
#!/usr/bin/python3

import gobject

Expand All @@ -10,16 +8,16 @@ import dbus.mainloop.glib

def property_changed(interface, changed, invalidated, path):
iface = interface[interface.rfind(".") + 1:]
for name, value in changed.iteritems():
for name, value in changed.items():
val = str(value)
print("{%s.PropertyChanged} [%s] %s = %s" % (iface, path, name,
val))


def interfaces_added(path, interfaces):
for iface, props in interfaces.iteritems():
for iface, props in interfaces.items():
print("{Added %s} [%s]" % (iface, path))
for name, value in props.iteritems():
for name, value in props.items():
print(" %s = %s" % (name, value))


Expand Down
20 changes: 10 additions & 10 deletions test/ndef-agent
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

import gobject

Expand All @@ -13,44 +13,44 @@ class NDEFAgent(dbus.service.Object):
@dbus.service.method("org.neard.NDEFAgent",
in_signature='', out_signature='')
def Release(self):
print "Release"
print("Release")
mainloop.quit()

@dbus.service.method("org.neard.NDEFAgent",
in_signature='a{sv}',
out_signature='')
def GetNDEF(self, fields):
print "GetNDEF"
print("GetNDEF")

if "Record" in fields:
print "Record path %s" % (fields["Record"])
print("Record path %s" % (fields["Record"]))

if "Payload" in fields:
val = "["
for i in fields["Payload"]:
val += " 0x%x" % i
val += " ]"
print "Record payload %s" % val
print("Record payload %s" % val)

if "NDEF" in fields:
val = "["
for i in fields["NDEF"]:
val += " 0x%x" % i
val += " ]"
print "Complete NDEF %s" % val
print("Complete NDEF %s" % val)

return

@dbus.service.method("org.neard.NDEFAgent",
in_signature='', out_signature='')
def Cancel(self):
print "Cancel"
print("Cancel")


def print_usage():
print "Usage:"
print "%s Type=<record type>" % (sys.argv[0])
print "Help: %s help" % (sys.argv[0])
print("Usage:")
print("%s Type=<record type>" % (sys.argv[0]))
print("Help: %s help" % (sys.argv[0]))
sys.exit(1)


Expand Down
Loading

0 comments on commit b8a4aee

Please sign in to comment.