vdstatus/vdstatus
2019-03-05 19:21:11 +03:00

90 lines
2.5 KiB
Python
Executable file

#!/usr/bin/python3
# pylint: disable=C0111
import argparse
import importlib
import json
import os
import sys
import time
import yaml
import plugins
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.yaml')
DEFAULTS = {
'output_format': 'term',
'output_freq': 1,
'hide_ok': False,
'plugins': [{'name': 'date'}]
}
def parse_arguments():
desc = ('A simple i3status replacement, '
'and more. Warning: WIP, may be broken.')
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-c', '--conf', default=DEFAULT_CONFIG,
help='configuration file')
return parser.parse_args()
class PluginRunner:
def __init__(self, config_file=DEFAULT_CONFIG):
config = dict()
with open(config_file) as config_data:
config = yaml.load(config_data)
self.conf = plugins.parse_config(config, DEFAULTS)
self.plugins_loaded = list()
self.format_output = self.format_term
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.conf['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.conf['hide_ok'] or \
not plugin.conf['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.conf['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)
def main():
args = parse_arguments()
plugin_runner = PluginRunner(args.conf)
plugin_runner.start()
time.sleep(0.1)
plugin_runner.run()
if __name__ == '__main__':
main()