plugins: improve readability, move to str().format() in many places

This commit is contained in:
Von Random 2019-07-08 14:19:05 +03:00
parent bd549b0cc9
commit d824345ba1
10 changed files with 23 additions and 17 deletions

View file

@ -19,7 +19,7 @@ class PluginThreadCommon:
def format_status(self, status, urgent=False): def format_status(self, status, urgent=False):
if 'title' in self.conf: if 'title' in self.conf:
full_text = self.conf['title'] + ': ' + status full_text = '{}: {}'.format(self.conf['title'], status)
else: else:
full_text = status full_text = status
self.status.update({'full_text': full_text, 'urgent': urgent}) self.status.update({'full_text': full_text, 'urgent': urgent})

View file

@ -1,7 +1,6 @@
import plugins import plugins
BATTERY_DIR = '/sys/class/power_supply/BAT0/'
BATT_DEFAULTS = { BATT_DEFAULTS = {
'problem': 15, 'problem': 15,
'symbol_charging': '\u2191', 'symbol_charging': '\u2191',
@ -15,10 +14,10 @@ class PluginThread(plugins.PluginThreadCommon):
def main(self): def main(self):
with \ with \
open(BATTERY_DIR + 'capacity', 'r') as batt_capacity, \ open('/sys/class/power_supply/BAT0/capacity', 'r') as bcap, \
open(BATTERY_DIR + 'status', 'r') as batt_status: open('/sys/class/power_supply/BAT0/status', 'r') as bstat:
capacity = batt_capacity.readline().strip() capacity = bcap.readline().strip()
status_value = batt_status.readline().strip() status_value = bstat.readline().strip()
if status_value != 'Discharging': if status_value != 'Discharging':
symbol = self.conf['symbol_charging'] symbol = self.conf['symbol_charging']
@ -27,4 +26,5 @@ class PluginThread(plugins.PluginThreadCommon):
symbol = self.conf['symbol_discharging'] symbol = self.conf['symbol_discharging']
urgent = float(capacity) <= self.conf['problem'] urgent = float(capacity) <= self.conf['problem']
self.format_status(capacity + '% ' + symbol, urgent=urgent) status = '{}% {}'.format(capacity, symbol)
self.format_status(status, urgent)

View file

@ -14,5 +14,7 @@ class PluginThread(plugins.PluginThreadCommon):
self.timezone = pytz.timezone(self.conf['tz']) self.timezone = pytz.timezone(self.conf['tz'])
def main(self): def main(self):
now = datetime.datetime.now(tz=self.timezone) now = datetime.datetime\
self.format_status(now.strftime(self.conf['format'])) .now(tz=self.timezone)\
.strftime(self.conf['format'])
self.format_status(now)

View file

@ -19,5 +19,6 @@ class PluginThread(plugins.PluginThreadCommon):
else: else:
self.hide = True self.hide = True
self.status['urgent'] = False self.status['urgent'] = False
status = '{:.2f}G'.format(du_stat.free / 2**30) status = '{:.2f}G'.format(du_stat.free / 2**30)
self.format_status(status) self.format_status(status)

View file

@ -11,7 +11,6 @@ class PluginThread(plugins.PluginThreadCommon):
def __init__(self, config): def __init__(self, config):
super(PluginThread, self).__init__(config, FILECAT_DEFAULTS) super(PluginThread, self).__init__(config, FILECAT_DEFAULTS)
self.hide = False self.hide = False
self.format_status(self.conf['title'], False)
def main(self): def main(self):
try: try:
@ -21,4 +20,5 @@ class PluginThread(plugins.PluginThreadCommon):
except FileNotFoundError: except FileNotFoundError:
contents = self.conf['nofile'] contents = self.conf['nofile']
urgent = True urgent = True
self.format_status(contents, urgent) self.format_status(contents, urgent)

View file

@ -25,6 +25,7 @@ class PluginThread(plugins.PluginThreadCommon):
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
fortune = 'N/A (offline)' fortune = 'N/A (offline)'
self.retry = True self.retry = True
self.format_status(fortune) self.format_status(fortune)
def run(self): def run(self):

View file

@ -18,4 +18,5 @@ class PluginThread(plugins.PluginThreadCommon):
self.hide = True self.hide = True
urgent = False urgent = False
status = '{:.2f} {:.2f} {:.2f}'.format(*loads) status = '{:.2f} {:.2f} {:.2f}'.format(*loads)
self.format_status(status, urgent=urgent)
self.format_status(status, urgent)

View file

@ -17,5 +17,6 @@ class PluginThread(plugins.PluginThreadCommon):
else: else:
self.hide = True self.hide = True
urgent = False urgent = False
mem_available = round(mem_stat.available / 2**30, 2)
self.format_status('{:.2f}G'.format(mem_available), urgent) status = '{:.2f}G'.format(mem_stat.available / 2**30)
self.format_status(status, urgent)

View file

@ -24,6 +24,6 @@ class PluginThread(plugins.PluginThreadCommon):
self.hide = False self.hide = False
else: else:
self.hide = True self.hide = True
self.format_status(
str(packages), urgent=packages >= self.conf['problem'] urgent = packages >= self.conf['problem']
) self.format_status(packages, urgent)

View file

@ -23,4 +23,4 @@ class PluginThread(plugins.PluginThreadCommon):
if fping.returncode == 0: if fping.returncode == 0:
self.format_status('up') self.format_status('up')
else: else:
self.format_status('down', urgent=True) self.format_status('down', True)