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 import errno as _errno
from select import select as _select from select import select as _select
from pyudev import Context as _Context, Monitor as _Monitor, Device as _Device from pyudev import Context as _Context, Monitor as _Monitor, Device as _Device
from pyudev import DeviceNotFoundError
native_implementation = 'udev' native_implementation = 'udev'
@ -338,27 +339,31 @@ def get_indexed_string(device_handle, index):
:param device_handle: a device handle returned by open() or open_path(). :param device_handle: a device handle returned by open() or open_path().
:param index: the index of the string to get. :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:
return None key = _DEVICE_STRINGS[index]
except KeyError:
return None
assert device_handle assert device_handle
stat = _os.fstat(device_handle) stat = _os.fstat(device_handle)
dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev) try:
if dev: dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev)
hid_dev = dev.find_parent('hid') except (DeviceNotFoundError, ValueError):
if hid_dev: return None
assert 'HID_ID' in hid_dev
bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')
if bus == '0003': # USB hid_dev = dev.find_parent('hid')
usb_dev = dev.find_parent('usb', 'usb_device') if hid_dev:
assert usb_dev assert 'HID_ID' in hid_dev
key = _DEVICE_STRINGS[index] bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')
attrs = usb_dev.attributes
if key in attrs:
return attrs[key]
elif bus == '0005': # BLUETOOTH if bus == '0003': # USB
# TODO usb_dev = dev.find_parent('usb', 'usb_device')
pass assert usb_dev
return usb_dev.attributes.get(key)
elif bus == '0005': # BLUETOOTH
# TODO
pass