use json instead of ini: less readable, easier to work with
This commit is contained in:
parent
f5c49008ec
commit
5e17aca8e2
11 changed files with 108 additions and 82 deletions
|
@ -2,17 +2,24 @@ 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
|
||||
|
||||
|
||||
class PluginThreadCommon:
|
||||
def __init__(self, section, config):
|
||||
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)
|
||||
self.status = dict()
|
||||
self.hide = False
|
||||
self.thread = threading.Thread(target=self.run)
|
||||
self.thread.daemon = True
|
||||
self.freq = config.getint(section, 'freq', fallback=1)
|
||||
self.problem_value = config.getint(section, 'problem', fallback=70)
|
||||
self.hide_ok = config.getboolean(section, 'hide_ok', fallback=True)
|
||||
if config.has_option(section, 'color'):
|
||||
self.status['color'] = config.get(section, 'color')
|
||||
|
||||
def start(self):
|
||||
self.thread.start()
|
||||
|
@ -23,4 +30,4 @@ class PluginThreadCommon:
|
|||
def run(self):
|
||||
while True:
|
||||
self.main()
|
||||
time.sleep(self.freq)
|
||||
time.sleep(self.conf['freq'])
|
||||
|
|
|
@ -5,11 +5,12 @@ BATTERY_DIR = '/sys/class/power_supply/BAT0/'
|
|||
|
||||
|
||||
class PluginThread(plugins.PluginThreadCommon):
|
||||
def __init__(self, section, config):
|
||||
super(PluginThread, self).__init__(section, config)
|
||||
def __init__(self, config):
|
||||
super(PluginThread, self).__init__(config)
|
||||
|
||||
def main(self):
|
||||
with open(BATTERY_DIR + 'capacity', 'r') as capacity, \
|
||||
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()
|
||||
|
|
|
@ -3,16 +3,14 @@ import plugins
|
|||
|
||||
|
||||
class PluginThread(plugins.PluginThreadCommon):
|
||||
def __init__(self, section, config):
|
||||
super(PluginThread, self).__init__(section, config)
|
||||
self.date_format = config.get(section, 'format', fallback='%c')
|
||||
tz = config.get(section, 'TZ', fallback=None)
|
||||
if tz:
|
||||
def __init__(self, config):
|
||||
defaults = {'format': '%c', 'tz': None}
|
||||
super(PluginThread, self).__init__(config, defaults)
|
||||
self.timezone = None
|
||||
if self.conf['tz']:
|
||||
import pytz
|
||||
self.tz = pytz.timezone(tz)
|
||||
else:
|
||||
self.tz = None
|
||||
self.timezone = pytz.timezone(self.conf['tz'])
|
||||
|
||||
def main(self):
|
||||
now = datetime.datetime.now(tz=self.tz)
|
||||
self.status['full_text'] = now.strftime(self.date_format)
|
||||
now = datetime.datetime.now(tz=self.timezone)
|
||||
self.status['full_text'] = now.strftime(self.conf['format'])
|
||||
|
|
|
@ -3,18 +3,18 @@ import psutil
|
|||
|
||||
|
||||
class PluginThread(plugins.PluginThreadCommon):
|
||||
def __init__(self, section, config):
|
||||
super(PluginThread, self).__init__(section, config)
|
||||
self.part = config.get(section, 'part')
|
||||
def __init__(self, config):
|
||||
defaults = {'partition': '/', 'problem': 80}
|
||||
super(PluginThread, self).__init__(config, defaults)
|
||||
|
||||
def main(self):
|
||||
du_stat = psutil.disk_usage(self.part)
|
||||
if du_stat.percent >= self.problem_value:
|
||||
du_stat = psutil.disk_usage(self.conf['partition'])
|
||||
if du_stat.percent >= self.conf['problem']:
|
||||
self.hide = False
|
||||
self.status['urgent'] = True
|
||||
else:
|
||||
self.hide = True
|
||||
self.status['urgent'] = False
|
||||
du_free = str(round(du_stat.free / 2**30, 2))
|
||||
du = self.part + ': ' + du_free + 'G'
|
||||
self.status['full_text'] = du
|
||||
disk_usage = self.conf['partition'] + ': ' + du_free + 'G'
|
||||
self.status['full_text'] = disk_usage
|
||||
|
|
|
@ -3,12 +3,13 @@ import plugins
|
|||
|
||||
|
||||
class PluginThread(plugins.PluginThreadCommon):
|
||||
def __init__(self, section, config):
|
||||
super(PluginThread, self).__init__(section, config)
|
||||
def __init__(self, config):
|
||||
defaults = {'problem': 1}
|
||||
super(PluginThread, self).__init__(config, defaults)
|
||||
|
||||
def main(self):
|
||||
loads = os.getloadavg()
|
||||
if loads[0] >= self.problem_value:
|
||||
if loads[0] >= self.conf['problem']:
|
||||
self.hide = False
|
||||
self.status['urgent'] = True
|
||||
else:
|
||||
|
|
|
@ -3,8 +3,8 @@ import plugins
|
|||
|
||||
|
||||
class PluginThread(plugins.PluginThreadCommon):
|
||||
def __init__(self, section, config):
|
||||
super(PluginThread, self).__init__(section, config)
|
||||
def __init__(self, config):
|
||||
super(PluginThread, self).__init__(config)
|
||||
|
||||
def main(self):
|
||||
mem_stat = psutil.virtual_memory()
|
||||
|
|
|
@ -3,9 +3,9 @@ import subprocess
|
|||
|
||||
|
||||
class PluginThread(plugins.PluginThreadCommon):
|
||||
def __init__(self, section, config):
|
||||
super(PluginThread, self).__init__(section, config)
|
||||
self.freq = config.getint(section, 'freq', fallback=15)
|
||||
def __init__(self, config):
|
||||
defaults = {'freq': 15}
|
||||
super(PluginThread, self).__init__(config, defaults)
|
||||
self.format_status(0)
|
||||
|
||||
def format_status(self, count):
|
||||
|
|
|
@ -4,15 +4,13 @@ import plugins
|
|||
|
||||
|
||||
class PluginThread(plugins.PluginThreadCommon):
|
||||
def __init__(self, section, config):
|
||||
super(PluginThread, self).__init__(section, config)
|
||||
self.hosts = config.get(section, 'hosts').split(',')
|
||||
self.title = config.get(section, 'title')
|
||||
self.timeout = config.get(section, 'timeout', fallback='150')
|
||||
def __init__(self, config):
|
||||
defaults = {'hosts': list(), 'title': 'PING', 'timeout': 150}
|
||||
super(PluginThread, self).__init__(config, defaults)
|
||||
self.format_status('n/a')
|
||||
|
||||
def format_status(self, state):
|
||||
self.status['full_text'] = self.title + ': ' + state
|
||||
self.status['full_text'] = self.conf['title'] + ': ' + state
|
||||
if state == 'on':
|
||||
self.status['urgent'] = False
|
||||
self.hide = True
|
||||
|
@ -20,9 +18,10 @@ class PluginThread(plugins.PluginThreadCommon):
|
|||
self.status['urgent'] = True
|
||||
|
||||
def main(self):
|
||||
random.shuffle(self.hosts)
|
||||
for host in self.hosts:
|
||||
fping = 'fping -qc1t' + self.timeout + ' ' + host + ' &>/dev/null'
|
||||
random.shuffle(self.conf['hosts'])
|
||||
for host in self.conf['hosts']:
|
||||
fping = 'fping -qc1t' + str(self.conf['timeout'])\
|
||||
+ ' ' + host + ' &>/dev/null'
|
||||
response = os.system(fping)
|
||||
if response == 0:
|
||||
self.format_status('on')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue