You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.1 KiB
137 lines
4.1 KiB
# This python file use the following encoding: utf-8 |
|
|
|
import psutil |
|
|
|
import e |
|
|
|
from efl import ecore |
|
from efl import evas |
|
from efl import edje |
|
from efl.evas import EXPAND_BOTH, FILL_BOTH |
|
from efl.elementary import Box, Frame, Progressbar |
|
|
|
|
|
__gadget_name__ = 'Memory Monitor' |
|
__gadget_vers__ = '0.2' |
|
__gadget_auth__ = 'DaveMDS' |
|
__gadget_mail__ = 'dave@gurumeditation.it' |
|
__gadget_desc__ = 'Ram + Swap monitor' |
|
__gadget_vapi__ = 2 |
|
__gadget_opts__ = { 'popup_on_desktop': False } |
|
|
|
|
|
# import sys |
|
# def DBG(msg): |
|
# print("MEM: %s" % msg) |
|
# sys.stdout.flush() |
|
|
|
|
|
class Gadget(e.Gadget): |
|
|
|
def __init__(self): |
|
super().__init__() |
|
self.poller = None |
|
self.mem = None |
|
self.swp = None |
|
|
|
def instance_created(self, obj, site): |
|
super().instance_created(obj, site) |
|
|
|
obj.size_hint_aspect = (evas.EVAS_ASPECT_CONTROL_BOTH, |
|
int(obj.data_get('aspect_w')), |
|
int(obj.data_get('aspect_h'))) |
|
|
|
if self.poller is None: |
|
self.poller = ecore.Poller(16, self.poller_cb, ecore.ECORE_POLLER_CORE) |
|
|
|
def instance_destroyed(self, obj): |
|
super().instance_destroyed(obj) |
|
|
|
if len(self._instances) < 1 and self.poller is not None: |
|
self.poller.delete() |
|
self.poller = None |
|
|
|
def popup_created(self, elm_parent): |
|
# main vertical box |
|
box = Box(elm_parent) |
|
box.show() |
|
|
|
# mem frame |
|
tot = self.format_mb(psutil.virtual_memory().total) |
|
fr = Frame(box, text='Memory Usage (available {})'.format(tot), |
|
size_hint_expand=EXPAND_BOTH, size_hint_fill=FILL_BOTH) |
|
box.pack_end(fr) |
|
fr.show() |
|
|
|
box2 = Box(fr, size_hint_expand=EXPAND_BOTH, size_hint_fill=FILL_BOTH) |
|
fr.content = box2 |
|
box2.show() |
|
|
|
pb1 = Progressbar(box2, text='Total used', |
|
span_size=200, size_hint_align=(1.0, 0.5)) |
|
box2.pack_end(pb1) |
|
pb1.show() |
|
|
|
pb2 = Progressbar(box2, text='active', |
|
span_size=200, size_hint_align=(1.0, 0.5)) |
|
box2.pack_end(pb2) |
|
pb2.show() |
|
|
|
pb3 = Progressbar(box2, text='buffers', |
|
span_size=200, size_hint_align=(1.0, 0.5)) |
|
box2.pack_end(pb3) |
|
pb3.show() |
|
|
|
pb4 = Progressbar(box2, text='cached', |
|
span_size=200, size_hint_align=(1.0, 0.5)) |
|
box2.pack_end(pb4) |
|
pb4.show() |
|
|
|
# swap frame |
|
tot = self.format_mb(psutil.swap_memory().total) |
|
fr = Frame(box, text='Swap Usage (available {})'.format(tot), |
|
size_hint_expand=EXPAND_BOTH, size_hint_fill=FILL_BOTH) |
|
box.pack_end(fr) |
|
fr.show() |
|
|
|
pb5 = Progressbar(fr) |
|
fr.content = pb5 |
|
pb5.show() |
|
|
|
# keep widget references in the popup |
|
box.data['usd_pb'] = pb1 |
|
box.data['act_pb'] = pb2 |
|
box.data['buf_pb'] = pb3 |
|
box.data['cac_pb'] = pb4 |
|
box.data['swp_pb'] = pb5 |
|
|
|
self.popup_update(box) |
|
return box |
|
|
|
def popup_update(self, popup): |
|
self.update_pb(popup.data['usd_pb'], self.mem.used, self.mem.total) |
|
active = self.mem.used - self.mem.buffers - self.mem.cached |
|
self.update_pb(popup.data['act_pb'], active, self.mem.total) |
|
self.update_pb(popup.data['buf_pb'], self.mem.buffers, self.mem.total) |
|
self.update_pb(popup.data['cac_pb'], self.mem.cached, self.mem.total) |
|
self.update_pb(popup.data['swp_pb'], self.swp.used, self.swp.total) |
|
|
|
def poller_cb(self): |
|
self.mem = psutil.virtual_memory() |
|
self.swp = psutil.swap_memory() |
|
|
|
for obj in self._instances: |
|
obj.message_send(0, (self.mem.percent, self.swp.percent)) |
|
|
|
for popup in self._popups: |
|
self.popup_update(popup) |
|
|
|
return ecore.ECORE_CALLBACK_RENEW |
|
|
|
def format_mb(self, val): |
|
return '{0:.0f} MB'.format(val / 1048576) |
|
|
|
def update_pb(self, pb, val, total): |
|
pb.value = val / total |
|
pb.unit_format = '{0} ({1:.0f} %%)'.format(self.format_mb(val), |
|
val / total * 100)
|
|
|