nsb

WifiMonitor

nsb
Sep 30th, 2011
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.89 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. """wifimon
  4.  
  5. wifimon monitors your wifi connection and sits in your systray
  6. """
  7.  
  8. # Imports
  9. #import os, sys
  10. import re       # regular expression
  11. import time     # for sleep()
  12. import signal   # To catch sig int (ctrl-c)
  13. import sys      # To catch sig int (ctrl-c)
  14.  
  15. from PyQt4 import QtGui, QtCore
  16.  
  17. # All the configurations
  18. class Conf:
  19.     def __init__(self):
  20.         self.interface = "no interface assigned!"
  21.         self.percentage = -1
  22.         self.intervall = 10
  23.         self.statfile = "/proc/net/wireless"
  24.         #self.statfile = "test_wlan"
  25.         #self.statfile = "test_nowlan"
  26.         self.iconPath = "./img/"
  27.  
  28. # Read in the configuration file
  29. def read_conf():
  30.     return 0
  31.    
  32. # The sys tray
  33. class WifiIcon(QtGui.QSystemTrayIcon):
  34.     def __init__(self, parent=None):
  35.         QtGui.QSystemTrayIcon.__init__(self, parent)
  36.  
  37.     def event(self, event):
  38.         if event == QtCore.QEvent.Show:
  39.             self.emit(QtCore.SIGNAL("startCycle()"))
  40.  
  41.         return 0
  42.  
  43.  
  44. # The main class monitoring wifi
  45. class WifiMonitor(QtGui.QWidget):
  46.     # The signals; can't be in constructor
  47.     start = QtCore.pyqtSignal()
  48.     show_tray = QtCore.pyqtSignal()
  49.     signal_get_status = QtCore.pyqtSignal()
  50.     signal_display_status = QtCore.pyqtSignal()
  51.     signal_busy = QtCore.pyqtSignal()
  52.  
  53.     def __init__(self):
  54.         QtGui.QWidget.__init__(self)
  55.         self.createActions()
  56.         self.createTrayIcon()
  57.         self.trayIcon.show()
  58.  
  59.     def createActions(self):
  60.         self.quitAction = QtGui.QAction(self.tr("&Quit"), self)
  61.         QtCore.QObject.connect(self.quitAction, QtCore.SIGNAL("triggered()"),
  62.                     QtGui.qApp, QtCore.SLOT("quit()"))
  63.         #self.start.connect(self.run)
  64.         self.signal_get_status.connect(self.get_status)
  65.         self.signal_display_status.connect(self.display_status)
  66.         self.signal_busy.connect(self.busy)
  67.         self.connect(self, QtCore.SIGNAL("startCycle"), self.get_status)
  68.  
  69.     def createTrayIcon(self):
  70.         self.trayIconMenu = QtGui.QMenu(self)
  71.         self.trayIconMenu.addAction(self.quitAction)
  72.  
  73.         #self.trayIcon = QtGui.QSystemTrayIcon(self)
  74.         self.trayIcon = WifiIcon(self)
  75.         self.trayIcon.setContextMenu(self.trayIconMenu)
  76.         #pathToIcon = "/usr/share/icons/default.kde4/22x22/devices/network-wireless-connected-100.png"
  77.         pathToIcon = conf.iconPath + "wifi_none.svg"
  78.         self.trayIcon.setIcon(QtGui.QIcon(pathToIcon))
  79.         self.show_tray.connect(self.trayIcon.show)
  80.  
  81.     #def showEvent(self, event):
  82.     #    #self.signal_busy.emit()
  83.     #    self.emit(QtCore.SIGNAL("startCycle()"))
  84.  
  85.     # Read in the satus of the connection
  86.     def get_status(self):
  87.    
  88.         # open status file and discard first two lines
  89.         f = open(conf.statfile)
  90.         f.readline()
  91.         f.readline()
  92.    
  93.         l = f.readline()
  94.         if (len(l) != 0):
  95.             print(l)
  96.             m = re.search("(\S+\d):[ ]+(\d+)[ ]+(\d+)\.", l)
  97.             conf.interface = m.group(1)
  98.             #status = m.group(2)
  99.             conf.percentage = int(m.group(3))
  100.    
  101.         self.signal_display_status.emit()
  102.    
  103.     # Display the status
  104.     def display_status(self):
  105.    
  106.         if (conf.percentage >= 0):
  107.             print("%s @ %d" % (conf.interface, conf.percentage))
  108.             if conf.percentage:
  109.                 filePercentage = (int((conf.percentage - 1) / 25) + 1) * 25
  110.             else:
  111.                 filePercentage = 0
  112.             pathToIcon = conf.iconPath + "wifi_" + str(filePercentage) + ".svg"
  113.             self.trayIcon.setIcon(QtGui.QIcon(pathToIcon))
  114.         else:
  115.             print("No wifi connection detected!")
  116.             pathToIcon = conf.iconPath + "wifi_none.svg"
  117.             self.trayIcon.setIcon(QtGui.QIcon(pathToIcon))
  118.    
  119.         print(pathToIcon)
  120.         self.show_tray.emit()
  121.         self.signal_busy.emit()
  122.  
  123.     def busy(self):
  124.         time.sleep(conf.intervall)
  125.         self.signal_get_status.emit()
  126.  
  127.     ## main loop
  128.     #def run(self):
  129.     #    while True:
  130.     #        self.get_status()
  131.     #        self.display_status()
  132.     #        time.sleep(conf.intervall)
  133.  
  134. # What to do, in the case of interruption
  135. def signal_handler(signal, frame):
  136.     print("\nProgram was interrupted!")
  137.     sys.exit(0)
  138.  
  139.  
  140. if (__name__ == "__main__"):
  141.     signal.signal(signal.SIGINT, signal_handler)
  142.     conf = Conf()
  143.     read_conf()
  144.    
  145.     app = QtGui.QApplication(sys.argv)
  146.     wifiMon = WifiMonitor()
  147.     #icon.start.emit()
  148.     #icon.show_tray.emit()
  149.     #icon.signal_get_status.emit()
  150.  
  151.     #timer = QtCore.QTimer()
  152.     ##timer.singleShot(10, icon.signal_get_status.emit)
  153.     #QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"),
  154.     #                       icon.signal_get_status.emit())
  155.     #                       #icon,  QtCore.SLOT("signal_get_status()"))
  156.     #timer.start(0)
  157.     app.exec_()
  158.     sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment