Startup and Shutdown Python Scripts
Workspace Parameters > Scripting > Startup Python Script, Shutdown Python Script
These parameters allow a user to specify a Python script that is executed prior to or after a translation.
There are several potential uses for such scripts:
- Check a database connection before running the translation.
- Move data prior to or after the translation.
- Scan the log for a particular message or condition
- Write the translation statistics to a custom log, database table, or e-mail them to an administrator.
Dependencies
Specifying a Python Interpreter
The Python interpreter used by FME to execute Python scripts is controlled by the Python Compatibility workspace parameter and the Preferred Python Interpreter setting under Tools > FME Options > Translation > Python Interpreter.
Installing Python Packages
If you would like to install a third party package for use by Python in FME, see Installing Python Packages to FME Form.
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.
Note: For a list of global variables available to the startup script see FME_BEGIN_PYTHON.
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 3.5 or higher.
def python_version_check():
if sys.version_info <= (3,5):
raise Exception("Python version 3.5+ required to run workspace")
# Call function for FME to execute.
python_version_check()
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.
Note: For a list of global variables available to the shutdown script, see FME_END_PYTHON.
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
# FME variables.
import fme
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()