From 8f7c929f38eae0d7d0b6547d45aa40df56dd0df9 Mon Sep 17 00:00:00 2001 From: stsdc <6031763+stsdc@users.noreply.github.com> Date: Sat, 9 Dec 2023 22:01:04 +0100 Subject: [PATCH] parse data from python --- flatpak/workaround/process_monitor.py | 39 +++++++++++++++----------- src/MainWindow.vala | 7 +++++ src/Services/DBusWorkaroundClient.vala | 39 ++++++++++++++++++++++++++ src/meson.build | 1 + 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 src/Services/DBusWorkaroundClient.vala diff --git a/flatpak/workaround/process_monitor.py b/flatpak/workaround/process_monitor.py index 49d19ade..02519e0c 100644 --- a/flatpak/workaround/process_monitor.py +++ b/flatpak/workaround/process_monitor.py @@ -5,7 +5,6 @@ import dbus.mainloop.glib from gi.repository import GLib - def get_pids(): # Define the path to the directory path = '/proc' @@ -27,26 +26,34 @@ def get_processes_stats(): class HelloWorld(dbus.service.Object): + def __init__(self, conn=None, object_path=None, bus_name=None): dbus.service.Object.__init__(self, conn, object_path, bus_name) - @dbus.service.method(dbus_interface="com.github.stsdc.monitor.workaround.GetProcesses", in_signature="s", out_signature="aas", sender_keyword="sender", connection_keyword="conn") - def get_processes(self, name, sender=None, conn=None): + @dbus.service.method(dbus_interface="com.github.stsdc.monitor.workaround.GetProcesses", in_signature="s", out_signature="aa{ss}", sender_keyword="sender", connection_keyword="conn") + def GetProcesses(self, name, sender=None, conn=None): + print("GetProcesses") processes = [] for pid in get_pids(): - process = [pid] + process = { + "pid": pid, + "cmdline": "", + "stat": "", + "statm": "", + "io": "" + } with open(f'/proc/{pid}/cmdline', 'rb') as file: - process.append(file.read().decode('utf-8', 'ignore').replace('\0', '')) - with open(f'/proc/{pid}/stat', 'rb') as file: - process.append(file.read().decode('utf-8', 'ignore').replace('\0', '')) - with open(f'/proc/{pid}/statm', 'rb') as file: - process.append(file.read().decode('utf-8', 'ignore').replace('\0', '')) - try: - with open(f'/proc/{pid}/io', 'r') as file: - process.append(file.read()) - except PermissionError as err: - # print(err) - pass + process["cmdline"] = (file.read().decode('utf-8', 'ignore').replace('\0', '')) + # with open(f'/proc/{pid}/stat', 'rb') as file: + # process.append(file.read().decode('utf-8', 'ignore').replace('\0', '')) + # with open(f'/proc/{pid}/statm', 'rb') as file: + # process.append(file.read().decode('utf-8', 'ignore').replace('\0', '')) + # try: + # with open(f'/proc/{pid}/io', 'r') as file: + # process.append(file.read()) + # except PermissionError as err: + # # print(err) + # pass processes.append(process) return processes @@ -54,7 +61,7 @@ def get_processes(self, name, sender=None, conn=None): dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) bus = dbus.SessionBus() name = dbus.service.BusName("com.github.stsdc.monitor.workaround", bus) - helloworld = HelloWorld(bus, "/processes") + helloworld = HelloWorld(bus, "/com/github/stsdc/monitor/workaround") mainloop = GLib.MainLoop() mainloop.run() diff --git a/src/MainWindow.vala b/src/MainWindow.vala index eba8a92b..77c7c0cb 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -69,6 +69,7 @@ public class Monitor.MainWindow : Hdy.ApplicationWindow { show_all (); dbusserver = DBusServer.get_default (); + var dbus_workaround_client = DBusWorkaroundClient.get_default(); headerbar.search_revealer.set_reveal_child (stack.visible_child_name == "process_view"); stack.notify["visible-child-name"].connect (() => { @@ -89,6 +90,12 @@ public class Monitor.MainWindow : Hdy.ApplicationWindow { var res = resources.serialize (); statusbar.update (res); dbusserver.update (res); + try { + HashTable[] p = dbus_workaround_client.interface.get_processes (""); + debug("%s", p[400]["cmdline"] ); + } catch (Error e) { + warning (e.message); + } return false; }); return true; diff --git a/src/Services/DBusWorkaroundClient.vala b/src/Services/DBusWorkaroundClient.vala new file mode 100644 index 00000000..bdf5ccff --- /dev/null +++ b/src/Services/DBusWorkaroundClient.vala @@ -0,0 +1,39 @@ +[DBus (name = "com.github.stsdc.monitor.workaround.GetProcesses")] +public interface Monitor.DBusWorkaroundClientInterface : Object { + public abstract HashTable[] get_processes (string empty) throws Error; + +} + +public class Monitor.DBusWorkaroundClient : Object { + public DBusWorkaroundClientInterface ? interface = null; + + private static GLib.Once instance; + public static unowned DBusWorkaroundClient get_default () { + return instance.once (() => { return new DBusWorkaroundClient (); }); + } + + public signal void monitor_vanished (); + public signal void monitor_appeared (); + + construct { + try { + interface = Bus.get_proxy_sync ( + BusType.SESSION, + "com.github.stsdc.monitor.workaround", + "/com/github/stsdc/monitor/workaround" + ); + + Bus.watch_name ( + BusType.SESSION, + "com.github.stsdc.monitor.workaround", + BusNameWatcherFlags.NONE, + () => monitor_appeared (), + () => monitor_vanished () + ); + } catch (IOError e) { + error ("Monitor Indicator DBus: %s\n", e.message); + } + + + } +} diff --git a/src/meson.build b/src/meson.build index cd68b508..8f85f0aa 100644 --- a/src/meson.build +++ b/src/meson.build @@ -63,6 +63,7 @@ source_app_files = [ # Services 'Services/Shortcuts.vala', 'Services/DBusServer.vala', + 'Services/DBusWorkaroundClient.vala', 'Services/Appearance.vala', # Resources