Merge pull request #143 from fredj/memory_format

[module/memory] Add new format string parameter
This commit is contained in:
tobi-wan-kenobi 2017-07-15 10:32:57 +02:00 committed by GitHub
commit 14f0af373c
2 changed files with 22 additions and 9 deletions

View file

@ -5,7 +5,8 @@
Parameters: Parameters:
* memory.warning : Warning threshold in % of memory used (defaults to 80%) * memory.warning : Warning threshold in % of memory used (defaults to 80%)
* memory.critical: Critical threshold in % of memory used (defaults to 90%) * 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: try:
@ -27,15 +28,18 @@ class Module(bumblebee.engine.Module):
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="gnome-system-monitor") cmd="gnome-system-monitor")
def memory_usage(self, widget): @property
used = self._mem.total - self._mem.available def _format(self):
if bumblebee.util.asbool(self.parameter("usedonly", False)): if bumblebee.util.asbool(self.parameter("usedonly", False)):
return bumblebee.util.bytefmt(used) return "{used}"
return "{}/{} ({:05.02f}%)".format( else:
bumblebee.util.bytefmt(used), return self.parameter("format", "{used}/{total} ({percent:05.02f}%)")
bumblebee.util.bytefmt(self._mem.total),
self._mem.percent 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): def update(self, widgets):
self._mem = psutil.virtual_memory() self._mem = psutil.virtual_memory()

View file

@ -40,6 +40,15 @@ class TestMemoryModule(unittest.TestCase):
self.module.update_all() self.module.update_all()
self.assertTrue("critical" in self.module.state(self.anyWidget)) 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): def test_usage(self):
rv = VirtualMemory(50) rv = VirtualMemory(50)
rv.total = 1000 rv.total = 1000