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


lowerCamelCase__   = get_logger(__name__)


if TYPE_CHECKING:
   from .builder import DatasetBuilder


def 							lowerCAmelCase__    (       a__  ,			a__ = None      )	->int:




  '''simple docstring'''

  _UpperCamelCase				=  importlib.import_module(A__      )

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

  def wrap_auth(a__      ):
    @wraps(A__      )
    def wrapper(*a__  ,			**a__      ):
      return function(*A__  ,			use_auth_token=A__  ,			**A__      )

    _UpperCamelCase				=  "wrap_auth"
    return wrapper

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







def 							lowerCAmelCase__    (       a__      )	->Union[str, Any]:




  '''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":
        _UpperCamelCase				=  imports[1]
        _UpperCamelCase				=  ".".join(builder.__module__.split("."      )[:-1] + [internal_import_name]      )
        extend_module_for_streaming(A__  ,			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

  _UpperCamelCase				=  [
      cls.__module__
      for cls in type(A__      ).__mro__[1:]  # make sure it's not the same module we've already patched
      if issubclass(cls  ,			A__      ) 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(A__  ,			use_auth_token=builder.use_auth_token      )






import os
import shutil
import unittest
from unittest.mock import patch

from transformers.testing_utils import CaptureStd, is_pt_tf_cross_test, require_torch







class      lowerCamelCase		(  unittest.TestCase  ):





     @patch("sys.argv"				,    ["fakeprogrampath", "env"]   )
     def 		UpperCAmelCase(self      :    Optional[Any]   )						->						Dict:
         # test transformers-cli env
         import transformers.commands.transformers_cli

         with CaptureStd() as cs:
             transformers.commands.transformers_cli.main()
         self.assertIn("Python version"				,    cs.out   )
         self.assertIn("Platform"				,    cs.out   )
         self.assertIn("Using distributed or parallel set-up in script?"				,    cs.out   )





     @is_pt_tf_cross_test
     @patch(
         "sys.argv"				,    ["fakeprogrampath", "pt-to-tf", "--model-name", "hf-internal-testing/tiny-random-gptj", "--no-pr"]   )
     def 		UpperCAmelCase(self      :    Any   )						->						int:
         import transformers.commands.transformers_cli

         shutil.rmtree("/tmp/hf-internal-testing/tiny-random-gptj"				,    ignore_errors=_A   )  # cleans potential past runs
         transformers.commands.transformers_cli.main()

         # The original repo has no TF weights -- if they exist, they were created by the CLI
         self.assertTrue(os.path.exists("/tmp/hf-internal-testing/tiny-random-gptj/tf_model.h5"   )   )





     @require_torch
     @patch("sys.argv"				,    ["fakeprogrampath", "download", "hf-internal-testing/tiny-random-gptj", "--cache-dir", "/tmp"]   )
     def 		UpperCAmelCase(self      :    Dict   )						->						List[Any]:
         import transformers.commands.transformers_cli

         # # remove any previously downloaded model to start clean
         shutil.rmtree("/tmp/models--hf-internal-testing--tiny-random-gptj"				,    ignore_errors=_A   )

         # run the command
         transformers.commands.transformers_cli.main()

         # check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--tiny-random-gptj
         self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--tiny-random-gptj/blobs"   )   )
         self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--tiny-random-gptj/refs"   )   )
         self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--tiny-random-gptj/snapshots"   )   )





     @require_torch
     @patch(
         "sys.argv"				,    [
             "fakeprogrampath",
             "download",
             "hf-internal-testing/test_dynamic_model_with_tokenizer",
             "--trust-remote-code",
             "--cache-dir",
             "/tmp",
         ]				,    )
     def 		UpperCAmelCase(self      :    Any   )						->						List[str]:
         import transformers.commands.transformers_cli

         # # remove any previously downloaded model to start clean
         shutil.rmtree("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer"				,    ignore_errors=_A   )

         # run the command
         transformers.commands.transformers_cli.main()

         # check if the model files are downloaded correctly on /tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer
         self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/blobs"   )   )
         self.assertTrue(os.path.exists("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/refs"   )   )
         self.assertTrue(
             os.path.exists("/tmp/models--hf-internal-testing--test_dynamic_model_with_tokenizer/snapshots"   )   )

