code: def find_connection_file(
      filename: str = "kernel-*.json",
      path: str | list[str] | None = None,
      profile: str | None = None,
  ) -> str:
      """find a connection file, and return its absolute path.
  
      The current working directory and optional search path
      will be searched for the file if it is not given by absolute path.
  
      If the argument does not match an existing file, it will be interpreted as a
      fileglob, and the matching file in the profile's security dir with
      the latest access time will be used.
  
      Parameters
      ----------
      filename : str
          The connection file or fileglob to search for.
      path : str or list of strs[optional]
          Paths in which to search for connection files.
  
      Returns
      -------
      str : The absolute path of the connection file.
      """
      if profile is not None:
          warnings.warn(
              "Jupyter has no profiles. profile=%s has been ignored."
              % profile,
              stacklevel=2,
          )
      if path is None:
          path = [".", jupyter_runtime_dir()]
      if isinstance(path, str):
          path = [path]
  
      try:
          # first, try explicit name
          return _filefind(filename, path)
      except OSError:
          pass
  
      # not found by full name
  
      if "*" in filename:
          # given as a glob already
          pat = filename
      else:
          # accept any substring match
          pat = "*%s*" % filename
  
      matches = []
      for p in path:
          matches.extend(glob.glob(os.path.join(p, pat)))
  
      matches = [os.path.abspath(m) for m in matches]
      if not matches:
          msg = f"Could not find {filename!r} in {path!r}"
          raise OSError(msg)
      elif len(matches) == 1:
          return matches[0]
      else:
          # get most recent match, by access time:
          return sorted(matches, key=lambda f: os.stat(f).st_atime)[-1]
  
imports: from __future__ import annotations
  import errno
  import glob
  import json
  import os
  import socket
  import stat
  import tempfile
  import warnings
  from getpass import getpass
  from typing import TYPE_CHECKING, Any, Dict, Union, cast
  import zmq
  from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir, secure_write
  from traitlets import Bool, CaselessStrEnum, Instance, Integer, Type, Unicode, observe
  from traitlets.config import LoggingConfigurable, SingletonConfigurable
  from .localinterfaces import localhost
  from .utils import _filefind
label_desc: Use image node_id 2 for calling a global function with example usage: find_connection_file(filename, path, profile) and returns: _filefind, matches, sorted