本文整理匯總了Python中pyudev.Device.from_device_number方法的典型用法代碼示例。如果您正苦於以下問題:Python Device.from_device_number方法的具體用法?Python Device.from_device_number怎麽用?Python Device.from_device_number使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyudev.Device
的用法示例。
在下文中一共展示了Device.from_device_number方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_indexed_string
# 需要導入模塊: from pyudev import Device [as 別名]
# 或者: from pyudev.Device import from_device_number [as 別名]
def get_indexed_string(device_handle, index):
"""Get a string from a HID device, based on its string index.
Note: currently not working in the ``hidraw`` native implementation.
:param device_handle: a device handle returned by open() or open_path().
:param index: the index of the string to get.
"""
if index not in _DEVICE_STRINGS:
return None
assert device_handle
stat = _os.fstat(device_handle)
dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev)
if dev:
hid_dev = dev.find_parent('hid')
if hid_dev:
assert 'HID_ID' in hid_dev
bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')
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]
elif bus == '0005': # BLUETOOTH
# TODO
pass
示例2: get_indexed_string
# 需要導入模塊: from pyudev import Device [as 別名]
# 或者: from pyudev.Device import from_device_number [as 別名]
def get_indexed_string(device_handle, index):
"""Get a string from a HID device, based on its string index.
Note: currently not working in the ``hidraw`` native implementation.
: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
"""
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)
except (DeviceNotFoundError, ValueError):
return None
hid_dev = dev.find_parent('hid')
if hid_dev:
assert 'HID_ID' in hid_dev
bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')
if bus == '0003': # USB
usb_dev = dev.find_parent('usb', 'usb_device')
assert usb_dev
return usb_dev.attributes.get(key)
elif bus == '0005': # BLUETOOTH
# TODO
pass
示例3: test_from_device_number
# 需要導入模塊: from pyudev import Device [as 別名]
# 或者: from pyudev.Device import from_device_number [as 別名]
def test_from_device_number(self, context, device_data):
if not device_data.device_node:
pytest.skip("no device node, no device number")
mode = os.stat(device_data.device_node).st_mode
type = "block" if stat.S_ISBLK(mode) else "char"
device = Device.from_device_number(context, type, device_data.device_number)
assert device.device_number == device_data.device_number
# make sure, we are really referring to the same device
assert device.device_path == device_data.device_path
示例4: test_from_device_number
# 需要導入模塊: from pyudev import Device [as 別名]
# 或者: from pyudev.Device import from_device_number [as 別名]
def test_from_device_number(self, context, device_path, device_number,
device_node):
if not device_node:
pytest.skip('no device node, no device number')
mode = os.stat(device_node).st_mode
type = 'block' if stat.S_ISBLK(mode) else 'char'
device = Device.from_device_number(context, type, device_number)
assert device.device_number == device_number
# make sure, we are really referring to the same device
assert device.device_path == device_path
示例5: test_from_device_number_wrong_type
# 需要導入模塊: from pyudev import Device [as 別名]
# 或者: from pyudev.Device import from_device_number [as 別名]
def test_from_device_number_wrong_type(self, context, device_data):
if not device_data.device_node:
pytest.skip("no device node, no device number")
mode = os.stat(device_data.device_node).st_mode
# deliberately use the wrong type here to cause either failure or at
# least device mismatch
type = "char" if stat.S_ISBLK(mode) else "block"
try:
# this either fails, in which case the caught exception is raised,
# or succeeds, but returns a wrong device (device numbers are not
# unique across device types)
device = Device.from_device_number(context, type, device_data.device_number)
# if it succeeds, the resulting device must not match the one, we
# are actually looking for!
assert device.device_path != device_data.device_path
except DeviceNotFoundByNumberError as error:
# check the correctness of the exception attributes
assert error.device_type == type
assert error.device_number == device_data.device_number
示例6: test_from_device_number_invalid_type
# 需要導入模塊: from pyudev import Device [as 別名]
# 或者: from pyudev.Device import from_device_number [as 別名]
def test_from_device_number_invalid_type(self, context):
with pytest.raises(ValueError) as exc_info:
Device.from_device_number(context, "foobar", 100)
assert str(exc_info.value) == ("Invalid type: {0!r}. Must be one of " '"char" or "block".'.format("foobar"))