a fully functional version

This commit is contained in:
Von Random 2016-10-22 19:27:28 +03:00
parent c272a21aa7
commit 9bcfcaf177
7 changed files with 135 additions and 33 deletions

View file

@ -1,15 +1,17 @@
[main] [main]
abc = useless value format = i3
[ping_ext] [network]
plugin = ping plugin = ping
hosts = de-ber-as20647.anchors.atlas.ripe.net,nl-ams-as1101.anchors.atlas.ripe.net,uk-boh-as196745.anchors.atlas.ripe.net hosts = de-ber-as20647.anchors.atlas.ripe.net,nl-ams-as1101.anchors.atlas.ripe.net,uk-boh-as196745.anchors.atlas.ripe.net
title = NET title = NET
colors = #00FF00,#FF0000
[ping_local] [memory]
plugin = ping plugin = mem
hosts = 10.2.13.2,10.2.13.4
title = BROKEN [battery]
plugin = batt
[day] [day]
plugin = date plugin = date

33
plugins/batt.py Normal file
View file

@ -0,0 +1,33 @@
import threading
import time
BATTERY_DIR = '/sys/class/power_supply/BAT0/'
class PluginThread(threading.Thread):
def __init__(self, section, config, thread_id):
threading.Thread.__init__(self)
self.threadID = thread_id
self.status = dict()
if config.has_option(section, 'color'):
self.status['color'] = config.get(section, 'color')
self.freq = 1
def main(self):
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()
if batt_stat != 'Discharging':
batt_stat = '\u2191'
else:
batt_stat = '\u2193'
batt = 'BAT: ' + batt_capacity + '% ' + batt_stat
self.status['full_text'] = batt
def run(self):
while True:
self.main()
time.sleep(self.freq)

View file

@ -6,13 +6,16 @@ class PluginThread(threading.Thread):
def __init__(self, section, config, thread_id): def __init__(self, section, config, thread_id):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.threadID = thread_id self.threadID = thread_id
self.date_format = config.get(section, 'format') self.date_format = config.get(section, 'format', fallback='%c')
self.status = dict() self.status = dict()
if config.has_option(section, 'color'): if config.has_option(section, 'color'):
self.status['color'] = config.get(section, 'color') self.status['color'] = config.get(section, 'color')
self.freq = 1 self.freq = 1
def main(self):
self.status['full_text'] = time.strftime(self.date_format)
def run(self): def run(self):
while True: while True:
self.status['full_text'] = time.strftime(self.date_format) self.main()
time.sleep(self.freq) time.sleep(self.freq)

25
plugins/mem.py Normal file
View file

@ -0,0 +1,25 @@
import psutil
import threading
import time
class PluginThread(threading.Thread):
def __init__(self, section, config, thread_id):
threading.Thread.__init__(self)
self.threadID = thread_id
self.status = dict()
if config.has_option(section, 'color'):
self.status['color'] = config.get(section, 'color')
self.freq = 1
def main(self):
mem_stat = psutil.virtual_memory()
mem_percentage = round(100.0 - mem_stat.percent, 2)
mem_available = round(mem_stat.available / 2**30, 2)
mem = 'RAM: ' + str(mem_percentage) + '% (' + str(mem_available) + 'G)'
self.status['full_text'] = mem
def run(self):
while True:
self.main()
time.sleep(self.freq)

View file

@ -10,25 +10,34 @@ class PluginThread(threading.Thread):
self.threadID = thread_id self.threadID = thread_id
self.hosts = config.get(section, 'hosts').split(',') self.hosts = config.get(section, 'hosts').split(',')
self.title = config.get(section, 'title') self.title = config.get(section, 'title')
if config.has_option(section, 'colors'):
self.colors = config.get(section, 'colors').split(',')
else:
self.colors = None
self.timeout = config.get(section, 'timeout', fallback='150')
self.status = dict() self.status = dict()
self.freq = 5 self.freq = config.getint(section, 'freq', fallback=5)
self.format_status('n/a') self.format_status('n/a')
def format_status(self, state): def format_status(self, state):
self.status['full_text'] = self.title + ': ' + state self.status['full_text'] = self.title + ': ' + state
if self.colors is not None:
if state == 'on':
self.status['color'] = self.colors[0]
else:
self.status['color'] = self.colors[1]
def run(self): def main(self):
while True:
random.shuffle(self.hosts) random.shuffle(self.hosts)
try:
for host in self.hosts: for host in self.hosts:
fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null' fping = 'fping -q -c1 -t' + self.timeout + ' ' + host + ' &>/dev/null'
response = os.system(fping) response = os.system(fping)
if response == 0: if response == 0:
self.format_status('on') self.format_status('on')
break break
self.format_status('off') self.format_status('off')
except (KeyboardInterrupt, SystemExit): def run(self):
break while True:
self.main()
time.sleep(self.freq) time.sleep(self.freq)

View file

@ -6,10 +6,7 @@ import plugins
import time import time
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini') DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.ini')
configuration = configparser.ConfigParser()
configuration.read(DEFAULT_CONFIG)
def load_plugins(config): def load_plugins(config):
@ -24,7 +21,29 @@ def load_plugins(config):
return plugins_loaded return plugins_loaded
def run_plugins(): def format_i3wm(inputs):
return json.dumps(inputs) + ','
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 run_plugins(config_file=DEFAULT_CONFIG):
configuration = configparser.ConfigParser()
configuration.read(config_file)
output_format = configuration.get('main', 'format', fallback='term')
if output_format == 'i3':
print('{"version":1}\n[')
format_outputs = format_i3wm
# default to terminal output
else:
format_outputs = format_term
plugins_l = load_plugins(configuration) plugins_l = load_plugins(configuration)
for plugin in plugins_l: for plugin in plugins_l:
plugin.start() plugin.start()
@ -33,5 +52,5 @@ def run_plugins():
outputs = list() outputs = list()
for plugin in plugins_l: for plugin in plugins_l:
outputs.append(plugin.status) outputs.append(plugin.status)
print(json.dumps(outputs) + ',') print(format_outputs(outputs))
time.sleep(1) time.sleep(1)

View file

@ -1,10 +1,21 @@
#!/usr/bin/python3 -u #!/usr/bin/python3 -u
import json from sys import argv
import argparse
import vdstatus import vdstatus
import time
print('{"version":1}') def parse_arguments(arguments=argv[1:]):
print('[') desc = ('A simple i3status replacement, '
while True: 'and more. Warning: WIP, may be broken.')
p = argparse.ArgumentParser(description=desc)
p.add_argument('-c', '--config', help='configuration file')
return p.parse_args(arguments)
if __name__ == '__main__':
args = parse_arguments()
if args.config is not None:
vdstatus.run_plugins(args.config)
else:
vdstatus.run_plugins() vdstatus.run_plugins()