Flame of udun
Flame of udun

Reputation: 2237

Pyudev : KeyError raised even though property exists

I am trying to monitor and filter usb mass storage devices using pyudev in python. In the given code devices are being filtered out if their ID_FS_USAGE property is None :

import gtk
from pyudev import Context,Monitor
from pyudev.glib import GUDevMonitorObserver

dev_label = ["0","0"]
serial_list = []
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='block')
observer = GUDevMonitorObserver(monitor)

print dev_label


def device_connected(observer, device):

    flag_device = False
    flag_serial = False

    print device['DEVNAME']
    if device['ID_FS_USAGE'] == None : #HERE IS WHERE THE TROUBLE IS
        flag_device = True

    for iden in serial_list:
        if iden == device.__getitem__('ID_SERIAL_SHORT'):
            flag_serial =True

    if flag_device == False and flag_serial == False:

        print dev_label
        serial_list.append(device.__getitem__('ID_SERIAL_SHORT'))
        Welcome.device_count+=1

        if device.__getitem__('ID_FS_LABEL')is not None:
            dev_label[Welcome.device_count-1]=str(device.__getitem__('ID_FS_LABEL'))
            label = gtk.Label('Device connected :: {0!r}'.format(dev_label[Welcome.device_count-1]))
        else :

            dev_label[Welcome.device_count-1]=str(device.__getitem__('ID_FS_UUID'))
            label = gtk.Label('Device connected :: {0!r}'.format(dev_label[Welcome.device_count-1]))
        Welcome.vbox.pack_start(label)
        Welcome.window.show_all()



        if Welcome.device_count<2:
            label = gtk.Label('Connect the second device')
            Welcome.vbox.pack_start(label)
            Welcome.window.show_all()


        else :
            Exchange()

observer.connect("device-added",device_connected)
monitor.start()

class Welcome:
    device_count = 0    
    window = gtk.Window()
    vbox= gtk.VBox(False, 5)


    def __init__(self):

        self.window.set_default_size(300, 300)
        self.window.set_title("Welcome")

        label = gtk.Label("Connect the desired device")

        self.vbox.pack_start(label)
        self.window.add(self.vbox)

        self.window.connect("destroy", lambda q: gtk.main_quit())
        self.window.show_all()

class Exchange:

    window1 = gtk.Window()
    window1.set_title(dev_label[0])
    window2 = gtk.Window()
    window2.set_title(dev_label[1])
    def __init__(self):

        width = gtk.gdk.screen_get_default().get_width()
        height = gtk.gdk.screen_get_default().get_height()

        self.window1.resize(width/2,height)
        self.window2.resize(width/2,height)

        self.window2.move(self.window1.get_position()[0]+width/2, self.window1.get_position()[1])

        label = gtk.Label("Hello")
        self.window1.add(label)

        self.window1.connect("destroy" , lambda q : gtk.main_quit())
        self.window1.show_all()

        label = gtk.Label("World")
        self.window2.add(label)

        self.window2.connect("destroy",lambda q : gtk.main_quit())
        self.window2.show_all()        

observer.connect("device-added",device_connected)
monitor.start()


Welcome()
gtk.main()

The traceback shown after executing the above code is :

['0', '0']
/dev/sdc
Traceback (most recent call last):
  File "project.py", line 27, in device_connected
    if device['ID_FS_USAGE'] == None :
  File "/usr/lib/python2.7/dist-packages/pyudev/device.py", line 831, in __getitem__
    raise KeyError(property)
KeyError: 'ID_FS_USAGE'
/dev/sdc
Traceback (most recent call last):
  File "project.py", line 27, in device_connected
    if device['ID_FS_USAGE'] == None :
  File "/usr/lib/python2.7/dist-packages/pyudev/device.py", line 831, in __getitem__
    raise KeyError(property)
KeyError: 'ID_FS_USAGE'
/dev/sdc1
['0', '0']
/dev/sdc1
/dev/sde
Traceback (most recent call last):
  File "project.py", line 27, in device_connected
    if device['ID_FS_USAGE'] == None :
  File "/usr/lib/python2.7/dist-packages/pyudev/device.py", line 831, in __getitem__
    raise KeyError(property)
KeyError: 'ID_FS_USAGE'
/dev/sde
Traceback (most recent call last):
  File "project.py", line 27, in device_connected
    if device['ID_FS_USAGE'] == None :
  File "/usr/lib/python2.7/dist-packages/pyudev/device.py", line 831, in __getitem__
    raise KeyError(property)
KeyError: 'ID_FS_USAGE'
/dev/sdd
Traceback (most recent call last):
  File "project.py", line 27, in device_connected
    if device['ID_FS_USAGE'] == None :
  File "/usr/lib/python2.7/dist-packages/pyudev/device.py", line 831, in __getitem__
    raise KeyError(property)
KeyError: 'ID_FS_USAGE'
/dev/sdd
Traceback (most recent call last):
  File "project.py", line 27, in device_connected
    if device['ID_FS_USAGE'] == None :
  File "/usr/lib/python2.7/dist-packages/pyudev/device.py", line 831, in __getitem__
    raise KeyError(property)
KeyError: 'ID_FS_USAGE'

I connected two devices,a usb memory drive and a smartphone in usb mass storage mode. The /dev entries that correspond to the usb memory drive are /dev/sdc and /dev/sdc1. /dev/sdd and /dev/sde correspond to the phone. Here the device /dev/sdd does have a property called ID_FS_USAGE which is not none. But it still raises a KeyError . Surprisingly no such error is raised for /dev/sdc1. What is going wrong? Please Help!

Upvotes: 0

Views: 665

Answers (1)

Steve Barnes
Steve Barnes

Reputation: 28405

The reason that you only get an error for the first device is probably that python stops on the first error.

If instead of using device['ID_FS_USAGE'] which fails if there is no attribute matching you need to use device.get('ID_FS_USAGE') which will return None for missing attributes.

Upvotes: 1

Related Questions