code: def _prepare_artifact(
      metadata_handle: metadata.Metadata,
      uri: str,
      properties: Dict[str, Any],
      custom_properties: Dict[str, Any],
      reimport: bool,
      output_artifact_class: Type[types.Artifact],
      mlmd_artifact_type: Optional[metadata_store_pb2.ArtifactType],
  ) -> types.Artifact:
      """Prepares the Importer's output artifact.
  
      If there is already an artifact in MLMD with the same URI, properties /
      custom properties, and type, that artifact will be reused unless the
      `reimport` argument is set to True.
  
      Args:
        metadata_handle: The handler of MLMD.
        uri: The uri of the artifact.
        properties: The properties of the artifact, given as a dictionary from
          string keys to integer / string values. Must conform to the declared
          properties of the destination channel's output type.
        custom_properties: The custom properties of the artifact, given as a
          dictionary from string keys to integer / string values.
        reimport: If set to True, will register a new artifact even if it already
          exists in the database.
        output_artifact_class: The class of the output artifact.
        mlmd_artifact_type: The MLMD artifact type of the Artifact to be created.
  
      Returns:
        An Artifact object representing the imported artifact.
      """
      absl.logging.info(
          "Processing source uri: %s, properties: %s, custom_properties: %s"
          % (uri, properties, custom_properties)
      )
  
      # Check types of custom properties.
      for key, value in custom_properties.items():
          if not isinstance(value, (int, str, bytes)):
              raise ValueError(
                  (
                      "Custom property value for key %r must be a string or integer "
                      "(got %r instead)"
                  )
                  % (key, value)
              )
  
      unfiltered_previous_artifacts = (
          metadata_handle.get_artifacts_by_uri(uri)
      )
      new_artifact_type = False
      if mlmd_artifact_type and not mlmd_artifact_type.id:
          try:
              mlmd_artifact_type = (
                  metadata_handle.store.get_artifact_type(
                      mlmd_artifact_type.name
                  )
              )
          except errors.NotFoundError:
              # Artifact type is not registered, so it must be new.
              new_artifact_type = True
  
      result = output_artifact_class(mlmd_artifact_type)
      result.uri = uri
      result.is_external = True
      _set_artifact_properties(result, properties, custom_properties)
  
      # Only consider previous artifacts as candidates to reuse if:
      #  * reimport is False
      #  * the given artifact type is recognized by MLMD
      #  * the type and properties match the imported artifact
      if not reimport and not new_artifact_type:
          previous_artifacts = []
          for candidate_mlmd_artifact in unfiltered_previous_artifacts:
              if (
                  mlmd_artifact_type
                  and candidate_mlmd_artifact.type_id
                  != mlmd_artifact_type.id
              ):
                  # If mlmd_artifact_type is defined, don't reuse existing artifacts if
                  # they don't match the given type.
                  continue
              is_candidate = True
              candidate_artifact = output_artifact_class(
                  mlmd_artifact_type
              )
              candidate_artifact.set_mlmd_artifact(
                  candidate_mlmd_artifact
              )
              for key, value in properties.items():
                  if (
                      not candidate_artifact.has_property(key)
                      or getattr(candidate_artifact, key) != value
                  ):
                      is_candidate = False
                      break
              for key, value in custom_properties.items():
                  if isinstance(value, int):
                      if (
                          not candidate_artifact.has_custom_property(
                              key
                          )
                          or candidate_artifact.get_int_custom_property(
                              key
                          )
                          != value
                      ):
                          is_candidate = False
                          break
                  elif isinstance(value, (str, bytes)):
                      if (
                          not candidate_artifact.has_custom_property(
                              key
                          )
                          or candidate_artifact.get_string_custom_property(
                              key
                          )
                          != value
                      ):
                          is_candidate = False
                          break
              if is_candidate:
                  previous_artifacts.append(candidate_mlmd_artifact)
          # If a registered artifact has the same uri and properties and the user does
          # not explicitly ask for reimport, reuse that artifact.
          if previous_artifacts:
              absl.logging.info("Reusing existing artifact")
              result.set_mlmd_artifact(
                  max(
                      previous_artifacts,
                      key=lambda m: m.create_time_since_epoch,
                  )
              )
  
      return result
  
imports: from typing import Any, Dict, List, Optional, Type, Union
  import absl
  from tfx import types
  from tfx.dsl.components.base import base_driver
  from tfx.dsl.components.base import base_node
  from tfx.orchestration import data_types
  from tfx.orchestration import metadata
  from tfx.types import channel_utils
  from tfx.utils import doc_controls
  from ml_metadata import errors
  from ml_metadata.proto import metadata_store_pb2
label_desc: Use image node_id 2 for calling a global function with example usage: _prepare_artifact(metadata_handle, uri, properties, custom_properties, reimport, output_artifact_class, mlmd_artifact_type) and returns: result