From 37434aa5e5c0b6712f72250ced1812b203c96e4d Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Sat, 15 Jul 2017 09:20:05 +0200 Subject: [PATCH] [module/memory] Add new format string parameter --- bumblebee/modules/memory.py | 22 +++++++++++++--------- tests/modules/test_memory.py | 9 +++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index 42816b9..e92fc10 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -5,7 +5,8 @@ Parameters: * memory.warning : Warning threshold in % of memory used (defaults to 80%) * memory.critical: Critical threshold in % of memory used (defaults to 90%) - * memory.usedonly: Only show the amount of RAM in use (defaults to False). + * memory.format: Format string (defaults to "{used}/{total} ({percent:05.02f}%)") + * memory.usedonly: Only show the amount of RAM in use (defaults to False). Same as memory.format="{used}" """ try: @@ -27,15 +28,18 @@ class Module(bumblebee.engine.Module): engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-system-monitor") - def memory_usage(self, widget): - used = self._mem.total - self._mem.available + @property + def _format(self): if bumblebee.util.asbool(self.parameter("usedonly", False)): - return bumblebee.util.bytefmt(used) - return "{}/{} ({:05.02f}%)".format( - bumblebee.util.bytefmt(used), - bumblebee.util.bytefmt(self._mem.total), - self._mem.percent - ) + return "{used}" + else: + return self.parameter("format", "{used}/{total} ({percent:05.02f}%)") + + def memory_usage(self, widget): + used = bumblebee.util.bytefmt(self._mem.total - self._mem.available) + total = bumblebee.util.bytefmt(self._mem.total) + + return self._format.format(used=used, total=total, percent=self._mem.percent) def update(self, widgets): self._mem = psutil.virtual_memory() diff --git a/tests/modules/test_memory.py b/tests/modules/test_memory.py index 94c61f9..3f98ca9 100644 --- a/tests/modules/test_memory.py +++ b/tests/modules/test_memory.py @@ -40,6 +40,15 @@ class TestMemoryModule(unittest.TestCase): self.module.update_all() self.assertTrue("critical" in self.module.state(self.anyWidget)) + def test_format(self): + self.config.set("memory.format", "memory used: {used}") + rv = VirtualMemory(50) + rv.total = 1000 + rv.available = 500 + self.psutil.virtual_memory.return_value = rv + self.module.update_all() + self.assertEquals("memory used: 500.00B", self.module.memory_usage(self.anyWidget)) + def test_usage(self): rv = VirtualMemory(50) rv.total = 1000