Commits

Tanel Liiv  committed 79ef502

Added plugins

  • Participants

Comments (0)

Files changed (8)

+#!/usr/bin/env python
+
+import pygtk
+pygtk.require('2.0')
+import gtk
+import gobject
+import time
+# my plugins folder
+import plugins
+
+global tin
+
+class TinPanel:
+    # This is a callback function. The data arguments are ignored
+    # in this example. More on callbacks below.
+    def refresh(widget, data=None):
+        global tin
+        tin.window.activate_focus()
+        #print "Hello World"
+        string = ""
+        for plugin in plugins.MODULES:
+            data = plugin()
+            string += "".join(map(tin.colourer, data))
+        tin.label.set_markup(string)
+        return True
+
+    def colourer(self, items):
+        print ">",items,"<"
+        # [["color", "text"], ....]
+        if len(items) == 3:
+            padding = items[2]
+        else:
+            padding = 5
+        outp = ""
+        outp +=  ('<span font="ProFontWindows 6" foreground="%s">%'+str(padding)+'s</span>')%(items[0], items[1])
+        return outp
+
+    def delete_event(self, widget, event, data=None):
+        # If you return FALSE in the "delete_event" signal handler,
+        # GTK will emit the "destroy" signal. Returning TRUE means
+        # you don't want the window to be destroyed.
+        # This is useful for popping up 'are you sure you want to quit?'
+        # type dialogs.
+        print "delete event occurred"
+
+        # Change FALSE to TRUE and the main window will not be destroyed
+        # with a "delete_event".
+        return False
+
+    def destroy(self, widget, data=None):
+        print "destroy signal occurred"
+        gtk.main_quit()
+
+    def __init__(self):
+        
+        # create a new window
+        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
+        self.window.set_decorated(False)
+        self.window.set_keep_above(True)
+        self.window.resize(1366, 1)
+        self.window.set_skip_taskbar_hint (True)
+        self.window.set_skip_pager_hint (True)
+    
+        # When the window is given the "delete_event" signal (this is given
+        # by the window manager, usually by the "close" option, or on the
+        # titlebar), we ask it to call the delete_event () function
+        # as defined above. The data passed to the callback
+        # function is NULL and is ignored in the callback function.
+        self.window.connect("delete_event", self.delete_event)
+    
+        # Here we connect the "destroy" event to a signal handler.  
+        # This event occurs when we call gtk_widget_destroy() on the window,
+        # or if we return FALSE in the "delete_event" callback.
+        self.window.connect("destroy", self.destroy)
+    
+        # Sets the border width of the window.
+        self.window.set_border_width(0)
+
+        # Create Table for widgets
+        self.hbox = gtk.HBox(homogeneous=False)
+        
+        # Create Textfield for all "information" output
+        self.label = gtk.Label()
+        #self.label.set_markup(labeltext)
+        
+        # Creates a new button with the label "Hello World".
+        #self.button = gtk.Button("HULLLAAAA")
+    
+        # When the button receives the "clicked" signal, it will call the
+        # function hello() passing it None as its argument.  The hello()
+        # function is defined above.
+        #self.button.connect("clicked", self.hello, None)
+    
+        # This will cause the window to be destroyed by calling
+        # gtk_widget_destroy(window) when "clicked".  Again, the destroy
+        # signal could come from here, or the window manager.
+        #self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
+    
+        # Attach button to table
+        #self.hbox.add(self.button)
+        self.hbox.add(self.label)
+        self.hbox.set_child_packing(self.label, expand=False, fill=False, padding=False, pack_type=0)
+        
+        #Build an eventbox around hbox
+        self.eb = gtk.EventBox()
+        self.eb.add(self.hbox)
+        self.eb.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
+        
+        # This packs the button into the window (a GTK container).
+        self.window.add(self.eb)
+    
+        # Add timer event to window
+        gobject.timeout_add(1000, self.refresh)
+    
+        # The final step is to display this newly created widget.
+        self.window.show_all()
+
+    def main(self):
+        # All PyGTK applications must have a gtk.main(). Control ends here
+        # and waits for an event to occur (like a key press or mouse event).
+        gtk.main()
+
+# If the program is run directly or passed as an argument to the python
+# interpreter then create a HelloWorld instance and show it
+if __name__ == "__main__":
+    tin = TinPanel()
+    tin.main()

File plugins/__init__.py

+import cpuinfo
+import meminfo
+import loadavg
+import temp
+import mpdinfo
+
+MODULES = [cpuinfo.cpuinfo, temp.temp, meminfo.meminfo, loadavg.loadavg, mpdinfo.mpdsong]

File plugins/cpuinfo.py

+import os
+
+def cpuinfo():
+    x = os.popen('cat /proc/cpuinfo | grep "cpu MHz" | tr -d "\n\tcpuMHz:" | tr -s " " | sed -r "s/.000//g"').read()[1:].split(" ")
+    out = []
+    mapper = {'800': 'green', '1600': 'yellow', '2133': 'red'}
+    out.append([mapper[x[0]], x[0], 4])
+    out.append([mapper[x[1]], x[1], 4])
+    out.append(['white', " |", 0])
+    return out
+        

File plugins/helloworld.py

+def main():
+    return [['white', 'Hello world']]

File plugins/loadavg.py

+import os
+
+def loadavg():
+    x = os.popen("""cat /proc/loadavg | awk '{print $1 " "  $2 " " $3}' | tr -d '\n'""").read()
+    u = lambda x: 'green' if x<0.75 else ('yellow' if x<1.5 else 'red')
+    x = x.split(" ")
+    
+    out = []
+    out.append(['red', " (", 0])
+    out.append([u(float(x[0])), x[0]])
+    out.append([u(float(x[1])), x[1]])
+    out.append([u(float(x[2])), x[2]])
+    out.append(['red', " ) ", 0])
+    return out

File plugins/meminfo.py

+import os
+
+def meminfo():
+    x = int(os.popen("""free -t | grep "-" | awk '{ print $4 }'""").read())
+    x /= 1024
+    return [['white', x, 5]]

File plugins/mpdinfo.py

+import mpd
+
+def mpdsong():
+    client = mpd.MPDClient()
+        
+    try:
+        playing = client.currentsong()
+    except:
+        client = mpd.MPDClient()
+        client.connect("localhost", 6600)
+
+    try:
+        playing = client.currentsong()
+        if 'artist' in playing:
+            artist = ['orange', unicode(playing['artist'])]
+            song = ['white', unicode(playing['title'])]
+            out = [artist, ['orange', " - ", 0] + song]
+        elif 'title' in playing:
+            title = playing['title']
+            title = title.split("-")
+            artist = ['orange', title[0]]
+            song = ['white', title[1]]
+            out = [artist, ['red', "-", 0], song]
+        else:
+            out = [["red", "MPD is sleepy", 0]]
+    except Exception as e:
+        print e
+        return [["red", "MPD is dead", 0]]
+    client.disconnect()       
+    return out

File plugins/temp.py

+import os
+
+def temp():
+    x = os.popen("""cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2}' | tr -d '\n'""").read()
+    out = ""
+    col = "white"
+       
+    x = int(x)
+    if x <= 30:
+        col = 'blue'
+    elif x <= 55:
+        col = 'green'
+    elif x <= 65:
+        col = 'yellow'
+    else:
+        col = 'red'
+    return [[col, x, 3],['white', "C", 0]]