Refactoring for cleanliness
This commit is contained in:
parent
383f078ee6
commit
4b1af303ba
93
pinger.py
93
pinger.py
|
@ -24,20 +24,31 @@
|
||||||
# <http://www.gnu.org/licenses/>
|
# <http://www.gnu.org/licenses/>
|
||||||
#
|
#
|
||||||
|
|
||||||
|
# User-editable variables
|
||||||
|
host = "4.2.2.2" # IP or hostname
|
||||||
|
ping_frequency = 5 # in seconds
|
||||||
|
|
||||||
|
#
|
||||||
|
# Dependencies
|
||||||
|
#
|
||||||
|
|
||||||
|
# System Tray Icon
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
from gi.repository import AppIndicator3 as appindicator
|
from gi.repository import AppIndicator3 as appindicator
|
||||||
|
|
||||||
# Timer
|
# Timer
|
||||||
from gi.repository import GObject as gobject
|
from gi.repository import GObject as gobject
|
||||||
# Pinging
|
# Pinging
|
||||||
import subprocess
|
import subprocess
|
||||||
# Regex
|
# Regex
|
||||||
import re
|
import re
|
||||||
|
# Ctrl-c
|
||||||
|
import signal
|
||||||
|
|
||||||
# Vars
|
#
|
||||||
host = "4.2.2.2"
|
# Main Class
|
||||||
|
#
|
||||||
|
|
||||||
class HelloWorld:
|
class Pinger:
|
||||||
|
|
||||||
def ping(self, widget=None, data=None):
|
def ping(self, widget=None, data=None):
|
||||||
ping = subprocess.Popen(
|
ping = subprocess.Popen(
|
||||||
|
@ -51,60 +62,48 @@ class HelloWorld:
|
||||||
else:
|
else:
|
||||||
m = re.search('time=(.*) ms', out)
|
m = re.search('time=(.*) ms', out)
|
||||||
label = m.group(1)+" ms"
|
label = m.group(1)+" ms"
|
||||||
ind.set_label (label, "100.0 ms")
|
self.ind.set_label (label, "100.0 ms")
|
||||||
#self.ping_menu_item.set_label(out)
|
#self.ping_menu_item.set_label(out)
|
||||||
gobject.timeout_add_seconds(self.timeout, self.ping)
|
gobject.timeout_add_seconds(self.timeout, self.ping)
|
||||||
|
|
||||||
|
def create_menu_item(self, text, callback):
|
||||||
|
menu_item = Gtk.MenuItem(text)
|
||||||
|
self.menu.append(menu_item)
|
||||||
|
menu_item.connect("activate", callback, text)
|
||||||
|
menu_item.show()
|
||||||
|
|
||||||
def destroy(self, widget, data=None):
|
def destroy(self, widget, data=None):
|
||||||
print "destroy signal occurred"
|
print "destroy signal occurred"
|
||||||
Gtk.main_quit()
|
Gtk.main_quit()
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# register a periodic timer
|
# Handle ctrl-c
|
||||||
|
signal.signal(signal.SIGINT, self.destroy)
|
||||||
|
|
||||||
|
# Create systray icon
|
||||||
|
self.ind = appindicator.Indicator.new (
|
||||||
|
"pinger",
|
||||||
|
"", # no icon
|
||||||
|
appindicator.IndicatorCategory.COMMUNICATIONS)
|
||||||
|
self.ind.set_status (appindicator.IndicatorStatus.ACTIVE)
|
||||||
|
self.ind.set_label ("0.0 ms", "100.0 ms")
|
||||||
|
|
||||||
|
# create a menu
|
||||||
|
self.menu = Gtk.Menu()
|
||||||
|
self.create_menu_item("Exit", self.destroy)
|
||||||
|
self.ind.set_menu(self.menu)
|
||||||
|
|
||||||
|
# start the ping process
|
||||||
self.counter = 0
|
self.counter = 0
|
||||||
self.timeout = 10
|
self.timeout = ping_frequency
|
||||||
gobject.timeout_add_seconds(self.timeout, self.ping)
|
|
||||||
self.ping()
|
self.ping()
|
||||||
|
|
||||||
|
# Begin runtime loop
|
||||||
|
Gtk.main()
|
||||||
|
|
||||||
def menuitem_response(w, buf):
|
#
|
||||||
print buf
|
# Runtime
|
||||||
|
#
|
||||||
|
|
||||||
def create_menu_item(menu, text, callback):
|
|
||||||
|
|
||||||
menu_items = Gtk.MenuItem(text)
|
|
||||||
|
|
||||||
menu.append(menu_items)
|
|
||||||
|
|
||||||
menu_items.connect("activate", callback, text)
|
|
||||||
|
|
||||||
# show the items
|
|
||||||
menu_items.show()
|
|
||||||
|
|
||||||
return menu_items
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ind = appindicator.Indicator.new (
|
pinger = Pinger()
|
||||||
"pinger",
|
|
||||||
"", #indicator-messages
|
|
||||||
appindicator.IndicatorCategory.COMMUNICATIONS)
|
|
||||||
ind.set_status (appindicator.IndicatorStatus.ACTIVE)
|
|
||||||
#ind.set_attention_icon ("indicator-messages-new")
|
|
||||||
ind.set_label ("0.0 ms", "100.0 ms")
|
|
||||||
|
|
||||||
# create a menu
|
|
||||||
menu = Gtk.Menu()
|
|
||||||
|
|
||||||
# and the app
|
|
||||||
hello = HelloWorld()
|
|
||||||
|
|
||||||
# create menu items
|
|
||||||
#hello.ping_menu_item = create_menu_item(menu, "Ping", hello.ping)
|
|
||||||
create_menu_item(menu, "Exit", hello.destroy)
|
|
||||||
|
|
||||||
# Add the menu to our statusbar
|
|
||||||
ind.set_menu(menu)
|
|
||||||
|
|
||||||
# Runtime loop
|
|
||||||
Gtk.main()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user