I am writting a small program to list the available usb devices. I have created a seperated file with function so that it can be accessed any time from other scripts. The main objective of this function is to return list/ array as output. If I use print command it successfully prints list of available devices. However, when i use return command it only retun the first detected device. I have gone through other similar question from SO but couldnot find any valid solution for this issue. Here is the code I have tried using dbus. Any help is appriciated.
#!/usr/bin/python2.7
import dbus
def find_usb(self):
bus = dbus.SystemBus()
ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
for dev in ud_manager.EnumerateDevices():
device_obj = bus.get_object("org.freedesktop.UDisks", dev)
device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
if device_props.Get('org.freedesktop.UDisks.Device', "DriveConnectionInterface") == "usb" and device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsPartition"):
if device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted"):
device_file = device_props.Get('org.freedesktop.UDisks.Device', "DeviceFile")
#print device_file
return device_file
else:
print "Device not mounted"
find_usb("")