Merge pull request #249 from mulkieran/master-pyudev

Fix some bugs in udev.get_indexed_string.
This commit is contained in:
Peter Wu 2016-01-13 15:24:51 +01:00
commit 92967eed23
1 changed files with 23 additions and 18 deletions

View File

@ -32,6 +32,7 @@ import os as _os
import errno as _errno
from select import select as _select
from pyudev import Context as _Context, Monitor as _Monitor, Device as _Device
from pyudev import DeviceNotFoundError
native_implementation = 'udev'
@ -338,14 +339,21 @@ def get_indexed_string(device_handle, index):
:param device_handle: a device handle returned by open() or open_path().
:param index: the index of the string to get.
:returns: the value corresponding to index, or None if no value found
:rtype: bytes or NoneType
"""
if index not in _DEVICE_STRINGS:
try:
key = _DEVICE_STRINGS[index]
except KeyError:
return None
assert device_handle
stat = _os.fstat(device_handle)
try:
dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev)
if dev:
except (DeviceNotFoundError, ValueError):
return None
hid_dev = dev.find_parent('hid')
if hid_dev:
assert 'HID_ID' in hid_dev
@ -354,10 +362,7 @@ def get_indexed_string(device_handle, index):
if bus == '0003': # USB
usb_dev = dev.find_parent('usb', 'usb_device')
assert usb_dev
key = _DEVICE_STRINGS[index]
attrs = usb_dev.attributes
if key in attrs:
return attrs[key]
return usb_dev.attributes.get(key)
elif bus == '0005': # BLUETOOTH
# TODO