From 56a617328248b9ae99328ecc34fb11599c42958e Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Fri, 6 Mar 2020 14:52:16 +0100 Subject: [PATCH] [modules/memory] Simplify and use util methods --- doc/MODULE_MIGRATION.md | 2 +- modules/core/memory.py | 20 ++++++++------------ util/format.py | 7 +++++++ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/doc/MODULE_MIGRATION.md b/doc/MODULE_MIGRATION.md index 607ebd8..80cd8f9 100644 --- a/doc/MODULE_MIGRATION.md +++ b/doc/MODULE_MIGRATION.md @@ -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) diff --git a/modules/core/memory.py b/modules/core/memory.py index beb73bc..7fe4e84 100644 --- a/modules/core/memory.py +++ b/modules/core/memory.py @@ -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 } diff --git a/util/format.py b/util/format.py index c3f5528..dc5cda9 100644 --- a/util/format.py +++ b/util/format.py @@ -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