Browse Source

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

master
Von Random 3 years ago
parent
commit
151c94c5f3
  1. 18
      plugins/__init__.py
  2. 12
      plugins/batt.py
  3. 8
      plugins/date.py
  4. 6
      plugins/disk.py
  5. 14
      plugins/fortune.py
  6. 6
      plugins/load.py
  7. 6
      plugins/mem.py
  8. 14
      plugins/pacman.py
  9. 12
      plugins/ping.py
  10. 7
      vdstatus

18
plugins/__init__.py

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

12
plugins/batt.py

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

8
plugins/date.py

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

6
plugins/disk.py

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

14
plugins/fga.py → plugins/fortune.py

@ -3,17 +3,20 @@ import requests
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):
def __init__(self, config):
defaults = {'freq': 120, 'retry': 3}
super(PluginThread, self).__init__(config, defaults)
super(PluginThread, self).__init__(config, FGA_DEFAULTS)
self.retry = False
def main(self):
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'
self.retry = False
except requests.exceptions.Timeout:
@ -24,7 +27,6 @@ class PluginThread(plugins.PluginThreadCommon):
self.retry = True
self.status['full_text'] = advice
def run(self):
while True:
self.main()

6
plugins/load.py

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

6
plugins/mem.py

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

14
plugins/pacman.py

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

12
plugins/ping.py

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

7
vdstatus

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

Loading…
Cancel
Save