Pyudev:即使属性存在,也会引发KeyError

时间:2014-03-23 06:01:32

标签: python usb udev pyudev

我正在尝试使用python中的pyudev监视和过滤usb大容量存储设备。在给定代码中,如果ID_FS_USAGE属性为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()

执行上述代码后显示的回溯是:

['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'

我在USB大容量存储模式下连接了两个设备,一个usb内存驱动器和一个智能手机。与usb内存驱动器对应的/ dev条目为/dev/sdc/dev/sdc1/dev/sdd/dev/sde对应于手机。这里设备/dev/sdd确实有一个名为ID_FS_USAGE的属性,它不是无。但它仍然引发了一个KeyError。令人惊讶的是,/dev/sdc1没有出现这样的错误。出了什么问题?请帮助!

1 个答案:

答案 0 :(得分:1)

你只得到第一个设备的错误的原因可能是python在第一个错误时停止。

如果没有属性匹配而不是使用失败的device['ID_FS_USAGE'],则需要使用device.get('ID_FS_USAGE'),这将返回None以查找缺失的属性。