commit
6d6b50c3af
88
pinger.py
88
pinger.py
|
@ -50,6 +50,8 @@ import sys
|
||||||
# Vars
|
# Vars
|
||||||
startup_active_label = "✓ Start Automatically"
|
startup_active_label = "✓ Start Automatically"
|
||||||
startup_inactive_label = "Start Automatically"
|
startup_inactive_label = "Start Automatically"
|
||||||
|
pause_label = "Pause"
|
||||||
|
play_label = "Resume"
|
||||||
home_path = os.path.expanduser("~")
|
home_path = os.path.expanduser("~")
|
||||||
startup_path = home_path+'/.config/autostart/pinger.desktop'
|
startup_path = home_path+'/.config/autostart/pinger.desktop'
|
||||||
startup_dir = home_path+'/.config/autostart/'
|
startup_dir = home_path+'/.config/autostart/'
|
||||||
|
@ -96,23 +98,26 @@ else:
|
||||||
|
|
||||||
class Pinger:
|
class Pinger:
|
||||||
ping_log = []
|
ping_log = []
|
||||||
|
paused = False
|
||||||
|
autostart = False
|
||||||
|
|
||||||
def ping(self, widget=None, data=None):
|
def ping(self, widget=None, data=None):
|
||||||
ping = subprocess.Popen(
|
if not self.paused:
|
||||||
["ping", "-c", "1", host],
|
ping = subprocess.Popen(
|
||||||
stdout = subprocess.PIPE,
|
["ping", "-c", "1", host],
|
||||||
stderr = subprocess.PIPE
|
stdout = subprocess.PIPE,
|
||||||
)
|
stderr = subprocess.PIPE
|
||||||
out, error = ping.communicate()
|
)
|
||||||
m = re.search('time=(.*) ms', out)
|
out, error = ping.communicate()
|
||||||
if error or m == None:
|
m = re.search('time=(.*) ms', out)
|
||||||
label = "PING FAIL"
|
if error or m == None:
|
||||||
self.log_ping(-1)
|
label = "PING FAIL"
|
||||||
else:
|
self.log_ping(-1)
|
||||||
label = m.group(1)+" ms"
|
else:
|
||||||
self.log_ping(m.group(1))
|
latency = "%.2f" % float(m.group(1))
|
||||||
self.ind.set_label (label, "100.0 ms")
|
label = latency+" ms"
|
||||||
#self.ping_menu_item.set_label(out)
|
self.log_ping(latency)
|
||||||
|
self.ind.set_label(label, "100.0 ms")
|
||||||
gobject.timeout_add_seconds(self.timeout, self.ping)
|
gobject.timeout_add_seconds(self.timeout, self.ping)
|
||||||
|
|
||||||
def log_ping(self, value):
|
def log_ping(self, value):
|
||||||
|
@ -135,29 +140,31 @@ class Pinger:
|
||||||
print "Quitting..."
|
print "Quitting..."
|
||||||
Gtk.main_quit()
|
Gtk.main_quit()
|
||||||
|
|
||||||
def create_autostart(self, widget, data=None):
|
def toggle_autostart(self, widget, data=None):
|
||||||
if not os.path.exists(startup_dir):
|
if not self.autostart:
|
||||||
os.makedirs(startup_dir)
|
if not os.path.exists(startup_dir):
|
||||||
with open(startup_path,'w') as f:
|
os.makedirs(startup_dir)
|
||||||
f.write("[Desktop Entry]\r\n"
|
with open(startup_path,'w') as f:
|
||||||
"Type=Application\r\n"
|
f.write("[Desktop Entry]\r\n"
|
||||||
"Exec=python "+os.path.abspath( __file__ )+arguments+"\r\n"
|
"Type=Application\r\n"
|
||||||
"X-GNOME-Autostart-enabled=true\r\n"
|
"Exec=python "+os.path.abspath( __file__ )+arguments+"\r\n"
|
||||||
"Name=Pinger\r\n"
|
"X-GNOME-Autostart-enabled=true\r\n"
|
||||||
"Comment=Pings the internet every few seconds")
|
"Name=Pinger\r\n"
|
||||||
self.update_startup_menu()
|
"Comment=Pings the internet every few seconds")
|
||||||
|
self.autostart = True
|
||||||
def remove_autostart(self, widget, data=None):
|
|
||||||
os.remove(startup_path)
|
|
||||||
self.update_startup_menu()
|
|
||||||
|
|
||||||
def update_startup_menu(self):
|
|
||||||
if os.path.exists(startup_path):
|
|
||||||
self.startup_menu.set_label(startup_active_label)
|
self.startup_menu.set_label(startup_active_label)
|
||||||
self.startup_menu.connect("activate", self.remove_autostart, startup_active_label)
|
|
||||||
else:
|
else:
|
||||||
|
os.remove(startup_path)
|
||||||
|
self.autostart = False
|
||||||
self.startup_menu.set_label(startup_inactive_label)
|
self.startup_menu.set_label(startup_inactive_label)
|
||||||
self.startup_menu.connect("activate", self.create_autostart, startup_inactive_label)
|
|
||||||
|
def toggle_pause(self, widget, data=None):
|
||||||
|
if self.paused:
|
||||||
|
self.paused = False
|
||||||
|
self.pause_menu.set_label(pause_label)
|
||||||
|
else:
|
||||||
|
self.paused = True
|
||||||
|
self.pause_menu.set_label(play_label)
|
||||||
|
|
||||||
def update_log_menu(self):
|
def update_log_menu(self):
|
||||||
graph = ""
|
graph = ""
|
||||||
|
@ -192,9 +199,16 @@ class Pinger:
|
||||||
|
|
||||||
# create a menu
|
# create a menu
|
||||||
self.menu = Gtk.Menu()
|
self.menu = Gtk.Menu()
|
||||||
|
# with pause option
|
||||||
|
self.pause_menu = self.create_menu_item(pause_label, self.toggle_pause)
|
||||||
# with autostart option
|
# with autostart option
|
||||||
self.startup_menu = self.create_menu_item(startup_inactive_label, self.create_autostart)
|
# first, check current autostart state by checking existance of .desktop file
|
||||||
self.update_startup_menu()
|
try:
|
||||||
|
with open(startup_dir):
|
||||||
|
self.autostart = True
|
||||||
|
except IOError:
|
||||||
|
self.autostart = False
|
||||||
|
self.startup_menu = self.create_menu_item(startup_inactive_label, self.toggle_autostart)
|
||||||
# and log display
|
# and log display
|
||||||
self.log_menu = self.create_menu_item("Ping Log", None)
|
self.log_menu = self.create_menu_item("Ping Log", None)
|
||||||
# and exit option
|
# and exit option
|
||||||
|
|
Loading…
Reference in New Issue
Block a user