diff options
author | Von Random <von@vdrandom.org> | 2017-12-16 02:55:28 +0300 |
---|---|---|
committer | Von Random <von@vdrandom.org> | 2017-12-16 02:55:28 +0300 |
commit | 5e17aca8e2b31d14b75fb55d589b8e216dc4ab49 (patch) | |
tree | ffb37aff4a32715d1d43abce787ce0ca8f692a89 | |
parent | f5c49008ec9770d64b6f7217098207116c2092d5 (diff) |
use json instead of ini: less readable, easier to work with
-rw-r--r-- | conf.ini | 23 | ||||
-rw-r--r-- | conf.json | 38 | ||||
-rw-r--r-- | plugins/__init__.py | 21 | ||||
-rw-r--r-- | plugins/batt.py | 7 | ||||
-rw-r--r-- | plugins/date.py | 18 | ||||
-rw-r--r-- | plugins/disk.py | 14 | ||||
-rw-r--r-- | plugins/load.py | 7 | ||||
-rw-r--r-- | plugins/mem.py | 4 | ||||
-rw-r--r-- | plugins/pacman.py | 6 | ||||
-rw-r--r-- | plugins/ping.py | 17 | ||||
-rwxr-xr-x | vdstatus | 35 |
11 files changed, 108 insertions, 82 deletions
diff --git a/conf.ini b/conf.ini deleted file mode 100644 index 3e390b7..0000000 --- a/conf.ini +++ /dev/null @@ -1,23 +0,0 @@ -[main] -format = i3 - -[network] -plugin = ping -hosts = de-ber-as20647.anchors.atlas.ripe.net,nl-ams-as1101.anchors.atlas.ripe.net,uk-boh-as196745.anchors.atlas.ripe.net -title = NET -hide_ok = false - -[memory] -plugin = mem - -[battery] -plugin = batt - -[day] -plugin = date -color = #FF0000 -format = %%A %%d - -[time] -plugin = date -format = %%H:%%M diff --git a/conf.json b/conf.json new file mode 100644 index 0000000..9968068 --- /dev/null +++ b/conf.json @@ -0,0 +1,38 @@ +{ + "output_format": "i3", + "plugins": [ + { + "name": "ping", + "hosts": [ + "de-ber-as20647.anchors.atlas.ripe.net", + "nl-ams-as1101.anchors.atlas.ripe.net", + "uk-boh-as196745.anchors.atlas.ripe.net" + ], + "title": "NET" + }, + { + "name": "disk", + "partition": "/", + "problem": 80, + "hide_ok": false + }, + { + "name": "disk", + "partition": "/home", + "problem": 90 + }, + { + "name": "pacman" + }, + { + "name": "mem" + }, + { + "name": "load" + }, + { + "name": "date", + "format": "%a %d %H:%M" + } + ] +} diff --git a/plugins/__init__.py b/plugins/__init__.py index 734c684..68df0c9 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -2,17 +2,24 @@ import threading import time +def parse_config(config, defaults): + result = dict() + for key in defaults: + result[key] = config[key] if key in config else defaults[key] + return result + + class PluginThreadCommon: - def __init__(self, section, config): + def __init__(self, config, defaults=dict()): + if 'freq' not in defaults: + defaults['freq'] = 1 + if 'hide_ok' not in defaults: + defaults['hide_ok'] = True + self.conf = parse_config(config, defaults) 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) - self.hide_ok = config.getboolean(section, 'hide_ok', fallback=True) - if config.has_option(section, 'color'): - self.status['color'] = config.get(section, 'color') def start(self): self.thread.start() @@ -23,4 +30,4 @@ class PluginThreadCommon: def run(self): while True: self.main() - time.sleep(self.freq) + time.sleep(self.conf['freq']) diff --git a/plugins/batt.py b/plugins/batt.py index 84e22b5..b0e53f6 100644 --- a/plugins/batt.py +++ b/plugins/batt.py @@ -5,11 +5,12 @@ BATTERY_DIR = '/sys/class/power_supply/BAT0/' class PluginThread(plugins.PluginThreadCommon): - def __init__(self, section, config): - super(PluginThread, self).__init__(section, config) + def __init__(self, config): + super(PluginThread, self).__init__(config) def main(self): - with open(BATTERY_DIR + 'capacity', 'r') as capacity, \ + with \ + open(BATTERY_DIR + 'capacity', 'r') as capacity, \ open(BATTERY_DIR + 'status', 'r') as status: batt_stat = status.read().strip() batt_capacity = capacity.read().strip() diff --git a/plugins/date.py b/plugins/date.py index d147051..ee6009a 100644 --- a/plugins/date.py +++ b/plugins/date.py @@ -3,16 +3,14 @@ import plugins class PluginThread(plugins.PluginThreadCommon): - def __init__(self, section, config): - super(PluginThread, self).__init__(section, config) - self.date_format = config.get(section, 'format', fallback='%c') - tz = config.get(section, 'TZ', fallback=None) - if tz: + def __init__(self, config): + defaults = {'format': '%c', 'tz': None} + super(PluginThread, self).__init__(config, defaults) + self.timezone = None + if self.conf['tz']: import pytz - self.tz = pytz.timezone(tz) - else: - self.tz = None + self.timezone = pytz.timezone(self.conf['tz']) def main(self): - now = datetime.datetime.now(tz=self.tz) - self.status['full_text'] = now.strftime(self.date_format) + now = datetime.datetime.now(tz=self.timezone) + self.status['full_text'] = now.strftime(self.conf['format']) diff --git a/plugins/disk.py b/plugins/disk.py index 887acd8..6b80dd6 100644 --- a/plugins/disk.py +++ b/plugins/disk.py @@ -3,18 +3,18 @@ import psutil class PluginThread(plugins.PluginThreadCommon): - def __init__(self, section, config): - super(PluginThread, self).__init__(section, config) - self.part = config.get(section, 'part') + def __init__(self, config): + defaults = {'partition': '/', 'problem': 80} + super(PluginThread, self).__init__(config, defaults) def main(self): - du_stat = psutil.disk_usage(self.part) - if du_stat.percent >= self.problem_value: + du_stat = psutil.disk_usage(self.conf['partition']) + if du_stat.percent >= self.conf['problem']: self.hide = False self.status['urgent'] = True else: self.hide = True self.status['urgent'] = False du_free = str(round(du_stat.free / 2**30, 2)) - du = self.part + ': ' + du_free + 'G' - self.status['full_text'] = du + disk_usage = self.conf['partition'] + ': ' + du_free + 'G' + self.status['full_text'] = disk_usage diff --git a/plugins/load.py b/plugins/load.py index 41c29e7..45f1685 100644 --- a/plugins/load.py +++ b/plugins/load.py @@ -3,12 +3,13 @@ import plugins class PluginThread(plugins.PluginThreadCommon): - def __init__(self, section, config): - super(PluginThread, self).__init__(section, config) + def __init__(self, config): + defaults = {'problem': 1} + super(PluginThread, self).__init__(config, defaults) def main(self): loads = os.getloadavg() - if loads[0] >= self.problem_value: + if loads[0] >= self.conf['problem']: self.hide = False self.status['urgent'] = True else: diff --git a/plugins/mem.py b/plugins/mem.py index 3be96a4..b26a8a6 100644 --- a/plugins/mem.py +++ b/plugins/mem.py @@ -3,8 +3,8 @@ import plugins class PluginThread(plugins.PluginThreadCommon): - def __init__(self, section, config): - super(PluginThread, self).__init__(section, config) + def __init__(self, config): + super(PluginThread, self).__init__(config) def main(self): mem_stat = psutil.virtual_memory() diff --git a/plugins/pacman.py b/plugins/pacman.py index 2f4eb8a..e162f12 100644 --- a/plugins/pacman.py +++ b/plugins/pacman.py @@ -3,9 +3,9 @@ import subprocess class PluginThread(plugins.PluginThreadCommon): - def __init__(self, section, config): - super(PluginThread, self).__init__(section, config) - self.freq = config.getint(section, 'freq', fallback=15) + def __init__(self, config): + defaults = {'freq': 15} + super(PluginThread, self).__init__(config, defaults) self.format_status(0) def format_status(self, count): diff --git a/plugins/ping.py b/plugins/ping.py index 1d80d58..2f0bb42 100644 --- a/plugins/ping.py +++ b/plugins/ping.py @@ -4,15 +4,13 @@ import plugins class PluginThread(plugins.PluginThreadCommon): - def __init__(self, section, config): - 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') + def __init__(self, config): + defaults = {'hosts': list(), 'title': 'PING', 'timeout': 150} + super(PluginThread, self).__init__(config, defaults) self.format_status('n/a') def format_status(self, state): - self.status['full_text'] = self.title + ': ' + state + self.status['full_text'] = self.conf['title'] + ': ' + state if state == 'on': self.status['urgent'] = False self.hide = True @@ -20,9 +18,10 @@ class PluginThread(plugins.PluginThreadCommon): self.status['urgent'] = True def main(self): - random.shuffle(self.hosts) - for host in self.hosts: - fping = 'fping -qc1t' + self.timeout + ' ' + host + ' &>/dev/null' + random.shuffle(self.conf['hosts']) + for host in self.conf['hosts']: + fping = 'fping -qc1t' + str(self.conf['timeout'])\ + + ' ' + host + ' &>/dev/null' response = os.system(fping) if response == 0: self.format_status('on') @@ -3,7 +3,6 @@ # TODO: add documentation / comments # TODO: interactivity support import argparse -import configparser import importlib import json import os @@ -12,7 +11,7 @@ import time import plugins -DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.ini') +DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.json') def parse_arguments(arguments=sys.argv[1:]): @@ -26,22 +25,25 @@ def parse_arguments(arguments=sys.argv[1:]): class PluginRunner: def __init__(self, config_file=DEFAULT_CONFIG): - self.config = configparser.ConfigParser() - self.config.read(config_file) - self.output_format = self.config.get('main', 'format', fallback='term') - self.output_freq = self.config.getint('main', 'output_freq', fallback=1) - self.hide_ok = self.config.getboolean('main', 'hide_ok', fallback=True) + defaults = { + 'output_format': 'term', + 'output_freq': 1, + 'hide_ok': True, + 'plugins': [{'name': 'date'}] + } + config = dict() + with open(config_file) as config_data: + config = json.load(config_data) + self.conf = plugins.parse_config(config, defaults) self.plugins_loaded = list() - self.config.remove_section('main') self.format_output = self.format_term - for section in self.config.sections(): - plugin_name = self.config.get(section, 'plugin') - mod = importlib.import_module('.' + plugin_name, 'plugins') - thread_object = mod.PluginThread(section, self.config) + for plugin in self.conf['plugins']: + mod = importlib.import_module('.' + plugin['name'], 'plugins') + thread_object = mod.PluginThread(plugin) self.plugins_loaded.append(thread_object) def start(self): - if self.output_format == 'i3': + if self.conf['output_format'] == 'i3': print('{"version":1}\n[', flush=True) self.format_output = self.format_i3wm for plugin in self.plugins_loaded: @@ -50,7 +52,10 @@ class PluginRunner: def query(self): outputs = list() for plugin in self.plugins_loaded: - if not self.hide_ok or not plugin.hide_ok or not plugin.hide: + if \ + not self.conf['hide_ok'] or \ + not plugin.conf['hide_ok'] or \ + not plugin.hide: outputs.append(plugin.status) print(self.format_output(outputs), flush=True) @@ -58,7 +63,7 @@ class PluginRunner: while True: try: self.query() - time.sleep(self.output_freq) + time.sleep(self.conf['output_freq']) except (KeyboardInterrupt, SystemExit): sys.exit() |