code: def format_name_for_ext(
      metadata, ext, cm_default_formats=None, explicit_default=True
  ):
      """Return the format name for that extension"""
      # Is the format information available in the text representation?
      text_repr = metadata.get("jupytext", {}).get(
          "text_representation", {}
      )
      if text_repr.get("extension", "").endswith(ext) and text_repr.get(
          "format_name"
      ):
          return text_repr.get("format_name")
  
      # Format from jupytext.formats
      formats = (
          metadata.get("jupytext", {}).get("formats", "")
          or cm_default_formats
      )
      formats = long_form_multiple_formats(formats)
      for fmt in formats:
          if fmt["extension"] == ext:
              if (not explicit_default) or fmt.get("format_name"):
                  return fmt.get("format_name")
  
      if (not explicit_default) or ext in [".md", ".markdown", ".Rmd"]:
          return None
  
      return get_format_implementation(ext).format_name
  
imports: import os
  import re
  import warnings
  import nbformat
  import yaml
  from .cell_reader import DoublePercentScriptCellReader, HydrogenCellReader, LightScriptCellReader, MarkdownCellReader, RMarkdownCellReader, RScriptCellReader, SphinxGalleryScriptCellReader
  from .cell_to_text import BareScriptCellExporter, DoublePercentCellExporter, HydrogenCellExporter, LightScriptCellExporter, MarkdownCellExporter, RMarkdownCellExporter, RScriptCellExporter, SphinxGalleryCellExporter
  from .header import header_to_metadata_and_cell, insert_or_test_version_number
  from .languages import _COMMENT_CHARS, _SCRIPT_EXTENSIONS, same_language
  from .magics import is_magic
  from .metadata_filter import metadata_filter_as_string
  from .myst import MYST_FORMAT_NAME, is_myst_available, matches_mystnb, myst_extensions, myst_version
  from .pandoc import is_pandoc_available, pandoc_version
  from .stringparser import StringParser
  from .version import __version__
label_desc: Use image node_id 7 for calling a global function with example usage: format_name_for_ext(metadata, ext, cm_default_formats, explicit_default) and returns: get_format_implementation, text_repr, None, fmt