class_docstr: ImageResource handling utility.
  
  Represents a request for reading or saving an image resource. This
  object wraps information to that request and acts as an interface
  for the plugins to several resources; it allows the user to read
  from filenames, files, http, zipfiles, raw bytes, etc., but offer
  a simple interface to the plugins via ``get_file()`` and
  ``get_local_filename()``.
  
  For each read/write operation a single Request instance is used and passed
  to the can_read/can_write method of a format, and subsequently to
  the Reader/Writer class. This allows rudimentary passing of
  information between different formats and between a format and
  associated reader/writer.
  
  Parameters
  ----------
  uri : {str, bytes, file}
      The resource to load the image from.
  mode : str
      The first character is "r" or "w", indicating a read or write
      request. The second character is used to indicate the kind of data:
      "i" for an image, "I" for multiple images, "v" for a volume,
      "V" for multiple volumes, "?" for don't care.
class_docstr_tok: ["ImageResource","handling","utility",".","Represents","a","request","for","reading","or","saving","an","image","resource",".","This","object","wraps","information","to","that","request","and","acts","as","an","interface","for","the","plugins","to","several","resources",";","it","allows","the","user","to","read","from","filenames",",","files",",","http",",","zipfiles",",","raw","bytes",",","etc.",",","but","offer","a","simple","interface","to","the","plugins","via","``","get_file","(",")","``","and","``","get_local_filename","(",")","``",".","For","each","read\/write","operation","a","single","Request","instance","is","used","and","passed","to","the","can_read\/can_write","method","of","a","format",",","and","subsequently","to","the","Reader\/Writer","class",".","This","allows","rudimentary","passing","of","information","between","different","formats","and","between","a","format","and","associated","reader\/writer",".","Parameters","--","--","--","--","--","uri",":","{","str",",","bytes",",","file","}","The","resource","to","load","the","image","from",".","mode",":","str","The","first","character","is","``","r","''","or","``","w","''",",","indicating","a","read","or","write","request",".","The","second","character","is","used","to","indicate","the","kind","of","data",":","``","i","''","for","an","image",",","``","I","''","for","multiple","images",",","``","v","''","for","a","volume",",","``","V","''","for","multiple","volumes",",","``","?","''","for","do","n't","care","."]
docstr: Wrap up this request.
  
  Finishes any pending reads or writes, closes any open files and frees
  any resources allocated by this request.
docstr_tok: ["Wrap","up","this","request",".","Finishes","any","pending","reads","or","writes",",","closes","any","open","files","and","frees","any","resources","allocated","by","this","request","."]
code: def finish(self) -> None:
      """Wrap up this request.
  
      Finishes any pending reads or writes, closes any open files and frees
      any resources allocated by this request.
      """
  
      if self.mode.io_mode == IOMode.write:
          # See if we "own" the data and must put it somewhere
          bytes = None
          if self._filename_local:
              bytes = Path(self._filename_local).read_bytes()
          elif self._file_is_local:
              self._file_is_local = False
              bytes = self._file.getvalue()
  
          # Put the data in the right place
          if bytes is not None:
              if self._uri_type == URI_BYTES:
                  self._result = bytes  # Picked up by imread function
              elif self._uri_type == URI_FILE:
                  self._file.write(bytes)
              elif self._uri_type == URI_ZIPPED:
                  zf = zipfile.ZipFile(self._filename_zip[0], "a")
                  zf.writestr(self._filename_zip[1], bytes)
                  zf.close()
              # elif self._uri_type == URI_FILENAME: -> is always direct
              # elif self._uri_type == URI_FTP/HTTP: -> write not supported
  
      # Close open files that we know of (and are responsible for)
      if self._file and self._uri_type != URI_FILE:
          self._file.close()
          self._file = None
      if self._zipfile:
          self._zipfile.close()
          self._zipfile = None
  
      # Remove temp file
      if self._filename_local:
          try:
              os.remove(self._filename_local)
          except Exception:  # pragma: no cover
              warnings.warn(
                  "Failed to delete the temporary file at "
                  f"`{self._filename_local}`. Please report this issue."
              )
          self._filename_local = None
  
      # Detach so gc can clean even if a reference of self lingers
      self._bytes = None
  
imports: import os
  from io import BytesIO
  import zipfile
  import tempfile
  import shutil
  import enum
  import warnings
  from ..core import urlopen, get_remote_file
  from pathlib import Path
  from urllib.parse import urlparse
  from typing import Optional
label_desc: Use image node_id 11 for calling the Request obj's underlying member method code with example usage: obj.finish() without return types