Startup and Shutdown Python and Tcl Scripts

Startup and Shutdown Scripts

The desire to have control of FME just before and just after translation has been discussed and debated for many years now. People have wanted to do different setup things before a translation begins, and different post-processing of log files or other translation artifacts after a translation ends.

At last this is possible, in a very powerful way with pre and post processing functions that provide the ability to execute a script both before and/or after a workspace is processed. The script has access to many of the translation artifacts (like counts, logfile, etc).

When Scripts Run

Startup scripts are executed after the mapping file has been completely parsed, and after the logfile has been opened, but before the readers have begun to do their processing. Shutdown scripts are executed after the writers have finished their processing and the logfile closed (or in the case of a translated terminated by error, after cleanup has taken place and the logfile closed).

Supported Scripting Languages

Startup and shutdown scripts can be in either Tcl or Python.

FME has a built-in Tcl interpreter, so no further installations are required.

If a script contains a procedure definition, the script must call the procedure it defined, otherwise the procedure itself will not be executed and no processing will occur.

Scripting with Workspaces

You can define scripts directly in the Workbench navigator pane. Workbench even provides a special editing window for the purpose of writing a script.

Above: The Tcl and Python script directives in the navigator pane.

Above: The Tcl end script directive setting dialog. Click the button highlighted to open the script editor.

Above: The script editor.

Scripts with FME Objects

Shutdown python scripts can make use of FME Objects by using import fmeobjects.

For TCL scripts there is no access to FMEObjects but you can start new processes via the "exec" command (Tcl), and these new processes could involve FME Objects Tcl without any conflict.

Scripts in Custom Formats

Startup and Shutdown scripts are not valid in an FDS (Custom Format). There are some architectural reasons for having to keep it that way as well.

They can, of course, be used in a mapping file or workspace that itself employs a custom format.

Published Parameters

It is possible to access published parameters within a startup or shutdown script.

The FME_MacroValues array/dictionary holds these (indexed by macro name, value is the actual value) – so in Tcl you can get at them via lines like this: FME_END_TCL puts [array names FME_MacroValues];<br>FME_BEGIN_TCL puts $FME_MacroValues(DestDataset);

And in Python you can end up accessing it as any other dictionary, for example, like this: for name,val in FME_MacroValues.iteritems(): logger.log( " %s:%s" %(name,val))

So, as my grandfather used to say about many of my inventions, “What Good Is It”? Well Grandpa, this has a bunch of uses, and here are some examples.

Global FME Variables available in TCL and Python

Passing Settings from Startup to Shutdown The Tcl startup and shutdown scripts share a Tcl interpreter, which means that the startup script can set up Tcl global variables that will later be available to the shutdown script.

However, this interpreter is completely separate from any other Tcl interpreter used in FME; so, for example, you cannot pass settings from @TCL and @Tcl2 FME functions to a shutdown script.

Startup Script Failure

If a startup script fails with an error message then FME will report the error and halt execution of the workspace.

However, you can stop FME from halting the workspace execution by catching the error as it occurs.

For example, if you were trying to write to a custom log file, but wanted to make sure that an error finding this file did not stop the overall process you might use: