enlightenment-module-edgar/gadgets/mem/__init__.py

138 lines
4.1 KiB
Python
Raw Permalink Normal View History

# 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'
2018-07-14 08:26:32 -07:00
__gadget_vers__ = '0.2'
__gadget_auth__ = 'DaveMDS'
__gadget_mail__ = 'dave@gurumeditation.it'
__gadget_desc__ = 'Ram + Swap monitor'
2018-07-14 08:26:32 -07:00
__gadget_vapi__ = 2
__gadget_opts__ = { 'popup_on_desktop': False }
2018-07-14 08:26:32 -07:00
# import sys
# def DBG(msg):
# print("MEM: %s" % msg)
# sys.stdout.flush()
class Gadget(e.Gadget):
def __init__(self):
super().__init__()
self.poller = None
2018-07-14 08:26:32 -07:00
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:
2018-07-14 08:26:32 -07:00
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
2018-07-14 08:26:32 -07:00
def popup_created(self, elm_parent):
# main vertical box
box = Box(elm_parent)
box.show()
2018-07-14 08:26:32 -07:00
# mem frame
tot = self.format_mb(psutil.virtual_memory().total)
2018-07-14 08:26:32 -07:00
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()
2018-07-14 08:26:32 -07:00
box2 = Box(fr, size_hint_expand=EXPAND_BOTH, size_hint_fill=FILL_BOTH)
fr.content = box2
box2.show()
2018-07-14 08:26:32 -07:00
pb1 = Progressbar(box2, text='Total used',
span_size=200, size_hint_align=(1.0, 0.5))
box2.pack_end(pb1)
pb1.show()
2018-07-14 08:26:32 -07:00
pb2 = Progressbar(box2, text='active',
span_size=200, size_hint_align=(1.0, 0.5))
box2.pack_end(pb2)
pb2.show()
2018-07-14 08:26:32 -07:00
pb3 = Progressbar(box2, text='buffers',
span_size=200, size_hint_align=(1.0, 0.5))
box2.pack_end(pb3)
pb3.show()
2018-07-14 08:26:32 -07:00
pb4 = Progressbar(box2, text='cached',
span_size=200, size_hint_align=(1.0, 0.5))
box2.pack_end(pb4)
pb4.show()
2018-07-14 08:26:32 -07:00
# swap frame
tot = self.format_mb(psutil.swap_memory().total)
2018-07-14 08:26:32 -07:00
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()
2018-07-14 08:26:32 -07:00
pb5 = Progressbar(fr)
fr.content = pb5
pb5.show()
2018-07-14 08:26:32 -07:00
# 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
2018-07-14 08:26:32 -07:00
self.popup_update(box)
return box
2018-07-14 08:26:32 -07:00
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)
2018-07-14 08:26:32 -07:00
def poller_cb(self):
self.mem = psutil.virtual_memory()
self.swp = psutil.swap_memory()
2018-07-14 08:26:32 -07:00
for obj in self._instances:
obj.message_send(0, (self.mem.percent, self.swp.percent))
2018-07-14 08:26:32 -07:00
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)