Fundamentally confused. I made a Python Script Toolbox with multiple tools (hopefully proper terminology):
Each tool works fine, and the basic Python code/structure is as shown below. NOTE! I am only showing one of the Tools (metadata_mcm) and not update_attr or xml_element_csv_creator - but they are the same in basic structure. Skip to my questions below before diving into code.
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = "toolbox"
# List of tool classes associated with this toolbox
self.tools = [metadata_mcm, create_xml_element_csv, update_attr]
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""
def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
return
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
self.params[1].clearMessage()
if self.params[1].value is None:
self.params[1].clearMessage()
else:
if os.path.exists(self.params[1]):
self.params[1].setErrorMessage('output path already exists')
else:
self.params[1].clearMessage()
return
class create_xml_element_csv(object):
def __init__(self):
self.label = "xml element csv creator"
self.desciption = "create inventory of xml elements relevant to fields"
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Input Feature",
name="fc_in",
datatype="Layer",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Path/to/file.csv",
name="fp_csv",
datatype="DEFile",
parameterType="Required",
direction="Input")
params = []
params.append(param0)
params.append(param1)
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
def execute(self, parameters, messages):
"""The source code of the tool."""
fc = parameters[0]
desc = arcpy.Describe(fc)
fp_fc = desc.featureClass.catalogPath
fp_csv = parameters[1].valueAsText
st0, st1 = os.path.splitext(fp_csv)
# csv
if st1 == '.csv':
pass
else:
fp_csv = '{}.csv'.format(fp_csv)
flds = [f.name for f in arcpy.ListFields(fp_fc)]
vals = np.column_stack([flds, [None] * len(flds), [None] * len(flds), [None] * len(flds)])
# attrdef = definition
# attrdefs = definition source
cn = ['attrlabl', 'attralias', 'attrdef', 'attrdefs']
df_fields = pd.DataFrame(vals, columns=cn)
df_fields.to_csv(fp_csv)
return
I don't understand a couple things:
1) Can the ToolValidator Class work with multiple tools? It's odd that I have essentially hard-coded the paramaters positionally in updateMessages for ALL subsequent tools. I want to ensure that the output file does not exist prior to running tool. This is argument 1 for create_xml_element_csv but doesn't exist for the two other tools. So can I create a ToolValidator Class and only apply the validation to one tool in the toolbox?
2) Is my syntax within ToolValidator/updateMessages method correct? Found this syntax from a StackExchange response.
The tools run BUT the validation of
if os.path.exists(self.params[1]):
self.params[1].setErrorMessage('output path already exists')
is not being performed. I can pass an argument of a path that exists, and no warming is issued, tool overwrites the existing file. No bueno.
Thanks,
Zach