An easier way to write Python plugins for Dia

Posted on Thu 07 January 2016 in Python

Dia is a free and open source diagram creation program that runs on the main dekstop platforms. It's getting a bit long in the tooth, but its still a great tool. It's very easy to extend with plugins written in Python to add new features but the actual process of writing those plugins is a little bit more complicated.

Firstly, the Python API is hard to find online. You can generate a diagram of the API from within Dia itself from the Help menu, but this doesn't give you a whole lot of information and the format is a little incovenient. The API can be a little tricky to navigate, and I'll share some tips on that in a later post, but the biggest problem lies in testing the plugins that you write. Dia loads all of its plugins at startup and they can't be modified during runtime which makes testing and debugging a laborious process. It's possible to write and test the parts of your plugin that don't depend on Dia separately but once you start interacting with the UI it all starts to get bogged down again.

My what a big API you have!

Frustrated by this and wanting to develop a plugin using Test Driven Development (TDD) techniques, I wrote a small Python module that mocks the Dia API. This way you can write and test the vast majority of your plugin without having to run Dia at all! There are some limitations - any interactions with the UI for example have to be done in Dia either manually or with an automation framework, and you can't generate usable diagrams from it. It should, however, reduce the amount of time and bugs it takes to get your plugin up and running. Don't forget to Open Source it and share it with the world!

If this is something you might find useful, you can check it out here. I originally developed it to meet the needs of a particular project I was working on at the time so there are some parts that may not be fully implemented, but I'm happy to take bug reports and pull requests.

Installation is easy:

$ python install

There is no practical difference in how you write your code and any existing plugin can use the module without modification. When the plugin runs outside of Dia, it loads the mock module, when it is run inside Dia, it loads the real one.

Now you're all set up to start writing plugins for Dia the easy way!