Monday, March 2, 2009

How Trac plugins are loaded.

Trac supports more than one method of loading plugins. One method is to place the .py file into the plugins directory of a given Trac environment directory. The other method is to install the plugin to site-packages as one would any other Python package. I prefer the latter method as it offers lower-coupling and hence a more robust deployment. The same effect can be achieved by using the former method to install Trac plugins but you'll have a tighter coupling to your Trac environment and I don't think this is desired.

Trac plugins are an excellent example of how to use entry points in Python. In order to function and be visible to Trac, the plugin must define a trac.plugins entry point that returns a list of all plugin components.

This functionality is implemented by the loader.py Trac module. Here is a simple illustration of what it does.



Here, the Trac loader retrieves a list of all Python eggs currently installed on the system. It will determine if the egg is a Trac plugin by examining the entry points provided by each egg. If it finds the trac.plugins entry point, all components of that plugin will be loaded.