#============================================================================
#
#  Name     : RunWorkspace_python3.py
#  
#  Purpose  : Sample of FMEWorkspaceRunner
#  
#  Author               Date            Changes made
#  ------------------   ------------    -------------------------------
#  Ken Bragg           July 15th, 2014   Original Definition
#  DLeung              March 10th, 2022  Updated to Python 3


# Note: 
# The path to fmeobjects must by in your python path so you will need to add it to PATH with sys.append()
# or ensure this directory is already in PYTHONPATH
#
# You may need to change the path to FME install directory on your machine and the specific version 
# of Python you used"

import sys 
sys.path.append(r"C:\Program Files\FME\fmeobjects\python38")

import fmeobjects

# initiate FMEWorkspaceRunner Class 
runner = fmeobjects.FMEWorkspaceRunner() 

# Full path to Workspace, example comes from the FME 2020 Training Dataset
workspace = r"C:\FMEData2020\Workspaces\IntroToDesktop\Ex2.1-Complete.fmw"

# Set workspace parameters by creating a dictionary of name value pairs
parameters = {}
parameters["SourceDataset_XLSXR"] ="https://s3.amazonaws.com/FMEData/Interopolis/CaseLocationsDetails_2017_XLS.zip"
parameters['DestDataset_CSV2'] = r"C:\FMEData2020\Output\Training"

# Use try-except block so we can get FME Exception
try:
    # Run Workspace with parameters set in above dictionary
    runner.runWithParameters(workspace, parameters)
    
    # Or use promptRun to prompt for published parameters
    # runner.promptRun(workspace)
    
    # Tell user the workspace ran
    print(f"The Workspace: {workspace}")
    print("...ran successfully")
    
except fmeobjects.FMEException as ex:
    # Print out FME Exception if worskspace failed
    print(ex.message)

# get rid of FMEWorkspace runner so we don't leave an FME process running
runner = None