
'''simple docstring'''







import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402


_lowercase			:  Optional[int]   							=	get_tests_dir("fixtures/dummy-config.json")






class 			lowerCAmelCase__       (  unittest.TestCase					):






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Dict													=	0






					def      _snake_case   (       self		):


											"""simple docstring"""





											self.assertIsNotNone(transformers.models.auto.__spec__		)
											self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''		)		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					str													=	AutoConfig.from_pretrained('''bert-base-uncased'''		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Union[str, Any]													=	AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Any													=	AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					int													=	AutoConfig.for_model('''roberta'''		)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											with tempfile.TemporaryDirectory() as tmp_dir:
																	# This model name contains bert and roberta, but roberta ends up being picked.
																	lowercase_     :					List[Any]													=	os.path.join(__SCREAMING_SNAKE_CASE    ,							'''fake-roberta'''		)
																	os.makedirs(__SCREAMING_SNAKE_CASE    ,							exist_ok=__SCREAMING_SNAKE_CASE		)
																	with open(os.path.join(__SCREAMING_SNAKE_CASE    ,							'''config.json'''		)    ,							'''w'''		) as f:
																							f.write(json.dumps({}		)		)
																	lowercase_     :					Optional[Any]													=	AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertEqual(type(__SCREAMING_SNAKE_CASE		)    ,							__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											try:
																	AutoConfig.register('''custom'''    ,							__SCREAMING_SNAKE_CASE		)
																	# Wrong model type will raise an error
																	with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																							AutoConfig.register('''model'''    ,							__SCREAMING_SNAKE_CASE		)
																	# Trying to register something existing in the Transformers library will raise an error
																	with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																							AutoConfig.register('''bert'''    ,							__SCREAMING_SNAKE_CASE		)

																	# Now that the config is registered, it can be used as any other config with the auto-API
																	lowercase_     :					str													=	CustomConfig()
																	with tempfile.TemporaryDirectory() as tmp_dir:
																							config.save_pretrained(__SCREAMING_SNAKE_CASE		)
																							lowercase_     :					Optional[int]													=	AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)
																							self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											finally:
																	if "custom" in CONFIG_MAPPING._extra_content:
																							del CONFIG_MAPPING._extra_content["custom"]






					def      _snake_case   (       self		):


											"""simple docstring"""





											with self.assertRaisesRegex(
											    __SCREAMING_SNAKE_CASE    ,							'''bert-base is not a local folder and is not a valid model identifier'''		):
																	lowercase_     :					Dict													=	AutoConfig.from_pretrained('''bert-base'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											with self.assertRaisesRegex(
											    __SCREAMING_SNAKE_CASE    ,							R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''		):
																	lowercase_     :					int													=	AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE    ,							revision='''aaaaaa'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											with self.assertRaisesRegex(
											    __SCREAMING_SNAKE_CASE    ,							'''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''    ,							):
																	lowercase_     :					Optional[Any]													=	AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																	lowercase_     :					Optional[int]													=	AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''		)
											# If remote code is disabled, we can't load this config.
											with self.assertRaises(__SCREAMING_SNAKE_CASE		):
																	lowercase_     :					Optional[Any]													=	AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,							trust_remote_code=__SCREAMING_SNAKE_CASE		)

											lowercase_     :					Any													=	AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,							trust_remote_code=__SCREAMING_SNAKE_CASE		)
											self.assertEqual(config.__class__.__name__    ,							'''NewModelConfig'''		)

											# Test config can be reloaded.
											with tempfile.TemporaryDirectory() as tmp_dir:
																	config.save_pretrained(__SCREAMING_SNAKE_CASE		)
																	lowercase_     :					Optional[Any]													=	AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE    ,							trust_remote_code=__SCREAMING_SNAKE_CASE		)
											self.assertEqual(reloaded_config.__class__.__name__    ,							'''NewModelConfig'''		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											class 			lowerCAmelCase__       (  lowerCamelCase_					):
																lowerCAmelCase_					       =					'''new-model'''

											try:
																	AutoConfig.register('''new-model'''    ,							__SCREAMING_SNAKE_CASE		)
																	# If remote code is not set, the default is to use local
																	lowercase_     :					str													=	AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''		)
																	self.assertEqual(config.__class__.__name__    ,							'''NewModelConfigLocal'''		)

																	# If remote code is disabled, we load the local one.
																	lowercase_     :					int													=	AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,							trust_remote_code=__SCREAMING_SNAKE_CASE		)
																	self.assertEqual(config.__class__.__name__    ,							'''NewModelConfigLocal'''		)

																	# If remote is enabled, we load from the Hub
																	lowercase_     :					Tuple													=	AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''    ,							trust_remote_code=__SCREAMING_SNAKE_CASE		)
																	self.assertEqual(config.__class__.__name__    ,							'''NewModelConfig'''		)

											finally:
																	if "new-model" in CONFIG_MAPPING._extra_content:
																							del CONFIG_MAPPING._extra_content["new-model"]





'''simple docstring'''







from __future__ import annotations






class 			lowerCAmelCase__       :






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											lowercase_     :					Any													=	key






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					List[str]													=	key or self.__key or 1

											# make sure key is an appropriate size
											key %= 2_55

											return [chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		) for ch in content]






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					Optional[int]													=	key or self.__key or 1

											# make sure key is an appropriate size
											key %= 2_55

											return [chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		) for ch in content]






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					List[Any]													=	key or self.__key or 1

											# make sure key can be any size
											while key > 2_55:
																	key -= 2_55

											# This will be returned
											lowercase_     :					str													=	''''''

											for ch in content:
																	ans += chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		)

											return ans






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					Any													=	key or self.__key or 1

											# make sure key can be any size
											while key > 2_55:
																	key -= 2_55

											# This will be returned
											lowercase_     :					Dict													=	''''''

											for ch in content:
																	ans += chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		)

											return ans






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											try:
																	with open(__SCREAMING_SNAKE_CASE		) as fin, open('''encrypt.out'''    ,							'''w+'''		) as fout:
																							# actual encrypt-process
																							for line in fin:
																													fout.write(self.encrypt_string(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)		)

											except OSError:
																	return False

											return True






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											try:
																	with open(__SCREAMING_SNAKE_CASE		) as fin, open('''decrypt.out'''    ,							'''w+'''		) as fout:
																							# actual encrypt-process
																							for line in fin:
																													fout.write(self.decrypt_string(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)		)

											except OSError:
																	return False

											return True


# Tests
# crypt = XORCipher()
# key = 67

# # test encrypt
# print(crypt.encrypt("hallo welt",key))
# # test decrypt
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))

# # test encrypt_string
# print(crypt.encrypt_string("hallo welt",key))

# # test decrypt_string
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))

# if (crypt.encrypt_file("test.txt",key)):
#       print("encrypt successful")
# else:
#       print("encrypt unsuccessful")

# if (crypt.decrypt_file("encrypt.out",key)):
#       print("decrypt successful")
# else:
#       print("decrypt unsuccessful")



