vdstatus/vdstatus

82 lines
2.6 KiB
Python
Executable file

#!/usr/bin/python3
# TODO: consider per plugin hide_ok overriding global, not the other way around
# TODO: add documentation / comments
# TODO: interactivity support
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=sys.argv[1:]):
desc = ('A simple i3status replacement, '
'and more. Warning: WIP, may be broken.')
p = argparse.ArgumentParser(description=desc)
p.add_argument('-c', '--conf', default=DEFAULT_CONFIG,
help='configuration file')
return p.parse_args(arguments)
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)
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')
module = importlib.import_module('.' + plugin_name, 'plugins')
thread_object = module.PluginThread(section, self.config)
self.plugins_loaded.append(thread_object)
def start(self):
if self.output_format == 'i3':
print('{"version":1}\n[', flush=True)
self.format_output = self.format_i3wm
for plugin in self.plugins_loaded:
plugin.start()
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:
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):
return json.dumps(inputs) + ','
@staticmethod
def format_term(inputs):
return_info = list()
for item in inputs:
return_info.append(item['full_text'])
return ' \033[1m|\033[0m '.join(return_info)
if __name__ == '__main__':
args = parse_arguments()
plugin_runner = PluginRunner(args.conf)
plugin_runner.start()
time.sleep(0.1)
plugin_runner.run()