Question

Available destination dataset in Start up script


Badge


Hi!

I’m trying to build a start up script in FME Desktop
2016.1, which will check if the destination dataset(s) is available for writing
or not. If it’s available I would like a message telling me so, if not I would
like the translation to fail and tell me why (and not continue for over an hour
and tell me at the end of the translation it failed).

The idea is that I have a script that I can use in all workbenches,
whatever the workbench is doing. So I’m trying to fix it in the start up script
and not with transformers.

I made a script in Python, which gives me a nice
exception when I run the translation… 

However, it also gives the exception when the destination
dataset is still available (not locked by me on purpose), and it shouldn’t
throw the exception... 

Am I missing something?

Thanks for the help!

Script:

import fme
import fmeobjects
import os

# Raise exception if the Writer destination is locked (before translation!)

# Create an empty list, later we will add the locations of the destinationt datasets.
DestDataset_list = []
DestDataset = "DestDataset"
print '\n'
# Create an FME Log file.
logger = fmeobjects.FMELogFile()

# Print only the destination datasets, and put them in a list.
for key, value in fme.macroValues.items():
    print "key: " + str(key)+ ", Value macroValues: " + str(value)
    # Find the text DestDataset in de macroValues dictionary,
    # it returns a '-1' if a string is NOT found.
    if key.find("DestDataset") != -1:
        # I found empty DestDataset strings, and want to skip those.
        if len (str(value)) >3:
            listtest = 0
            # I will test if the location of the data is not allready included in the list (multiple feature classes in the same geodatabase still give the same DestDatset.
            for DestDataset_item_test in DestDataset_list:
                if DestDataset_item_test == value:
                   listtest += 1
            # If the DestDatset is not yet in the list, we append it to the list.
            if listtest == 0:     
                DestDataset_list.append(value)
            print "Value DestDataset_list: " + str(value)
        
print "\n"
print "Lengte DestDataset_list: " + str(len(DestDataset_list))
for DestDataset_item in DestDataset_list:
    print "DestDataset_item: " +str(DestDataset_item)
print "\n"

for DestDataset_item in DestDataset_list:
    # For every DestDatset in the list, test if the location allready exists...
    if os.path.exists(DestDataset_item):
        print "Destination dataset: " + str(DestDataset_item)
        try:
            # If it exists, try to remove it.
            os.remove(DestDataset_item)
            logger.logMessageString('Toegang op ' + DestDataset_item + ' is beschikbaar!')
            print 'Toegang op ' + DestDataset_item + ' is beschikbaar!'
        # If it can't be removed, raise an exception because it is probably locked...
        except OSError as e:
            logger.logMessageString("Geen toegang op " + DestDataset_item + "\n")
            raise Exception ("Geen toegang op " + DestDataset_item + "\n")


5 replies

Userlevel 4

Can you please repost the code in a code block?

It's very difficult to read the code if the indentation isn't preserved.

Userlevel 2
Badge +17

Hi @casper,

Please also post the log file from your run - this will help us see the progress of your Python.

Badge

Hi @casper,

Please also post the log file from your run - this will help us see the progress of your Python.

@DaveAtSafe

 

 

Thansk for replying!

 

I have added both logfiles in txtfile and as code block.

 

Badge

Can you please repost the code in a code block?

It's very difficult to read the code if the indentation isn't preserved.

@david_r

 

 

Thank for replying! I have added the code in the code block.

 

Hope it helps.

 

Userlevel 4

If the path specified in DestDataset_item, e.g. this value from the exception message:

G:\Beheer\GIS\Tools\FME\Test_map\StartUpScript\Output

is a folder, then you cannot use os.remove() to delete it, os.remove() only deletes files, see also the docs.

Look into using shutil.rmtree() for deleting directories along with any content.

Reply