diff options
author | Von Random <von@vdrandom.org> | 2016-11-18 18:42:28 +0300 |
---|---|---|
committer | Von Random <von@vdrandom.org> | 2016-11-18 18:42:28 +0300 |
commit | 49d14d410a9e70d2c284051c6b6c62c7ccaaf243 (patch) | |
tree | 2320960923d0b9727f8bf0821de3237c87bc03c3 | |
parent | 9db9a935d486a227c16051aef49b40a267533525 (diff) |
some primitive exit handling, still have to do something about waiting till the longest freq value is reached
-rw-r--r-- | plugins/batt.py | 11 | ||||
-rw-r--r-- | plugins/date.py | 11 | ||||
-rw-r--r-- | plugins/disk.py | 11 | ||||
-rw-r--r-- | plugins/load.py | 11 | ||||
-rw-r--r-- | plugins/mem.py | 11 | ||||
-rw-r--r-- | plugins/ping.py | 18 | ||||
-rwxr-xr-x | vdstatus | 20 |
7 files changed, 74 insertions, 19 deletions
diff --git a/plugins/batt.py b/plugins/batt.py index 34cc7a0..f579486 100644 --- a/plugins/batt.py +++ b/plugins/batt.py @@ -14,6 +14,7 @@ class PluginThread(threading.Thread): 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): with open(BATTERY_DIR + 'capacity', 'r') as capacity, \ @@ -34,7 +35,13 @@ class PluginThread(threading.Thread): self.status['full_text'] = batt + def stop(self): + self.should_stop = True + def run(self): while True: - self.main() - time.sleep(self.freq) + if self.should_stop is False: + self.main() + time.sleep(self.freq) + else: + break diff --git a/plugins/date.py b/plugins/date.py index cf022cb..37f7c67 100644 --- a/plugins/date.py +++ b/plugins/date.py @@ -12,11 +12,18 @@ class PluginThread(threading.Thread): 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: - self.main() - time.sleep(self.freq) + 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 4f54241..53a5e7e 100644 --- a/plugins/disk.py +++ b/plugins/disk.py @@ -13,6 +13,7 @@ class PluginThread(threading.Thread): 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): @@ -27,7 +28,13 @@ class PluginThread(threading.Thread): du = self.part + ': ' + du_free + 'G' self.status['full_text'] = du + def stop(self): + self.should_stop = True + def run(self): while True: - self.main() - time.sleep(self.freq) + 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 e140ba0..02332d4 100644 --- a/plugins/load.py +++ b/plugins/load.py @@ -14,6 +14,7 @@ class PluginThread(threading.Thread): self.freq = config.getint(section, 'freq', fallback=10) 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): @@ -27,7 +28,13 @@ class PluginThread(threading.Thread): 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: - self.main() - time.sleep(self.freq) + 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 cf07abf..2c4b1a5 100644 --- a/plugins/mem.py +++ b/plugins/mem.py @@ -12,6 +12,7 @@ class PluginThread(threading.Thread): 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): mem_stat = psutil.virtual_memory() @@ -19,7 +20,13 @@ class PluginThread(threading.Thread): mem = 'RAM: ' + mem_available + 'G' self.status['full_text'] = mem + def stop(self): + self.should_stop = True + def run(self): while True: - self.main() - time.sleep(self.freq) + 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 790c9f7..aeb4386 100644 --- a/plugins/ping.py +++ b/plugins/ping.py @@ -15,6 +15,7 @@ class PluginThread(threading.Thread): 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,7 +35,20 @@ class PluginThread(threading.Thread): 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: - self.main() - time.sleep(self.freq) + if self.should_stop is False: + self.main() + self.sleep() + else: + break @@ -1,22 +1,23 @@ #!/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 -from sys import argv import argparse import configparser import importlib import json import os import plugins +import sys import time DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.ini') -def parse_arguments(arguments=argv[1:]): +def parse_arguments(arguments=sys.argv[1:]): desc = ('A simple i3status replacement, ' 'and more. Warning: WIP, may be broken.') p = argparse.ArgumentParser(description=desc) @@ -69,11 +70,16 @@ def run_plugins(config_file=DEFAULT_CONFIG): while True: outputs = list() - for plugin in plugins_l: - if not plugin.hide: - outputs.append(plugin.status) - print(format_outputs(outputs), flush=True) - time.sleep(1) + try: + for plugin in plugins_l: + if not plugin.hide: + outputs.append(plugin.status) + print(format_outputs(outputs), flush=True) + time.sleep(1) + except (KeyboardInterrupt, SystemExit): + for plugin in plugins_l: + plugin.stop() + sys.exit('stopping threads...') if __name__ == '__main__': |