Add the new NetSpeed python gadget

Total coding time: 3 hours... python rocks !!!
This commit is contained in:
Davide Andreoli 2017-09-09 00:29:38 +02:00
parent 1f7548132e
commit c67f14167c
7 changed files with 330 additions and 0 deletions

36
GADGETS/netspeed/Makefile Normal file
View File

@ -0,0 +1,36 @@
# Simple Makefile for Enlightenment (edgar) gadgets
# gadget specific config
GADGET_NAME = netspeed
EXTRA_FILES =
# nothing should be changed below this point
GADGET_FILES = __init__.pyc $(GADGET_NAME).edj
prefix = $(shell pkg-config --variable=libdir enlightenment)
gadget_folder = ${prefix}/enlightenment/gadgets/$(GADGET_NAME)
.PHONY: all install clean
all: $(GADGET_FILES) $(EXTRA_FILES)
install: all
@mkdir -p -v ${gadget_folder}
@cp --preserve=mode -v $(GADGET_FILES) $(EXTRA_FILES) $(gadget_folder)
uninstall: all
@rm -rfv ${gadget_folder}
clean:
@rm -fv *.edj *.pyc
EDJE_CC = edje_cc
EDJE_FLAGS = -v -id images/ -fd fonts/
%.edj: %.edc images/*
$(EDJE_CC) $(EDJE_FLAGS) $<
@chmod -v og+r $@
%.pyc: %.py
python3 -c "from py_compile import compile; compile('$<', '$@')"

View File

@ -0,0 +1,127 @@
# 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 = 0
tot_out = 0
for line in open("/proc/net/dev", "r").readlines()[2:]:
vals = line.split(None, 3)
tot_in += int(vals[1])
tot_out += int(vals[2])
curtime = time.time()
timediff = curtime - self.last_time
self.last_time = curtime
indiff = 0
if self.last_in > 0:
indiff = tot_in - self.last_in
self.last_in = tot_in
outdiff = 0
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
# def format_mb(self, val):
# return '{0:.0f} MB'.format(val / 1048576)

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -0,0 +1,167 @@
/**
* EDGAR Network speed gadget
*/
#define DBG_SHOW(_PART, _R, _G, _B) \
part { name: "dbg_show"_PART; \
type: RECT; mouse_events: 0; \
description { state: "default" 0.0; \
rel1.to: _PART; rel2.to: _PART; \
color: _R _G _B 50; } }
#define SHOW(_PART) DBG_SHOW(_PART, 50, 0, 0)
images {
image: "base.png" COMP;
image: "over.png" COMP;
image: "rx.png" COMP;
image: "tx.png" COMP;
// TODO gadget icon
}
collections {
/**
* API [e/gadget/icon] The group used for the icon of the gadget
*/
group { name: "e/gadgets/netspeed/icon";
parts {
image { "icon";
desc { "default";
aspect: 1.0 1.0;
aspect_preference: BOTH;
image.normal: "base.png"; // TODO gadget icon
}
}
}
}
/**
* API [e/gadget/main] The main group of the gadget
*/
group { name: "e/gadgets/netspeed/main";
data {
item: "aspect_w" "1";
item: "aspect_h" "1";
}
script {
public message(Msg_Type:type, id, ...) {
if ((type == MSG_INT_SET) && (id == 1)) {
// new rxcur = getarg(2);
new rxper = getarg(3);
// new rxmax = getarg(4);
// new txcur = getarg(5);
new txper = getarg(6);
// new txmax = getarg(7);
new Float:val = rxper / 100.0;
custom_state(PART:"rx_clip", "default", 0.0);
set_state_val(PART:"rx_clip", STATE_REL2, 1.0, val);
set_state(PART:"rx_clip", "custom", 0.0);
val = txper / 100.0;
custom_state(PART:"tx_clip", "default", 0.0);
set_state_val(PART:"tx_clip", STATE_REL1, 0.0, 1.0 - val);
set_state(PART:"tx_clip", "custom", 0.0);
}
}
}
parts {
part { name: "base";
description { state: "default" 0.0;
aspect: 1.0 1.0;
aspect_preference: BOTH;
image.normal: "base.png";
}
}
part { name: "rx_spacer"; type: SPACER;
description { state: "default" 0.0;
rel1.relative: 0.0 0.09;
rel2.relative: 1.0 0.91;
}
}
part { name: "rx_clip"; type: RECT;
description { state: "default" 0.0;
rel1.to: "rx_spacer";
rel2.to: "rx_spacer";
}
}
part { name: "rx"; type: IMAGE;
clip_to: "rx_clip";
description { state: "default" 0.0;
rel1.to: "base";
rel2.to: "base";
image.normal: "rx.png";
}
}
part { name: "tx_spacer"; type: SPACER;
description { state: "default" 0.0;
rel1.relative: 0.0 0.09;
rel2.relative: 1.0 0.91;
}
}
part { name: "tx_clip"; type: RECT;
description { state: "default" 0.0;
rel1.to: "tx_spacer";
rel2.to: "tx_spacer";
}
}
part { name: "tx"; type: IMAGE;
clip_to: "tx_clip";
description { state: "default" 0.0;
rel1.to: "base";
rel2.to: "base";
image.normal: "tx.png";
}
}
part { name: "over";
description { state: "default" 0.0;
rel1.to: "base";
rel2.to: "base";
image.normal: "over.png";
}
}
}
}
/**
* API [e/gadget/popup] This is the group that will be placed inside popups
*/
group { name: "e/gadgets/netspeed/popup";
parts {
// part { name: "label"; type: TEXTBLOCK;
// description { state: "default" 0.0;
// text {
// text: "";
// font: "sans";
// size: 12;
// text_class: "";
// min: 1 1;
// ellipsis: -1;
// }
// }
// }
// part { name: "out_label"; type: TEXT;
// description { state: "default" 0.0;
// rel1.to_y: "in_label";
// rel1.relative: 0.0 1.0;
// rel2.to_y: "in_label";
// rel2.relative: 1.0 1.0;
// text {
// text: "";
// font: "sans";
// size: 12;
// text_class: "";
// min: 1 1;
// ellipsis: -1;
// }
// }
// }
swallow { "main.swallow";
desc { "default";
}
}
}
}
}