code: def _list_kernels_in(dir):
      """Return a mapping of kernel names to resource directories from dir.
  
      If dir is None or does not exist, returns an empty dict.
      """
      if dir is None or not os.path.isdir(dir):
          return {}
      kernels = {}
      for f in os.listdir(dir):
          path = pjoin(dir, f)
          if not _is_kernel_dir(path):
              continue
          key = f.lower()
          if not _is_valid_kernel_name(key):
              warnings.warn(
                  "Invalid kernelspec directory name (%s): %s"
                  % (_kernel_name_description, path),
                  stacklevel=3,
              )
          kernels[key] = path
      return kernels
  
imports: import errno
  import io
  import json
  import os
  import re
  import shutil
  import warnings
  from ipython_genutils.py3compat import PY3
  from traitlets import HasTraits, List, Unicode, Dict, Set, Bool, Type, CaselessStrEnum
  from traitlets.config import LoggingConfigurable
  from jupyter_core.paths import jupyter_data_dir, jupyter_path, SYSTEM_JUPYTER_PATH
label_desc: Use image node_id 3 for calling a global function with example usage: _list_kernels_in(dir) and returns: kernels, dict