Search code examples
pythonmacrostrac

writing my first Trac macro


Ok, I've looked all over, and I think I'm doing this right, but I'm not getting any results. Is there anyone out there who's written Trac macros that can guide me through the first steps? Here's what I've written:

from trac.wiki.macros import WikiMacroBase
from genshi.builder import tag

class MyMacro(WikiMacroBase):
    """Proof of concept"""

    revision = "$Rev$"
    url = "$URL$"

    def expand_macro(self, formatter, name, args):
        return tag.b("Hello world.")

I've saved it as a .py file and put it in my Trac project's /plugins directory. Do I need to restart apache? Am I correct in expecting [[MyMacro]] to output a Hello world. on the page?


Solution

  • When creating macros using that format, Trac expects your class to be named "<name>Macro". For example, if you wanted a macro named JustASample, you would name the class JustASampleMacro. Since you named your class MyMacro, Trac thinks that you want your macro to be named My. Try using [[My]] on a wiki page and see if you get the output you're expecting.

    After you copy the file into the plugins directory, you will indeed want to restart the web server. Before doing so, delete any .pyc files that were created for your plugin. Also, ensure that the file is readable by the account under which the web server runs.