summaryrefslogtreecommitdiff
path: root/src/dfeet/introspection_helper.py
blob: b718c1a622ad5f0e09c473196556eb58072a8918 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-

from gi.repository import GObject, Gio, Gtk
from dfeet import dbus_utils

_style_context = None
_fg_color = "#000000"


def fg_color():
    global _style_context, _fg_color
    if _style_context is None:
        _style_context = Gtk.StyleContext()
        color = _style_context.get_color(Gtk.StateFlags.NORMAL)
        _fg_color = "#%02x%02x%02x" % (
            int(color.red) * 255,
            int(color.green) * 255,
            int(color.blue) * 255,
        )
    return _fg_color


def args_signature_markup(arg_signature):
    return '<small><span foreground="#2E8B57">%s</span></small>' % (arg_signature)


def args_name_markup(arg_name):
    return '<small><span foreground="%s">%s</span></small>' % (fg_color(), arg_name)


class DBusNode(GObject.GObject):
    """object to represent a DBus Node (object path)"""
    def __init__(self, name, object_path, node_info):
        GObject.GObject.__init__(self)
        self.__name = name
        self.__object_path = object_path
        self.__node_info = node_info  # Gio.GDBusNodeInfo object

    def __repr__(self):
        return "Name: %s ; ObjPath: %s ; NodeInfo: %s" % (
            self.name, self.object_path, self.node_info)

    @property
    def name(self):
        return self.__name

    @property
    def object_path(self):
        return self.__object_path

    @property
    def node_info(self):
        return self.__node_info


class DBusInterface(DBusNode):
    """object to represent a DBus Interface"""
    def __init__(self, dbus_node_obj, iface_info):
        DBusNode.__init__(self, dbus_node_obj.name,
                          dbus_node_obj.object_path, dbus_node_obj.node_info)
        self.__iface_info = iface_info  # Gio.GDBusInterfaceInfo object

    def __repr__(self):
        return "iface '%s' on node '%s'" % (self.iface_info.name, self.node_info.path)

    @property
    def iface_info(self):
        return self.__iface_info


class DBusProperty(DBusInterface):
    """object to represent a DBus Property"""
    def __init__(self, dbus_iface_obj, property_info):
        DBusInterface.__init__(self, dbus_iface_obj, dbus_iface_obj.iface_info)
        self.__property_info = property_info  # Gio.GDBusPropertyInfo object
        self.__value = None  # the value

    def __repr__(self):
        sig = dbus_utils.sig_to_string(self.property_info.signature)
        return "%s %s (%s)" % (sig, self.property_info.name, self.property_info.flags)

    @property
    def property_info(self):
        return self.__property_info

    @property
    def value(self):
        return self.__value

    @value.setter
    def value(self, new_val):
        self.__value = new_val

    @property
    def markup_str(self):
        sig = dbus_utils.sig_to_string(self.property_info.signature)
        readwrite = list()
        if self.readable:
            readwrite.append("read")
        if self.writable:
            readwrite.append("write")
        s = "%s %s <small>(%s)</small>" % (
            args_signature_markup(sig),
            args_name_markup(self.property_info.name), " / ".join(readwrite))
        if self.value is not None:
            s += " = %s" % (self.value,)
        return s

    @property
    def readable(self):
        if int(self.property_info.flags) == int(Gio.DBusPropertyInfoFlags.READABLE) or \
                int(self.property_info.flags) == \
                (int(Gio.DBusPropertyInfoFlags.WRITABLE | Gio.DBusPropertyInfoFlags.READABLE)):
            return True
        else:
            return False

    @property
    def writable(self):
        if int(self.property_info.flags) == int(Gio.DBusPropertyInfoFlags.WRITABLE) or \
                int(self.property_info.flags) == \
                (int(Gio.DBusPropertyInfoFlags.WRITABLE | Gio.DBusPropertyInfoFlags.READABLE)):
            return True
        else:
            return False


class DBusSignal(DBusInterface):
    """object to represent a DBus Signal"""
    def __init__(self, dbus_iface_obj, signal_info):
        DBusInterface.__init__(self, dbus_iface_obj,
                               dbus_iface_obj.iface_info)
        self.__signal_info = signal_info  # Gio.GDBusSignalInfo object

    def __repr__(self):
        return "%s" % (self.signal_info.name)

    @property
    def signal_info(self):
        return self.__signal_info

    @property
    def args(self):
        args = list()
        for arg in self.signal_info.args:
            sig = dbus_utils.sig_to_string(arg.signature)
            args.append({'signature': sig, 'name': arg.name})
        return args

    @property
    def args_markup_str(self):
        result = ''
        result += '<span foreground="#FF00FF">(</span>'
        result += ', '.join('%s' % (args_signature_markup(arg['signature'])) for arg in self.args)
        result += '<span foreground="#FF00FF">)</span>'
        return result

    @property
    def markup_str(self):
        return "%s %s" % (self.signal_info.name, self.args_markup_str)


class DBusMethod(DBusInterface):
    """object to represent a DBus Method"""
    def __init__(self, dbus_iface_obj, method_info):
        DBusInterface.__init__(self, dbus_iface_obj, dbus_iface_obj.iface_info)
        self.__method_info = method_info  # Gio.GDBusMethodInfo object

    def __repr__(self):
        return "%s(%s) ↦ %s (%s)" % (
            self.method_info.name, self.in_args_str,
            self.out_args_str, DBusInterface.__repr__(self))

    @property
    def in_args_code(self):
        in_args = ""
        for a in self.__method_info.in_args:
            in_args += a.signature
        return in_args

    @property
    def method_info(self):
        return self.__method_info

    @property
    def markup_str(self):
        return "%s %s <b>↦</b> %s" % (
            self.method_info.name, self.in_args_markup_str, self.out_args_markup_str)

    @property
    def in_args(self):
        in_args = list()
        for in_arg in self.method_info.in_args:
            sig = dbus_utils.sig_to_string(in_arg.signature)
            in_args.append({'signature': sig, 'name': in_arg.name})
        return in_args

    @property
    def out_args(self):
        out_args = list()
        for out_arg in self.method_info.out_args:
            sig = dbus_utils.sig_to_string(out_arg.signature)
            out_args.append({'signature': sig, 'name': out_arg.name})
        return out_args

    @property
    def in_args_str(self):
        result = ""
        for arg in self.in_args:
            result += "%s %s, " % (arg['signature'], arg['name'])

        return result[0:-2]

    @property
    def out_args_str(self):
        result = ""
        for arg in self.out_args:
            result += "%s %s, " % (arg['signature'], arg['name'])

        return result[0:-2]

    def __args_markup_str(self, args):
        """markup a given list of args"""
        result = ''
        result += '<span foreground="#FF00FF">(</span>'
        result += ', '.join(
            '%s %s' % (
                args_signature_markup(arg['signature']),
                args_name_markup(arg['name'])) for arg in args)
        result += '<span foreground="#FF00FF">)</span>'
        return result

    @property
    def in_args_markup_str(self):
        return self.__args_markup_str(self.in_args)

    @property
    def out_args_markup_str(self):
        return self.__args_markup_str(self.out_args)


class DBusAnnotation(DBusInterface):
    """object to represent a DBus Annotation"""
    def __init__(self, dbus_iface_obj, annotation_info):
        DBusInterface.__init__(self, dbus_iface_obj,
                               dbus_iface_obj.iface_info)
        self.__annotation_info = annotation_info  # Gio.GDBusAnnotationInfo object

    def __repr__(self):
        return "%s: %s" % (self.annotation_info.key, self.annotation_info.value)

    @property
    def annotation_info(self):
        return self.__annotation_info

    @property
    def markup_str(self):
        return "%s: %s" % (self.annotation_info.key, self.annotation_info.value)