Python plugin developer guide, part 1

I’m a Python newbie, so if you are at least that good :-D, you should be able to dive in and write useful Python plugins for your favourite editor too! This is the first in what I hope will be a series of notes and tutorials to help you along the way.

Where can I find examples to steal from inspire me?

Any good developer knows that a good way to launch into a new area is to look at examples, so here is a list of places I’ve encountered.

Remember to respect others’ copyrights, and give credit where it is due.

Where do I put my code?

The Pâté Python plugin looks in several places to find usable plugins, you should be able to use the Python console to find a directory you can use for your developments. Select it, and press “Reload”:

Launch it from the View menu, and then ask KDE where your files should go:

Or, if that’s a bit hard to read, like this:

>>> from PyKDE4.kdecore import *

>>> print KStandardDirs.locateLocal(“appdata”,”pate”)

/home/srhaque/.kde/share/apps/kate/pate

Try the directory

If the given directory does not exist, create it. Press Reload, and the new directory should be visible:

You are ready to create your plugin in the new directory! This plugin directory will be added to the initial value of sys.path used for all plugins.

Plugin structure and naming

Your plugin can be just a single .py file, such as “console.py”, or multiple files contained in a directory. A single .py file is useful for simple things, but if you want to use .ui files or multiple Python files, a directory is recommended. If you use this option, the main plugin file must be named after the directory, so:

  • If the directory is called “console”
  • The main plugin file must be named “console/console.py”

In the directory case, the directory is added to sys.path to load the main plugin file and other Python modules you may have. Since sys.path is searched by Python in order to load modules, having the same plugin in multiple locations would be very confusing. Pâté supports this by refusing to load identically named plugins; instead they are highlighted as being hidden:

This also means you can just copy one of the plugins supplied with Kate to get started!

 

5 thoughts on “Python plugin developer guide, part 1”

  1. Interesting. I will definitely look at this since I have an interest in trying to make a 9P KIO slave using the py9p implementation of the protocol/file system.

    https://github.com/staalmannen/python2-kio_9p

    The info found on writing kio slaves is however rather limited and I have basically only uploaded some ideas and no real functioning code.

  2. This guide was looking nice to me… until I looked at the date.

    Is it planned to continue this series sometime? Atm there is no such thing like a Pâté tutorial (or at least I don’t find it), and the documentation also isn’t really good, so it would be really appreciated.

    1. The information here is still up to date. As for documentation, have you seen what is there already? What is it that you find is missing?

      1. This guide just teaches you how to you create a new empty plugin file, but not what you write into it. When I wrote that it looked nice to me I meant it’s a good start and that I expected more of this. Then I saw this guide has been written almost 13 months ago, so it’s questionable if there will be more parts of it, which is very unfortunate.
        And yes, I looked at the docs, and they are somewhat useless. For example, it took me three days to find out how to open a new file view with Python, and I found the solution in the docs of KTextEditor, not in Pâté’s ones.

        1. OK, that makes some sense to me. The trouble is that this initial learning that you’ve just gone through was a very long time ago for me, and what you found seems obvious from where I am *now*. So I have a proposal for you: if you have time to write a few notes on what you would have liked to have known, I will make some time to write them up as a follow up posting.

Leave a Reply