code: def prepare_gallery(app=None):
      srcdir = (
          app.srcdir
          if app
          else os.path.abspath(pjoin(os.path.dirname(__file__), ".."))
      )
      examples_dir = os.path.join(srcdir, "examples")
      examples_revamp_dir = os.path.join(srcdir, "examples_revamped")
      os.makedirs(examples_revamp_dir, exist_ok=True)
  
      f_example_desc = Path(examples_dir, "_valid_examples.toml")
      if not f_example_desc.exists():
          msg = f"No valid examples description file found in {examples_dir}"
          msg += "(e.g '_valid_examples.toml')"
          abort(msg)
  
      with open(f_example_desc, "rb") as fobj:
          try:
              desc_examples = tomllib.load(fobj)
          except Exception as e:
              msg = f"Error Loading examples description file: {e}.\n\n"
              msg += "Please check the file format."
              abort(msg)
  
      if "main" not in desc_examples.keys():
          msg = "No main section found in examples description file"
          abort(msg)
  
      try:
          examples_config = sorted(
              [
                  examplesConfig(folder_name=k.lower(), **v)
                  for k, v in desc_examples.items()
              ]
          )
      except Exception as e:
          msg = f"Error parsing examples description file: {e}.\n\n"
          msg += "Please check the file format."
          abort(msg)
  
      if examples_config[0].position != 0:
          msg = "Main section must be first in examples description file with"
          msg += "position=0"
          abort(msg)
      elif examples_config[0].folder_name != "main":
          msg = "Main section must be named 'main' in examples description file"
          abort(msg)
      elif examples_config[0].enable is False:
          msg = "Main section must be enabled in examples description file"
          abort(msg)
  
      disable_examples_section = []
      included_examples = []
      for example in examples_config:
          if not example.enable:
              disable_examples_section.append(example.folder_name)
              continue
  
          # Create folder for each example
          if example.position != 0:
              folder = Path(
                  examples_revamp_dir, f"{example.folder_name}"
              )
          else:
              folder = Path(examples_revamp_dir)
  
          if not folder.exists():
              os.makedirs(folder)
  
          # Create readme file
          if example.readme.startswith("file:"):
              filename = example.readme.split("file:")[1].strip()
              shutil.copy(
                  Path(examples_dir, filename),
                  Path(folder, "README.rst"),
              )
          else:
              with open(
                  Path(folder, "README.rst"), "w", encoding="utf8"
              ) as fi:
                  fi.write(example.readme)
  
          # Copy files to folder
          if not example.files:
              continue
  
          for filename in example.files:
              if not Path(examples_dir, filename).exists():
                  msg = f"\tFile {filename} not found in examples folder:  "
                  msg += f"{examples_dir}.Please, Add the file or remove it "
                  msg += "from the description file."
                  logger.warning(msg)
                  continue
  
              with open(
                  Path(examples_dir, filename), encoding="utf8"
              ) as f:
                  xfile = f.readlines()
  
              new_name = None
              if filename in included_examples:
                  # file need to be renamed to make it unique for sphinx-gallery
                  occurrences = included_examples.count(fi)
                  new_name = f"{filename[:-3]}_{occurrences+1}.py"
              if already_converted(xfile):
                  shutil.copy(
                      Path(examples_dir, filename),
                      Path(folder, new_name or filename),
                  )
              else:
                  with open(
                      Path(folder, new_name or filename),
                      "w",
                      encoding="utf8",
                  ) as fi:
                      fi.write(convert_to_sphinx_gallery_format(xfile))
              # Add additional link_names
              with open(
                  Path(folder, new_name or filename),
                  "r+",
                  encoding="utf8",
              ) as fi:
                  content = fi.read()
                  fi.seek(0, 0)
                  link_name = f"\n{sphx_glr_sep}\n"
                  link_name += (
                      "# .. include:: ../../links_names.inc\n#\n"
                  )
                  fi.write(content + link_name)
  
              included_examples.append(filename)
  
      # Check if all python examples are in the description file
      files_in_config = [
          fi for ex in examples_config for fi in ex.files
      ]
      all_examples = fnmatch.filter(os.listdir(examples_dir), "*.py")
      for all_ex in all_examples:
          if all_ex in files_in_config:
              continue
          msg = f"File {all_ex} not found in examples "
          msg += f"description file: {f_example_desc}"
          logger.warning(msg)
  
file_path: dipy/doc/sphinxext/prepare_gallery.py
imports: import fnmatch
  import sys
  import os
  import shutil
  from os.path import join
  from dataclasses import dataclass, field
  from pathlib import Path
  from sphinx.util import logging
label_desc: Use image node_id 5 for calling a global function with example usage: prepare_gallery(app) without return types