code: def _ensure_filesystem(
      filesystem, use_mmap=False, allow_legacy_filesystem=False
  ):
      if isinstance(filesystem, FileSystem):
          return filesystem
      elif isinstance(filesystem, str):
          if use_mmap:
              raise ValueError(
                  "Specifying to use memory mapping not supported for "
                  "filesystem specified as an URI string"
              )
          return _filesystem_from_str(filesystem)
  
      # handle fsspec-compatible filesystems
      try:
          import fsspec
      except ImportError:
          pass
      else:
          if isinstance(filesystem, fsspec.AbstractFileSystem):
              if type(filesystem).__name__ == "LocalFileSystem":
                  # In case its a simple LocalFileSystem, use native arrow one
                  return LocalFileSystem(use_mmap=use_mmap)
              return PyFileSystem(FSSpecHandler(filesystem))
  
      # map old filesystems to new ones
      import pyarrow.filesystem as legacyfs
  
      if isinstance(filesystem, legacyfs.LocalFileSystem):
          return LocalFileSystem(use_mmap=use_mmap)
      # TODO handle HDFS?
      if allow_legacy_filesystem and isinstance(
          filesystem, legacyfs.FileSystem
      ):
          return filesystem
  
      raise TypeError(
          "Unrecognized filesystem: {}. `filesystem` argument must be a "
          "FileSystem instance or a valid file system URI'".format(
              type(filesystem)
          )
      )
  
imports: from pyarrow.util import _is_path_like, _stringify_path
  from pyarrow._fs import FileSelector, FileType, FileInfo, FileSystem, LocalFileSystem, SubTreeFileSystem, _MockFileSystem, FileSystemHandler, PyFileSystem, _copy_files, _copy_files_selector
label_desc: Use image node_id 3 for calling a global function with example usage: _ensure_filesystem(filesystem, use_mmap, allow_legacy_filesystem) and returns: filesystem, LocalFileSystem, filesystem, _filesystem_from_str, PyFileSystem, LocalFileSystem