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

View file

@ -23,4 +23,11 @@ def aslist(val):
return val return val
return str(val).replace(' ', '').split(',') 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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4