Pâté is a plugin for Kate (of KDE Desktop) that exposes editor’s functionality to Python. In short; with Pâté, you can write Kate plugins in Python.
I use Kate for (almost) all my text editing. I think it suits my needs perfectly. It is both as simple as I would be comfortable with and has as many features (such as multi document interface, regex search and replace, etc.) as I need to be productive. I am not an IDE person. Nothing against IDE’s, I have just never been comfortable with them. On the other side I have never taken the time to learn the classic (read antique) editors such as Emacs and Vim. I am sure learning them would be worth my time. But I doubt I will ever take the time for that. At present, I just fire up my Kate and it works pretty well.
Meanwhile, I keep hearing about these neat hacks with Emacs that when you do C-t, C-M-w and then C-k your active buffer is translated to Chinese and then automatically sent to your grandmother’s cell phone as SMS. Wow! And you can write your own macros (in Lisp, elisp) to extend the editor’s capabilities. There is virtually no limit to what you can do and it is not rare that these extensions exceed the borders of an editor. Of course you have to use (and learn) Emacs to take advantage.
This was true, before I discovered Pâté. It was always possible to write plugins for Kate, but Kate was not scriptable. Now using Pâté, you can extend Kate with ease (at least much much easier than writing C). The only thing that bugs me is I couldn’t figure out a way to reload my plugins without restarting Kate.
Creating Simple Pâté Plugins
The first plugin we write will turn the selection into a Django password hash. I use this when I want to create initial_data fixtures for User’s quickly.
Since we do not want to instantiate a complete Django environment we won’t be able to import anything from django.contrib.auth.models. Instead let us copy a dumbed down version of get_hexdigest into our own module.
import random
def get_hexdigest(algorithm, salt, raw_password):
try:
import hashlib
except ImportError:
import sha
return sha.new(salt + raw_password).hexdigest()
else:
return hashlib.sha1(salt + raw_password).hexdigest()
It should be clear enough, it returns a hash of the given password using the given salt[1]. Now we simply add kate to our imports (remember kate and not pate):
import kate
And add our own callback code:
@kate.onAction('Django Password', 'Shift+Alt+P')
def setPassword():
v = kate.view()
raw_password = v.selection.text
v.selection.removeSelectedText()
algo = 'sha1'
salt = get_hexdigest(algo, str(random.random()), \
str(random.random()))[:5]
hsh = get_hexdigest(algo, salt, raw_password)
v.insertText('%s$%s$%s' % (algo, salt, hsh))
The first line makes our function kate-aware. 'Django Password' will be the label for our menu item (it will be listed under Tools) and Shift-Alt-P will be the keyboard shortcut. The rest of the code should be self-explanatory.[2]
Now we copy our module into ~/.kde/share/apps/kate/pyplugins/ and restart Kate. It should show up in the menu and work now.
Second example is a JSON prettifier. I use JSON format for my fixtures, but valid JSON is not very readable. So I have this small plugin to convert a document between JSON and Python literals:
import sys, pprint
import kate
from django.utils import simplejson
HEADER = '# Pretty Printed\\n'
@kate.onAction('Django Pretty Json', 'Shift+Alt+J')
def togglePrettyJsonFormat():
d = kate.document()
source = d.text
if source.startswith(HEADER):
target = simplejson.dumps(eval(source))
else:
pp = pprint.PrettyPrinter(indent=2)
target = HEADER + pp.pformat(simplejson.loads(source))
d.text = target
I need HEADER to distinguish between two states. It can actually be anything, but it would be a good idea to make it a quote to have valid Python just in case.
Pâté Is Fun
I have enjoyed experimenting with Pâté. I hope it gets more attention and therefore ends up a much better plugin. If you ask me it should already ship with Kate. Kate is a nice editor, and empowering the users would only make it nicer and more popular.
If you have any ideas for pâté plugins, especially stuff that is useful in Django context, please add it to the comments. I would love to play a little more with pâté.
1: If it is not clear, take a look at django.contrib.auth.models.
2: To learn API, you can fire up Interactive Console under View and type help(kate).

