Developing my first plasmoid: The Data Engine (in python)
By EricMesa
- 3 minutes read - 626 wordsI figured it’d be neat to show you how my plasmoid works so you could use it when developing your own plasmoids. Here’s the main.py of my data engine. The indentation is off, in case you try to copy and past this in.
Here are the imports:
from PyQt4.QtCore import * from PyKDE4.kdecore import * from PyKDE4 import plasmascript #for flickr import views
Those are pretty standard. The last one is the part of my engine that interacts with flickr. Right now I have some work to do to get that presentable, but all you need to know is that it outputs XML to this main part of the data engine.
class PyFlickrEngine(plasmascript.DataEngine): def __init__(self,parent,args=None): plasmascript.DataEngine.__init__(self,parent)
def init(self): self.setMinimumPollingInterval(333) #for flickr views.initialize()
The above is mostly standard. Below are the sources. These are the sources you’ll present to any plasmoids that want to grab from your data engine.
def sources(self): sources = [“25”, “50”, “75”, “100”, “200”, “300”, “400”, “500”, “600”, “700”, “800”, “900”, “1000”, “1250”, “1500”, “1750”, “2000”,“3000”, “4000”, “5000”, “10000”] return sources
The following is also pretty standard:
def sourceRequestEvent(self, name): print “source request event” #debugging return self.updateSourceEvent(name)
Here’s the main part of the engine. This is how the data actually ends up in the data engine. So I’m updating all of my sources here:
def updateSourceEvent(self,group): print “updateSourceEvent” #grouplist = [] if group == “25”: print “i’m @ 25” #debug grouplist = views.analyzeviews(views.views25, views.views50) #self.setData(grouplist, “Group List”, QVariant.List) #original line self.setData(“25”, “Group 25”, grouplist) elif group == “50”: print “i’m @ 50” #debug grouplist = views.analyzeviews(views.views50, views.views75) self.setData(“50”,“Group 50”, grouplist) elif group == “75”: print “i’m @ 75” #debug grouplist = views.analyzeviews(views.views75,views.views100) self.setData(“75”,“Group 75”, grouplist) elif group == “100”: print “i’m @ 100” #debug grouplist = views.analyzeviews(views.views100,views.views200) self.setData(“100”,“Group 100”, grouplist) elif group == “200”: print “i’m @ 200” #debug grouplist = views.analyzeviews(views.views200, views.views300) self.setData(“200”,“Group 200”, grouplist) elif group == “300”: print “i’m @ 300” #debug grouplist = views.analyzeviews(views.views300, views.views400) self.setData(“300”,“Group 300”, grouplist) elif group == “400”: print “i’m @ 400” #debug grouplist = views.analyzeviews(views.views400, views.views500) self.setData(“400”,“Group 400”, grouplist) elif group == “500”: print “i’m @ 500” #debug grouplist = views.analyzeviews(views.views500, views.views600) self.setData(“500”,“Group 500”, grouplist) elif group == “600”: print “i’m @ 600” #debug grouplist = views.analyzeviews(views.views600, views.views700) self.setData(“600”,“Group 600”, grouplist) elif group == “700”: print “i’m @ 700” #debug grouplist = views.analyzeviews(views.views700, views.views800) self.setData(“700”,“Group 700”, grouplist) elif group == “800”: print “i’m @ 800” #debug grouplist = views.analyzeviews(views.views800, views.views900) self.setData(“800”,“Group 800”, grouplist) elif group == “900”: print “i’m @ 900” #debug grouplist = views.analyzeviews(views.views900, views.views1000) self.setData(“900”,“Group 900”, grouplist) elif group == “1000”: print “i’m @ 1000” #debug grouplist = views.analyzeviews(views.views1000, views.views1250) self.setData(“1000”,“Group 1000”, grouplist) elif group == “1250”: print “i’m @ 1250” #debug grouplist = views.analyzeviews(views.views1250, views.views1500) self.setData(“1250”,“Group 1250”, grouplist) elif group == “1500”: print “i’m @ 1500” #debug grouplist = views.analyzeviews(views.views1500, views.views1750) self.setData(“1500”,“Group 1500”, grouplist) elif group == “1750”: print “i’m @ 1750” #debug grouplist = views.analyzeviews(views.views1750, views.views2000) self.setData(“1750”,“Group 1750”, grouplist) elif group == “2000”: print “i’m @ 2000” #debug grouplist = views.analyzeviews(views.views2000, views.views3000) self.setData(“2000”,“Group 2000”, grouplist) elif group == “3000”: print “i’m @ 3000” #debug grouplist = views.analyzeviews(views.views3000, views.views4000) self.setData(“3000”,“Group 3000”, grouplist) elif group == “4000”: print “i’m @ 4000” #debug grouplist = views.analyzeviews(views.views4000, views.views5000) self.setData(“4000”,“Group 4000”, grouplist) elif group == “5000”: print “i’m @ 5000” #debug grouplist = views.analyzeviews(views.views5000, views.views10000) self.setData(“5000”,“Group 5000”, grouplist) elif group == “10000”: print “i’m @ 10000” #debug grouplist = views.analyzeviews(views.views10000, views.views10000) self.setData(“10000”,“Group 10000”, grouplist) return True
And, finally, initializing the engine - a standard part:
def CreateDataEngine(parent): return PyFlickrEngine(parent)
As you can see, writing a data engine in python is pretty simple. Most of the work is done in the other python file, but the data engine part is so simple, eh? Between this and the official tutorial you should be in pretty good shape.