[modules/memory] Fix imports an module constructor

This commit is contained in:
Tobias Witek 2020-03-06 14:48:11 +01:00
parent 5ccf60a16b
commit 13e512d1f1

View file

@ -1,72 +1,69 @@
# pylint: disable=C0111,R0903 # pylint: disable=C0111,R0903
"""Displays available RAM, total amount of RAM and percentage available. '''Displays available RAM, total amount of RAM and percentage available.
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.format: Format string (defaults to "{used}/{total} ({percent:05.02f}%)") * 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}" * memory.usedonly: Only show the amount of RAM in use (defaults to False). Same as memory.format='{used}'
""" '''
import re import re
import bumblebee.util import core.module
import bumblebee.input import core.widget
import bumblebee.output import core.input
import bumblebee.engine
class Container(object): class Container(object):
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.__dict__.update(kwargs) self.__dict__.update(kwargs)
class Module(bumblebee.engine.Module): class Module(core.module.Module):
def __init__(self, engine, config): def __init__(self, config=None):
super(Module, self).__init__(engine, config, super().__init__(config, core.widget.Widget(self.memory_usage))
bumblebee.output.Widget(full_text=self.memory_usage)
)
self.update(None) self.update(None)
engine.input.register_callback(self, button=bumblebee.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 bumblebee.util.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}%)')
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, widgets):
data = {} data = {}
with open("/proc/meminfo", "r") as f: with open('/proc/meminfo', 'r') as f:
for line in f: for line in f:
tmp = re.split(r"[:\s]+", line) tmp = re.split(r'[:\s]+', line)
value = int(tmp[1]) value = int(tmp[1])
if tmp[2] == "kB": value = value*1024 if tmp[2] == 'kB': value = value*1024
if tmp[2] == "mB": value = value*1024*1024 if tmp[2] == 'mB': value = value*1024*1024
if tmp[2] == "gB": value = value*1024*1024*1024 if tmp[2] == 'gB': value = value*1024*1024*1024
data[tmp[0]] = value data[tmp[0]] = value
if "MemAvailable" in data: if 'MemAvailable' in data:
used = data["MemTotal"] - data["MemAvailable"] used = data['MemTotal'] - data['MemAvailable']
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': bumblebee.util.bytefmt(data['MemTotal']),
"available": bumblebee.util.bytefmt(data["MemAvailable"]), 'available': bumblebee.util.bytefmt(data['MemAvailable']),
"free": bumblebee.util.bytefmt(data["MemFree"]), 'free': bumblebee.util.bytefmt(data['MemFree']),
"used": bumblebee.util.bytefmt(used), 'used': bumblebee.util.bytefmt(used),
"percent": float(used)/float(data["MemTotal"])*100.0 'percent': float(used)/float(data['MemTotal'])*100.0
} }
def state(self, widget): def state(self, widget):
if self._mem["percent"] > float(self.parameter("critical", 90)): if self._mem['percent'] > float(self.parameter('critical', 90)):
return "critical" return 'critical'
if self._mem["percent"] > float(self.parameter("warning", 80)): if self._mem['percent'] > float(self.parameter('warning', 80)):
return "warning" return 'warning'
return None return None
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4