You are here: FME Workbench > Workspace Navigator > Workspace Parameters > Startup and Shutdown Python Scripts

Startup and Shutdown Python Scripts

This parameter allows a user to specify a Python script that is executed prior to or after a translation.

There are several potential uses for such scripts:

Dependencies

An FME installation includes a Python interpreter, which is used by default for all Python processing. FME 2012 ships with Python version 2.7. The FME Objects Python API supports Python 2.5 to 2.7.

To choose a different Python interpreter for FME, locate the interpreter's DLL file and follow these steps:

Startup Python Script

A startup script is executed prior to a translation. This script can read all Published Parameters and write to the translation’s log file.

For a list of global variables available to the startup script see FME_BEGIN_PYTHON in the FME Fundamentals manual.

An unhandled exception thrown by a startup script will cause the translation to abort.

This example of a startup script checks to ensure that the Python interpreter is the required version for a translation. If it is not, the translation is aborted before any data is read. This is a good example of the type of pre-translation check for which a startup script is useful.

import sys
 
# Raises an exception if the Python interpreter is not version 2.6.
def pythonVersionCheck():
pythonVersion = str(sys.version_info[0]) + '.' + str(sys.version_info[1])
if pythonVersion != "2.6":
raise Exception("Python version 2.6 required to run workspace")
 
# Call function for FME to execute.
pythonVersionCheck()

Shutdown Python Script

A shutdown script is executed after a translation has completed. This script has access to a number of Python global variables that contain statistics, the values of the published parameters, and other information about the translation.

For a list of global variables available to the shutdown script, see FME_END_PYTHON in the FME Fundamentals manual.

A shutdown script should not use any modules from the FME Objects Python API. During shutdown, the resources required by FME Objects are no longer available so calls to the API will have unpredictable results.

This example shutdown script shows how to send a notification e-mail if the translation fails. The translation log file will be attached to the e-mail.

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
 
# Global variables.
# FME global variables.
status = FME_Status
errorMsg = FME_FailureMessage
logFile = FME_LogFileName
 
# E-mail message values.
subject = "FME Translation FAILURE"
to = "receiver@domain.com"
sender = "Your FME script <sender@domain.com>"
text = "FME translation failed with error message: " + errorMsg + \
"\r\n\r\nSee attached logfile for details."

 

# Credentials.
AUTHREQUIRED = 0
username = "smtp.user@domain.com"
password = "smtppassword"
smtpServer = "smtp.server.com"

 

# Create and return a message with a logfile attachment.
def createMessage():
# Set up the e-mail.
message = MIMEMultipart()
message["Subject"] = subject
message["To"] = to
message["From"] = sender
message["Date"] = formatdate(localtime=True)
message.attach(MIMEText(text))
 
# Attach the logfile.
attachment = MIMEBase("application", "octet-stream")
attachment.set_payload(open(logFile, "rb").read())
Encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition",
'attachment; filename="%s"' % os.path.basename(logFile))
message.attach(attachment)
return message
 
# Send the passed in message.
def sendMessage(message):
server = smtplib.SMTP(smtpServer)
if AUTHREQUIRED:
server.login(username, password)
server.sendmail(sender, to, message.as_string())
server.quit()
 
# E-mails the translation results on failure.
def mailResults():
if status == 0:
message = createMessage()
sendMessage(message)
 
# Call function for FME to execute.
mailResults()