



'''simple docstring'''







import logging
from pathlib import Path
from typing import Any, List, Tuple

import yaml


# loading package files: https://stackoverflow.com/a/20885799
try:
							import importlib.resources as pkg_resources
except ImportError:
							# Try backported to PY<37 `importlib_resources`.
							import importlib_resources as pkg_resources

from . import resources


SCREAMING_SNAKE_CASE__     : Dict				=   """https://github.com/huggingface/datasets/tree/main/src/datasets/utils"""
SCREAMING_SNAKE_CASE__     : List[Any]				=   f"""{BASE_REF_URL}/{__file__}"""
SCREAMING_SNAKE_CASE__     : Optional[Any]				=   logging.getLogger(__name__)







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE     ):
		SCREAMING_SNAKE_CASE_						:Any										=      pkg_resources.read_text(lowerCamelCase__ ,		lowerCamelCase__     )
		return yaml.safe_load(lowerCamelCase__     ), F'{BASE_REF_URL}/resources/{resource}'


SCREAMING_SNAKE_CASE__	,							SCREAMING_SNAKE_CASE__     : Optional[int]				=   load_yaml_resource("""readme_structure.yaml""")

SCREAMING_SNAKE_CASE__     : Optional[int]				=   [
    """[Needs More Information]""",
    """[More Information Needed]""",
    """(https://github.com/huggingface/datasets/blob/main/CONTRIBUTING.md#how-to-contribute-to-the-dataset-cards)""",
]

# Dictionary representation of section/readme, error_list, warning_list
SCREAMING_SNAKE_CASE__     : str				=   Tuple[dict, List[str], List[str]]




class 				__lowerCAmelCase:






			def __init__(   self  :							List[Any]      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							List[str] = None      ,       SCREAMING_SNAKE_CASE  :							bool = False	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      name
					SCREAMING_SNAKE_CASE_						:Any										=      level
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      lines
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      ''''''
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      True
					SCREAMING_SNAKE_CASE_						:int										=      {}
					SCREAMING_SNAKE_CASE_						:str										=      []
					SCREAMING_SNAKE_CASE_						:str										=      []
					if self.lines is not None:
							self.parse(suppress_parsing_errors=_lowerCamelCase	)






			def 				_lowercase	(   self  :							Optional[int]      ,       SCREAMING_SNAKE_CASE  :							bool = False	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      ''''''
					SCREAMING_SNAKE_CASE_						:List[str]										=      []
					SCREAMING_SNAKE_CASE_						:Any										=      False
					for line in self.lines:
							if line.strip(' \n'	) == "":
									continue
							elif line.strip(' \n'	)[:3] == "```":
									SCREAMING_SNAKE_CASE_						:Any										=      not code_start
							elif line.split()[0] == self.level + "#" and not code_start:
									if current_sub_level != "":
											SCREAMING_SNAKE_CASE_						:Dict										=      Section(_lowerCamelCase      ,       self.level + '#'      ,       _lowerCamelCase	)
											SCREAMING_SNAKE_CASE_						:List[str]										=      []
									else:
											if current_lines != []:
													self.text += "".join(_lowerCamelCase	).strip()
													if self.text != "" and self.text not in FILLER_TEXT:
															SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      False
													SCREAMING_SNAKE_CASE_						:str										=      []

									SCREAMING_SNAKE_CASE_						:int										=      ''' '''.join(line.split()[1:]	).strip(' \n'	)
							else:
									current_lines.append(_lowerCamelCase	)
					else:
							if current_sub_level != "":
									if current_sub_level in self.content:
											self.parsing_error_list.append(
											    f'Multiple sections with the same heading `{current_sub_level}` have been found. Please keep only one of these sections.'	)
									SCREAMING_SNAKE_CASE_						:List[str]										=      Section(_lowerCamelCase      ,       self.level + '#'      ,       _lowerCamelCase	)
							else:
									if current_lines != []:
											self.text += "".join(_lowerCamelCase	).strip()
											if self.text != "" and self.text not in FILLER_TEXT:
													SCREAMING_SNAKE_CASE_						:str										=      False

					if self.level == "" and not suppress_parsing_errors:
							if self.parsing_error_list != [] or self.parsing_warning_list != []:
									SCREAMING_SNAKE_CASE_						:List[str]										=      '''\n'''.join('-\t' + x for x in self.parsing_error_list + self.parsing_warning_list	)
									SCREAMING_SNAKE_CASE_						:List[Any]										=      f'The following issues were found while parsing the README at `{self.name}`:\n' + errors
									raise ValueError(_lowerCamelCase	)






			def 				_lowercase	(   self  :							List[Any]      ,       SCREAMING_SNAKE_CASE  :							dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      []
					SCREAMING_SNAKE_CASE_						:List[Any]										=      []
					if structure["allow_empty"] is False:
							# If content is expected
							if self.is_empty_text and self.content == {}:
									# If no content is found, mention it in the error_list
									error_list.append(f'Expected some content in section `{self.name}` but it is empty.'	)

					if structure["allow_empty_text"] is False:
							# If some text is expected
							if self.is_empty_text:
									# If no text is found, mention it in the error_list
									error_list.append(
									    f'Expected some text in section `{self.name}` but it is empty (text in subsections are ignored).'	)
        # Subsections Validation
					if structure["subsections"] is not None:
							# If subsections are expected
							if self.content == {}:
									# If no subsections are present
									SCREAMING_SNAKE_CASE_						:int										=      [subsection['''name'''] for subsection in structure['''subsections''']]
									# Mention the expected values in the error_list
									error_list.append(
									    f'Section `{self.name}` expected the following subsections: {", ".join(["`"+x+"`" for x in values]	)}. Found \'None\'.'	)
							else:
									# If some subsections are present
									SCREAMING_SNAKE_CASE_						:Tuple										=      [subsection['''name'''] for subsection in structure['''subsections''']]
									SCREAMING_SNAKE_CASE_						:Dict										=      False
									for idx, name in enumerate(_lowerCamelCase	):
											if name not in self.content:
													# If the expected subsection is not present
													error_list.append(f'Section `{self.name}` is missing subsection: `{name}`.'	)
													SCREAMING_SNAKE_CASE_						:int										=      True
											else:
													# If the subsection is present, validate subsection, return the result
													# and concat the errors from subsection to section error_list

													# Skip sublevel validation if current level is `###`
													if self.level == "###":
															continue
													else:
															SCREAMING_SNAKE_CASE_						:Optional[int]										=      self.content[name].validate(
															    structure['subsections'][idx]	)
													error_list += subsec_error_list
													warning_list += subsec_warning_list

									if has_missing_subsections:  # we only allow to have extra subsections if all the other ones are here
											for name in self.content:
													if name not in structure_names:
															# If an extra subsection is present
															warning_list.append(
															    f'`{self.name}` has an extra subsection: `{name}`. Skipping further validation checks for this subsection as expected structure is unknown.'	)
					if error_list:
							# If there are errors, do not return the dictionary as it is invalid
							return {}, error_list, warning_list
					else:
							return self.to_dict(), error_list, warning_list






			def 				_lowercase	(   self  :							List[Any]	):




					"""simple docstring"""







					return {
					    "name": self.name,
					    "text": self.text,
					    "is_empty_text": self.is_empty_text,
					    "subsections": [value.to_dict() for value in self.content.values()],
					}




class 				__lowerCAmelCase(							a__      ):  # Level 0






			def __init__(   self  :							str      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							List[str]      ,       SCREAMING_SNAKE_CASE  :							dict = None      ,       SCREAMING_SNAKE_CASE  :							bool = False	):




					"""simple docstring"""







					super().__init__(name=_lowerCamelCase      ,       level=''	)  # Not using lines here as we need to use a child class parse
					SCREAMING_SNAKE_CASE_						:Dict										=      structure
					SCREAMING_SNAKE_CASE_						:str										=      -2
					SCREAMING_SNAKE_CASE_						:int										=      0
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      lines
					if self.lines is not None:
							self.parse(suppress_parsing_errors=_lowerCamelCase	)






			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					if self.structure is None:
							SCREAMING_SNAKE_CASE_						:Tuple										=      self._validate(_lowerCamelCase	)
					else:
							SCREAMING_SNAKE_CASE_						:List[Any]										=      self._validate(self.structure	)
					if error_list != [] or warning_list != []:
							SCREAMING_SNAKE_CASE_						:str										=      '''\n'''.join(['-\t' + x for x in error_list + warning_list]	)
							SCREAMING_SNAKE_CASE_						:int										=      f'The following issues were found for the README at `{self.name}`:\n' + errors
							raise ValueError(_lowerCamelCase	)






			@classmethod
			def 				_lowercase	(   cls  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							Path      ,       SCREAMING_SNAKE_CASE  :							dict = None      ,       SCREAMING_SNAKE_CASE  :							bool = False	):




					"""simple docstring"""







					with open(_lowerCamelCase      ,       encoding='utf-8'	) as f:
							SCREAMING_SNAKE_CASE_						:List[str]										=      f.readlines()
					return cls(_lowerCamelCase      ,       _lowerCamelCase      ,       _lowerCamelCase      ,       suppress_parsing_errors=_lowerCamelCase	)






			@classmethod
			def 				_lowercase	(   cls  :							Optional[Any]      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							dict = None      ,       SCREAMING_SNAKE_CASE  :							str = "root"      ,       SCREAMING_SNAKE_CASE  :							bool = False	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[int]										=      string.split('\n'	)
					return cls(_lowerCamelCase      ,       _lowerCamelCase      ,       _lowerCamelCase      ,       suppress_parsing_errors=_lowerCamelCase	)






			def 				_lowercase	(   self  :							str      ,       SCREAMING_SNAKE_CASE  :							bool = False	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[Any]										=      0

					for line in self.lines:
							self.yaml_tags_line_count += 1
							if line.strip(' \n'	) == "---":
									self.tag_count += 1
									if self.tag_count == 2:
											break
							line_count += 1
					if self.tag_count == 2:
							SCREAMING_SNAKE_CASE_						:Tuple										=      self.lines[line_count + 1 :]  # Get the last + 1 th item.
					else:
							SCREAMING_SNAKE_CASE_						:Any										=      self.lines[self.tag_count :]
					super().parse(suppress_parsing_errors=_lowerCamelCase	)






			def __str__(   self  :							Union[str, Any]	):




					"""simple docstring"""







					return str(self.to_dict()	)






			def 				_lowercase	(   self  :							List[str]      ,       SCREAMING_SNAKE_CASE  :							str	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      []
					SCREAMING_SNAKE_CASE_						:Tuple										=      []
					if self.yaml_tags_line_count == 0:
							warning_list.append('Empty YAML markers are present in the README.'	)
					elif self.tag_count == 0:
							warning_list.append('No YAML markers are present in the README.'	)
					elif self.tag_count == 1:
							warning_list.append('Only the start of YAML tags present in the README.'	)
					# Check how many first level sections are present.
					SCREAMING_SNAKE_CASE_						:Tuple										=      len(self.content.keys()	)
					if num_first_level_keys > 1:
							# If more than one, add to the error list, continue
							error_list.append(
							    f'The README has several first-level headings: {", ".join(["`"+x+"`" for x in list(self.content.keys()	)]	)}. Only one heading is expected. Skipping further validation for this README.'	)
					elif num_first_level_keys < 1:
							# If less than one, append error.
							error_list.append(
							    'The README has no first-level headings. One heading is expected. Skipping further validation for this README.'	)

					else:
							# If one exactly
							SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      list(self.content.keys()	)[0]  # Get the key
							if start_key.startswith('Dataset Card for'	):  # Check correct start
									# If the starting is correct, validate all the sections
									SCREAMING_SNAKE_CASE_						:Any										=      self.content[start_key].validate(
									    readme_structure['subsections'][0]	)
									error_list += sec_error_list
									warning_list += sec_warning_list
							else:
									# If not found, append error
									error_list.append(
									    'No first-level heading starting with `Dataset Card for` found in README. Skipping further validation for this README.'	)
					if error_list:
							# If there are errors, do not return the dictionary as it is invalid
							return {}, error_list, warning_list
					else:
							return self.to_dict(), error_list, warning_list


if __name__ == "__main__":
							from argparse import ArgumentParser

							SCREAMING_SNAKE_CASE__     : Tuple				=   ArgumentParser(usage="""Validate the content (excluding YAML tags) of a README.md file.""")
							ap.add_argument("""readme_filepath""")
							SCREAMING_SNAKE_CASE__     : int				=   ap.parse_args()
							SCREAMING_SNAKE_CASE__     : List[str]				=   Path(args.readme_filepath)
							SCREAMING_SNAKE_CASE__     : Optional[Any]				=   ReadMe.from_readme(readme_filepath)






'''simple docstring'''




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import SwinaSRImageProcessor
							from transformers.image_transforms import get_image_size






class      UpperCamelCase_  (unittest.TestCase  ):







						"""simple docstring"""


						def __init__(  self   :							Tuple				,			_lowerCamelCase   :							str				,			_lowerCamelCase   :							str=7				,			_lowerCamelCase   :							Tuple=3				,			_lowerCamelCase   :							Any=18				,			_lowerCamelCase   :							Optional[int]=30				,			_lowerCamelCase   :							List[Any]=400				,			_lowerCamelCase   :							int=True				,			_lowerCamelCase   :							Dict=1 / 255				,			_lowerCamelCase   :							List[str]=True				,			_lowerCamelCase   :							Optional[int]=8				,			):





											"""simple docstring"""







											A_			:  Tuple			=				parent
											A_			:  str			=				batch_size
											A_			:  Optional[Any]			=				num_channels
											A_			:  Union[str, Any]			=				image_size
											A_			:  Optional[int]			=				min_resolution
											A_			:  List[Any]			=				max_resolution
											A_			:  int			=				do_rescale
											A_			:  Optional[Any]			=				rescale_factor
											A_			:  Any			=				do_pad
											A_			:  Tuple			=				pad_size







						def    _a     (  self   :							List[Any]     ):





											"""simple docstring"""







											return {
											    "do_rescale": self.do_rescale,
											    "rescale_factor": self.rescale_factor,
											    "do_pad": self.do_pad,
											    "pad_size": self.pad_size,
											}







						def    _a     (  self   :							Tuple				,			_lowerCamelCase   :							List[str]=False				,			_lowerCamelCase   :							Tuple=False				,			_lowerCamelCase   :							str=False     ):





											"""simple docstring"""







											assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

											if equal_resolution:
																A_			:  List[Any]			=				[]
																for i in range(self.batch_size     ):
																					image_inputs.append(
																					    np.random.randint(
																					        255				,			size=(self.num_channels, self.max_resolution, self.max_resolution)				,			dtype=np.uinta     )     )
											else:
																A_			:  List[Any]			=				[]
																for i in range(self.batch_size     ):
																					A_			,A_			:  List[str]			=				np.random.choice(np.arange(self.min_resolution				,			self.max_resolution     )				,			2     )
																					image_inputs.append(np.random.randint(255				,			size=(self.num_channels, width, height)				,			dtype=np.uinta     )     )

											if not numpify and not torchify:
																# PIL expects the channel dimension as last dimension
																A_			:  Any			=				[Image.fromarray(np.moveaxis(_lowerCamelCase				,			0				,			-1     )     ) for x in image_inputs]

											if torchify:
																A_			:  str			=				[torch.from_numpy(_lowerCamelCase     ) for x in image_inputs]

											return image_inputs






@require_torch
@require_vision
class      UpperCamelCase_  (a__,   unittest.TestCase  ):







						"""simple docstring"""


						_lowerCAmelCase         =       SwinaSRImageProcessor if is_vision_available() else None







						def    _a     (  self   :							List[str]     ):





											"""simple docstring"""







											A_			:  int			=				SwinaSRImageProcessingTester(self     )







						@property
						def    _a     (  self   :							Union[str, Any]     ):





											"""simple docstring"""







											return self.image_processor_tester.prepare_image_processor_dict()







						def    _a     (  self   :							Any     ):





											"""simple docstring"""







											A_			:  Optional[int]			=				self.image_processing_class(**self.image_processor_dict     )
											self.assertTrue(hasattr(_lowerCamelCase				,			'''do_rescale'''     )     )
											self.assertTrue(hasattr(_lowerCamelCase				,			'''rescale_factor'''     )     )
											self.assertTrue(hasattr(_lowerCamelCase				,			'''do_pad'''     )     )
											self.assertTrue(hasattr(_lowerCamelCase				,			'''pad_size'''     )     )







						def    _a     (  self   :							Tuple     ):





											"""simple docstring"""







											pass







						def    _a     (  self   :							Optional[int]				,			_lowerCamelCase   :							Optional[int]     ):





											"""simple docstring"""







											A_			,A_			:  Any			=				get_image_size(_lowerCamelCase     )
											A_			:  List[Any]			=				self.image_processor_tester.pad_size

											A_			:  Dict			=				(old_height // size + 1) * size - old_height
											A_			:  Optional[Any]			=				(old_width // size + 1) * size - old_width
											return old_height + pad_height, old_width + pad_width







						def    _a     (  self   :							int     ):





											"""simple docstring"""







											A_			:  Dict			=				self.image_processing_class(**self.image_processor_dict     )
											# create random PIL images
											A_			:  Optional[int]			=				self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCamelCase     )
											for image in image_inputs:
																self.assertIsInstance(_lowerCamelCase				,			Image.Image     )

											# Test not batched input
											A_			:  Any			=				image_processor(image_inputs[0]				,			return_tensors='''pt'''     ).pixel_values
											A_			,A_			:  List[Any]			=				self.calculate_expected_size(np.array(image_inputs[0]     )     )
											self.assertEqual(
											    encoded_images.shape				,			(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )				,			)







						def    _a     (  self   :							Optional[Any]     ):





											"""simple docstring"""







											A_			:  Optional[Any]			=				self.image_processing_class(**self.image_processor_dict     )
											# create random numpy tensors
											A_			:  Dict			=				self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCamelCase				,			numpify=_lowerCamelCase     )
											for image in image_inputs:
																self.assertIsInstance(_lowerCamelCase				,			np.ndarray     )

											# Test not batched input
											A_			:  Union[str, Any]			=				image_processor(image_inputs[0]				,			return_tensors='''pt'''     ).pixel_values
											A_			,A_			:  Union[str, Any]			=				self.calculate_expected_size(image_inputs[0]     )
											self.assertEqual(
											    encoded_images.shape				,			(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )				,			)







						def    _a     (  self   :							int     ):





											"""simple docstring"""







											A_			:  Union[str, Any]			=				self.image_processing_class(**self.image_processor_dict     )
											# create random PyTorch tensors
											A_			:  List[Any]			=				self.image_processor_tester.prepare_inputs(equal_resolution=_lowerCamelCase				,			torchify=_lowerCamelCase     )
											for image in image_inputs:
																self.assertIsInstance(_lowerCamelCase				,			torch.Tensor     )

											# Test not batched input
											A_			:  int			=				image_processor(image_inputs[0]				,			return_tensors='''pt'''     ).pixel_values
											A_			,A_			:  str			=				self.calculate_expected_size(image_inputs[0]     )
											self.assertEqual(
											    encoded_images.shape				,			(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )				,			)



