0

我正在尝试在 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. 出了什么问题?请帮忙!

4

1 回答 1

1

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.

于 2014-03-23T06:07:56.193 回答