# Path: imageio/core/util.py
# def urlopen(*args, **kwargs):
#     """Compatibility function for the urlopen function. Raises an
#     RuntimeError if urlopen could not be imported (which can occur in
#     frozen applications.
#     """
#     try:
#         from urllib.request import urlopen
#     except ImportError:
#         raise RuntimeError("Could not import urlopen.")
#     return urlopen(*args, **kwargs)
# 
# Path: imageio/core/fetching.py
# def get_remote_file(fname, directory=None, force_download=False, auto=True):
#     """Get a the filename for the local version of a file from the web
# 
#     Parameters
#     ----------
#     fname : str
#         The relative filename on the remote data repository to download.
#         These correspond to paths on
#         ``https://github.com/imageio/imageio-binaries/``.
#     directory : str | None
#         The directory where the file will be cached if a download was
#         required to obtain the file. By default, the appdata directory
#         is used. This is also the first directory that is checked for
#         a local version of the file. If the directory does not exist,
#         it will be created.
#     force_download : bool | str
#         If True, the file will be downloaded even if a local copy exists
#         (and this copy will be overwritten). Can also be a YYYY-MM-DD date
#         to ensure a file is up-to-date (modified date of a file on disk,
#         if present, is checked).
#     auto : bool
#         Whether to auto-download the file if its not present locally. Default
#         True. If False and a download is needed, raises NeedDownloadError.
# 
#     Returns
#     -------
#     fname : str
#         The path to the file on the local system.
#     """
#     _url_root = "https://github.com/imageio/imageio-binaries/raw/master/"
#     url = _url_root + fname
#     nfname = op.normcase(fname)  # convert to native
#     # Get dirs to look for the resource
#     given_directory = directory
#     directory = given_directory or appdata_dir("imageio")
#     dirs = resource_dirs()
#     dirs.insert(0, directory)  # Given dir has preference
#     # Try to find the resource locally
#     for dir in dirs:
#         filename = op.join(dir, nfname)
#         if op.isfile(filename):
#             if not force_download:  # we're done
#                 if given_directory and given_directory != dir:
#                     filename2 = os.path.join(given_directory, nfname)
#                     # Make sure the output directory exists
#                     if not op.isdir(op.dirname(filename2)):
#                         os.makedirs(op.abspath(op.dirname(filename2)))
#                     shutil.copy(filename, filename2)
#                     return filename2
#                 return filename
#             if isinstance(force_download, str):
#                 ntime = time.strptime(force_download, "%Y-%m-%d")
#                 ftime = time.gmtime(op.getctime(filename))
#                 if ftime >= ntime:
#                     if given_directory and given_directory != dir:
#                         filename2 = os.path.join(given_directory, nfname)
#                         # Make sure the output directory exists
#                         if not op.isdir(op.dirname(filename2)):
#                             os.makedirs(op.abspath(op.dirname(filename2)))
#                         shutil.copy(filename, filename2)
#                         return filename2
#                     return filename
#                 else:
#                     print("File older than %s, updating..." % force_download)
#                     break
# 
#     # If we get here, we're going to try to download the file
#     if os.getenv("IMAGEIO_NO_INTERNET", "").lower() in ("1", "true", "yes"):
#         raise InternetNotAllowedError(
#             "Will not download resource from the "
#             "internet because environment variable "
#             "IMAGEIO_NO_INTERNET is set."
#         )
# 
#     # Can we proceed with auto-download?
#     if not auto:
#         raise NeedDownloadError()
# 
#     # Get filename to store to and make sure the dir exists
#     filename = op.join(directory, nfname)
#     if not op.isdir(op.dirname(filename)):
#         os.makedirs(op.abspath(op.dirname(filename)))
#     # let's go get the file
#     if os.getenv("CONTINUOUS_INTEGRATION", False):  # pragma: no cover
#         # On CI, we retry a few times ...
#         for i in range(2):
#             try:
#                 _fetch_file(url, filename)
#                 return filename
#             except IOError:
#                 time.sleep(0.5)
#         else:
#             _fetch_file(url, filename)
#             return filename
#     else:  # pragma: no cover
#         _fetch_file(url, filename)
#         return filename


import os
import zipfile
import tempfile
import shutil
import enum
import warnings
from io import BytesIO
from ..core import urlopen, get_remote_file
from pathlib import Path
from urllib.parse import urlparse
from typing import Optional
                # Create new file object, we catch the bytes in finish()
                self._file = BytesIO()
                self._file_is_local = True
            else:
                self._file = BytesIO(self._bytes)

        elif self._uri_type == URI_FILENAME:
            if want_to_write:
                self._file = open(self.filename, "wb")
            else:
                self._file = open(self.filename, "rb")

        elif self._uri_type == URI_ZIPPED:
            # Get the correct filename
            filename, name = self._filename_zip
            if want_to_write:
                # Create new file object, we catch the bytes in finish()
                self._file = BytesIO()
                self._file_is_local = True
            else:
                # Open zipfile and open new file object for specific file
                self._zipfile = zipfile.ZipFile(filename, "r")
                self._file = self._zipfile.open(name, "r")
                self._file = SeekableFileObject(self._file)

        elif self._uri_type in [URI_HTTP or URI_FTP]:
            assert not want_to_write  # This should have been tested in init
            timeout = os.getenv("IMAGEIO_REQUEST_TIMEOUT")
            if timeout is None or not timeout.isdigit():
                timeout = 5

# Path: imageio/core/util.py
# def urlopen(*args, **kwargs):
#     """Compatibility function for the urlopen function. Raises an
#     RuntimeError if urlopen could not be imported (which can occur in
#     frozen applications.
#     """
#     try:
#         from urllib.request import urlopen
#     except ImportError:
#         raise RuntimeError("Could not import urlopen.")
#     return urlopen(*args, **kwargs)
# 
# Path: imageio/core/fetching.py
# def get_remote_file(fname, directory=None, force_download=False, auto=True):
#     """Get a the filename for the local version of a file from the web
# 
#     Parameters
#     ----------
#     fname : str
#         The relative filename on the remote data repository to download.
#         These correspond to paths on
#         ``https://github.com/imageio/imageio-binaries/``.
#     directory : str | None
#         The directory where the file will be cached if a download was
#         required to obtain the file. By default, the appdata directory
#         is used. This is also the first directory that is checked for
#         a local version of the file. If the directory does not exist,
#         it will be created.
#     force_download : bool | str
#         If True, the file will be downloaded even if a local copy exists
#         (and this copy will be overwritten). Can also be a YYYY-MM-DD date
#         to ensure a file is up-to-date (modified date of a file on disk,
#         if present, is checked).
#     auto : bool
#         Whether to auto-download the file if its not present locally. Default
#         True. If False and a download is needed, raises NeedDownloadError.
# 
#     Returns
#     -------
#     fname : str
#         The path to the file on the local system.
#     """
#     _url_root = "https://github.com/imageio/imageio-binaries/raw/master/"
#     url = _url_root + fname
#     nfname = op.normcase(fname)  # convert to native
#     # Get dirs to look for the resource
#     given_directory = directory
#     directory = given_directory or appdata_dir("imageio")
#     dirs = resource_dirs()
#     dirs.insert(0, directory)  # Given dir has preference
#     # Try to find the resource locally
#     for dir in dirs:
#         filename = op.join(dir, nfname)
#         if op.isfile(filename):
#             if not force_download:  # we're done
#                 if given_directory and given_directory != dir:
#                     filename2 = os.path.join(given_directory, nfname)
#                     # Make sure the output directory exists
#                     if not op.isdir(op.dirname(filename2)):
#                         os.makedirs(op.abspath(op.dirname(filename2)))
#                     shutil.copy(filename, filename2)
#                     return filename2
#                 return filename
#             if isinstance(force_download, str):
#                 ntime = time.strptime(force_download, "%Y-%m-%d")
#                 ftime = time.gmtime(op.getctime(filename))
#                 if ftime >= ntime:
#                     if given_directory and given_directory != dir:
#                         filename2 = os.path.join(given_directory, nfname)
#                         # Make sure the output directory exists
#                         if not op.isdir(op.dirname(filename2)):
#                             os.makedirs(op.abspath(op.dirname(filename2)))
#                         shutil.copy(filename, filename2)
#                         return filename2
#                     return filename
#                 else:
#                     print("File older than %s, updating..." % force_download)
#                     break
# 
#     # If we get here, we're going to try to download the file
#     if os.getenv("IMAGEIO_NO_INTERNET", "").lower() in ("1", "true", "yes"):
#         raise InternetNotAllowedError(
#             "Will not download resource from the "
#             "internet because environment variable "
#             "IMAGEIO_NO_INTERNET is set."
#         )
# 
#     # Can we proceed with auto-download?
#     if not auto:
#         raise NeedDownloadError()
# 
#     # Get filename to store to and make sure the dir exists
#     filename = op.join(directory, nfname)
#     if not op.isdir(op.dirname(filename)):
#         os.makedirs(op.abspath(op.dirname(filename)))
#     # let's go get the file
#     if os.getenv("CONTINUOUS_INTEGRATION", False):  # pragma: no cover
#         # On CI, we retry a few times ...
#         for i in range(2):
#             try:
#                 _fetch_file(url, filename)
#                 return filename
#             except IOError:
#                 time.sleep(0.5)
#         else:
#             _fetch_file(url, filename)
#             return filename
#     else:  # pragma: no cover
#         _fetch_file(url, filename)
#         return filename


# Path: imageio/core/request.py
import os
import zipfile
import tempfile
import shutil
import enum
import warnings
from io import BytesIO
from ..core import urlopen, get_remote_file
from pathlib import Path
from urllib.parse import urlparse
from typing import Optional

                # Create new file object, we catch the bytes in finish()
                self._file = BytesIO()
                self._file_is_local = True
            else:
                self._file = BytesIO(self._bytes)

        elif self._uri_type == URI_FILENAME:
            if want_to_write:
                self._file = open(self.filename, "wb")
            else:
                self._file = open(self.filename, "rb")

        elif self._uri_type == URI_ZIPPED:
            # Get the correct filename
            filename, name = self._filename_zip
            if want_to_write:
                # Create new file object, we catch the bytes in finish()
                self._file = BytesIO()
                self._file_is_local = True
            else:
                # Open zipfile and open new file object for specific file
                self._zipfile = zipfile.ZipFile(filename, "r")
                self._file = self._zipfile.open(name, "r")
                self._file = SeekableFileObject(self._file)

        elif self._uri_type in [URI_HTTP or URI_FTP]:
            assert not want_to_write  # This should have been tested in init
            timeout = os.getenv("IMAGEIO_REQUEST_TIMEOUT")
            if timeout is None or not timeout.isdigit():
                timeout = 5

            self._file = urlopen(self.filename, timeout=float(timeout))