
"""simple docstring"""






import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
__SCREAMING_SNAKE_CASE							=     'src/transformers'


# This is to make sure the transformers module imported is the one in the repo.
__SCREAMING_SNAKE_CASE							=     importlib.util.spec_from_file_location(
    'transformers',
    os.path.join(PATH_TO_TRANSFORMERS, '__init__.py'),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
__SCREAMING_SNAKE_CASE							=     spec.loader.load_module()

__SCREAMING_SNAKE_CASE							=     transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
__SCREAMING_SNAKE_CASE							=     re.compile('\[(.+?)\]\((https://huggingface\.co/.+?)\)')


__SCREAMING_SNAKE_CASE							=     {
    'CLIPConfigMixin',
    'DecisionTransformerConfigMixin',
    'EncoderDecoderConfigMixin',
    'RagConfigMixin',
    'SpeechEncoderDecoderConfigMixin',
    'VisionEncoderDecoderConfigMixin',
    'VisionTextDualEncoderConfigMixin',
}




def       A_  (				):
 UpperCamelCase_     :					str   	=[]

 for config_class in list(CONFIG_MAPPING.values()	):
  UpperCamelCase_     :					Tuple   	=False

  # source code of `config_class`
  UpperCamelCase_     :					Union[str, Any]   	=inspect.getsource(__lowercase	)
  UpperCamelCase_     :					Dict   	=_re_checkpoint.findall(__lowercase	)

  for checkpoint in checkpoints:
   # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
   # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
   UpperCamelCase_	,     UpperCamelCase_     :					Optional[Any]   	=checkpoint

   # verify the checkpoint name corresponds to the checkpoint link
   UpperCamelCase_     :					Union[str, Any]   	=F'''https://huggingface.co/{ckpt_name}'''
   if ckpt_link == ckpt_link_from_name:
    UpperCamelCase_     :					Union[str, Any]   	=True
    break

  UpperCamelCase_     :					List[Any]   	=config_class.__name__
  if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
   configs_without_checkpoint.append(__lowercase	)

 if len(__lowercase	) > 0:
  UpperCamelCase_     :					Optional[Any]   	='\n'.join(sorted(__lowercase	)	)
  raise ValueError(F'''The following configurations don\'t contain any valid checkpoint:\n{message}'''	)


if __name__ == "__main__":
 check_config_docstrings_have_checkpoints()


"""simple docstring"""






from pathlib import PurePosixPath
from typing import Optional

import fsspec
from fsspec import AbstractFileSystem
from huggingface_hub.hf_api import DatasetInfo

from ..utils.file_utils import get_authentication_headers_for_url
from ..utils.hub import hf_hub_url





class 				a__					(						A__       ):
     UpperCAmelCase__         =      ''''''
     UpperCAmelCase__         =      '''hf-legacy'''  # "hf://"" is reserved for hffs



     def __init__(							self :Dict      ,  _lowerCamelCase :Optional[DatasetInfo] = None      ,  _lowerCamelCase :Optional[str] = None      ,  **_lowerCamelCase :Tuple      ,  ):







      '''simple docstring'''



      super().__init__(self      ,  **_lowerCamelCase   )
      UpperCamelCase_     :					List[str]   	=repo_info
      UpperCamelCase_     :					Any   	=token
      UpperCamelCase_     :					Tuple   	=None



     def  lowerCamelCase_						(							self :Dict   ):







      '''simple docstring'''



      if self.dir_cache is None:
       UpperCamelCase_     :					Any   	={}
       for hf_file in self.repo_info.siblings:
        # TODO(QL): add sizes
        UpperCamelCase_     :					Optional[Any]   	={
            'name': hf_file.rfilename,
            'size': None,
            'type': 'file',
        }
        self.dir_cache.update(
            {
                str(_lowerCamelCase   ): {'name': str(_lowerCamelCase   ), 'size': None, 'type': 'directory'}
                for d in list(PurePosixPath(hf_file.rfilename   ).parents   )[:-1]
            }   )



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :str      ,  _lowerCamelCase :str = "rb"      ,  **_lowerCamelCase :str      ,  ):







      '''simple docstring'''



      if not isinstance(self.repo_info      ,  _lowerCamelCase   ):
       raise NotImplementedError(f'''Open is only implemented for dataset repositories, but got {self.repo_info}'''   )
      UpperCamelCase_     :					List[Any]   	=hf_hub_url(self.repo_info.id      ,  _lowerCamelCase      ,  revision=self.repo_info.sha   )
      return fsspec.open(
          _lowerCamelCase      ,  mode=_lowerCamelCase      ,  headers=get_authentication_headers_for_url(_lowerCamelCase      ,  use_auth_token=self.token   )      ,  client_kwargs={'trust_env': True}      ,  ).open()



     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :Tuple      ,  **_lowerCamelCase :Any   ):







      '''simple docstring'''



      self._get_dirs()
      UpperCamelCase_     :					Tuple   	=self._strip_protocol(_lowerCamelCase   )
      if path in self.dir_cache:
       return self.dir_cache[path]
      else:
       raise FileNotFoundError(_lowerCamelCase   )




     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :List[str]      ,  _lowerCamelCase :List[Any]=False      ,  **_lowerCamelCase :Any   ):







      '''simple docstring'''



      self._get_dirs()
      UpperCamelCase_     :					str   	=PurePosixPath(path.strip('/'   )   )
      UpperCamelCase_     :					List[str]   	={}
      for p, f in self.dir_cache.items():
       UpperCamelCase_     :					List[Any]   	=PurePosixPath(p.strip('/'   )   )
       UpperCamelCase_     :					Tuple   	=p.parent
       if root == path:
        UpperCamelCase_     :					int   	=f
      UpperCamelCase_     :					Optional[int]   	=list(paths.values()   )
      if detail:
       return out
      else:
       return sorted(f['name'] for f in out   )
