Skip to main content

KRunner in Python (I)

This is my first post, so I don't really know how to start... [DONE].

My goal is to improve the usability of the time tracking software included in KDE: KTimeTracker, just because I don't want to spend time to track it.

I'll try to expose the whole (good! It'll mean that I found a solution!) of the process I am following to get a working KDE Runner written in Python.

KDE4 (actually Plasma) brings a very useful tool called KRunner. It's a launcher that can be customized with our own actions. The plugins that implements this functionality are Runners.

So, after looking for a tutorial or example, I found the tutorials page for Plasma and, starting from the generic plasmoid tutorial, I developed a first version that looks like:

# -*- coding: utf-8 -*-
from PyKDE4.plasma import Plasma
import sys
sys.path.append('/usr/share/kde4/apps/plasma_scriptengine_python')
import plasma_importer


class PythonRunner(Plasma.RunnerScript):
    importer = None

    def __init__(self, parent):
        super(PythonRunner, self).__init__(parent)
        if PythonRunner.importer is None:
            PythonRunner.importer = plasma_importer.PlasmaImporter()
        self.initialized = False

    def init(self):
        self.m_moduleName = str(self.runner().name())
        self.plugin_name = self.m_moduleName.replace('-', '_')
        PythonRunner.importer.register_top_level(self.plugin_name,
           str(self.runner().package().path()))
        self.initialized = True
        return True

    def __dtor__(self):
        PythonRunner.importer.unregister_top_level(self.plugin_name)
        self.pyrunner = None

    def match(self, search):
        if not search.isValid():
            return
            
        term = search.query()
        if term.length < 2:
            return

        m = Plasma.QueryMatch(self)
        m.setType(Plasma.QueryMatch.ExactMatch)
        m.setText('You said "%s"' % term)
        m.setData(term)
        search.addMatch(m)

def run(self, search, action):
        print(action.data().toString())


def CreatePlugin(widget_parent, parent, component_data):
    return PythonRunner(parent)

To highlight:
  1. match(...) method receives the query from KRunner and fills the response with the found matches (if any).
  2. run(...) method receives the match that user selects in KRunner if it's one of the ours. Through the action.data() method, we reach the QVariant object stored in the match(...) method.
Everything else is explained in the tutorial, with the only exception of the install command, which should be:
plasmapkg -t runner -i hello-python.zip

Then you have to summon KRunner (usually Alt+F2) and activate the new Runner in Settings->Plugins.

Wonderful! But... it doesn't work!

To be continued...

Comments

Post a Comment

Popular posts from this blog

KRunner and Skype with a Plasma Python runner

After the odyssey of developing a KDE Plasma runner in Python I have finally develop a simple one that interacts with Skype. You can read the full story (part 1 , 2 , 3 and 4 ) or just the (useful part)  modifications to my KDE  and the final example . You can download this version from here or download the latest (and beta) version from the bazaar repository... To uninstall it just... I hope to get a fully stable version very soon and then, I will publish a downloadable file in project page and a deb package in my PPA .

KRunner in Python (II)

Digging into the way that kde handles services, I've found that this code emulates its behaviour : from PyQt4 import QtGui from PyKDE4.kdecore import KServiceTypeTrader QtGui.QApplication([]) constraint = "[X-Plasma-API] == '%s' and '%s' in [X-Plasma-ComponentTypes]" KServiceTypeTrader.self().query("Plasma/ScriptEngine", constraint % ("python", "Runner")) # -> [] KServiceTypeTrader.self().query("Plasma/ScriptEngine", constraint % ("python", "Applet")) # -> [<PyKDE4.kdecore.KService object at 0xb75bee6c>] KServiceTypeTrader.self().query("Plasma/ScriptEngine", constraint % ("python", "DataEngine")) # -> [<pykde4.kdecore.kservice 0xb75befac="" at="" object="">] Conclusion: There is no Python script engine for runners but there is for applets and data engines. :( To be continued (again)...

Keep It Simple, Stupid!

KISS stands for the guiding principle " keep it simple and stupid ". It is related to Occam's razor , which states: entia non sunt multiplicanda praeter necessitatem (entities must not be multiplied beyond necessity). This should probably be the main ground rule in any design, but it's often overlooked, if not plain unknown by many self-appointed systems architects. The KISS principle states that every system should be as simple as possible ( but not simpler ). It doesn't talk about the easiness of a system, but the complexity . Some people tend to mix these two concepts up. There is nothing to be proud of when designing complex systems per se . It is always way harder to design simple systems. They are also superior to more complex systems, provided that they support the same functionality. This philosophy is also related with some other aphorism, such as: Perfect is the enemy of good . This means that, most of the time, trying to design the best (the m...