本文整理汇总了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"))