The Grail Startup File


When Grail starts up, it will attempt to import the module grailrc after it adds $GRAILDIR/user/ to sys.path. This mechanism can be used to initialize and configure various aspects of Grail. For example, you can set up the remote control feature so that Grail can be remotely instructed to load URLs, etc.

You can also use this mechanism to set up exit handlers that get executed when Grail exits. An exit handler can be any function that takes zero arguments (except, of course, for self if the function is a method). It gets called as late as possible in the Grail shutdown phase. Your exit handlers will get called when Grail exits for any reason, other than a core dump of the Python interpreter! :-)

To register an exit handler, call app.register_on_exit(func) where app is the Application instance, which is obtained using grailutil.get_grailapp(). To unregister the same exit handler, call app.unregister_on_exit(func). Note that all exceptions raised in your exit handler are ignored.

Here's a simple example that prints a message when Grail starts and when it exits. Put this in the file ~/.grail/user/grailrc.py.


# Grail initialization file

print 'Grail is starting'

def goodbye():
    print 'Grail is exiting'

import grailutil
grailutil.get_grailapp().register_on_exit(goodbye)