From: "Mantas Mikulėnas" <grawity@gmail.com>
To: linux-usb@vger.kernel.org
Cc: gregkh@linuxfoundation.org, "Mantas Mikulėnas" <grawity@gmail.com>
Subject: [PATCH 30/34] lsusb.py: convert readattr() and readlink() to methods of the container
Date: Mon, 6 May 2019 12:02:37 +0300 [thread overview]
Message-ID: <20190506090241.169665-31-grawity@gmail.com> (raw)
In-Reply-To: <20190506090241.169665-1-grawity@gmail.com>
Signed-off-by: Mantas Mikulėnas <grawity@gmail.com>
---
lsusb.py.in | 69 +++++++++++++++++++++++++----------------------------
1 file changed, 33 insertions(+), 36 deletions(-)
diff --git a/lsusb.py.in b/lsusb.py.in
index 361615d..a9559ce 100644
--- a/lsusb.py.in
+++ b/lsusb.py.in
@@ -46,15 +46,6 @@ usbvendors = {}
usbproducts = {}
usbclasses = {}
-def readattr(path, name):
- "Read attribute from sysfs and return as string"
- f = open(prefix + path + "/" + name);
- return f.readline().rstrip("\n");
-
-def readlink(path, name):
- "Read symlink and return basename"
- return os.path.basename(os.readlink(prefix + path + "/" + name));
-
def ishexdigit(str):
"return True if all digits are valid hex digits"
for dg in str:
@@ -231,7 +222,13 @@ def find_dev(driver, usbname):
class UsbObject:
- pass
+ def read_attr(self, name):
+ path = prefix + self.path + "/" + name
+ return open(path).readline().rstrip("\n")
+
+ def read_link(self, name):
+ path = prefix + self.path + "/" + name
+ return os.path.basename(os.readlink(path))
class UsbEndpoint(UsbObject):
"Container for USB endpoint info"
@@ -252,14 +249,14 @@ class UsbEndpoint(UsbObject):
def read(self, fname):
self.fname = fname
self.path = self.parent.path + "/" + fname
- self.epaddr = int(readattr(self.path, "bEndpointAddress"), 16)
- ival = int(readattr(self.path, "bInterval"), 16)
+ self.epaddr = int(self.read_attr("bEndpointAddress"), 16)
+ ival = int(self.read_attr("bInterval"), 16)
if ival:
- self.ival = "(%s)" % readattr(self.path, "interval")
- self.len = int(readattr(self.path, "bLength"), 16)
- self.type = readattr(self.path, "type")
- self.attr = int(readattr(self.path, "bmAttributes"), 16)
- self.max = int(readattr(self.path, "wMaxPacketSize"), 16)
+ self.ival = "(%s)" % self.read_attr("interval")
+ self.len = int(self.read_attr("bLength"), 16)
+ self.type = self.read_attr("type")
+ self.attr = int(self.read_attr("bmAttributes"), 16)
+ self.max = int(self.read_attr("wMaxPacketSize"), 16)
def __repr__(self):
return "<UsbEndpoint[%r]>" % self.fname
@@ -292,12 +289,12 @@ class UsbInterface(UsbObject):
def read(self, fname):
self.fname = fname
self.path = self.parent.path + "/" + fname
- self.iclass = int(readattr(self.path, "bInterfaceClass"),16)
- self.isclass = int(readattr(self.path, "bInterfaceSubClass"),16)
- self.iproto = int(readattr(self.path, "bInterfaceProtocol"),16)
- self.noep = int(readattr(self.path, "bNumEndpoints"))
+ self.iclass = int(self.read_attr("bInterfaceClass"),16)
+ self.isclass = int(self.read_attr("bInterfaceSubClass"),16)
+ self.iproto = int(self.read_attr("bInterfaceProtocol"),16)
+ self.noep = int(self.read_attr("bNumEndpoints"))
try:
- self.driver = readlink(self.path, "driver")
+ self.driver = self.read_link("driver")
self.devname = find_dev(self.driver, self.path)
except:
pass
@@ -353,14 +350,14 @@ class UsbDevice(UsbObject):
def read(self, fname):
self.fname = fname
self.path = fname
- self.iclass = int(readattr(self.path, "bDeviceClass"), 16)
- self.isclass = int(readattr(self.path, "bDeviceSubClass"), 16)
- self.iproto = int(readattr(self.path, "bDeviceProtocol"), 16)
- self.vid = int(readattr(self.path, "idVendor"), 16)
- self.pid = int(readattr(self.path, "idProduct"), 16)
+ self.iclass = int(self.read_attr("bDeviceClass"), 16)
+ self.isclass = int(self.read_attr("bDeviceSubClass"), 16)
+ self.iproto = int(self.read_attr("bDeviceProtocol"), 16)
+ self.vid = int(self.read_attr("idVendor"), 16)
+ self.pid = int(self.read_attr("idProduct"), 16)
try:
- self.name = readattr(self.path, "manufacturer") + " " \
- + readattr(self.path, "product")
+ self.name = self.read_attr("manufacturer") + " " \
+ + self.read_attr("product")
except:
pass
if self.name:
@@ -376,22 +373,22 @@ class UsbDevice(UsbObject):
if not self.name:
self.name = oldnm
try:
- ser = readattr(self.path, "serial")
+ ser = self.read_attr("serial")
# Some USB devs report "serial" as serial no. suppress
if (ser and ser != "serial"):
self.name += " " + ser
except:
pass
- self.usbver = readattr(self.path, "version")
- self.speed = readattr(self.path, "speed")
- self.maxpower = readattr(self.path, "bMaxPower")
- self.noports = int(readattr(self.path, "maxchild"))
+ self.usbver = self.read_attr("version")
+ self.speed = self.read_attr("speed")
+ self.maxpower = self.read_attr("bMaxPower")
+ self.noports = int(self.read_attr("maxchild"))
try:
- self.nointerfaces = int(readattr(self.path, "bNumInterfaces"))
+ self.nointerfaces = int(self.read_attr("bNumInterfaces"))
except:
self.nointerfaces = 0
try:
- self.driver = readlink(self.path, "driver")
+ self.driver = self.read_link("driver")
self.devname = find_dev(self.driver, self.path)
except:
pass
--
2.21.0
next prev parent reply other threads:[~2019-05-06 9:04 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-06 9:02 usbutils - various patches to the lsusb.py script Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 01/34] lsusb.py: sort devices and interfaces numerically Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 02/34] lsusb.py: sort toplevel entries Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 03/34] lsusb.py: improve usage text Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 04/34] lsusb.py: replace fake deepcopy() Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 05/34] lsusb.py: remove -w (warn if usb.ids not sorted) option Mantas Mikulėnas
2019-05-06 11:12 ` Greg KH
2019-05-06 11:21 ` Mantas Mikulėnas
2019-05-06 12:19 ` Greg KH
2019-05-06 9:02 ` [PATCH 06/34] lsusb.py: ensure all error messages are written to stderr Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 07/34] lsusb.py: support long options Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 08/34] lsusb.py: do not entirely hide usb.ids exceptions Mantas Mikulėnas
2019-05-06 12:29 ` Greg KH
2019-05-06 12:47 ` Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 09/34] lsusb.py: use regular print() instead of hand-rolling the same thing Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 10/34] lsusb.py: avoid shadowing Python's built-in 'str' Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 11/34] lsusb.py: replace usb.ids binary search with dict lookup Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 12/34] lsusb.py: remove now-unused bin_search() Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 13/34] lsusb.py: avoid manual calls to __foo__() Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 14/34] lsusb.py: replace __repr__() for USB IDs with __str__() Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 15/34] lsusb.py: insert class FF:FF:FF into usbclasses to avoid special casing Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 16/34] lsusb.py: entirely remove Usb* classes Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 17/34] lsusb.py: cosmetic - replace tuples-as-"immutable lists" with regular lists Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 18/34] lsusb.py: use 'elif' where suitable Mantas Mikulėnas
2019-05-06 12:22 ` Greg KH
2019-05-06 9:02 ` [PATCH 19/34] lsusb.py: remove dead code Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 20/34] lsusb.py: move unrelated code out of try..except Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 21/34] lsusb.py: allow - as well as _ when matching hci module names Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 22/34] lsusb.py: use a constant for the magic class number 9 Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 23/34] lsusb.py: Usb* classes: call read() automatically from constructor Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 24/34] lsusb.py: UsbEndpoint: indent is a class implementation detail Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 25/34] lsusb.py: a few cosmetic changes Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 26/34] lsusb.py: shorten find_usb_class() Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 27/34] lsusb.py: give all Usb* objects a .path attribute Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 28/34] lsusb.py: add an actual __repr__() to classes Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 29/34] lsusb.py: give all Usb* classes a superclass Mantas Mikulėnas
2019-05-06 9:02 ` Mantas Mikulėnas [this message]
2019-05-06 9:02 ` [PATCH 31/34] lsusb.py: use color by default Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 32/34] lsusb.py: rework output for more consistent indent of both columns Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 33/34] lsusb.py: fix endpoint interval spacing Mantas Mikulėnas
2019-05-06 9:02 ` [PATCH 34/34] lsusb.py: visually group USB-version-related fields Mantas Mikulėnas
2019-05-06 12:32 ` usbutils - various patches to the lsusb.py script Greg KH
2019-05-06 12:57 ` Mantas Mikulėnas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190506090241.169665-31-grawity@gmail.com \
--to=grawity@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-usb@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).