# This python file use the following encoding: utf-8 import time import e from efl import ecore from efl import evas from efl import edje from efl import elementary as elm from efl.evas import EXPAND_BOTH, EXPAND_HORIZ, FILL_BOTH __gadget_name__ = 'Network Speed Monitor' __gadget_vers__ = '0.1' __gadget_auth__ = 'DaveMDS' __gadget_mail__ = 'dave@gurumeditation.it' __gadget_desc__ = 'Network speed monitor' __gadget_vapi__ = 1 __gadget_opts__ = { 'popup_on_desktop': False } # import sys # def DBG(msg): # print("NETSPEED: %s" % msg) # sys.stdout.flush() class Gadget(e.Gadget): def __init__(self): super().__init__() self.poller = None self.last_in = 0 self.last_out = 0 self.last_time = 0 def instance_created(self, obj, site): super().instance_created(obj, site) obj.size_hint_aspect = evas.EVAS_ASPECT_CONTROL_BOTH, 16, 16 if self.poller is None: self.poller = ecore.Poller(16, self.poller_cb, ecore.ECORE_POLLER_CORE) self.last_time = time.time() 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, popup): super().popup_created(popup) box = elm.Box(popup) popup.part_swallow('main.swallow', box) box.show() in_label = elm.Label(popup, size_hint_expand=EXPAND_HORIZ) box.pack_end(in_label) in_label.show() out_label = elm.Label(popup, size_hint_expand=EXPAND_HORIZ) box.pack_end(out_label) out_label.show() popup.data['in_label'] = in_label popup.data['out_label'] = out_label # tell the popup to always recalculate it's size popup.update_hints = True # do an update now self.poller_cb() def poller_cb(self): byte_per_second_in, byte_per_second_out = self.parse_proc() kb_in = byte_per_second_in / 1000.0 kb_out = byte_per_second_out / 1000.0 in_perc = int(kb_in / 1000 * 100) # TODO CONFIGURABLE MAX out_perc = int(kb_out / 1000 * 100) # TODO CONFIGURABLE MAX for obj in self._instances: obj.message_send(1, (0, in_perc, 0, 0, out_perc, 0)) for popup in self._popups: popup.data['in_label'].text = 'Recv: %.2f KB/s' % kb_in popup.data['out_label'].text = 'Trans: %.2f KB/s' % kb_out return ecore.ECORE_CALLBACK_RENEW def parse_proc(self): tot_in = tot_out = indiff = outdiff = 0 for line in open("/proc/net/dev", "r").readlines(): if ':' in line: vals = line.split() tot_in += int(vals[1]) tot_out += int(vals[9]) curtime = time.time() timediff = curtime - self.last_time self.last_time = curtime if self.last_in > 0: indiff = tot_in - self.last_in self.last_in = tot_in if self.last_out > 0: outdiff = tot_out - self.last_out self.last_out = tot_out # DBG("TIME: %s" % timediff) # DBG("TOT in:%d out:%d" % (tot_in, tot_out)) # DBG("DIFF in:%d out:%d" % (indiff, outdiff)) byte_per_second_in = indiff / timediff byte_per_second_out = outdiff / timediff return byte_per_second_in, byte_per_second_out