]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/bus.py
Add graph info functionality
[collecty.git] / src / collecty / bus.py
index 6a3f0bd1b3b0ae907190aee84122f4a3b1d318e6..fe22fea157da491bd618f2256c4c80247321be9c 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 ###############################################################################
 #                                                                             #
 # collecty - A system statistics collection daemon for IPFire                 #
 import dbus
 import dbus.mainloop.glib
 import dbus.service
-import gobject
+import gi.repository.GLib
+import gi.repository.GObject
 import threading
 
-from constants import *
-from i18n import _
+from .constants import *
+from .i18n import _
 
 import logging
 log = logging.getLogger("collecty.bus")
 log.propagate = 1
 
-# Initialise the glib main loop
-dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-dbus.mainloop.glib.threads_init()
-
 class Bus(threading.Thread):
        def __init__(self, collecty):
                threading.Thread.__init__(self)
@@ -44,8 +41,11 @@ class Bus(threading.Thread):
                self.collecty = collecty
 
                # Initialise the main loop
-               gobject.threads_init()
-               self.loop = gobject.MainLoop()
+               gi.repository.GObject.threads_init()
+               dbus.mainloop.glib.threads_init()
+               dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+               self.loop = gi.repository.GLib.MainLoop()
 
                # Register the GraphGenerator interface
                self.generator = GraphGenerator(self.collecty)
@@ -54,7 +54,12 @@ class Bus(threading.Thread):
                log.debug(_("Bus thread has started"))
 
                # Run the main loop
-               self.loop.run()
+               try:
+                       self.loop.run()
+               except KeyboardInterrupt:
+                       self.collecty.shutdown()
+
+               log.debug(_("Bus thread has ended"))
 
        def shutdown(self):
                log.debug(_("Stopping bus thread"))
@@ -73,14 +78,25 @@ class GraphGenerator(dbus.service.Object):
 
                self.collecty = collecty
 
-       @dbus.service.method(BUS_DOMAIN, in_signature="sa{sv}", out_signature="ay")
+       @dbus.service.method(BUS_DOMAIN, in_signature="sa{sv}", out_signature="a{sv}")
        def GenerateGraph(self, template_name, kwargs):
                """
                        Returns a graph generated from the given template and object.
                """
                graph = self.collecty.generate_graph(template_name, **kwargs)
 
-               return dbus.ByteArray(graph or [])
+               # Convert the graph back to normal Python format
+               if graph:
+                       graph["image"] = dbus.ByteArray(graph["image"] or [])
+
+               return graph
+
+       @dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="a{sv}")
+       def GraphInfo(self, template_name, kwargs):
+               """
+                       Returns a dictionary with information about the graph.
+               """
+               return self.collecty.graph_info(template_name, **kwargs)
 
        @dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="as")
        def ListTemplates(self):
@@ -88,3 +104,7 @@ class GraphGenerator(dbus.service.Object):
                        Returns a list of all available templates
                """
                return [t.name for t in self.collecty.templates]
+
+       @dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="s")
+       def Version(self):
+               return COLLECTY_VERSION