code: def parse_filename(
      filename: FileSpec,
      types_exts: ty.Sequence[ExtensionSpec],
      trailing_suffixes: ty.Sequence[str],
      match_case: bool = False,
  ) -> tuple[str, str, str | None, str | None]:
      """Split filename into fileroot, extension, trailing suffix; guess type.
  
      Parameters
      ----------
      filename : str or os.PathLike
         filename in which to search for type extensions
      types_exts : sequence of sequences
         sequence of (name, extension) str sequences defining type to
         extension mapping.
      trailing_suffixes : sequence of strings
          suffixes that should be ignored when looking for
          extensions
      match_case : bool, optional
         If True, match case of extensions and trailing suffixes when
         searching in `filename`, otherwise do case-insensitive match.
  
      Returns
      -------
      pth : str
         path with any matching extensions or trailing suffixes removed
      ext : str
         If there were any matching extensions, in `types_exts` return
         that; otherwise return extension derived from
         ``os.path.splitext``.
      trailing : str
         If there were any matching `trailing_suffixes` return that
         matching suffix, otherwise ''
      guessed_type : str
         If we found a matching extension in `types_exts` return the
         corresponding ``type``
  
      Examples
      --------
      >>> types_exts = (('t1', 'ext1'),('t2', 'ext2'))
      >>> parse_filename('/path/fname.funny', types_exts, ())
      ('/path/fname', '.funny', None, None)
      >>> parse_filename('/path/fnameext2', types_exts, ())
      ('/path/fname', 'ext2', None, 't2')
      >>> parse_filename('/path/fnameext2', types_exts, ('.gz',))
      ('/path/fname', 'ext2', None, 't2')
      >>> parse_filename('/path/fnameext2.gz', types_exts, ('.gz',))
      ('/path/fname', 'ext2', '.gz', 't2')
      """
      filename = _stringify_path(filename)
  
      ignored = None
      if match_case:
          endswith = _endswith
      else:
          endswith = _iendswith
      for ext in trailing_suffixes:
          if endswith(filename, ext):
              extpos = -len(ext)
              ignored = filename[extpos:]
              filename = filename[:extpos]
              break
      guessed_name = None
      found_ext = None
      for name, type_ext in types_exts:
          if type_ext and endswith(filename, type_ext):
              extpos = -len(type_ext)
              found_ext = filename[extpos:]
              filename = filename[:extpos]
              guessed_name = name
              break
      else:
          filename, found_ext = os.path.splitext(filename)
      return (filename, found_ext, ignored, guessed_name)
  
file_path: nibabel/nibabel/filename_parser.py
imports: from __future__ import annotations
  import os
  import pathlib
  import typing
label_desc: Use image node_id 3 for calling a global function with example usage: parse_filename(filename, types_exts, trailing_suffixes, match_case) and returns: filename,  found_ext,  ignored,  guessed_name