Skip to content

Commit edef5b9

Browse files
committed
python: iio_info.py rewrite.
I have rewritten the iio_info.py module in a similar fashion as the iio_readdev, iio_writedev and iio_attr modules. Signed-off-by: Cristi Iacob <[email protected]>
1 parent 3df16ee commit edef5b9

File tree

1 file changed

+88
-59
lines changed

1 file changed

+88
-59
lines changed

bindings/python/examples/iio_info.py

Lines changed: 88 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,82 +18,111 @@
1818
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
"""
2020

21-
from sys import argv
21+
import sys
2222
import iio
2323

2424

25-
def main():
26-
"""Dump iio devices, list all iio attributes."""
27-
print("Library version: %u.%u (git tag: %s)" % iio.version)
28-
29-
if len(argv) == 3 and argv[1] == "--uri":
30-
uri = argv[2]
25+
def _create_context():
26+
if len(sys.argv) == 3 and sys.argv[1] == "--uri":
27+
uri = sys.argv[2]
3128
else:
3229
contexts = iio.scan_contexts()
3330
if len(contexts) > 1:
3431
print("Multiple contexts found. Please select one using --uri:")
35-
for index, each in enumerate(contexts):
36-
print("\t%d: %s [%s]" % (index, contexts[each], each))
37-
return
32+
for uri, description in contexts.items():
33+
print("\t%s: %s" % (uri, description))
34+
sys.exit(0)
35+
36+
return iio.Context(uri)
37+
38+
39+
class Information:
40+
"""Class for retrieving the requested information."""
3841

39-
uri = next(iter(contexts), None)
42+
def __init__(self, context):
43+
"""
44+
Class constructor.
4045
41-
ctx = iio.Context(uri)
46+
Args:
47+
context: type=iio.Context
48+
Context used for retrieving the information.
49+
"""
50+
self.context = context
4251

43-
if uri is not None:
44-
print('Using auto-detected IIO context at URI "%s"' % uri)
52+
def write_information(self):
53+
"""Write the information about the current context."""
54+
self._context_info()
4555

46-
print("IIO context created: " + ctx.name)
47-
print("Backend version: %u.%u (git tag: %s)" % ctx.version)
48-
print("Backend description string: " + ctx.description)
56+
def _context_info(self):
57+
print("IIO context created: " + self.context.name)
58+
print("Backend version: %u.%u (git tag: %s" % self.context.version)
59+
print("Backend description string: " + self.context.description)
4960

50-
if len(ctx.attrs) > 0:
51-
print("IIO context has %u attributes:" % len(ctx.attrs))
52-
for attr, value in ctx.attrs.items():
53-
print("\t" + attr + ": " + value)
61+
if len(self.context.attrs) > 0:
62+
print("IIO context has %u attributes: " % len(self.context.attrs))
5463

55-
print("IIO context has %u devices:" % len(ctx.devices))
64+
for attr, value in self.context.attrs.items():
65+
print("\t" + attr + ": " + value)
5666

57-
for dev in ctx.devices:
67+
print("IIO context has %u devices:" % len(self.context.devices))
68+
69+
for dev in self.context.devices:
70+
self._device_info(dev)
71+
72+
def _device_info(self, dev):
5873
print("\t" + dev.id + ": " + dev.name)
5974

6075
if dev is iio.Trigger:
61-
print("Found trigger! Rate: %u Hz" % dev.frequency)
62-
63-
print("\t\t%u channels found:" % len(dev.channels))
64-
65-
for chn in dev.channels:
66-
print(
67-
"\t\t\t%s: %s (%s)"
68-
% (chn.id, chn.name or "", "output" if chn.output else "input")
69-
)
70-
71-
if len(chn.attrs) != 0:
72-
print("\t\t\t%u channel-specific attributes found:" % len(chn.attrs))
73-
74-
for attr in chn.attrs:
75-
try:
76-
print("\t\t\t\t" + attr + ", value: " + chn.attrs[attr].value)
77-
except OSError as err:
78-
print("Unable to read " + attr + ": " + err.strerror)
79-
80-
if len(dev.attrs) != 0:
81-
print("\t\t%u device-specific attributes found:" % len(dev.attrs))
82-
83-
for attr in dev.attrs:
84-
try:
85-
print("\t\t\t" + attr + ", value: " + dev.attrs[attr].value)
86-
except OSError as err:
87-
print("Unable to read " + attr + ": " + err.strerror)
88-
89-
if len(dev.debug_attrs) != 0:
90-
print("\t\t%u debug attributes found:" % len(dev.debug_attrs))
91-
92-
for attr in dev.debug_attrs:
93-
try:
94-
print("\t\t\t" + attr + ", value: " + dev.debug_attrs[attr].value)
95-
except OSError as err:
96-
print("Unable to read " + attr + ": " + err.strerror)
76+
print("Found trigger! Rate: %u HZ" % dev.frequency)
77+
78+
print("\t\t%u channels found: " % len(dev.channels))
79+
for channel in dev.channels:
80+
self._channel_info(channel)
81+
82+
if len(dev.attrs) > 0:
83+
print("\t\t%u device-specific attributes found: " % len(dev.attrs))
84+
for device_attr in dev.attrs:
85+
self._device_attribute_info(dev, device_attr)
86+
87+
if len(dev.debug_attrs) > 0:
88+
print("\t\t%u debug attributes found: " % len(dev.debug_attrs))
89+
for debug_attr in dev.debug_attrs:
90+
self._device_debug_attribute_info(dev, debug_attr)
91+
92+
def _channel_info(self, channel):
93+
print("\t\t\t%s: %s (%s)" % (channel.id, channel.name or "", "output" if channel.output else "input"))
94+
if len(channel.attrs) > 0:
95+
print("\t\t\t%u channel-specific attributes found: " % len(channel.attrs))
96+
for channel_attr in channel.attrs:
97+
self._channel_attribute_info(channel, channel_attr)
98+
99+
@staticmethod
100+
def _channel_attribute_info(channel, channel_attr):
101+
try:
102+
print("\t\t\t\t" + channel_attr + ", value: " + channel.attrs[channel_attr].value)
103+
except OSError as err:
104+
print("Unable to read " + channel_attr + ": " + err.strerror)
105+
106+
@staticmethod
107+
def _device_attribute_info(dev, device_attr):
108+
try:
109+
print("\t\t\t" + device_attr + ", value: " + dev.attrs[device_attr].value)
110+
except OSError as err:
111+
print("Unable to read " + device_attr + ": " + err.strerror)
112+
113+
@staticmethod
114+
def _device_debug_attribute_info(dev, debug_attr):
115+
try:
116+
print("\t\t\t" + debug_attr + ", value: " + dev.debug_attrs[debug_attr].value)
117+
except OSError as err:
118+
print("Unable to read " + debug_attr + ": " + err.strerror)
119+
120+
121+
def main():
122+
"""Module's main method."""
123+
context = _create_context()
124+
information = Information(context)
125+
information.write_information()
97126

98127

99128
if __name__ == "__main__":

0 commit comments

Comments
 (0)