






'''simple docstring'''
import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


UpperCamelCase__			=      'http://www.mocksite.com/file1.txt'
UpperCamelCase__			=      '"text": ["foo", "foo"]'
UpperCamelCase__			=      '6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8'


class 							_UpperCAmelCase    :
    __lowerCamelCase: str									=     200
    __lowerCamelCase: Tuple									=     {'Content-Length': '100'}
    __lowerCamelCase: List[Any]									=     {}




    def 					lowerCAmelCase__					(      self	:							Union[str, Any]	,  **a	:							Dict      ):






       '''simple docstring'''






       return [bytes(a	,  "utf-8"      )]




def 		__SCREAMING_SNAKE_CASE       (	*_UpperCamelCase    ,		**_UpperCamelCase				):


   """simple docstring"""




   return MockResponse()




@pytest.mark.parametrize("urls_type"    ,		[str, list, dict]				)
def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   import requests

   monkeypatch.setattr(_UpperCamelCase    ,		"request"    ,		_UpperCamelCase				)

   lowercase_						:     Union[str, Any]							     =  URL
   if issubclass(_UpperCamelCase    ,		_UpperCamelCase				):
      lowercase_						:     Optional[Any]							     =  url
   elif issubclass(_UpperCamelCase    ,		_UpperCamelCase				):
      lowercase_						:     Tuple							     =  [url]
   elif issubclass(_UpperCamelCase    ,		_UpperCamelCase				):
      lowercase_						:     Optional[int]							     =  {"train": url}
   lowercase_						:     Union[str, Any]							     =  "dummy"
   lowercase_						:     Tuple							     =  "downloads"
   lowercase_						:     Tuple							     =  tmp_path
   lowercase_						:     Any							     =  DownloadConfig(
       cache_dir=os.path.join(_UpperCamelCase    ,		_UpperCamelCase				)    ,		use_etag=_UpperCamelCase    ,		)
   lowercase_						:     Union[str, Any]							     =  DownloadManager(dataset_name=_UpperCamelCase    ,		download_config=_UpperCamelCase				)
   lowercase_						:     Optional[Any]							     =  dl_manager.download(_UpperCamelCase				)
   lowercase_						:     Dict							     =  urls
   for downloaded_paths in [downloaded_paths]:
      if isinstance(_UpperCamelCase    ,		_UpperCamelCase				):
         lowercase_						:     List[Any]							     =  [downloaded_paths]
         lowercase_						:     List[Any]							     =  [urls]
      elif isinstance(_UpperCamelCase    ,		_UpperCamelCase				):
         assert "train" in downloaded_paths.keys()
         lowercase_						:     Tuple							     =  downloaded_paths.values()
         lowercase_						:     List[str]							     =  urls.values()
      assert downloaded_paths
      for downloaded_path, input_url in zip(_UpperCamelCase    ,		_UpperCamelCase				):
         assert downloaded_path == dl_manager.downloaded_paths[input_url]
         lowercase_						:     int							     =  Path(_UpperCamelCase				)
         lowercase_						:     str							     =  downloaded_path.parts
         assert parts[-1] == HASH
         assert parts[-2] == cache_subdir
         assert downloaded_path.exists()
         lowercase_						:     Tuple							     =  downloaded_path.read_text()
         assert content == CONTENT
         lowercase_						:     Any							     =  downloaded_path.with_suffix(".json"				)
         assert metadata_downloaded_path.exists()
         lowercase_						:     Union[str, Any]							     =  json.loads(metadata_downloaded_path.read_text()				)
         assert metadata_content == {"url": URL, "etag": None}




@pytest.mark.parametrize("paths_type"    ,		[str, list, dict]				)
def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     str							     =  str(_UpperCamelCase				)
   if issubclass(_UpperCamelCase    ,		_UpperCamelCase				):
      lowercase_						:     List[Any]							     =  filename
   elif issubclass(_UpperCamelCase    ,		_UpperCamelCase				):
      lowercase_						:     Union[str, Any]							     =  [filename]
   elif issubclass(_UpperCamelCase    ,		_UpperCamelCase				):
      lowercase_						:     Tuple							     =  {"train": filename}
   lowercase_						:     str							     =  "dummy"
   lowercase_						:     Union[str, Any]							     =  xz_file.parent
   lowercase_						:     List[str]							     =  "extracted"
   lowercase_						:     Optional[int]							     =  DownloadConfig(
       cache_dir=_UpperCamelCase    ,		use_etag=_UpperCamelCase    ,		)
   lowercase_						:     int							     =  DownloadManager(dataset_name=_UpperCamelCase    ,		download_config=_UpperCamelCase				)
   lowercase_						:     Tuple							     =  dl_manager.extract(_UpperCamelCase				)
   lowercase_						:     Union[str, Any]							     =  paths
   for extracted_paths in [extracted_paths]:
      if isinstance(_UpperCamelCase    ,		_UpperCamelCase				):
         lowercase_						:     int							     =  [extracted_paths]
         lowercase_						:     int							     =  [paths]
      elif isinstance(_UpperCamelCase    ,		_UpperCamelCase				):
         assert "train" in extracted_paths.keys()
         lowercase_						:     Union[str, Any]							     =  extracted_paths.values()
         lowercase_						:     List[Any]							     =  paths.values()
      assert extracted_paths
      for extracted_path, input_path in zip(_UpperCamelCase    ,		_UpperCamelCase				):
         assert extracted_path == dl_manager.extracted_paths[input_path]
         lowercase_						:     List[Any]							     =  Path(_UpperCamelCase				)
         lowercase_						:     Optional[int]							     =  extracted_path.parts
         assert parts[-1] == hash_url_to_filename(_UpperCamelCase    ,		etag=_UpperCamelCase				)
         assert parts[-2] == extracted_subdir
         assert extracted_path.exists()
         lowercase_						:     Optional[int]							     =  extracted_path.read_text()
         lowercase_						:     Dict							     =  text_file.read_text()
         assert extracted_file_content == expected_file_content




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   assert path.endswith(".jsonl"				)
   for num_items, line in enumerate(_UpperCamelCase    ,		start=1				):
      lowercase_						:     Optional[int]							     =  json.loads(line.decode("utf-8"				)				)
      assert item.keys() == {"col_1", "col_2", "col_3"}
   assert num_items == 4




@pytest.mark.parametrize("archive_jsonl"    ,		["tar_jsonl_path", "zip_jsonl_path"]				)
def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     Tuple							     =  request.getfixturevalue(_UpperCamelCase				)
   lowercase_						:     Dict							     =  DownloadManager()
   for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(_UpperCamelCase				)    ,		start=1				):
      _test_jsonl(_UpperCamelCase    ,		_UpperCamelCase				)
   assert num_jsonl == 2




@pytest.mark.parametrize("archive_nested_jsonl"    ,		["tar_nested_jsonl_path", "zip_nested_jsonl_path"]				)
def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     Dict							     =  request.getfixturevalue(_UpperCamelCase				)
   lowercase_						:     Dict							     =  DownloadManager()
   for num_tar, (path, file) in enumerate(dl_manager.iter_archive(_UpperCamelCase				)    ,		start=1				):
      for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(_UpperCamelCase				)    ,		start=1				):
         _test_jsonl(_UpperCamelCase    ,		_UpperCamelCase				)
   assert num_tar == 1
   assert num_jsonl == 2





def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     Tuple							     =  DownloadManager()
   for num_file, file in enumerate(dl_manager.iter_files(_UpperCamelCase				)    ,		start=1				):
      assert os.path.basename(_UpperCamelCase				) == ("test.txt" if num_file == 1 else "train.txt")
   assert num_file == 2












'''simple docstring'''
from __future__ import annotations




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     Optional[int]							     =  2
   lowercase_						:     Tuple							     =  []
   while i * i <= n:
      if n % i:
         i += 1
      else:
         n //= i
         factors.append(_UpperCamelCase				)
   if n > 1:
      factors.append(_UpperCamelCase				)
   return factors


if __name__ == "__main__":
 import doctest

 doctest.testmod()




