comparison velib_python/tools/dbus_signal_cntr.py @ 8:9c0435a617db

Import velib_python
author Daniel O'Connor <darius@dons.net.au>
date Sun, 05 Dec 2021 14:35:36 +1030
parents
children
comparison
equal deleted inserted replaced
5:982eeffe9d95 8:9c0435a617db
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 from dbus.mainloop.glib import DBusGMainLoop
5 import gobject
6 import dbus
7 import dbus.service
8 from pprint import pprint
9 import os
10 import signal
11 from time import time
12
13 items = {}
14 total = 0
15 t_started = time()
16
17 class DbusTracker(object):
18 def __init__(self):
19
20 self.items = {}
21
22 # For a PC, connect to the SessionBus, otherwise (Venus device) connect to the systembus
23 self.dbusConn = dbus.SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else dbus.SystemBus()
24
25 # subscribe to all signals
26 self.dbusConn.add_signal_receiver(self._signal_receive_handler,
27 sender_keyword='sender',
28 path_keyword='path')
29
30 names = self.dbusConn.list_names()
31 for name in names:
32 if name.startswith(":"):
33 continue
34
35 items[str(self.dbusConn.get_name_owner(name))] = {"_total": 0, "_name": str(name)}
36
37
38 def _signal_receive_handler(*args, **kwargs):
39 global total
40 total = total + 1
41
42 sender = str(kwargs['sender'])
43 path = str(kwargs['path'])
44
45 d = items.get(sender)
46 if d is None:
47 items[sender] = {"_total": 1, path: 1}
48 return
49
50 d["_total"] = d["_total"] + 1
51
52 p = d.get(path)
53 if p is None:
54 d[path] = 1
55 return
56
57 d[path] = p + 1
58
59
60 def printall():
61 t_elapsed = time() - t_started
62
63 print(chr(27) + "[2J" + chr(27) + "[;H")
64
65 row_format = "{:<60} {:>4} {:>4}% {:>4.2f} / s"
66
67 print(row_format.format("Total", total, 100, total / t_elapsed))
68
69 for service, values in items.iteritems():
70 # skip the services that didn't emit any signals
71 if len(values) == 2 and "_name" in values:
72 continue
73
74 print(row_format.format(values.get("_name", service), values["_total"], values["_total"] * 100 / total, values["_total"] / t_elapsed))
75
76 # uncomment this to see all the paths as well.
77 # print("--------------")
78 # pprint(items)
79 return True
80
81
82 def main():
83 DBusGMainLoop(set_as_default=True)
84
85 d = DbusTracker()
86
87 gobject.timeout_add(2000, printall)
88
89 mainloop = gobject.MainLoop()
90 mainloop.run()
91
92
93 if __name__ == "__main__":
94 main()