[virt-tools-list] [PATCH] nodedev: Be more descriptive about USB device identification

Lubomir Rintel lkundrak at v3.sk
Tue Sep 9 12:22:38 UTC 2014


libvirt often passes devices with vendor and product information missing or
malformed, making the USB sharing dialog ugly or useless to the point it's
impossible to identify the device.

This aims to fix pretty-printing of weird textural identification and fall
back to numeric product and device id (in case textual represetations are
not present in the descriptor and device is too new to be in device
database).
---
Before: http://v3.sk/~lkundrak/virt/old.png
After: http://v3.sk/~lkundrak/virt/virtmanager.png

 virtinst/nodedev.py | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/virtinst/nodedev.py b/virtinst/nodedev.py
index 17524dc..2d6405d 100644
--- a/virtinst/nodedev.py
+++ b/virtinst/nodedev.py
@@ -187,9 +187,36 @@ class USBDevice(NodeDevice):
     vendor_id = XMLProperty("./capability/vendor/@id")
 
     def pretty_name(self):
-        devstr = "%.3d:%.3d" % (int(self.bus), int(self.device))
-        desc = "%s %s %s" % (devstr, str(self.vendor_name),
-                             str(self.product_name))
+        # Hypervisor may return a rather sparse structure, missing
+        # some ol all stringular descriptions of the device altogether.
+        # Do our best to help user identify the device.
+
+        # Certain devices pad their vendor with trailing spaces,
+        # such as "LENOVO       ". It does not look well.
+        product = str(self.product_name).strip()
+        vendor = str(self.vendor_name).strip()
+
+        if product == "":
+            product = str(self.product_id)
+            if vendor == "":
+                # No stringular descriptions altogether
+                vendor = str(self.vendor_id)
+                devstr = "%s:%s" % (vendor, product)
+            else:
+                # Only the vendor is known
+                devstr = "%s %s" % (vendor, product)
+        else:
+            if vendor == "":
+                # Sometimes vendor is left out empty, but product is
+                # already descriptive enough or contains the vendor string:
+                # "Lenovo USB Laser Mouse"
+                devstr = product
+            else:
+                # We know everything. Perfect.
+                devstr = "%s %s" % (vendor, product)
+
+        busstr = "%.3d:%.3d" % (int(self.bus), int(self.device))
+        desc = "%s %s" % (busstr, devstr)
         return desc
 
 
-- 
1.9.3




More information about the virt-tools-list mailing list