summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVon Random <von@vdrandom.org>2016-11-19 18:44:40 +0300
committerVon Random <von@vdrandom.org>2016-11-19 18:44:40 +0300
commitbb9658d0536e50871e4a7ba852eec10ef82de4f3 (patch)
treedb355723df417b55019c537feee3a2aad7cae10c
parent240fff91ee62909e10b213d58f6c9b358b91b778 (diff)
yay, found out about Thread.daemon lol, also more oop and less code duplication
-rw-r--r--plugins/batt.py25
-rw-r--r--plugins/common.py25
-rw-r--r--plugins/date.py24
-rw-r--r--plugins/disk.py26
-rw-r--r--plugins/load.py27
-rw-r--r--plugins/mem.py25
-rw-r--r--plugins/ping.py30
-rwxr-xr-xvdstatus27
8 files changed, 56 insertions, 153 deletions
diff --git a/plugins/batt.py b/plugins/batt.py
index f579486..861359c 100644
--- a/plugins/batt.py
+++ b/plugins/batt.py
@@ -1,20 +1,12 @@
-import threading
-import time
+import plugins.common
BATTERY_DIR = '/sys/class/power_supply/BAT0/'
-class PluginThread(threading.Thread):
+class PluginThread(plugins.common.PluginThreadCommon):
def __init__(self, section, config, thread_id):
- threading.Thread.__init__(self)
- self.threadID = thread_id
- self.status = dict()
- if config.has_option(section, 'color'):
- self.status['color'] = config.get(section, 'color')
- self.freq = config.getint(section, 'freq', fallback=1)
- self.hide = False
- self.should_stop = False
+ super(PluginThread, self).__init__(section, config)
def main(self):
with open(BATTERY_DIR + 'capacity', 'r') as capacity, \
@@ -34,14 +26,3 @@ class PluginThread(threading.Thread):
batt = 'BAT: ' + batt_capacity + '% ' + batt_stat
self.status['full_text'] = batt
-
- def stop(self):
- self.should_stop = True
-
- def run(self):
- while True:
- if self.should_stop is False:
- self.main()
- time.sleep(self.freq)
- else:
- break
diff --git a/plugins/common.py b/plugins/common.py
new file mode 100644
index 0000000..b747e38
--- /dev/null
+++ b/plugins/common.py
@@ -0,0 +1,25 @@
+import threading
+import time
+
+
+class PluginThreadCommon:
+ def __init__(self, section, config):
+ self.status = dict()
+ self.hide = False
+ self.thread = threading.Thread(target=self.run)
+ self.thread.daemon = True
+ self.freq = config.getint(section, 'freq', fallback=1)
+ self.problem_value = config.getint(section, 'problem', fallback=70)
+ if config.has_option(section, 'color'):
+ self.status['color'] = config.get(section, 'color')
+
+ def start(self):
+ self.thread.start()
+
+ def main(self):
+ self.status['full_text'] = 'placeholder'
+
+ def run(self):
+ while True:
+ self.main()
+ time.sleep(self.freq)
diff --git a/plugins/date.py b/plugins/date.py
index 37f7c67..18ae91f 100644
--- a/plugins/date.py
+++ b/plugins/date.py
@@ -1,29 +1,11 @@
import time
-import threading
+import plugins.common
-class PluginThread(threading.Thread):
+class PluginThread(plugins.common.PluginThreadCommon):
def __init__(self, section, config, thread_id):
- threading.Thread.__init__(self)
- self.threadID = thread_id
+ super(PluginThread, self).__init__(section, config)
self.date_format = config.get(section, 'format', fallback='%c')
- self.status = dict()
- if config.has_option(section, 'color'):
- self.status['color'] = config.get(section, 'color')
- self.freq = config.getint(section, 'freq', fallback=1)
- self.hide = False
- self.should_stop = False
def main(self):
self.status['full_text'] = time.strftime(self.date_format)
-
- def stop(self):
- self.should_stop = True
-
- def run(self):
- while True:
- if self.should_stop is False:
- self.main()
- time.sleep(self.freq)
- else:
- break
diff --git a/plugins/disk.py b/plugins/disk.py
index 53a5e7e..0f92574 100644
--- a/plugins/disk.py
+++ b/plugins/disk.py
@@ -1,20 +1,11 @@
+import plugins.common
import psutil
-import threading
-import time
-class PluginThread(threading.Thread):
+class PluginThread(plugins.common.PluginThreadCommon):
def __init__(self, section, config, thread_id):
- threading.Thread.__init__(self)
- self.threadID = thread_id
- self.status = dict()
+ super(PluginThread, self).__init__(section, config)
self.part = config.get(section, 'part')
- if config.has_option(section, 'color'):
- self.status['color'] = config.get(section, 'color')
- self.freq = config.getint(section, 'freq', fallback=30)
- self.hide = False
- self.should_stop = False
- self.problem_value = config.getint(section, 'problem', fallback=70)
def main(self):
du_stat = psutil.disk_usage(self.part)
@@ -27,14 +18,3 @@ class PluginThread(threading.Thread):
du_free = str(round(du_stat.free / 2**30, 2))
du = self.part + ': ' + du_free + 'G'
self.status['full_text'] = du
-
- def stop(self):
- self.should_stop = True
-
- def run(self):
- while True:
- if self.should_stop is False:
- self.main()
- time.sleep(self.freq)
- else:
- break
diff --git a/plugins/load.py b/plugins/load.py
index 02332d4..76059b8 100644
--- a/plugins/load.py
+++ b/plugins/load.py
@@ -1,21 +1,11 @@
import os
-import time
-import threading
+import plugins.common
-class PluginThread(threading.Thread):
+class PluginThread(plugins.common.PluginThreadCommon):
def __init__(self, section, config, thread_id):
- threading.Thread.__init__(self)
- self.threadID = thread_id
- self.date_format = config.get(section, 'format', fallback='%c')
- self.status = dict()
- if config.has_option(section, 'color'):
- self.status['color'] = config.get(section, 'color')
- self.freq = config.getint(section, 'freq', fallback=10)
+ super(PluginThread, self).__init__(section, config)
self.hide_ok = config.getboolean(section, 'hide_ok', fallback=False)
- self.hide = False
- self.should_stop = False
- self.problem_value = config.getint(section, 'problem', fallback=100)
def main(self):
loads = os.getloadavg()
@@ -27,14 +17,3 @@ class PluginThread(threading.Thread):
self.status['urgent'] = False
loads = [str(i) for i in loads]
self.status['full_text'] = 'LA: ' + ' '.join(loads)
-
- def stop(self):
- self.should_stop = True
-
- def run(self):
- while True:
- if self.should_stop is False:
- self.main()
- time.sleep(self.freq)
- else:
- break
diff --git a/plugins/mem.py b/plugins/mem.py
index 2c4b1a5..ebc0ced 100644
--- a/plugins/mem.py
+++ b/plugins/mem.py
@@ -1,32 +1,13 @@
import psutil
-import threading
-import time
+import plugins.common
-class PluginThread(threading.Thread):
+class PluginThread(plugins.common.PluginThreadCommon):
def __init__(self, section, config, thread_id):
- threading.Thread.__init__(self)
- self.threadID = thread_id
- self.status = dict()
- if config.has_option(section, 'color'):
- self.status['color'] = config.get(section, 'color')
- self.freq = config.getint(section, 'freq', fallback=1)
- self.hide = False
- self.should_stop = False
+ super(PluginThread, self).__init__(section, config)
def main(self):
mem_stat = psutil.virtual_memory()
mem_available = str(round(mem_stat.available / 2**30, 2))
mem = 'RAM: ' + mem_available + 'G'
self.status['full_text'] = mem
-
- def stop(self):
- self.should_stop = True
-
- def run(self):
- while True:
- if self.should_stop is False:
- self.main()
- time.sleep(self.freq)
- else:
- break
diff --git a/plugins/ping.py b/plugins/ping.py
index aeb4386..26cb657 100644
--- a/plugins/ping.py
+++ b/plugins/ping.py
@@ -1,21 +1,15 @@
import os
import random
-import time
-import threading
+import plugins.common
-class PluginThread(threading.Thread):
+class PluginThread(plugins.common.PluginThreadCommon):
def __init__(self, section, config, thread_id):
- threading.Thread.__init__(self)
- self.threadID = thread_id
+ super(PluginThread, self).__init__(section, config)
self.hosts = config.get(section, 'hosts').split(',')
self.title = config.get(section, 'title')
self.timeout = config.get(section, 'timeout', fallback='150')
- self.status = dict()
- self.freq = config.getint(section, 'freq', fallback=5)
self.format_status('n/a')
- self.hide = False
- self.should_stop = False
def format_status(self, state):
self.status['full_text'] = self.title + ': ' + state
@@ -34,21 +28,3 @@ class PluginThread(threading.Thread):
self.format_status('on')
break
self.format_status('off')
-
- def sleep(self):
- seconds = 0
- while seconds < self.freq:
- time.sleep(1)
- seconds += 1
- del seconds
-
- def stop(self):
- self.should_stop = True
-
- def run(self):
- while True:
- if self.should_stop is False:
- self.main()
- self.sleep()
- else:
- break
diff --git a/vdstatus b/vdstatus
index d2a1ab2..931e61c 100755
--- a/vdstatus
+++ b/vdstatus
@@ -1,6 +1,5 @@
#!/usr/bin/python3
# TODO: handle SIGINT properly
-# TODO: remove code duplication in plugins, probably use a common class?
# TODO: add documentation / comments
# TODO: add a dummy plugin to use as a starting point
# TODO: interactivity support
@@ -51,17 +50,18 @@ class PluginRunner:
def query(self):
outputs = list()
- try:
- for plugin in self.plugins_loaded:
- if not plugin.hide:
- outputs.append(plugin.status)
- print(self.format_output(outputs), flush=True)
- time.sleep(self.output_freq)
- except (KeyboardInterrupt, SystemExit):
- for plugin in self.plugins_loaded:
- plugin.stop()
- sys.exit('stopping threads...')
- del outputs
+ for plugin in self.plugins_loaded:
+ if not plugin.hide:
+ outputs.append(plugin.status)
+ print(self.format_output(outputs), flush=True)
+
+ def run(self):
+ while True:
+ try:
+ self.query()
+ time.sleep(self.output_freq)
+ except (KeyboardInterrupt, SystemExit):
+ sys.exit()
@staticmethod
def format_i3wm(inputs):
@@ -80,5 +80,4 @@ if __name__ == '__main__':
plugin_runner = PluginRunner(args.conf)
plugin_runner.start()
time.sleep(0.1)
- while True:
- plugin_runner.query()
+ plugin_runner.run()