File: iio_info.py

package info (click to toggle)
libiio 0.26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,832 kB
  • sloc: ansic: 21,880; python: 1,844; cs: 1,232; sh: 1,062; cpp: 688; yacc: 441; xml: 192; lex: 172; makefile: 40
file content (129 lines) | stat: -rwxr-xr-x 4,472 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
"""
Copyright (C) 2015 Analog Devices, Inc.
Author: Paul Cercueil <paul.cercueil@analog.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""

import sys
import iio


def _create_context():
    if len(sys.argv) == 3 and sys.argv[1] == "--uri":
        uri = sys.argv[2]
    else:
        contexts = iio.scan_contexts()
        if len(contexts) > 1:
            print("Multiple contexts found. Please select one using --uri:")
            for uri, description in contexts.items():
                print("\t%s: %s" % (uri, description))
        sys.exit(0)

    return iio.Context(uri)


class Information:
    """Class for retrieving the requested information."""

    def __init__(self, context):
        """
        Class constructor.

        Args:
            context: type=iio.Context
                Context used for retrieving the information.
        """
        self.context = context

    def write_information(self):
        """Write the information about the current context."""
        self._context_info()

    def _context_info(self):
        print("IIO context created: " + self.context.name)
        print("Backend version: %u.%u (git tag: %s" % self.context.version)
        print("Backend description string: " + self.context.description)

        if len(self.context.attrs) > 0:
            print("IIO context has %u attributes: " % len(self.context.attrs))

        for attr, value in self.context.attrs.items():
            print("\t" + attr + ": " + value)

        print("IIO context has %u devices:" % len(self.context.devices))

        for dev in self.context.devices:
            self._device_info(dev)

    def _device_info(self, dev):
        print("\t" + dev.id + ": " + dev.name)

        if dev is iio.Trigger:
            print("Found trigger! Rate: %u HZ" % dev.frequency)

        print("\t\t%u channels found: " % len(dev.channels))
        for channel in dev.channels:
            self._channel_info(channel)

        if len(dev.attrs) > 0:
            print("\t\t%u device-specific attributes found: " % len(dev.attrs))
            for device_attr in dev.attrs:
                self._device_attribute_info(dev, device_attr)

        if len(dev.debug_attrs) > 0:
            print("\t\t%u debug attributes found: " % len(dev.debug_attrs))
            for debug_attr in dev.debug_attrs:
                self._device_debug_attribute_info(dev, debug_attr)

    def _channel_info(self, channel):
        print("\t\t\t%s: %s (%s)" % (channel.id, channel.name or "", "output" if channel.output else "input"))
        if len(channel.attrs) > 0:
            print("\t\t\t%u channel-specific attributes found: " % len(channel.attrs))
            for channel_attr in channel.attrs:
                self._channel_attribute_info(channel, channel_attr)

    @staticmethod
    def _channel_attribute_info(channel, channel_attr):
        try:
            print("\t\t\t\t" + channel_attr + ", value: " + channel.attrs[channel_attr].value)
        except OSError as err:
            print("Unable to read " + channel_attr + ": " + err.strerror)

    @staticmethod
    def _device_attribute_info(dev, device_attr):
        try:
            print("\t\t\t" + device_attr + ", value: " + dev.attrs[device_attr].value)
        except OSError as err:
            print("Unable to read " + device_attr + ": " + err.strerror)

    @staticmethod
    def _device_debug_attribute_info(dev, debug_attr):
        try:
            print("\t\t\t" + debug_attr + ", value: " + dev.debug_attrs[debug_attr].value)
        except OSError as err:
            print("Unable to read " + debug_attr + ": " + err.strerror)


def main():
    """Module's main method."""
    context = _create_context()
    information = Information(context)
    information.write_information()


if __name__ == "__main__":
    main()