


import os
import sys

from . import rsa_key_generator as rkg

SCREAMING_SNAKE_CASE : List[Any]          =    1_28
SCREAMING_SNAKE_CASE : int          =    2_56





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: str	,	SCREAMING_SNAKE_CASE_				: int = DEFAULT_BLOCK_SIZE      ):






							"""simple docstring"""



							a_       :   List[Any]			=	message.encode("""ascii"""      )

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





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: list[int]	,	SCREAMING_SNAKE_CASE_				: int	,	SCREAMING_SNAKE_CASE_				: int = DEFAULT_BLOCK_SIZE      ):






							"""simple docstring"""



							a_       :   list[str]			=	[]
							for block_int in block_ints:
														a_       :   list[str]			=	[]
														for i in range(block_size - 1	,	-1	,	-1      ):
																					if len(SCREAMING_SNAKE_CASE_      ) + i < message_length:
																												a_       :   Dict			=	block_int // (BYTE_SIZE**i)
																												a_       :   Optional[int]			=	block_int % (BYTE_SIZE**i)
																												block_message.insert(0	,	chr(SCREAMING_SNAKE_CASE_      )      )
														message.extend(SCREAMING_SNAKE_CASE_      )
							return "".join(SCREAMING_SNAKE_CASE_      )





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: str	,	SCREAMING_SNAKE_CASE_				: tuple[int, int]	,	SCREAMING_SNAKE_CASE_				: int = DEFAULT_BLOCK_SIZE      ):






							"""simple docstring"""



							a_       :   Any			=	[]
							a_       :   Tuple			=	key

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





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: list[int]	,	SCREAMING_SNAKE_CASE_				: int	,	SCREAMING_SNAKE_CASE_				: tuple[int, int]	,	SCREAMING_SNAKE_CASE_				: int = DEFAULT_BLOCK_SIZE	,	):






							"""simple docstring"""



							a_       :   Optional[int]			=	[]
							a_       :   Union[str, Any]			=	key
							for block in encrypted_blocks:
														decrypted_blocks.append(pow(SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_      )      )
							return get_text_from_blocks(SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_      )





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: str      ):






							"""simple docstring"""



							with open(SCREAMING_SNAKE_CASE_      ) as fo:
														a_       :   List[Any]			=	fo.read()
							a_       :   Optional[Any]			=	content.split(""","""      )
							return (int(SCREAMING_SNAKE_CASE_      ), int(SCREAMING_SNAKE_CASE_      ), int(SCREAMING_SNAKE_CASE_      ))





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: str	,	SCREAMING_SNAKE_CASE_				: str	,	SCREAMING_SNAKE_CASE_				: str	,	SCREAMING_SNAKE_CASE_				: int = DEFAULT_BLOCK_SIZE	,	):






							"""simple docstring"""



							a_       :   Any			=	read_key_file(SCREAMING_SNAKE_CASE_      )
							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	,	SCREAMING_SNAKE_CASE_      )      )

							a_       :   Tuple			=	[str(SCREAMING_SNAKE_CASE_      ) for i in encrypt_message(SCREAMING_SNAKE_CASE_	,	(n, e)	,	SCREAMING_SNAKE_CASE_      )]

							a_       :   str			=	",".join(SCREAMING_SNAKE_CASE_      )
							a_       :   Tuple			=	F"""{len(SCREAMING_SNAKE_CASE_      )}_{block_size}_{encrypted_content}"""
							with open(SCREAMING_SNAKE_CASE_	,	"""w"""      ) as fo:
														fo.write(SCREAMING_SNAKE_CASE_      )
							return encrypted_content





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: str	,	SCREAMING_SNAKE_CASE_				: str      ):






							"""simple docstring"""



							a_       :   Optional[Any]			=	read_key_file(SCREAMING_SNAKE_CASE_      )
							with open(SCREAMING_SNAKE_CASE_      ) as fo:
														a_       :   str			=	fo.read()
							a_       :   Dict			=	content.split("""_"""      )
							a_       :   str			=	int(SCREAMING_SNAKE_CASE_      )
							a_       :   Optional[int]			=	int(SCREAMING_SNAKE_CASE_      )

							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	,	SCREAMING_SNAKE_CASE_      )      )

							a_       :   Union[str, Any]			=	[]
							for block in encrypted_message.split(""","""      ):
														encrypted_blocks.append(int(SCREAMING_SNAKE_CASE_      )      )

							return decrypt_message(SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	(n, d)	,	SCREAMING_SNAKE_CASE_      )





def 						_lowerCamelCase		(							):






							"""simple docstring"""



							a_       :   Optional[Any]			=	"encrypted_file.txt"
							a_       :   Any			=	input(r"""Encrypt\Decrypt [e\d]: """      )

							if response.lower().startswith("""e"""      ):
														a_       :   str			=	"encrypt"
							elif response.lower().startswith("""d"""      ):
														a_       :   Union[str, Any]			=	"decrypt"

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

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

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

							elif mode == "decrypt":
														a_       :   Dict			=	"rsa_privkey.txt"
														print(F"""Reading from {filename} and decrypting..."""      )
														a_       :   int			=	read_from_file_and_decrypt(SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_      )
														print("""writing decryption to rsa_decryption.txt..."""      )
														with open("""rsa_decryption.txt"""	,	"""w"""      ) as dec:
																					dec.write(SCREAMING_SNAKE_CASE_      )

														print("""\nDecryption:"""      )
														print(SCREAMING_SNAKE_CASE_      )


if __name__ == "__main__":
	main()



import itertools
from dataclasses import dataclass
from typing import Optional

import pyarrow as pa

import datasets
from datasets.table import table_cast


SCREAMING_SNAKE_CASE : int          =    datasets.utils.logging.get_logger(__name__)





@dataclass
class 					snake_case__  ( datasets.BuilderConfig       ):
	UpperCAmelCase   :							Optional[datasets.Features]         = None





class 					snake_case__  ( datasets.ArrowBasedBuilder       ):
	UpperCAmelCase   :							Optional[int]         = ArrowConfig





	def     A				(  self							)		->			str:
								"""simple docstring"""






								return datasets.DatasetInfo(features=self.config.features							)





	def     A				(  self				,    UpperCamelCase_							)		->			str:
								"""simple docstring"""






								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}"""							)
								a_       :   List[str]			=	dl_manager.download_and_extract(self.config.data_files							)
								if isinstance(UpperCamelCase_				,    (str, list, tuple)							):
															a_       :   Dict			=	data_files
															if isinstance(UpperCamelCase_				,    UpperCamelCase_							):
																						a_       :   Optional[Any]			=	[files]
															# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
															a_       :   List[Any]			=	[dl_manager.iter_files(UpperCamelCase_							) for file in files]
															return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,    gen_kwargs={"""files""": files}							)]
								a_       :   Any			=	[]
								for split_name, files in data_files.items():
															if isinstance(UpperCamelCase_				,    UpperCamelCase_							):
																						a_       :   Dict			=	[files]
															# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
															a_       :   List[Any]			=	[dl_manager.iter_files(UpperCamelCase_							) for file in files]
															# Infer features is they are stoed in the arrow schema
															if self.info.features is None:
																						for file in itertools.chain.from_iterable(UpperCamelCase_							):
																													with open(UpperCamelCase_				,    """rb"""							) as f:
																																				a_       :   List[str]			=	datasets.Features.from_arrow_schema(pa.ipc.open_stream(UpperCamelCase_							).schema							)
																													break
															splits.append(datasets.SplitGenerator(name=UpperCamelCase_				,    gen_kwargs={"""files""": files}							)							)
								return splits





	def     A				(  self				,    UpperCamelCase_							)		->			pa.Table:
								"""simple docstring"""






								if self.info.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
															a_       :   int			=	table_cast(UpperCamelCase_				,    self.info.features.arrow_schema							)
								return pa_table





	def     A				(  self				,    UpperCamelCase_							)		->			Optional[Any]:
								"""simple docstring"""






								for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCamelCase_							)							):
															with open(UpperCamelCase_				,    """rb"""							) as f:
																						try:
																													for batch_idx, record_batch in enumerate(pa.ipc.open_stream(UpperCamelCase_							)							):
																																				a_       :   Tuple			=	pa.Table.from_batches([record_batch]							)
																																				# Uncomment for debugging (will print the Arrow table size and elements)
																																				# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
																																				# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
																																				yield f"""{file_idx}_{batch_idx}""", self._cast_table(UpperCamelCase_							)
																						except ValueError as e:
																													logger.error(f"""Failed to read file '{file}' with error {type(UpperCamelCase_							)}: {e}"""							)
																													raise


