Python FilePathAttribute using absolute vs relative paths

Hello TAP folks,

I’m using the FilePathAttribute in Python and can get the “Open” behavior to work in the Editor GUI, but when I select a file using the browser, it ends up using a relative path rather than an absolute path.

How do I specify that this attribute should use the absolute path?

This is the section of my test step code that builds that GUI element:

prop1 = self.AddProperty("s2pFile", None, String)
        prop1.AddAttribute(OpenTap.FilePathAttribute, OpenTap.FilePathAttribute.BehaviorChoice.Open)
        prop1.AddAttribute(OpenTap.DisplayAttribute, "S2P File", "Absolute path of location of s2p file to be loaded.", "Settings", 0)

There is no way to tell FilePathAttribute to select an absolute path. Instead you can convert the relative path to a full path by defining a property setter. This can be a bit cumbersome in python, but you can use setattr to do inject some code before the attribute is set. e.g:

    def __setattr__(self, name, value):
        if name == "s2pFile":
                 # I dont remember how to convert to full path in normal Python, but this should work:
                 value = System.IO.Path.GetFullPath(value) # Remember to import System.IO.Path
        super(StepWithPath, self).__setattr__(name, value)

Where does this method go?

Figured it out. It goes under the __init__ method in the test step in question (which, in my case is called SaveS2pFile.

Here is how to do this operation in Python:

    def __setattr__(self, name, value):
        if name == "s2pFile":
                 # Convert from relative path to absolute path
                 value = os.path.abspath(value)
        super(SaveS2pFile, self).__setattr__(name, value)
2 Likes