# pylint: disable=C0111,R0903

"""Displays available RAM, total amount of RAM and percentage available.

Parameters:
    * memory.warning : Warning threshold in % of memory used (defaults to 80%)
    * memory.critical: Critical threshold in % of memory used (defaults to 90%)
    * 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:
    import psutil
except ImportError:
    pass

import bumblebee.util
import bumblebee.input
import bumblebee.output
import bumblebee.engine

class Module(bumblebee.engine.Module):
    def __init__(self, engine, config):
        super(Module, self).__init__(engine, config,
            bumblebee.output.Widget(full_text=self.memory_usage)
        )
        self._mem = psutil.virtual_memory()
        engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
            cmd="gnome-system-monitor")

    @property
    def _format(self):
        if bumblebee.util.asbool(self.parameter("usedonly", False)):
            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()

    def state(self, widget):
        if self._mem.percent > float(self.parameter("critical", 90)):
            return "critical"
        if self._mem.percent > float(self.parameter("warning", 80)):
            return "warning"
        return None

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

@@ -474,59 +474,17 @@
 %22%22%0A%0A
-try:%0A    import psutil%0Aexcept ImportError:%0A    pass
+import re
 %0A%0Aim
@@ -575,16 +575,113 @@
 engine%0A%0A
+class Container(object):%0A    def __init__(self, **kwargs):%0A        self.__dict__.update(kwargs)%0A%0A
 class Mo
@@ -896,38 +896,21 @@
 elf.
-_mem = psutil.virtual_memory()
+update(None)%0A
 %0A   
@@ -1306,295 +1306,918 @@
     
-used = bumblebee.util.bytefmt(self._mem.total - self._mem.available)%0A        total = bumblebee.util.bytefmt(self._mem.total)%0A%0A        return self._format.format(used=used, total=total, percent=self._mem.percent)%0A%0A    def update(self, widgets):%0A        self._mem = psutil.virtual_memory()
+return self._format.format(**self._mem)%0A%0A    def update(self, widgets):%0A        data = %7B%7D%0A        with open(%22/proc/meminfo%22, %22r%22) as f:%0A            for line in f:%0A                tmp = re.split(r%22%5B:%5Cs%5D+%22, line)%0A                value = int(tmp%5B1%5D)%0A                if tmp%5B2%5D == %22kB%22: value = value*1024%0A                if tmp%5B2%5D == %22mB%22: value = value*1024*1024%0A                if tmp%5B2%5D == %22gB%22: value = value*1024*1024*1024%0A                data%5Btmp%5B0%5D%5D = value%0A        self._mem = %7B%0A            %22total%22: bumblebee.util.bytefmt(data%5B%22MemTotal%22%5D),%0A            %22available%22: bumblebee.util.bytefmt(data%5B%22MemAvailable%22%5D),%0A            %22free%22: bumblebee.util.bytefmt(data%5B%22MemFree%22%5D),%0A            %22used%22: bumblebee.util.bytefmt(data%5B%22MemTotal%22%5D - data%5B%22MemFree%22%5D - data%5B%22Buffers%22%5D - data%5B%22Cached%22%5D - data%5B%22Slab%22%5D),%0A            %22percent%22: (float(data%5B%22MemTotal%22%5D - data%5B%22MemAvailable%22%5D)/data%5B%22MemTotal%22%5D)*100%0A        %7D
 %0A%0A  
@@ -2259,32 +2259,35 @@
 if self._mem
-.
+%5B%22
 percent
+%22%5D
  %3E float(sel
@@ -2370,16 +2370,19 @@
 _mem
-.
+%5B%22
 percent
+%22%5D
  %3E f

Use /proc/meminfo instead of psutil