



'''simple docstring'''




import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand






class 						_UpperCamelCase  (							lowerCamelCase__   ):


			'''simple docstring'''

			@staticmethod
			def UpperCamelCase__ (							lowerCAmelCase__					:			ArgumentParser						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		str   	=		parser.add_parser("""login"""							,					help="""Log in using the same credentials as on huggingface.co"""						)
					login_parser.set_defaults(func=lambda lowerCAmelCase__						: LoginCommand(lowerCAmelCase__						)						)
					__SCREAMING_SNAKE_CASE	:		Dict   	=		parser.add_parser("""whoami"""							,					help="""Find out which huggingface.co account you are logged in as."""						)
					whoami_parser.set_defaults(func=lambda lowerCAmelCase__						: WhoamiCommand(lowerCAmelCase__						)						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		parser.add_parser("""logout"""							,					help="""Log out"""						)
					logout_parser.set_defaults(func=lambda lowerCAmelCase__						: LogoutCommand(lowerCAmelCase__						)						)

					# new system: git-based repo system
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		parser.add_parser(
					    """repo"""							,					help="""Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos."""							,					)
					__SCREAMING_SNAKE_CASE	:		str   	=		repo_parser.add_subparsers(
					    help="""Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands"""						)
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		repo_subparsers.add_parser(
					    """create"""							,					help="""Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co"""						)
					repo_create_parser.add_argument(
					    """name"""							,					type=lowerCAmelCase__							,					help="""Name for your model's repo. Will be namespaced under your username to build the model id."""							,					)
					repo_create_parser.add_argument("""--organization"""							,					type=lowerCAmelCase__							,					help="""Optional: organization namespace."""						)
					repo_create_parser.add_argument("""-y"""							,					"""--yes"""							,					action="""store_true"""							,					help="""Optional: answer Yes to the prompt"""						)
					repo_create_parser.set_defaults(func=lambda lowerCAmelCase__						: RepoCreateCommand(lowerCAmelCase__						)						)






class 						_UpperCamelCase  :


			'''simple docstring'''

			_A				:		Dict         =		'''\u001b[1m'''
			_A				:		Tuple         =		'''\u001b[31m'''
			_A				:		Union[str, Any]         =		'''\u001b[90m'''
			_A				:		str         =		'''\u001b[0m'''

			@classmethod
			def UpperCamelCase__ (							cls					:			Dict							,					lowerCAmelCase__					:			int						):





					"""simple docstring"""






					return F"{cls._bold}{s}{cls._reset}"

			@classmethod
			def UpperCamelCase__ (							cls					:			Dict							,					lowerCAmelCase__					:			Tuple						):





					"""simple docstring"""






					return F"{cls._bold}{cls._red}{s}{cls._reset}"



			@classmethod
			def UpperCamelCase__ (							cls					:			Dict							,					lowerCAmelCase__					:			Optional[int]						):





					"""simple docstring"""






					return F"{cls._gray}{s}{cls._reset}"


def     lowerCAmelCase_       (     _lowerCamelCase:       List[List[Union[str, int]]]	,				_lowerCamelCase:       List[str] ):
		__SCREAMING_SNAKE_CASE	:		Any   	=		[max(len(str(_lowerCamelCase ) ) for x in col ) for col in zip(*_lowerCamelCase	,				_lowerCamelCase )]
		__SCREAMING_SNAKE_CASE	:		Any   	=		("""{{:{}}} """ * len(_lowerCamelCase )).format(*_lowerCamelCase )
		__SCREAMING_SNAKE_CASE	:		List[str]   	=		[]
		lines.append(row_format.format(*_lowerCamelCase ) )
		lines.append(row_format.format(*["""-""" * w for w in col_widths] ) )
		for row in rows:
				lines.append(row_format.format(*_lowerCamelCase ) )
		return "\n".join(_lowerCamelCase )






class 						_UpperCamelCase  :


			'''simple docstring'''

			def __init__(							self					:			Optional[int]							,					lowerCAmelCase__					:			List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		int   	=		args






class 						_UpperCamelCase  (							lowerCamelCase__   ):


			'''simple docstring'''

			def UpperCamelCase__ (							self					:			List[Any]						):





					"""simple docstring"""






					print(
					    ANSI.red(
					        """ERROR! `huggingface-cli login` uses an outdated login mechanism """
					        """that is not compatible with the Hugging Face Hub backend anymore. """
					        """Please use `huggingface-cli login instead."""						)						)






class 						_UpperCamelCase  (							lowerCamelCase__   ):


			'''simple docstring'''

			def UpperCamelCase__ (							self					:			Any						):





					"""simple docstring"""






					print(
					    ANSI.red(
					        """WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use """
					        """`huggingface-cli whoami` instead."""						)						)
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		HfFolder.get_token()
					if token is None:
							print("""Not logged in"""						)
							exit()
					try:
							__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		whoami(lowerCAmelCase__						)
							print(lowerCAmelCase__						)
							if orgs:
									print(ANSI.bold("""orgs: """						)							,					""",""".join(lowerCAmelCase__						)						)
					except HTTPError as e:
							print(lowerCAmelCase__						)
							print(ANSI.red(e.response.text						)						)
							exit(1						)






class 						_UpperCamelCase  (							lowerCamelCase__   ):


			'''simple docstring'''

			def UpperCamelCase__ (							self					:			str						):





					"""simple docstring"""






					print(
					    ANSI.red(
					        """ERROR! `transformers-cli logout` uses an outdated logout mechanism """
					        """that is not compatible with the Hugging Face Hub backend anymore. """
					        """Please use `huggingface-cli logout instead."""						)						)






class 						_UpperCamelCase  (							lowerCamelCase__   ):


			'''simple docstring'''

			def UpperCamelCase__ (							self					:			Dict						):





					"""simple docstring"""






					print(
					    ANSI.red(
					        """WARNING! Managing repositories through transformers-cli is deprecated. """
					        """Please use `huggingface-cli` instead."""						)						)
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		HfFolder.get_token()
					if token is None:
							print("""Not logged in"""						)
							exit(1						)
					try:
							__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		subprocess.check_output(["""git""", """--version"""]						).decode("""utf-8"""						)
							print(ANSI.gray(stdout.strip()						)						)
					except FileNotFoundError:
							print("""Looks like you do not have git installed, please install."""						)

					try:
							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		subprocess.check_output(["""git-lfs""", """--version"""]						).decode("""utf-8"""						)
							print(ANSI.gray(stdout.strip()						)						)
					except FileNotFoundError:
							print(
							    ANSI.red(
							        """Looks like you do not have git-lfs installed, please install."""
							        """ You can install from https://git-lfs.github.com/."""
							        """ Then run `git lfs install` (you only have to do this once)."""						)						)
					print(""""""						)

					__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		whoami(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		self.args.organization if self.args.organization is not None else user
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		F"{namespace}/{self.args.name}"
					print(F"You are about to create {ANSI.bold(lowerCAmelCase__						)}"						)

					if not self.args.yes:
							__SCREAMING_SNAKE_CASE	:		Any   	=		input("""Proceed? [Y/n] """						).lower()
							if not (choice == "" or choice == "y" or choice == "yes"):
									print("""Abort"""						)
									exit()
					try:
							__SCREAMING_SNAKE_CASE	:		List[Any]   	=		create_repo(lowerCAmelCase__							,					name=self.args.name							,					organization=self.args.organization						)
					except HTTPError as e:
							print(lowerCAmelCase__						)
							print(ANSI.red(e.response.text						)						)
							exit(1						)
					print("""\nYour repo now lives at:"""						)
					print(F"  {ANSI.bold(lowerCAmelCase__						)}"						)
					print("""\nYou can clone it locally with the command below, and commit/push as usual."""						)
					print(F"\n  git clone {url}"						)
					print(""""""						)




'''simple docstring'''




import os
import sys

from . import rsa_key_generator as rkg

UpperCamelCase__	:     Any    =			1_28
UpperCamelCase__	:     Optional[int]    =			2_56


def     lowerCAmelCase_       (     _lowerCamelCase:       str	,				_lowerCamelCase:       int = DEFAULT_BLOCK_SIZE ):
		__SCREAMING_SNAKE_CASE	:		Tuple   	=		message.encode("""ascii""" )

		__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		[]
		for block_start in range(0	,				len(_lowerCamelCase )	,				_lowerCamelCase ):
				__SCREAMING_SNAKE_CASE	:		str   	=		0
				for i in range(_lowerCamelCase	,				min(block_start + block_size	,				len(_lowerCamelCase ) ) ):
						block_int += message_bytes[i] * (BYTE_SIZE ** (i % block_size))
				block_ints.append(_lowerCamelCase )
		return block_ints


def     lowerCAmelCase_       (     _lowerCamelCase:       list[int]	,				_lowerCamelCase:       int	,				_lowerCamelCase:       int = DEFAULT_BLOCK_SIZE ):
		__SCREAMING_SNAKE_CASE	:		list[str]   	=		[]
		for block_int in block_ints:
				__SCREAMING_SNAKE_CASE	:		list[str]   	=		[]
				for i in range(block_size - 1	,				-1	,				-1 ):
						if len(_lowerCamelCase ) + i < message_length:
								__SCREAMING_SNAKE_CASE	:		List[Any]   	=		block_int // (BYTE_SIZE**i)
								__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		block_int % (BYTE_SIZE**i)
								block_message.insert(0	,				chr(_lowerCamelCase ) )
				message.extend(_lowerCamelCase )
		return "".join(_lowerCamelCase )


def     lowerCAmelCase_       (     _lowerCamelCase:       str	,				_lowerCamelCase:       tuple[int, int]	,				_lowerCamelCase:       int = DEFAULT_BLOCK_SIZE ):
		__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		[]
		__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		key

		for block in get_blocks_from_text(_lowerCamelCase	,				_lowerCamelCase ):
				encrypted_blocks.append(pow(_lowerCamelCase	,				_lowerCamelCase	,				_lowerCamelCase ) )
		return encrypted_blocks


def     lowerCAmelCase_       (     _lowerCamelCase:       list[int]	,				_lowerCamelCase:       int	,				_lowerCamelCase:       tuple[int, int]	,				_lowerCamelCase:       int = DEFAULT_BLOCK_SIZE	,				):
		__SCREAMING_SNAKE_CASE	:		Dict   	=		[]
		__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		key
		for block in encrypted_blocks:
				decrypted_blocks.append(pow(_lowerCamelCase	,				_lowerCamelCase	,				_lowerCamelCase ) )
		return get_text_from_blocks(_lowerCamelCase	,				_lowerCamelCase	,				_lowerCamelCase )


def     lowerCAmelCase_       (     _lowerCamelCase:       str ):
		with open(_lowerCamelCase ) as fo:
				__SCREAMING_SNAKE_CASE	:		Tuple   	=		fo.read()
		__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		int   	=		content.split(""",""" )
		return (int(_lowerCamelCase ), int(_lowerCamelCase ), int(_lowerCamelCase ))


def     lowerCAmelCase_       (     _lowerCamelCase:       str	,				_lowerCamelCase:       str	,				_lowerCamelCase:       str	,				_lowerCamelCase:       int = DEFAULT_BLOCK_SIZE	,				):
		__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Tuple   	=		read_key_file(_lowerCamelCase )
		if key_size < block_size * 8:
				sys.exit(
				    """ERROR: Block size is {} bits and key size is {} bits. The RSA cipher """
				    """requires the block size to be equal to or greater than the key size. """
				    """Either decrease the block size or use different keys.""".format(
				        block_size * 8	,				_lowerCamelCase ) )

		__SCREAMING_SNAKE_CASE	:		Dict   	=		[str(_lowerCamelCase ) for i in encrypt_message(_lowerCamelCase	,				(n, e)	,				_lowerCamelCase )]

		__SCREAMING_SNAKE_CASE	:		int   	=		""",""".join(_lowerCamelCase )
		__SCREAMING_SNAKE_CASE	:		List[Any]   	=		F"{len(_lowerCamelCase )}_{block_size}_{encrypted_content}"
		with open(_lowerCamelCase	,				"""w""" ) as fo:
				fo.write(_lowerCamelCase )
		return encrypted_content


def     lowerCAmelCase_       (     _lowerCamelCase:       str	,				_lowerCamelCase:       str ):
		__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Tuple   	=		read_key_file(_lowerCamelCase )
		with open(_lowerCamelCase ) as fo:
				__SCREAMING_SNAKE_CASE	:		Any   	=		fo.read()
		__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		content.split("""_""" )
		__SCREAMING_SNAKE_CASE	:		int   	=		int(_lowerCamelCase )
		__SCREAMING_SNAKE_CASE	:		int   	=		int(_lowerCamelCase )

		if key_size < block_size * 8:
				sys.exit(
				    """ERROR: Block size is {} bits and key size is {} bits. The RSA cipher """
				    """requires the block size to be equal to or greater than the key size. """
				    """Did you specify the correct key file and encrypted file?""".format(
				        block_size * 8	,				_lowerCamelCase ) )

		__SCREAMING_SNAKE_CASE	:		Dict   	=		[]
		for block in encrypted_message.split(""",""" ):
				encrypted_blocks.append(int(_lowerCamelCase ) )

		return decrypt_message(_lowerCamelCase	,				_lowerCamelCase	,				(n, d)	,				_lowerCamelCase )


def     lowerCAmelCase_       (     ):
		__SCREAMING_SNAKE_CASE	:		Dict   	=		"""encrypted_file.txt"""
		__SCREAMING_SNAKE_CASE	:		Tuple   	=		input(r"""Encrypt\Decrypt [e\d]: """ )

		if response.lower().startswith("""e""" ):
				__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		"""encrypt"""
		elif response.lower().startswith("""d""" ):
				__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		"""decrypt"""

		if mode == "encrypt":
				if not os.path.exists("""rsa_pubkey.txt""" ):
						rkg.make_key_files("""rsa"""	,				10_24 )

				__SCREAMING_SNAKE_CASE	:		int   	=		input("""\nEnter message: """ )
				__SCREAMING_SNAKE_CASE	:		str   	=		"""rsa_pubkey.txt"""
				print(F"Encrypting and writing to {filename}..." )
				__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		encrypt_and_write_to_file(_lowerCamelCase	,				_lowerCamelCase	,				_lowerCamelCase )

				print("""\nEncrypted text:""" )
				print(_lowerCamelCase )

		elif mode == "decrypt":
				__SCREAMING_SNAKE_CASE	:		List[str]   	=		"""rsa_privkey.txt"""
				print(F"Reading from {filename} and decrypting..." )
				__SCREAMING_SNAKE_CASE	:		List[str]   	=		read_from_file_and_decrypt(_lowerCamelCase	,				_lowerCamelCase )
				print("""writing decryption to rsa_decryption.txt...""" )
				with open("""rsa_decryption.txt"""	,				"""w""" ) as dec:
						dec.write(_lowerCamelCase )

				print("""\nDecryption:""" )
				print(_lowerCamelCase )


if __name__ == "__main__":
						main()