I want to give a /dev/DEVICE
path as input and get the device "human friendly" name as output.
I was having success getting the name from ID_MODEL_ENC
, like in this snippet:
def dev_name(dev_path):
from pyudev import Context
for device in Context().list_devices(DEVNAME=dev_path):
print device.get('ID_MODEL_ENC').decode('string_escape')
But it doesn't work with a bluetooth device. It seems that ID_MODEL_ENC
is not so widely used.
In my application I'll use it only for joysticks, then the device path will be always /dev/input/js*
.
Example 1: USB joystick is the js0
$ dev_name.py /dev/input/js0
Twin USB Joystick
Example 2: Bluetooth joystick is the js2
$ dev_name.py /dev/input/js2
Traceback (most recent call last):
File "pyudev_get_js_name.py", line 9, in <module>
dev_name(sys.argv[1])
File "pyudev_get_js_name.py", line 7, in dev_name
print '"'+ device.get('ID_MODEL_ENC').decode('string_escape') +'"'
AttributeError: 'NoneType' object has no attribute 'decode'
It obviously occurs because that device doesn't have the ID_MODEL_ENC
attribute.
Just to make sure that the system knows the device's name we can do this directly in the shell prompt:
$ sys_dev_path="$(udevadm info --name=/dev/input/js2 | grep DEVPATH | cut -d= -f2)"
$ cat "/sys$(dirname $sys_dev_path)/name"
8Bitdo NES30 GamePad
I know I can make something similar with python and check the contents of /sys/devices/.../name
file, but it looks like a makeshift. Is there a way to make pyudev give me the joystick name?
Note: I know it's pretty simple to get joystick names using pygame, but it's not an option here.
Thanks in advance.