CrunchBang Linux Pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

CrunchBang Linux Pastebin

Posted by iggykoopa on Sun 26th Apr 08:54 (modification of post by iggykoopa view diff)
diff | download | new post

  1. #!/usr/bin/env python
  2.  
  3. import os,commands,pygtk,pynotify,gtk,datetime
  4.  
  5. def check(interval):
  6.  
  7.     sizes = ["kB", "MB", "GB"]
  8.     tx,rx,total = 0.0,0.0,0.0
  9.     tx_size,rx_size,total_size = "kB","kB","kB"
  10.     date = datetime.date.today()
  11.     day,month,year = date.day,date.month,date.year
  12.     if day < 10:
  13.         day = "0" + str(day)
  14.     else:
  15.         day = str(day)
  16.     if month < 10:
  17.         month = "0" + str(month)
  18.     else:
  19.         month = str(month)   
  20.     if interval == "daily":
  21.         vnstat = commands.getoutput("vnstat -d").split("\n")
  22.         for line in vnstat:
  23.             if  "%s.%s." % (day, month) in line:
  24.                 stats = line.replace("|","").split()
  25.                 if rx_size == stats[2]:
  26.                     rx = rx + float(stats[1])
  27.                 elif sizes.index(rx_size) > sizes.index(stats[2]):
  28.                     difference = float(sizes.index(rx_size) - sizes.index(stats[2]))
  29.                     rx = rx + (float(stats[1]) / 1024.0 ** difference)
  30.                 else:
  31.                     difference = float(sizes.index(stats[2]) - sizes.index(rx_size))
  32.                     rx = float(stats[1]) + ( rx / 1024.0 ** difference)
  33.                     rx_size = stats[2]
  34.                 if tx_size == stats[4]:
  35.                     tx = tx + float(stats[3])
  36.                 elif sizes.index(tx_size) > sizes.index(stats[4]):
  37.                     difference = float(sizes.index(tx_size) - sizes.index(stats[4]))
  38.                     tx = tx + (float(stats[3]) / 1024.0 ** difference)
  39.                 else:
  40.                     difference = float(sizes.index(stats[4]) - sizes.index(tx_size))
  41.                     tx = float(stats[3]) + ( tx / 1024.0 ** difference)
  42.                     tx_size = stats[4]
  43.                 if total_size == stats[6]:
  44.                     total = total + float(stats[5])
  45.                 elif sizes.index(total_size) > sizes.index(stats[6]):
  46.                     difference = float(sizes.index(total_size) - sizes.index(stats[6]))
  47.                     total = total + (float(stats[5]) / 1024.0 ** difference)
  48.                 else:
  49.                     difference = float(sizes.index(stats[6]) - sizes.index(total_size))
  50.                     total = float(stats[5]) + ( total / 1024.0 ** difference)
  51.                     total_size = stats[6]
  52.     if interval == "weekly":
  53.         vnstat = commands.getoutput("vnstat -w").split("\n")
  54.         for line in vnstat:
  55.             if "current week" in line:
  56.                 stats = line.replace("|","").split()
  57.                 if rx_size == stats[3]:
  58.                     rx = rx + float(stats[2])
  59.                 elif sizes.index(rx_size) > sizes.index(stats[3]):
  60.                     difference = float(sizes.index(rx_size) - sizes.index(stats[3]))
  61.                     rx = rx + (float(stats[2]) / 1024.0 ** difference)
  62.                 else:
  63.                     difference = float(sizes.index(stats[3]) - sizes.index(rx_size))
  64.                     rx = float(stats[2]) + ( rx / 1024.0 ** difference)
  65.                     rx_size = stats[3]
  66.                 if tx_size == stats[5]:
  67.                     tx = tx + float(stats[4])
  68.                 elif sizes.index(tx_size) > sizes.index(stats[5]):
  69.                     difference = float(sizes.index(tx_size) - sizes.index(stats[5]))
  70.                     tx = tx + (float(stats[4]) / 1024.0 ** difference)
  71.                 else:
  72.                     difference = float(sizes.index(stats[5]) - sizes.index(tx_size))
  73.                     tx = float(stats[4]) + ( tx / 1024.0 ** difference)
  74.                     tx_size = stats[5]
  75.                 if total_size == stats[7]:
  76.                     total = total + float(stats[6])
  77.                 elif sizes.index(total_size) > sizes.index(stats[7]):
  78.                     difference = float(sizes.index(total_size) - sizes.index(stats[7]))
  79.                     total = total + (float(stats[6]) / 1024.0 ** difference)
  80.                 else:
  81.                     difference = float(sizes.index(stats[7]) - sizes.index(total_size))
  82.                     total = float(stats[6]) + ( total / 1024.0 ** difference)
  83.                     total_size = stats[7]
  84.     if interval == "monthly":
  85.         vnstat = commands.getoutput("vnstat -m").split("\n")
  86.         for line in vnstat:
  87.             if date.strftime("%b") in line:
  88.                 stats = line.replace("|","").split()
  89.                 if rx_size == stats[3]:
  90.                     rx = rx + float(stats[2])
  91.                 elif sizes.index(rx_size) > sizes.index(stats[3]):
  92.                     difference = float(sizes.index(rx_size) - sizes.index(stats[3]))
  93.                     rx = rx + (float(stats[2]) / 1024.0 ** difference)
  94.                 else:
  95.                     difference = float(sizes.index(stats[3]) - sizes.index(rx_size))
  96.                     rx = float(stats[2]) + ( rx / 1024.0 ** difference)
  97.                     rx_size = stats[3]
  98.                 if tx_size == stats[5]:
  99.                     tx = tx + float(stats[4])
  100.                 elif sizes.index(tx_size) > sizes.index(stats[5]):
  101.                     difference = float(sizes.index(tx_size) - sizes.index(stats[5]))
  102.                     tx = tx + (float(stats[4]) / 1024.0 ** difference)
  103.                 else:
  104.                     difference = float(sizes.index(stats[5]) - sizes.index(tx_size))
  105.                     tx = float(stats[4]) + ( tx / 1024.0 ** difference)
  106.                     tx_size = stats[5]
  107.                 if total_size == stats[7]:
  108.                     total = total + float(stats[6])
  109.                 elif sizes.index(total_size) > sizes.index(stats[7]):
  110.                     difference = float(sizes.index(total_size) - sizes.index(stats[7]))
  111.                     total = total + (float(stats[6]) / 1024.0 ** difference)
  112.                 else:
  113.                     difference = float(sizes.index(stats[7]) - sizes.index(total_size))
  114.                     total = float(stats[6]) + ( total / 1024.0 ** difference)
  115.                     total_size = stats[7]
  116.     pynotify.init("VNstat-icon")
  117.     n = pynotify.Notification("recieve: %s%s\ntransmit: %s%s\ntotal: %s%s" % (rx, rx_size, tx, tx_size, total, total_size))
  118.     n.attach_to_status_icon(icon)
  119.     n.show()
  120.    
  121. def change_interval(new_interval):
  122.    
  123.     global interval
  124.     interval = new_interval
  125.    
  126. def exit_app(data=None):
  127.     "exit application"
  128.    
  129.     gtk.main_quit()
  130.  
  131. def make_menu(event_button, event_time, data=None):
  132.     "make the right click menu"
  133.    
  134.     menu = gtk.Menu()
  135.    
  136.     daily = gtk.MenuItem("Daily")
  137.     weekly = gtk.MenuItem("Weekly")
  138.     monthly = gtk.MenuItem("Monthly")
  139.     menu.append(daily)
  140.     menu.append(weekly)
  141.     menu.append(monthly)
  142.     daily.connect_object("activate", change_interval, "daily")
  143.     weekly.connect_object("activate", change_interval, "weekly")
  144.     monthly.connect_object("activate", change_interval, "monthly")
  145.  
  146.     separator1 = gtk.MenuItem()
  147.     menu.append(separator1)
  148.     separator1.show()
  149.        
  150.     #show the exit option no matter what is configured
  151.     exit_item = gtk.MenuItem("Exit")
  152.     menu.append(exit_item)
  153.     exit_item.connect_object("activate", exit_app, "Exit App")
  154.     daily.show()
  155.     weekly.show()
  156.     monthly.show()
  157.     exit_item.show()
  158.  
  159.     #Popup the menu
  160.     menu.popup(None, None, None, event_button,
  161.         event_time)
  162.        
  163. def on_right_click(data, event_button, event_time):
  164.     "set the right click tray icon menu"
  165.    
  166.     make_menu(event_button, event_time)
  167.  
  168. def on_left_click(event):
  169.     "make the preferences menu"
  170.  
  171.     global interval
  172.     check(interval)
  173.    
  174. if __name__ == "__main__":
  175.     "main section of program"
  176.    
  177.     global interval
  178.    
  179.     interval = "daily"
  180.     # Set icon, right and left click menus
  181.     icon = gtk.status_icon_new_from_stock(gtk.STOCK_NETWORK)
  182.     icon.connect('popup-menu', on_right_click)
  183.     icon.connect('activate', on_left_click)
  184.    
  185.     gtk.main()

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me