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]
abc = useless value
format = i3
[ping_ext]
[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
colors = #00FF00,#FF0000
[ping_local]
plugin = ping
hosts = 10.2.13.2,10.2.13.4
title = BROKEN
[memory]
plugin = mem
[battery]
plugin = batt
[day]
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):
threading.Thread.__init__(self)
self.threadID = thread_id
self.date_format = config.get(section, 'format')
self.date_format = config.get(section, 'format', fallback='%c')
self.status = dict()
if config.has_option(section, 'color'):
self.status['color'] = config.get(section, 'color')
self.freq = 1
def main(self):
self.status['full_text'] = time.strftime(self.date_format)
def run(self):
while True:
self.status['full_text'] = time.strftime(self.date_format)
self.main()
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.hosts = config.get(section, 'hosts').split(',')
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.freq = 5
self.freq = config.getint(section, 'freq', fallback=5)
self.format_status('n/a')
def format_status(self, 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 main(self):
random.shuffle(self.hosts)
for host in self.hosts:
fping = 'fping -q -c1 -t' + self.timeout + ' ' + host + ' &>/dev/null'
response = os.system(fping)
if response == 0:
self.format_status('on')
break
self.format_status('off')
def run(self):
while True:
random.shuffle(self.hosts)
try:
for host in self.hosts:
fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null'
response = os.system(fping)
if response == 0:
self.format_status('on')
break
self.format_status('off')
except (KeyboardInterrupt, SystemExit):
break
self.main()
time.sleep(self.freq)

View file

@ -6,10 +6,7 @@ import plugins
import time
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini')
configuration = configparser.ConfigParser()
configuration.read(DEFAULT_CONFIG)
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.ini')
def load_plugins(config):
@ -24,7 +21,29 @@ def load_plugins(config):
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)
for plugin in plugins_l:
plugin.start()
@ -33,5 +52,5 @@ def run_plugins():
outputs = list()
for plugin in plugins_l:
outputs.append(plugin.status)
print(json.dumps(outputs) + ',')
print(format_outputs(outputs))
time.sleep(1)

View file

@ -1,10 +1,21 @@
#!/usr/bin/python3 -u
import json
from sys import argv
import argparse
import vdstatus
import time
print('{"version":1}')
print('[')
while True:
vdstatus.run_plugins()
def parse_arguments(arguments=argv[1:]):
desc = ('A simple i3status replacement, '
'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()