'''simple docstring'''

import logging
import os
import threading
import time


try:
 import warnings
except ImportError:
 _A			:      List[str]					      =				None

try:
 import msvcrt
except ImportError:
 _A			:      Any					      =				None

try:
 import fcntl
except ImportError:
 _A			:      Tuple					      =				None


# Backward compatibility
# ------------------------------------------------
try:
 TimeoutError
except NameError:
 _A			:      Optional[int]					      =				OSError


# Data
# ------------------------------------------------
_A			:      Dict					      =				[
    '''Timeout''',
    '''BaseFileLock''',
    '''WindowsFileLock''',
    '''UnixFileLock''',
    '''SoftFileLock''',
    '''FileLock''',
]

_A			:      List[str]					      =				'''3.0.12'''


_A			:      Optional[int]					      =				None







def      UpperCamelCase_						(					)					->     Optional[int]:






 '''simple docstring'''

 global _logger
 __lowerCAmelCase											=						_logger or logging.getLogger(__name__      )
 return _logger







class 				_lowercase     (				UpperCAmelCase__			):


 '''simple docstring'''






 def __init__(      self   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]							)  ->    Any:
  __lowerCAmelCase											=						lock_file
  return None





 def __str__(      self   :	Optional[int]							)  ->    Optional[Any]:
  __lowerCAmelCase											=						f"""The file lock '{self.lock_file}' could not be acquired."""
  return temp







class 				_lowercase     :


 '''simple docstring'''






 def __init__(      self   :	Optional[Any]      ,       SCREAMING_SNAKE_CASE__   :	List[Any]							)  ->    Optional[int]:
  __lowerCAmelCase											=						lock
  return None
 def __enter__(      self   :	Dict							)  ->    Dict:
  return self.lock





 def __exit__(      self   :	Optional[Any]      ,       SCREAMING_SNAKE_CASE__   :	Tuple      ,       SCREAMING_SNAKE_CASE__   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]							)  ->    Optional[int]:
  self.lock.release()
  return None







class 				_lowercase     :


 '''simple docstring'''






 def __init__(      self   :	Optional[Any]      ,       SCREAMING_SNAKE_CASE__   :	str      ,       SCREAMING_SNAKE_CASE__   :	int=-1      ,       SCREAMING_SNAKE_CASE__   :	Tuple=None							)  ->    Union[str, Any]:
  __lowerCAmelCase											=						max_filename_length if max_filename_length is not None else 2_55
  # Hash the filename if it's too long
  __lowerCAmelCase											=						self.hash_filename_if_too_long(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							)
  # The path to the lock file.
  __lowerCAmelCase											=						lock_file

  # The file descriptor for the *_lock_file* as it is returned by the
  # os.open() function.
  # This file lock is only NOT None, if the object currently holds the
  # lock.
  __lowerCAmelCase											=						None

  # The default timeout value.
  __lowerCAmelCase											=						timeout

  # We use this lock primarily for the lock counter.
  __lowerCAmelCase											=						threading.Lock()

  # The lock counter is used for implementing the nested locking
  # mechanism. Whenever the lock is acquired, the counter is increased and
  # the lock is only released, when this value is 0 again.
  __lowerCAmelCase											=						0
  return None
 @property
 def a					(      self   :	Optional[Any]							)  ->    Union[str, Any]:
  return self._lock_file
 @property
 def a					(      self   :	List[str]							)  ->    Tuple:
  return self._timeout
 @timeout.setter
 def a					(      self   :	List[str]      ,       SCREAMING_SNAKE_CASE__   :	List[Any]							)  ->    str:
  __lowerCAmelCase											=						float(SCREAMING_SNAKE_CASE__							)
  return None
 def a					(      self   :	Tuple							)  ->    Any:
  raise NotImplementedError()
 def a					(      self   :	Any							)  ->    Union[str, Any]:
  raise NotImplementedError()
 @property
 def a					(      self   :	Any							)  ->    List[Any]:
  return self._lock_file_fd is not None
 def a					(      self   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=None      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]=0.0_5							)  ->    List[str]:
  # Use the default timeout, if no timeout is provided.
  if timeout is None:
   __lowerCAmelCase											=						self.timeout

  # Increment the number right at the beginning.
  # We can still undo it, if something fails.
  with self._thread_lock:
   self._lock_counter += 1

  __lowerCAmelCase											=						id(self							)
  __lowerCAmelCase											=						self._lock_file
  __lowerCAmelCase											=						time.time()
  try:
   while True:
    with self._thread_lock:
     if not self.is_locked:
      logger().debug(f"""Attempting to acquire lock {lock_id} on {lock_filename}"""							)
      self._acquire()

    if self.is_locked:
     logger().debug(f"""Lock {lock_id} acquired on {lock_filename}"""							)
     break
    elif timeout >= 0 and time.time() - start_time > timeout:
     logger().debug(f"""Timeout on acquiring lock {lock_id} on {lock_filename}"""							)
     raise Timeout(self._lock_file							)
    else:
     logger().debug(
         f"""Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ..."""							)
     time.sleep(SCREAMING_SNAKE_CASE__							)
  except:  # noqa
   # Something did go wrong, so decrement the counter.
   with self._thread_lock:
    __lowerCAmelCase											=						max(0      ,       self._lock_counter - 1							)

   raise
  return _Acquire_ReturnProxy(lock=self							)
 def a					(      self   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	Dict=False							)  ->    List[str]:
  with self._thread_lock:
   if self.is_locked:
    self._lock_counter -= 1

    if self._lock_counter == 0 or force:
     __lowerCAmelCase											=						id(self							)
     __lowerCAmelCase											=						self._lock_file

     logger().debug(f"""Attempting to release lock {lock_id} on {lock_filename}"""							)
     self._release()
     __lowerCAmelCase											=						0
     logger().debug(f"""Lock {lock_id} released on {lock_filename}"""							)

  return None
 def __enter__(      self   :	Dict							)  ->    Dict:
  self.acquire()
  return self
 def __exit__(      self   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Dict							)  ->    Union[str, Any]:
  self.release()
  return None
 def __del__(      self   :	Optional[Any]							)  ->    Optional[Any]:
  self.release(force=SCREAMING_SNAKE_CASE__							)
  return None





 def a					(      self   :	Optional[Any]      ,       SCREAMING_SNAKE_CASE__   :	str      ,       SCREAMING_SNAKE_CASE__   :	int							)  ->    str:
  __lowerCAmelCase											=						os.path.basename(SCREAMING_SNAKE_CASE__							)
  if len(SCREAMING_SNAKE_CASE__							) > max_length and max_length > 0:
   __lowerCAmelCase											=						os.path.dirname(SCREAMING_SNAKE_CASE__							)
   __lowerCAmelCase											=						str(hash(SCREAMING_SNAKE_CASE__							)							)
   __lowerCAmelCase											=						filename[: max_length - len(SCREAMING_SNAKE_CASE__							) - 8] + """...""" + hashed_filename + """.lock"""
   return os.path.join(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							)
  else:
   return path







class 				_lowercase     (				UpperCAmelCase__			):


 '''simple docstring'''






 def __init__(      self   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	str=-1      ,       SCREAMING_SNAKE_CASE__   :	int=None							)  ->    List[Any]:
  from .file_utils import relative_to_absolute_path

  super().__init__(SCREAMING_SNAKE_CASE__      ,       timeout=SCREAMING_SNAKE_CASE__      ,       max_filename_length=SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						"""\\\\?\\""" + relative_to_absolute_path(self.lock_file							)
 def a					(      self   :	str							)  ->    Tuple:
  __lowerCAmelCase											=						os.O_RDWR | os.O_CREAT | os.O_TRUNC

  try:
   __lowerCAmelCase											=						os.open(self._lock_file      ,       SCREAMING_SNAKE_CASE__							)
  except OSError:
   pass
  else:
   try:
    msvcrt.locking(SCREAMING_SNAKE_CASE__      ,       msvcrt.LK_NBLCK      ,       1							)
   except OSError:
    os.close(SCREAMING_SNAKE_CASE__							)
   else:
    __lowerCAmelCase											=						fd
  return None





 def a					(      self   :	List[str]							)  ->    Dict:
  __lowerCAmelCase											=						self._lock_file_fd
  __lowerCAmelCase											=						None
  msvcrt.locking(SCREAMING_SNAKE_CASE__      ,       msvcrt.LK_UNLCK      ,       1							)
  os.close(SCREAMING_SNAKE_CASE__							)

  try:
   os.remove(self._lock_file							)
  # Probably another instance of the application
  # that acquired the file lock.
  except OSError:
   pass
  return None







class 				_lowercase     (				UpperCAmelCase__			):


 '''simple docstring'''






 def __init__(      self   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	int=-1      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]=None							)  ->    Optional[Any]:
  __lowerCAmelCase											=						os.statvfs(os.path.dirname(SCREAMING_SNAKE_CASE__							)							).f_namemax
  super().__init__(SCREAMING_SNAKE_CASE__      ,       timeout=SCREAMING_SNAKE_CASE__      ,       max_filename_length=SCREAMING_SNAKE_CASE__							)
 def a					(      self   :	int							)  ->    Union[str, Any]:
  __lowerCAmelCase											=						os.O_RDWR | os.O_CREAT | os.O_TRUNC
  __lowerCAmelCase											=						os.open(self._lock_file      ,       SCREAMING_SNAKE_CASE__							)

  try:
   fcntl.flock(SCREAMING_SNAKE_CASE__      ,       fcntl.LOCK_EX | fcntl.LOCK_NB							)
  except OSError:
   os.close(SCREAMING_SNAKE_CASE__							)
  else:
   __lowerCAmelCase											=						fd
  return None





 def a					(      self   :	Tuple							)  ->    Optional[int]:
  # Do not remove the lockfile:
  #
  #   https://github.com/benediktschmitt/py-filelock/issues/31
  #   https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
  __lowerCAmelCase											=						self._lock_file_fd
  __lowerCAmelCase											=						None
  fcntl.flock(SCREAMING_SNAKE_CASE__      ,       fcntl.LOCK_UN							)
  os.close(SCREAMING_SNAKE_CASE__							)
  return None







class 				_lowercase     (				UpperCAmelCase__			):


 '''simple docstring'''






 def a					(      self   :	str							)  ->    Tuple:
  __lowerCAmelCase											=						os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
  try:
   __lowerCAmelCase											=						os.open(self._lock_file      ,       SCREAMING_SNAKE_CASE__							)
  except OSError:
   pass
  else:
   __lowerCAmelCase											=						fd
  return None





 def a					(      self   :	List[str]							)  ->    Optional[int]:
  os.close(self._lock_file_fd							)
  __lowerCAmelCase											=						None

  try:
   os.remove(self._lock_file							)
  # The file is already deleted and that's what we want.
  except OSError:
   pass
  return None







_A			:      List[str]					      =				None

if msvcrt:
 _A			:      Optional[int]					      =				WindowsFileLock
elif fcntl:
 _A			:      Optional[int]					      =				UnixFileLock
else:
 _A			:      Union[str, Any]					      =				SoftFileLock

 if warnings is not None:
  warnings.warn('''only soft file lock is available''')







'''simple docstring'''

import itertools
from dataclasses import dataclass
from typing import Optional

import pandas as pd
import pyarrow as pa

import datasets
from datasets.table import table_cast







@dataclass
class 				_lowercase     (				datasets.BuilderConfig			):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE     :       Optional[datasets.Features]      	=					None







class 				_lowercase     (				datasets.ArrowBasedBuilder			):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE     :       Union[str, Any]      	=					PandasConfig
 def a					(      self   :	Union[str, Any]							)  ->    int:
  return datasets.DatasetInfo(features=self.config.features							)
 def a					(      self   :	Any      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]							)  ->    str:
  if not self.config.data_files:
   raise ValueError(f"""At least one data file must be specified, but got data_files={self.config.data_files}"""							)
  __lowerCAmelCase											=						dl_manager.download_and_extract(self.config.data_files							)
  if isinstance(SCREAMING_SNAKE_CASE__      ,       (str, list, tuple)							):
   __lowerCAmelCase											=						data_files
   if isinstance(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							):
    __lowerCAmelCase											=						[files]
   # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
   __lowerCAmelCase											=						[dl_manager.iter_files(SCREAMING_SNAKE_CASE__							) for file in files]
   return [datasets.SplitGenerator(name=datasets.Split.TRAIN      ,       gen_kwargs={"""files""": files}							)]
  __lowerCAmelCase											=						[]
  for split_name, files in data_files.items():
   if isinstance(SCREAMING_SNAKE_CASE__      ,       SCREAMING_SNAKE_CASE__							):
    __lowerCAmelCase											=						[files]
   # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
   __lowerCAmelCase											=						[dl_manager.iter_files(SCREAMING_SNAKE_CASE__							) for file in files]
   splits.append(datasets.SplitGenerator(name=SCREAMING_SNAKE_CASE__      ,       gen_kwargs={"""files""": files}							)							)
  return splits
 def a					(      self   :	Any      ,       SCREAMING_SNAKE_CASE__   :	pa.Table							)  ->    pa.Table:
  if self.config.features is not None:
   # more expensive cast to support nested features with keys in a different order
   # allows str <-> int/float or str to Audio for example
   __lowerCAmelCase											=						table_cast(SCREAMING_SNAKE_CASE__      ,       self.config.features.arrow_schema							)
  return pa_table





 def a					(      self   :	Any      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]							)  ->    List[Any]:
  for i, file in enumerate(itertools.chain.from_iterable(SCREAMING_SNAKE_CASE__							)							):
   with open(SCREAMING_SNAKE_CASE__      ,       """rb"""							) as f:
    __lowerCAmelCase											=						pa.Table.from_pandas(pd.read_pickle(SCREAMING_SNAKE_CASE__							)							)
    yield i, self._cast_table(SCREAMING_SNAKE_CASE__							)






