From 3dc99ab5b2de1098d39a2dedf1efb0b056538d6c Mon Sep 17 00:00:00 2001 From: enkore Date: Mon, 25 Feb 2013 21:32:38 +0100 Subject: [PATCH] runwatch module It's exactly the same as i3status... I even copied the manpage! --- i3pystatus/runwatch.py | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 i3pystatus/runwatch.py diff --git a/i3pystatus/runwatch.py b/i3pystatus/runwatch.py new file mode 100644 index 0000000..91ff048 --- /dev/null +++ b/i3pystatus/runwatch.py @@ -0,0 +1,45 @@ +import glob +import os.path + +from i3pystatus import IntervalModule + +class RunWatch(IntervalModule): + """ + Expands the given path using glob to a pidfile and checks if the process ID found inside is valid + (that is, if the process is running). + You can use this to check if a specific application, such as a VPN client or your DHCP client is running. + + Available formatters are {pid} and {name}. + """ + + format_up = "{name}" + format_down = "{name}" + color_up = "#00FF00" + color_down = "#FF0000" + settings = ( + "format_up", "format_down", + "color_up", "color_down", + "path", "name", + ) + required = ("path", "name") + + @staticmethod + def is_process_alive(pid): + return os.path.exists("/proc/{pid}/".format(pid=pid)) + + def run(self): + with open(glob.glob(self.path)[0], "r") as f: + pid = int(f.read().strip()) + + if self.is_process_alive(pid): + fmt = self.format_up + color = self.color_up + else: + fmt = self.format_down + color = self.color_down + + self.output = { + "full_text": fmt.format(name=self.name, pid=pid), + "color": color, + "instance": self.name + }