


import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


__UpperCamelCase    		=       'hf-internal-testing/tiny-random-bert'
__UpperCamelCase    		=       os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
__UpperCamelCase    		=       '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'







class        lowerCamelCase__	(							unittest.TestCase      ):






   """simple docstring"""






   def 				snake_case__				(							self     ):

      '''simple docstring'''


      UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						UpperCAmelCase_     )
      # Should have downloaded the file in here
      self.assertTrue(os.path.isdir(UpperCAmelCase_     )     )
      # Cache should contain at least those three subfolders:
      for subfolder in ["blobs", "refs", "snapshots"]:
         self.assertTrue(os.path.isdir(os.path.join(UpperCAmelCase_      ,						UpperCAmelCase_     )     )     )
      with open(os.path.join(UpperCAmelCase_      ,						"refs"      ,						"main"     )     ) as f:
         UpperCamelCase__					=       f.read()
      self.assertEqual(UpperCAmelCase_      ,						os.path.join(UpperCAmelCase_      ,						"snapshots"      ,						UpperCAmelCase_      ,						UpperCAmelCase_     )     )
      self.assertTrue(os.path.isfile(UpperCAmelCase_     )     )

      # File is cached at the same place the second time.
      UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						UpperCAmelCase_     )
      self.assertEqual(UpperCAmelCase_      ,						UpperCAmelCase_     )

      # Using a specific revision to test the full commit hash.
      UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						UpperCAmelCase_      ,						revision="9b8c223"     )
      self.assertEqual(UpperCAmelCase_      ,						os.path.join(UpperCAmelCase_      ,						"snapshots"      ,						UpperCAmelCase_      ,						UpperCAmelCase_     )     )




   def 				snake_case__				(							self     ):

      '''simple docstring'''


      with self.assertRaisesRegex(UpperCAmelCase_      ,						"is not a valid model identifier"     ):
         UpperCamelCase__					=       cached_file("tiny-random-bert"      ,						UpperCAmelCase_     )

      with self.assertRaisesRegex(UpperCAmelCase_      ,						"is not a valid git identifier"     ):
         UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						UpperCAmelCase_      ,						revision="aaaa"     )

      with self.assertRaisesRegex(UpperCAmelCase_      ,						"does not appear to have a file named"     ):
         UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						"conf"     )




   def 				snake_case__				(							self     ):

      '''simple docstring'''


      with self.assertRaisesRegex(UpperCAmelCase_      ,						"does not appear to have a file named"     ):
         UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						"conf"     )

      with open(os.path.join(UpperCAmelCase_      ,						"refs"      ,						"main"     )     ) as f:
         UpperCamelCase__					=       f.read()
      self.assertTrue(os.path.isfile(os.path.join(UpperCAmelCase_      ,						".no_exist"      ,						UpperCAmelCase_      ,						"conf"     )     )     )

      UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						"conf"      ,						_raise_exceptions_for_missing_entries=UpperCAmelCase_     )
      self.assertIsNone(UpperCAmelCase_     )

      UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						"conf"      ,						local_files_only=UpperCAmelCase_      ,						_raise_exceptions_for_missing_entries=UpperCAmelCase_     )
      self.assertIsNone(UpperCAmelCase_     )

      UpperCamelCase__					=       mock.Mock()
      UpperCamelCase__					=       500
      UpperCamelCase__					=       {}
      UpperCamelCase__					=       HTTPError
      UpperCamelCase__					=       {}

      # Under the mock environment we get a 500 error when trying to reach the tokenizer.
      with mock.patch("requests.Session.request"      ,						return_value=UpperCAmelCase_     ) as mock_head:
         UpperCamelCase__					=       cached_file(UpperCAmelCase_      ,						"conf"      ,						_raise_exceptions_for_connection_errors=UpperCAmelCase_     )
         self.assertIsNone(UpperCAmelCase_     )
         # This check we did call the fake head request
         mock_head.assert_called()




   def 				snake_case__				(							self     ):

      '''simple docstring'''


      self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"      ,						UpperCAmelCase_     )     )
      self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"      ,						UpperCAmelCase_     )     )
      self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"      ,						UpperCAmelCase_     )     )




   def 				snake_case__				(							self     ):

      '''simple docstring'''


      self.assertIsNone(get_file_from_repo("bert-base-cased"      ,						"ahah.txt"     )     )

      # The function raises if the repository does not exist.
      with self.assertRaisesRegex(UpperCAmelCase_      ,						"is not a valid model identifier"     ):
         get_file_from_repo("bert-base-case"      ,						UpperCAmelCase_     )

      # The function raises if the revision does not exist.
      with self.assertRaisesRegex(UpperCAmelCase_      ,						"is not a valid git identifier"     ):
         get_file_from_repo("bert-base-cased"      ,						UpperCAmelCase_      ,						revision="ahaha"     )

      UpperCamelCase__					=       get_file_from_repo("bert-base-cased"      ,						UpperCAmelCase_     )
      # The name is the cached name which is not very easy to test, so instead we load the content.
      UpperCamelCase__					=       json.loads(open(UpperCAmelCase_      ,						"r"     ).read()     )
      self.assertEqual(config["hidden_size"]      ,						768     )







   def 				snake_case__				(							self     ):

      '''simple docstring'''


      with tempfile.TemporaryDirectory() as tmp_dir:
         UpperCamelCase__					=       Path(UpperCAmelCase_     ) / "a.txt"
         filename.touch()
         self.assertEqual(get_file_from_repo(UpperCAmelCase_      ,						"a.txt"     )      ,						str(UpperCAmelCase_     )     )

         self.assertIsNone(get_file_from_repo(UpperCAmelCase_      ,						"b.txt"     )     )






def        UpperCamelCase_(				_A   :str  )->		str:
			UpperCamelCase__					=       ""
			for ch in key:
						if ch == " " or ch not in key_no_dups and ch.isalpha():
									key_no_dups += ch
			return key_no_dups
def        UpperCamelCase_(				_A   :str  )->		dict[str, str]:
			UpperCamelCase__					=       [chr(i + 65  ) for i in range(26  )]
			# Remove duplicate characters from key
			UpperCamelCase__					=       remove_duplicates(key.upper()  )
			UpperCamelCase__					=       len(_A  )
			# First fill cipher with key characters
			UpperCamelCase__					=       {alphabet[i]: char for i, char in enumerate(_A  )}
			# Then map remaining characters in alphabet to
			# the alphabet from the beginning
			for i in range(len(_A  )				,     26  ):
						UpperCamelCase__					=       alphabet[i - offset]
						# Ensure we are not mapping letters to letters previously mapped
						while char in key:
									offset -= 1
									UpperCamelCase__					=       alphabet[i - offset]
						UpperCamelCase__					=       char
			return cipher_alphabet
def        UpperCamelCase_(				_A   :str				,     _A   :dict[str, str]  )->		str:
			return "".join(cipher_map.get(_A				,     _A  ) for ch in message.upper()  )
def        UpperCamelCase_(				_A   :str				,     _A   :dict[str, str]  )->		str:
			UpperCamelCase__					=       {v: k for k, v in cipher_map.items()}
			return "".join(rev_cipher_map.get(_A				,     _A  ) for ch in message.upper()  )
def        UpperCamelCase_(				)->		None:
			UpperCamelCase__					=       input("Enter message to encode or decode: "  ).strip()
			UpperCamelCase__					=       input("Enter keyword: "  ).strip()
			UpperCamelCase__					=       input("Encipher or decipher? E/D:"  ).strip()[0].lower()
			try:
						UpperCamelCase__					=       {"e": encipher, "d": decipher}[option]
			except KeyError:
						raise KeyError("invalid input option"  )
			UpperCamelCase__					=       create_cipher_map(_A  )
			print(func(_A				,     _A  )  )


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						main()


