





import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


__lowercase							:Tuple							=	get_logger(__name__)


if TYPE_CHECKING:
				from .builder import DatasetBuilder







def    UpperCAmelCase				(  _lowerCamelCase   :	List[Any]      , _lowerCamelCase   :	Tuple = None  ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__  :					Optional[Any] =       importlib.import_module(_lowerCamelCase  )

						# TODO(QL): always update the module to add subsequent new authentication
						if hasattr(_lowerCamelCase      , "_patched_for_streaming"  ) and module._patched_for_streaming:
												return

						def wrap_auth(_lowerCamelCase   :	Dict  ):
												@wraps(_lowerCamelCase  )
												def wrapper(*_lowerCamelCase   :	Optional[Any]      , **_lowerCamelCase   :	List[Any]  ):
																		return function(*_lowerCamelCase      , use_auth_token=_lowerCamelCase      , **_lowerCamelCase  )

												SCREAMING_SNAKE_CASE__  :					List[Any] =       "wrap_auth"
												return wrapper

						# open files in a streaming fashion
						patch_submodule(_lowerCamelCase      , "open"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "os.listdir"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "os.walk"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "glob.glob"      , wrap_auth(_lowerCamelCase  )  ).start()
						# allow to navigate in remote zip files
						patch_submodule(_lowerCamelCase      , "os.path.join"      , _lowerCamelCase  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.dirname"      , _lowerCamelCase  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.basename"      , _lowerCamelCase  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.relpath"      , _lowerCamelCase  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.split"      , _lowerCamelCase  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.splitext"      , _lowerCamelCase  ).start()
						# allow checks on paths
						patch_submodule(_lowerCamelCase      , "os.path.exists"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.isdir"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.isfile"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "os.path.getsize"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "pathlib.Path"      , _lowerCamelCase  ).start()
						# file readers
						patch_submodule(_lowerCamelCase      , "gzip.open"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "numpy.load"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "pandas.read_csv"      , wrap_auth(_lowerCamelCase  )      , attrs=["__version__"]  ).start()
						patch_submodule(_lowerCamelCase      , "pandas.read_excel"      , wrap_auth(_lowerCamelCase  )      , attrs=["__version__"]  ).start()
						patch_submodule(_lowerCamelCase      , "scipy.io.loadmat"      , wrap_auth(_lowerCamelCase  )      , attrs=["__version__"]  ).start()
						patch_submodule(_lowerCamelCase      , "xml.etree.ElementTree.parse"      , wrap_auth(_lowerCamelCase  )  ).start()
						patch_submodule(_lowerCamelCase      , "xml.dom.minidom.parse"      , wrap_auth(_lowerCamelCase  )  ).start()
						SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       True







def    UpperCAmelCase				(  _lowerCamelCase   :	Tuple  ):





						'''simple docstring'''


						extend_module_for_streaming(builder.__module__      , use_auth_token=builder.use_auth_token  )
						# if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
						if not builder.__module__.startswith("datasets."  ):  # check that it's not a packaged builder like csv
												for imports in get_imports(inspect.getfile(builder.__class__  )  ):
																		if imports[0] == "internal":
																								SCREAMING_SNAKE_CASE__  :					Any =       imports[1]
																								SCREAMING_SNAKE_CASE__  :					Any =       ".".join(builder.__module__.split("."  )[:-1] + [internal_import_name]  )
																								extend_module_for_streaming(_lowerCamelCase      , use_auth_token=builder.use_auth_token  )

												# builders can inherit from other builders that might use streaming functionality
												# (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
												# but these parents builders are not patched automatically as they are not instantiated, so we patch them here
						from .builder import DatasetBuilder

						SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       [
						    cls.__module__
						    for cls in type(_lowerCamelCase  ).__mro__[1:]  # make sure it's not the same module we've already patched
						    if issubclass(cls      , _lowerCamelCase  ) and cls.__module__ != DatasetBuilder.__module__
						]  # check it's not a standard builder from datasets.builder
						for module in parent_builder_modules:
												extend_module_for_streaming(_lowerCamelCase      , use_auth_token=builder.use_auth_token  )





import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
     from PIL import Image




class  __lowerCAmelCase							(				unittest.TestCase   ,			lowerCAmelCase):






       def        SCREAMING_SNAKE_CASE     ( self:       Dict					):
         lowercase     :Any   						=    load_tool("image-segmentation"					)
         self.tool.setup()
         lowercase     :int   						=    load_tool("image-segmentation"							,       remote=_lowerCAmelCase					)






       def        SCREAMING_SNAKE_CASE     ( self:       str					):
         lowercase     :Any   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :Dict   						=    self.tool(_lowerCAmelCase							,       "cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Union[str, Any]					):
         lowercase     :Dict   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :List[Any]   						=    self.remote_tool(_lowerCAmelCase							,       "cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       List[Any]					):
         lowercase     :Optional[Any]   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :Dict   						=    self.tool(image=_lowerCAmelCase							,       label="cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)






       def        SCREAMING_SNAKE_CASE     ( self:       Tuple					):
         lowercase     :int   						=    Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"					)					) / "000000039769.png"					)
         lowercase     :List[str]   						=    self.remote_tool(image=_lowerCAmelCase							,       label="cat"					)
         self.assertTrue(isinstance(_lowerCAmelCase							,       Image.Image					)					)



