some primitive exit handling, still have to do something about waiting till the longest freq value is reached

This commit is contained in:
Von Random 2016-11-18 18:42:28 +03:00
parent 9db9a935d4
commit 49d14d410a
7 changed files with 74 additions and 19 deletions

View file

@ -14,6 +14,7 @@ class PluginThread(threading.Thread):
self.status['color'] = config.get(section, 'color') self.status['color'] = config.get(section, 'color')
self.freq = config.getint(section, 'freq', fallback=1) self.freq = config.getint(section, 'freq', fallback=1)
self.hide = False self.hide = False
self.should_stop = False
def main(self): def main(self):
with open(BATTERY_DIR + 'capacity', 'r') as capacity, \ with open(BATTERY_DIR + 'capacity', 'r') as capacity, \
@ -34,7 +35,13 @@ class PluginThread(threading.Thread):
self.status['full_text'] = batt self.status['full_text'] = batt
def stop(self):
self.should_stop = True
def run(self): def run(self):
while True: while True:
self.main() if self.should_stop is False:
time.sleep(self.freq) self.main()
time.sleep(self.freq)
else:
break

View file

@ -12,11 +12,18 @@ class PluginThread(threading.Thread):
self.status['color'] = config.get(section, 'color') self.status['color'] = config.get(section, 'color')
self.freq = config.getint(section, 'freq', fallback=1) self.freq = config.getint(section, 'freq', fallback=1)
self.hide = False self.hide = False
self.should_stop = False
def main(self): def main(self):
self.status['full_text'] = time.strftime(self.date_format) self.status['full_text'] = time.strftime(self.date_format)
def stop(self):
self.should_stop = True
def run(self): def run(self):
while True: while True:
self.main() if self.should_stop is False:
time.sleep(self.freq) self.main()
time.sleep(self.freq)
else:
break

View file

@ -13,6 +13,7 @@ class PluginThread(threading.Thread):
self.status['color'] = config.get(section, 'color') self.status['color'] = config.get(section, 'color')
self.freq = config.getint(section, 'freq', fallback=30) self.freq = config.getint(section, 'freq', fallback=30)
self.hide = False self.hide = False
self.should_stop = False
self.problem_value = config.getint(section, 'problem', fallback=70) self.problem_value = config.getint(section, 'problem', fallback=70)
def main(self): def main(self):
@ -27,7 +28,13 @@ class PluginThread(threading.Thread):
du = self.part + ': ' + du_free + 'G' du = self.part + ': ' + du_free + 'G'
self.status['full_text'] = du self.status['full_text'] = du
def stop(self):
self.should_stop = True
def run(self): def run(self):
while True: while True:
self.main() if self.should_stop is False:
time.sleep(self.freq) self.main()
time.sleep(self.freq)
else:
break

View file

@ -14,6 +14,7 @@ class PluginThread(threading.Thread):
self.freq = config.getint(section, 'freq', fallback=10) self.freq = config.getint(section, 'freq', fallback=10)
self.hide_ok = config.getboolean(section, 'hide_ok', fallback=False) self.hide_ok = config.getboolean(section, 'hide_ok', fallback=False)
self.hide = False self.hide = False
self.should_stop = False
self.problem_value = config.getint(section, 'problem', fallback=100) self.problem_value = config.getint(section, 'problem', fallback=100)
def main(self): def main(self):
@ -27,7 +28,13 @@ class PluginThread(threading.Thread):
loads = [str(i) for i in loads] loads = [str(i) for i in loads]
self.status['full_text'] = 'LA: ' + ' '.join(loads) self.status['full_text'] = 'LA: ' + ' '.join(loads)
def stop(self):
self.should_stop = True
def run(self): def run(self):
while True: while True:
self.main() if self.should_stop is False:
time.sleep(self.freq) self.main()
time.sleep(self.freq)
else:
break

View file

@ -12,6 +12,7 @@ class PluginThread(threading.Thread):
self.status['color'] = config.get(section, 'color') self.status['color'] = config.get(section, 'color')
self.freq = config.getint(section, 'freq', fallback=1) self.freq = config.getint(section, 'freq', fallback=1)
self.hide = False self.hide = False
self.should_stop = False
def main(self): def main(self):
mem_stat = psutil.virtual_memory() mem_stat = psutil.virtual_memory()
@ -19,7 +20,13 @@ class PluginThread(threading.Thread):
mem = 'RAM: ' + mem_available + 'G' mem = 'RAM: ' + mem_available + 'G'
self.status['full_text'] = mem self.status['full_text'] = mem
def stop(self):
self.should_stop = True
def run(self): def run(self):
while True: while True:
self.main() if self.should_stop is False:
time.sleep(self.freq) self.main()
time.sleep(self.freq)
else:
break

View file

@ -15,6 +15,7 @@ class PluginThread(threading.Thread):
self.freq = config.getint(section, 'freq', fallback=5) self.freq = config.getint(section, 'freq', fallback=5)
self.format_status('n/a') self.format_status('n/a')
self.hide = False self.hide = False
self.should_stop = False
def format_status(self, state): def format_status(self, state):
self.status['full_text'] = self.title + ': ' + state self.status['full_text'] = self.title + ': ' + state
@ -34,7 +35,20 @@ class PluginThread(threading.Thread):
break break
self.format_status('off') 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): def run(self):
while True: while True:
self.main() if self.should_stop is False:
time.sleep(self.freq) self.main()
self.sleep()
else:
break

View file

@ -1,22 +1,23 @@
#!/usr/bin/python3 #!/usr/bin/python3
# TODO: handle SIGINT properly # TODO: handle SIGINT properly
# TODO: remove code duplication in plugins, probably use a common class?
# TODO: add documentation / comments # TODO: add documentation / comments
# TODO: add a dummy plugin to use as a starting point # TODO: add a dummy plugin to use as a starting point
# TODO: interactivity support # TODO: interactivity support
from sys import argv
import argparse import argparse
import configparser import configparser
import importlib import importlib
import json import json
import os import os
import plugins import plugins
import sys
import time import time
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.ini') 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, ' desc = ('A simple i3status replacement, '
'and more. Warning: WIP, may be broken.') 'and more. Warning: WIP, may be broken.')
p = argparse.ArgumentParser(description=desc) p = argparse.ArgumentParser(description=desc)
@ -69,11 +70,16 @@ def run_plugins(config_file=DEFAULT_CONFIG):
while True: while True:
outputs = list() outputs = list()
for plugin in plugins_l: try:
if not plugin.hide: for plugin in plugins_l:
outputs.append(plugin.status) if not plugin.hide:
print(format_outputs(outputs), flush=True) outputs.append(plugin.status)
time.sleep(1) 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__': if __name__ == '__main__':