vdstatus, plugins: some refactoring and better usage of dict() methods; rename fga to fortune and make uri configurable

This commit is contained in:
Von Random 2019-03-28 13:21:15 +03:00
parent 7459d29c68
commit 151c94c5f3
10 changed files with 56 additions and 47 deletions

View file

@ -2,21 +2,17 @@ import threading
import time import time
def parse_config(config, defaults): PLUGIN_DEFAULTS = {'freq': 1, 'hide_ok': True}
result = dict()
for key in defaults:
result[key] = config[key] if key in config else defaults[key]
return result
class PluginThreadCommon: class PluginThreadCommon:
def __init__(self, config, defaults=dict()): def __init__(self, config, defaults=None):
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.status = dict()
self.conf = dict()
self.conf.update(PLUGIN_DEFAULTS)
if defaults:
self.conf.update(defaults)
self.conf.update(config)
self.hide = False self.hide = False
self.thread = threading.Thread(target=self.run) self.thread = threading.Thread(target=self.run)
self.thread.daemon = True self.thread.daemon = True

View file

@ -2,16 +2,16 @@ import plugins
BATTERY_DIR = '/sys/class/power_supply/BAT0/' BATTERY_DIR = '/sys/class/power_supply/BAT0/'
BATT_DEFAULTS = {
'problem': 15,
'symbol_charging': '\u2191',
'symbol_discharging': '\u2193'
}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = { super(PluginThread, self).__init__(config, BATT_DEFAULTS)
'problem': 15,
'symbol_charging': '\u2191',
'symbol_discharging': '\u2193'
}
super(PluginThread, self).__init__(config, defaults)
def main(self): def main(self):
with \ with \

View file

@ -2,12 +2,14 @@ import datetime
import plugins import plugins
DATE_DEFAULTS = {'format': '%c'}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = {'format': '%c', 'tz': None} super(PluginThread, self).__init__(config, DATE_DEFAULTS)
super(PluginThread, self).__init__(config, defaults)
self.timezone = None self.timezone = None
if self.conf['tz']: if 'tz' in self.conf:
import pytz import pytz
self.timezone = pytz.timezone(self.conf['tz']) self.timezone = pytz.timezone(self.conf['tz'])

View file

@ -2,10 +2,12 @@ import plugins
import psutil import psutil
DISK_DEFAULTS = {'partition': '/', 'problem': 80, 'freq': 15}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = {'partition': '/', 'problem': 80, 'freq': 15} super(PluginThread, self).__init__(config, DISK_DEFAULTS)
super(PluginThread, self).__init__(config, defaults)
def main(self): def main(self):
du_stat = psutil.disk_usage(self.conf['partition']) du_stat = psutil.disk_usage(self.conf['partition'])

View file

@ -3,17 +3,20 @@ import requests
import time import time
URI = 'http://fucking-great-advice.ru/api/random' FGA_DEFAULTS = {
'uri': 'http://fucking-great-advice.ru/api/random',
'freq': 120, 'retry': 3
}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = {'freq': 120, 'retry': 3} super(PluginThread, self).__init__(config, FGA_DEFAULTS)
super(PluginThread, self).__init__(config, defaults)
self.retry = False self.retry = False
def main(self): def main(self):
try: try:
req = requests.get(URI, timeout=2) req = requests.get(self.conf['uri'], timeout=2)
advice = req.json()['text'] if req.status_code == 200 else 'N/A' advice = req.json()['text'] if req.status_code == 200 else 'N/A'
self.retry = False self.retry = False
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
@ -24,7 +27,6 @@ class PluginThread(plugins.PluginThreadCommon):
self.retry = True self.retry = True
self.status['full_text'] = advice self.status['full_text'] = advice
def run(self): def run(self):
while True: while True:
self.main() self.main()

View file

@ -2,10 +2,12 @@ import os
import plugins import plugins
LOAD_DEFAULTS = {'freq': 20, 'problem': 1}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = {'freq': 20, 'problem': 1} super(PluginThread, self).__init__(config, LOAD_DEFAULTS)
super(PluginThread, self).__init__(config, defaults)
def main(self): def main(self):
loads = os.getloadavg() loads = os.getloadavg()

View file

@ -2,10 +2,12 @@ import psutil
import plugins import plugins
MEM_DEFAULTS = {'problem': 85}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = {'problem': 85} super(PluginThread, self).__init__(config, MEM_DEFAULTS)
super(PluginThread, self).__init__(config, defaults)
def main(self): def main(self):
mem_stat = psutil.virtual_memory() mem_stat = psutil.virtual_memory()

View file

@ -2,15 +2,15 @@ import plugins
import subprocess import subprocess
PACMAN_DEFAULTS = {
'cmd': ('/usr/bin/pacman', '-Qu'),
'title': 'UPD', 'freq': 15, 'problem': 10
}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = { super(PluginThread, self).__init__(config, PACMAN_DEFAULTS)
'cmd': ('/usr/bin/pacman', '-Qu'),
'title': 'UPD',
'freq': 15,
'problem': 10
}
super(PluginThread, self).__init__(config, defaults)
self.format_status(0) self.format_status(0)
def format_status(self, count): def format_status(self, count):

View file

@ -3,15 +3,19 @@ import random
import plugins import plugins
PING_DEFAULTS = {
'hosts': list(), 'title': 'PING', 'timeout': 150
}
class PluginThread(plugins.PluginThreadCommon): class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
defaults = {'hosts': list(), 'title': 'PING', 'timeout': 150} super(PluginThread, self).__init__(config, PING_DEFAULTS)
super(PluginThread, self).__init__(config, defaults)
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.conf['title'] + ': ' + state self.status['full_text'] = self.conf['title'] + ': ' + state
if state == 'on': if state == 'up':
self.status['urgent'] = False self.status['urgent'] = False
self.hide = True self.hide = True
else: else:
@ -26,4 +30,4 @@ class PluginThread(plugins.PluginThreadCommon):
if response == 0: if response == 0:
self.format_status('on') self.format_status('on')
break break
self.format_status('off') self.format_status('down')

View file

@ -6,7 +6,6 @@ import os
import sys import sys
import time import time
import yaml import yaml
import plugins
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.yaml') DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.yaml')
@ -28,10 +27,10 @@ def parse_arguments():
class PluginRunner: class PluginRunner:
def __init__(self, config_file=DEFAULT_CONFIG): def __init__(self, config_file=DEFAULT_CONFIG):
config = dict() self.conf = dict()
self.conf.update(DEFAULTS)
with open(config_file) as config_data: with open(config_file) as config_data:
config = yaml.load(config_data) self.conf.update(yaml.load(config_data))
self.conf = plugins.parse_config(config, DEFAULTS)
self.plugins_loaded = list() self.plugins_loaded = list()
self.format_output = self.format_term self.format_output = self.format_term
for plugin in self.conf['plugins']: for plugin in self.conf['plugins']: