Hello all,
I'm troubleshooting this attempted custom metadata import tool
the latest is a curious error of deleting the output location upon completion...
If anyone can give a clue as to why or how to fix would be much appreciated
here's the code *in attached file:
import arcpy
from arcpy import metadata as md
class ImportMetadataTool:
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Import Metadata Tool"
self.description = "Imports metadata from a source to a target item in ArcGIS Pro."
def getParameterInfo(self):
"""Define the tool parameters."""
params = [
arcpy.Parameter(
displayName="Source Metadata",
name="source_metadata",
datatype="GPString",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Target Metadata",
name="target_metadata",
datatype="GPString",
parameterType="Required",
direction="Input"
),
arcpy.Parameter(
displayName="Enable Automatic Updates",
name="enable_automatic_updates",
datatype="GPBoolean",
parameterType="Optional",
direction="Input"
),
arcpy.Parameter(
displayName="Output Location",
name="output_location",
datatype="GPString",
parameterType="Required",
direction="Input"
),
]
return params
def isLicensed(self):
"""Set whether the 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."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
source_metadata = parameters[0].valueAsText
target_metadata = parameters[1].valueAsText
enable_automatic_updates = parameters[2].value
output_location = parameters[3].valueAsText
try:
# Create a Metadata object for the target item
target_item_md = md.Metadata(target_metadata)
# Import metadata from the source
target_item_md.importMetadata(source_metadata, output_location)
# Set automatic updates if enabled
if enable_automatic_updates:
target_item_md.metadata_sync_option = 'ALWAYS'
target_item_md.reload()
# Save the changes to the target metadata
target_item_md.save()
messages.addMessage("Metadata imported successfully.")
except Exception as e:
messages.addErrorMessage(f"An error occurred: {e}")
#def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return
*edit* code added- appreciate the heads up as pasted in fisrt post and got flagged for spam
Code formatting ... the Community Version - Esri Community
It makes it easier than flippin back and forth between the image and your question
First thing is, arcpy.conversion.ImportMetadata doesn't have an output path as the second argument, the second argument is Import_Type, and target_md.importMetadata(source_metadata)
try:
try:
target_item_md = md.Metadata(target_metadata)
# Import metadata
target_item_md.importMetadata(source_metadata)
if enable_automatic_updates:
target_item_md.metadata_sync_option = 'ALWAYS'
target_item_md.reload()
target_item_md.save()
messages.addMessage("Metadata imported successfully.")
# output location
if output_location:
target_item_md.exportMetadata(output_location, "FGDC")
messages.addMessage(f"Metadata also exported to {output_location}")
except Exception as e:
messages.addErrorMessage(f"An error occurred: {e}")
@TonyAlmeida Thank you seems a step in the right direction. the output gdb did not disappear after running this time. although there is no output feature in there though.
Make sure the output is an .xml file.
if output_location:
# Ensure output_location is a folder (not GDB)
output_xml = os.path.join(output_location, "exported_metadata.xml")
target_item_md.exportMetadata(output_xml, "FGDC")
messages.addMessage(f"Metadata exported to {output_xml}")