[modules/memory] Simplify and use util methods

This commit is contained in:
Tobias Witek 2020-03-06 14:52:16 +01:00
parent 13e512d1f1
commit 56a6173282
3 changed files with 16 additions and 13 deletions

View file

@ -5,5 +5,5 @@
- module __init__ has less parameters
- super() works differently
- engine.input.register_callback is now core.input.register
- update() doesn't have a list of widgets anymore
- update() only has a single parameter (self) (no widgets anymore)

View file

@ -15,21 +15,17 @@ import core.module
import core.widget
import core.input
class Container(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
import util.format
class Module(core.module.Module):
def __init__(self, config=None):
super().__init__(config, core.widget.Widget(self.memory_usage))
self.update(None)
core.input.register_callback(self, button=core.input.LEFT_MOUSE,
core.input.register(self, button=core.input.LEFT_MOUSE,
cmd='gnome-system-monitor')
@property
def _format(self):
if bumblebee.util.asbool(self.parameter('usedonly', False)):
if util.format.asbool(self.parameter('usedonly', False)):
return '{used}'
else:
return self.parameter('format', '{used}/{total} ({percent:05.02f}%)')
@ -37,7 +33,7 @@ class Module(core.module.Module):
def memory_usage(self, widget):
return self._format.format(**self._mem)
def update(self, widgets):
def update(self):
data = {}
with open('/proc/meminfo', 'r') as f:
for line in f:
@ -52,10 +48,10 @@ class Module(core.module.Module):
else:
used = data['MemTotal'] - data['MemFree'] - data['Buffers'] - data['Cached'] - data['Slab']
self._mem = {
'total': bumblebee.util.bytefmt(data['MemTotal']),
'available': bumblebee.util.bytefmt(data['MemAvailable']),
'free': bumblebee.util.bytefmt(data['MemFree']),
'used': bumblebee.util.bytefmt(used),
'total': util.format.byte(data['MemTotal']),
'available': util.format.byte(data['MemAvailable']),
'free': util.format.byte(data['MemFree']),
'used': util.format.byte(used),
'percent': float(used)/float(data['MemTotal'])*100.0
}

View file

@ -23,4 +23,11 @@ def aslist(val):
return val
return str(val).replace(' ', '').split(',')
def byte(val, fmt='{:.2f}'):
for unit in ['', 'Ki', 'Mi', 'Gi']:
if val < 1024.0:
return '{}{}B'.format(fmt, unit).format(val)
val /= 1024.0
return '{}GiB'.format(fmt).format(val*1024.0)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4