"""simple docstring"""





import collections
import inspect
import unittest

from transformers import SwinConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch
       from torch import nn

       from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
       from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor


class 					UpperCAmelCase_     :


      def __init__(       self		,      a		,      a=1_3		,      a=3_2		,      a=2		,      a=3		,      a=1_6		,      a=[1, 2, 1]		,      a=[2, 2, 4]		,      a=2		,      a=2.0		,      a=True		,      a=0.0		,      a=0.0		,      a=0.1		,      a="gelu"		,      a=False		,      a=True		,      a=0.02		,      a=1e-5		,      a=True		,      a=None		,      a=True		,      a=1_0		,      a=8		,      a=["stage1", "stage2"]		,      a=[1, 2]		,      )	-> Optional[Any]:
             lowercase__      :		int          =     parent
             lowercase__      :		int          =     batch_size
             lowercase__      :		Any          =     image_size
             lowercase__      :		Any          =     patch_size
             lowercase__      :		List[Any]          =     num_channels
             lowercase__      :		int          =     embed_dim
             lowercase__      :		Any          =     depths
             lowercase__      :		Union[str, Any]          =     num_heads
             lowercase__      :		List[Any]          =     window_size
             lowercase__      :		List[str]          =     mlp_ratio
             lowercase__      :		Any          =     qkv_bias
             lowercase__      :		Any          =     hidden_dropout_prob
             lowercase__      :		Tuple          =     attention_probs_dropout_prob
             lowercase__      :		List[str]          =     drop_path_rate
             lowercase__      :		Any          =     hidden_act
             lowercase__      :		Any          =     use_absolute_embeddings
             lowercase__      :		Union[str, Any]          =     patch_norm
             lowercase__      :		List[Any]          =     layer_norm_eps
             lowercase__      :		Optional[Any]          =     initializer_range
             lowercase__      :		Any          =     is_training
             lowercase__      :		Dict          =     scope
             lowercase__      :		Any          =     use_labels
             lowercase__      :		List[str]          =     type_sequence_label_size
             lowercase__      :		Union[str, Any]          =     encoder_stride
             lowercase__      :		Optional[int]          =     out_features
             lowercase__      :		Optional[Any]          =     out_indices


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Optional[Any]          =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

             lowercase__      :		int          =     None
             if self.use_labels:
                    lowercase__      :		str          =     ids_tensor([self.batch_size]		,      self.type_sequence_label_size							)

             lowercase__      :		Optional[Any]          =     self.get_config()

             return config, pixel_values, labels


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             return SwinConfig(
                 image_size=self.image_size		,      patch_size=self.patch_size		,      num_channels=self.num_channels		,      embed_dim=self.embed_dim		,      depths=self.depths		,      num_heads=self.num_heads		,      window_size=self.window_size		,      mlp_ratio=self.mlp_ratio		,      qkv_bias=self.qkv_bias		,      hidden_dropout_prob=self.hidden_dropout_prob		,      attention_probs_dropout_prob=self.attention_probs_dropout_prob		,      drop_path_rate=self.drop_path_rate		,      hidden_act=self.hidden_act		,      use_absolute_embeddings=self.use_absolute_embeddings		,      path_norm=self.patch_norm		,      layer_norm_eps=self.layer_norm_eps		,      initializer_range=self.initializer_range		,      encoder_stride=self.encoder_stride		,      out_features=self.out_features		,      out_indices=self.out_indices		,      )


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a							)	-> str:
             lowercase__      :		Dict          =     SwinModel(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		Any          =     model(a							)

             lowercase__      :		Optional[int]          =     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
             lowercase__      :		List[Any]          =     int(config.embed_dim * 2 ** (len(config.depths							) - 1)							)

             self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, expected_seq_len, expected_dim)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a							)	-> Union[str, Any]:
             lowercase__      :		Dict          =     SwinBackbone(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		int          =     model(a							)

             # verify hidden states
             self.parent.assertEqual(len(result.feature_maps							)		,      len(config.out_features							)							)
             self.parent.assertListEqual(list(result.feature_maps[0].shape							)		,      [self.batch_size, model.channels[0], 1_6, 1_6]							)

             # verify channels
             self.parent.assertEqual(len(model.channels							)		,      len(config.out_features							)							)

             # verify backbone works with out_features=None
             lowercase__      :		Any          =     None
             lowercase__      :		Tuple          =     SwinBackbone(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		List[str]          =     model(a							)

             # verify feature maps
             self.parent.assertEqual(len(result.feature_maps							)		,      1							)
             self.parent.assertListEqual(list(result.feature_maps[0].shape							)		,      [self.batch_size, model.channels[-1], 4, 4]							)

             # verify channels
             self.parent.assertEqual(len(model.channels							)		,      1							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a							)	-> str:
             lowercase__      :		Union[str, Any]          =     SwinForMaskedImageModeling(config=a							)
             model.to(a							)
             model.eval()
             lowercase__      :		int          =     model(a							)
             self.parent.assertEqual(
                 result.logits.shape		,      (self.batch_size, self.num_channels, self.image_size, self.image_size)							)

             # test greyscale images
             lowercase__      :		List[Any]          =     1
             lowercase__      :		List[Any]          =     SwinForMaskedImageModeling(a							)
             model.to(a							)
             model.eval()

             lowercase__      :		Dict          =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
             lowercase__      :		Union[str, Any]          =     model(a							)
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, 1, self.image_size, self.image_size)							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a							)	-> str:
             lowercase__      :		Optional[int]          =     self.type_sequence_label_size
             lowercase__      :		Optional[Any]          =     SwinForImageClassification(a							)
             model.to(a							)
             model.eval()
             lowercase__      :		Dict          =     model(a		,      labels=a							)
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.type_sequence_label_size)							)

             # test greyscale images
             lowercase__      :		Any          =     1
             lowercase__      :		Dict          =     SwinForImageClassification(a							)
             model.to(a							)
             model.eval()

             lowercase__      :		Any          =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
             lowercase__      :		List[Any]          =     model(a							)
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.type_sequence_label_size)							)


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Optional[int]          =     self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				(
                 lowercase__
             )   ,				
             )      :		Any          =     config_and_inputs
             lowercase__      :		List[str]          =     {'pixel_values': pixel_values}
             return config, inputs_dict





@require_torch
class 					UpperCAmelCase_     (   _a			,							_a			,							unittest.TestCase):
      lowerCamelCase__		:   List[str]			  =  (
          (
              SwinModel,
              SwinBackbone,
              SwinForImageClassification,
              SwinForMaskedImageModeling,
          )
          if is_torch_available()
          else ()
      )
      lowerCamelCase__		:   Union[str, Any]			  =  (
          {"feature-extraction": SwinModel, "image-classification": SwinForImageClassification}
          if is_torch_available()
          else {}
      )
      lowerCamelCase__		:   List[str]			  =  True

      lowerCamelCase__		:   List[str]			  =  False
      lowerCamelCase__		:   int			  =  False
      lowerCamelCase__		:   Any			  =  False


      def 		_UpperCAmelCase	(       self							)	-> str:
             lowercase__      :		Union[str, Any]          =     SwinModelTester(self							)
             lowercase__      :		Optional[int]          =     ConfigTester(self		,      config_class=a		,      embed_dim=3_7							)


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             self.create_and_test_config_common_properties()
             self.config_tester.create_and_test_config_to_json_string()
             self.config_tester.create_and_test_config_to_json_file()
             self.config_tester.create_and_test_config_from_and_save_pretrained()
             self.config_tester.create_and_test_config_with_num_labels()
             self.config_tester.check_config_can_be_init_without_params()
             self.config_tester.check_config_arguments_init()


      def 		_UpperCAmelCase	(       self							)	-> Optional[Any]:
             return


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             lowercase__      :		List[Any]          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*a							)


      @unittest.skip(reason='Got `CUDA error: misaligned address` with PyTorch 2.0.0.'							)
      def 		_UpperCAmelCase	(       self							)	-> Union[str, Any]:
             pass


      def 		_UpperCAmelCase	(       self							)	-> str:
             super().test_training_gradient_checkpointing()


      def 		_UpperCAmelCase	(       self							)	-> Union[str, Any]:
             lowercase__      :		Tuple          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_backbone(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Any:
             lowercase__      :		Any          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*a							)


      def 		_UpperCAmelCase	(       self							)	-> Optional[int]:
             lowercase__      :		Optional[Any]          =     self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*a							)


      @unittest.skip(reason='Swin does not use inputs_embeds'							)
      def 		_UpperCAmelCase	(       self							)	-> int:
             pass


      @unittest.skip(reason='Swin Transformer does not use feedforward chunking'							)
      def 		_UpperCAmelCase	(       self							)	-> int:
             pass


      def 		_UpperCAmelCase	(       self							)	-> Any:
             lowercase__   ,				lowercase__      :		Optional[int]          =     self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    lowercase__      :		int          =     model_class(a							)
                    self.assertIsInstance(model.get_input_embeddings()		,      (nn.Module)							)
                    lowercase__      :		Tuple          =     model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(a		,      nn.Linear							)							)


      def 		_UpperCAmelCase	(       self							)	-> int:
             lowercase__   ,				lowercase__      :		int          =     self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    lowercase__      :		Union[str, Any]          =     model_class(a							)
                    lowercase__      :		Optional[int]          =     inspect.signature(model.forward							)
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    lowercase__      :		List[str]          =     [*signature.parameters.keys()]

                    lowercase__      :		List[str]          =     ['pixel_values']
                    self.assertListEqual(arg_names[:1]		,      a							)


      def 		_UpperCAmelCase	(       self							)	-> str:
             lowercase__   ,				lowercase__      :		Union[str, Any]          =     self.model_tester.prepare_config_and_inputs_for_common()
             lowercase__      :		List[Any]          =     True

             for model_class in self.all_model_classes:
                    lowercase__      :		List[str]          =     True
                    lowercase__      :		Optional[Any]          =     False
                    lowercase__      :		Union[str, Any]          =     True
                    lowercase__      :		Union[str, Any]          =     model_class(a							)
                    model.to(a							)
                    model.eval()
                    with torch.no_grad():
                           lowercase__      :		Optional[Any]          =     model(**self._prepare_for_class(a		,      a							)							)
                    lowercase__      :		List[str]          =     outputs.attentions
                    lowercase__      :		Tuple          =     len(self.model_tester.depths							)
                    self.assertEqual(len(a							)		,      a							)

                    # check that output_attentions also work using config
                    del inputs_dict["output_attentions"]
                    lowercase__      :		Tuple          =     True
                    lowercase__      :		List[Any]          =     config.window_size**2
                    lowercase__      :		List[str]          =     model_class(a							)
                    model.to(a							)
                    model.eval()
                    with torch.no_grad():
                           lowercase__      :		int          =     model(**self._prepare_for_class(a		,      a							)							)
                    lowercase__      :		List[str]          =     outputs.attentions
                    self.assertEqual(len(a							)		,      a							)

                    self.assertListEqual(
                        list(attentions[0].shape[-3:]							)		,      [self.model_tester.num_heads[0], window_size_squared, window_size_squared]		,      )
                    lowercase__      :		Optional[int]          =     len(a							)

                    # Check attention is always last and order is fine
                    lowercase__      :		Optional[Any]          =     True
                    lowercase__      :		List[Any]          =     True
                    lowercase__      :		Union[str, Any]          =     model_class(a							)
                    model.to(a							)
                    model.eval()
                    with torch.no_grad():
                           lowercase__      :		Tuple          =     model(**self._prepare_for_class(a		,      a							)							)

                    # also another +1 for reshaped_hidden_states
                    lowercase__      :		Any          =     1 if model_class.__name__ == 'SwinBackbone' else 2
                    self.assertEqual(out_len + added_hidden_states		,      len(a							)							)

                    lowercase__      :		List[Any]          =     outputs.attentions

                    self.assertEqual(len(a							)		,      a							)

                    self.assertListEqual(
                        list(self_attentions[0].shape[-3:]							)		,      [self.model_tester.num_heads[0], window_size_squared, window_size_squared]		,      )


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a							)	-> Dict:
             lowercase__      :		Union[str, Any]          =     model_class(a							)
             model.to(a							)
             model.eval()

             with torch.no_grad():
                    lowercase__      :		Tuple          =     model(**self._prepare_for_class(a		,      a							)							)

             lowercase__      :		Optional[int]          =     outputs.hidden_states

             lowercase__      :		Any          =     getattr(
                 self.model_tester		,      'expected_num_hidden_layers'		,      len(self.model_tester.depths							) + 1							)
             self.assertEqual(len(a							)		,      a							)

             # Swin has a different seq_length
             lowercase__      :		Union[str, Any]          =     (
                 config.patch_size
                 if isinstance(config.patch_size		,      collections.abc.Iterable							)
                 else (config.patch_size, config.patch_size)
             )

             lowercase__      :		List[Any]          =     (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]							)		,      [num_patches, self.model_tester.embed_dim]		,      )

             if not model_class.__name__ == "SwinBackbone":
                    lowercase__      :		Any          =     outputs.reshaped_hidden_states
                    self.assertEqual(len(a							)		,      a							)

                    lowercase__   ,				lowercase__   ,				lowercase__   ,				lowercase__      :		Union[str, Any]          =     reshaped_hidden_states[0].shape
                    lowercase__      :		Optional[int]          =     (
                        reshaped_hidden_states[0].view(a		,      a		,      height * width							).permute(0		,      2		,      1							)
                    )
                    self.assertListEqual(
                        list(reshaped_hidden_states.shape[-2:]							)		,      [num_patches, self.model_tester.embed_dim]		,      )


      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
             lowercase__   ,				lowercase__      :		str          =     self.model_tester.prepare_config_and_inputs_for_common()

             lowercase__      :		Union[str, Any]          =     (
                 self.model_tester.image_size
                 if isinstance(self.model_tester.image_size		,      collections.abc.Iterable							)
                 else (self.model_tester.image_size, self.model_tester.image_size)
             )

             for model_class in self.all_model_classes:
                    lowercase__      :		List[Any]          =     True
                    self.check_hidden_states_output(a		,      a		,      a		,      a							)

                    # check that output_hidden_states also work using config
                    del inputs_dict["output_hidden_states"]
                    lowercase__      :		List[str]          =     True

                    self.check_hidden_states_output(a		,      a		,      a		,      a							)


      def 		_UpperCAmelCase	(       self							)	-> Any:
             lowercase__   ,				lowercase__      :		Union[str, Any]          =     self.model_tester.prepare_config_and_inputs_for_common()
             lowercase__      :		Any          =     3

             lowercase__      :		List[str]          =     (
                 self.model_tester.image_size
                 if isinstance(self.model_tester.image_size		,      collections.abc.Iterable							)
                 else (self.model_tester.image_size, self.model_tester.image_size)
             )
             lowercase__      :		int          =     (
                 config.patch_size
                 if isinstance(config.patch_size		,      collections.abc.Iterable							)
                 else (config.patch_size, config.patch_size)
             )

             lowercase__      :		int          =     image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
             lowercase__      :		List[Any]          =     image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

             for model_class in self.all_model_classes:
                    lowercase__      :		int          =     True
                    self.check_hidden_states_output(a		,      a		,      a		,      (padded_height, padded_width)							)

                    # check that output_hidden_states also work using config
                    del inputs_dict["output_hidden_states"]
                    lowercase__      :		Union[str, Any]          =     True
                    self.check_hidden_states_output(a		,      a		,      a		,      (padded_height, padded_width)							)


      @slow
      def 		_UpperCAmelCase	(       self							)	-> int:
             for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__      :		Dict          =     SwinModel.from_pretrained(a							)
                    self.assertIsNotNone(a							)


      def 		_UpperCAmelCase	(       self							)	-> Optional[Any]:
             lowercase__   ,				lowercase__      :		Dict          =     self.model_tester.prepare_config_and_inputs_for_common()

             lowercase__      :		List[str]          =     _config_zero_init(a							)
             for model_class in self.all_model_classes:
                    lowercase__      :		Union[str, Any]          =     model_class(config=a							)
                    for name, param in model.named_parameters():
                           if "embeddings" not in name and param.requires_grad:
                                  self.assertIn(
                                      ((param.data.mean() * 1e9).round() / 1e9).item()		,      [0.0, 1.0]		,      msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""		,      )





@require_vision
@require_torch
class 					UpperCAmelCase_     (   unittest.TestCase):


      @cached_property
      def 		_UpperCAmelCase	(       self							)	-> str:
             return (
                 AutoImageProcessor.from_pretrained('microsoft/swin-tiny-patch4-window7-224'							)
                 if is_vision_available()
                 else None
             )


      @slow
      def 		_UpperCAmelCase	(       self							)	-> Tuple:
             lowercase__      :		Any          =     SwinForImageClassification.from_pretrained('microsoft/swin-tiny-patch4-window7-224'							).to(a							)
             lowercase__      :		List[str]          =     self.default_image_processor

             lowercase__      :		Dict          =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
             lowercase__      :		str          =     image_processor(images=a		,      return_tensors='pt'							).to(a							)

             # forward pass
             with torch.no_grad():
                    lowercase__      :		Any          =     model(**a							)

             # verify the logits
             lowercase__      :		Optional[int]          =     torch.Size((1, 1_0_0_0)							)
             self.assertEqual(outputs.logits.shape		,      a							)
             lowercase__      :		Union[str, Any]          =     torch.tensor([-0.0_948, -0.6_454, -0.0_921]							).to(a							)
             self.assertTrue(torch.allclose(outputs.logits[0, :3]		,      a		,      atol=1e-4							)							)





@require_torch
class 					UpperCAmelCase_     (   unittest.TestCase			,							_a):
      lowerCamelCase__		:   List[Any]			  =  (SwinBackbone,) if is_torch_available() else ()
      lowerCamelCase__		:   Dict			  =  SwinConfig


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             lowercase__      :		Any          =     SwinModelTester(self							)


"""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


_UpperCamelCase       :						Optional[int]     =   "https://github.com/huggingface/datasets/tree/main/src/datasets/utils"
_UpperCamelCase       :						Union[str, Any]     =   f'''{BASE_REF_URL}/{__file__}'''
_UpperCamelCase       :						Any     =   logging.getLogger(__name__)





def     a_					(				_lowerCAmelCase  :			str					):





       '''simple docstring'''

       lowercase__      :		Optional[Any]          =     pkg_resources.read_text(_lowerCAmelCase       ,			_lowerCAmelCase					)
       return yaml.safe_load(_lowerCAmelCase					), f"""{BASE_REF_URL}/resources/{resource}"""


_UpperCamelCase   ,				_UpperCamelCase       :						int     =   load_yaml_resource("readme_structure.yaml")

_UpperCamelCase       :						Optional[Any]     =   [
    "[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
_UpperCamelCase       :						List[Any]     =   Tuple[dict, List[str], List[str]]


class 					UpperCAmelCase_     :


      def __init__(       self		,      a		,      a		,      a = None		,      a = False							)	-> str:
             lowercase__      :		List[str]          =     name
             lowercase__      :		int          =     level
             lowercase__      :		Dict          =     lines
             lowercase__      :		Optional[int]          =     ''
             lowercase__      :		List[str]          =     True
             lowercase__      :		Union[str, Any]          =     {}
             lowercase__      :		Any          =     []
             lowercase__      :		Optional[int]          =     []
             if self.lines is not None:
                    self.parse(suppress_parsing_errors=a							)


      def 		_UpperCAmelCase	(       self		,      a = False							)	-> Any:
             lowercase__      :		List[str]          =     ''
             lowercase__      :		int          =     []
             lowercase__      :		Any          =     False
             for line in self.lines:
                    if line.strip(' \n'							) == "":
                           continue
                    elif line.strip(' \n'							)[:3] == "```":
                           lowercase__      :		Optional[Any]          =     not code_start
                    elif line.split()[0] == self.level + "#" and not code_start:
                           if current_sub_level != "":
                                  lowercase__      :		Tuple          =     Section(a		,      self.level + '#'		,      a							)
                                  lowercase__      :		str          =     []
                           else:
                                  if current_lines != []:
                                         self.text += "".join(a							).strip()
                                         if self.text != "" and self.text not in FILLER_TEXT:
                                                lowercase__      :		List[Any]          =     False
                                         lowercase__      :		List[str]          =     []

                           lowercase__      :		Dict          =     ' '.join(line.split()[1:]							).strip(' \n'							)
                    else:
                           current_lines.append(a							)
             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."""							)
                           lowercase__      :		Tuple          =     Section(a		,      self.level + '#'		,      a							)
                    else:
                           if current_lines != []:
                                  self.text += "".join(a							).strip()
                                  if self.text != "" and self.text not in FILLER_TEXT:
                                         lowercase__      :		Dict          =     False

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


      def 		_UpperCAmelCase	(       self		,      a							)	-> ReadmeValidatorOutput:
             lowercase__      :		Tuple          =     []
             lowercase__      :		int          =     []
             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
                           lowercase__      :		str          =     [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
                           lowercase__      :		Union[str, Any]          =     [subsection['name'] for subsection in structure['subsections']]
                           lowercase__      :		Any          =     False
                           for idx, name in enumerate(a							):
                                  if name not in self.content:
                                         # If the expected subsection is not present
                                         error_list.append(f"""Section `{self.name}` is missing subsection: `{name}`."""							)
                                         lowercase__      :		str          =     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:
                                                lowercase__   ,				lowercase__   ,				lowercase__      :		Optional[Any]          =     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 		_UpperCAmelCase	(       self							)	-> dict:
             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 					UpperCAmelCase_     (   _a):  # Level 0


      def __init__(       self		,      a		,      a		,      a = None		,      a = False							)	-> Any:
             super().__init__(name=a		,      level=''							)  # Not using lines here as we need to use a child class parse
             lowercase__      :		List[Any]          =     structure
             lowercase__      :		Dict          =     -2
             lowercase__      :		Union[str, Any]          =     0
             lowercase__      :		Dict          =     lines
             if self.lines is not None:
                    self.parse(suppress_parsing_errors=a							)


      def 		_UpperCAmelCase	(       self							)	-> Tuple:
             if self.structure is None:
                    lowercase__   ,				lowercase__   ,				lowercase__      :		Any          =     self._validate(a							)
             else:
                    lowercase__   ,				lowercase__   ,				lowercase__      :		Dict          =     self._validate(self.structure							)
             if error_list != [] or warning_list != []:
                    lowercase__      :		Optional[Any]          =     '\n'.join(['-\t' + x for x in error_list + warning_list]							)
                    lowercase__      :		Union[str, Any]          =     f"""The following issues were found for the README at `{self.name}`:\n""" + errors
                    raise ValueError(a							)


      @classmethod
      def 		_UpperCAmelCase	(       cls		,      a		,      a = None		,      a = False							)	-> Tuple:
             with open(a		,      encoding='utf-8'							) as f:
                    lowercase__      :		List[Any]          =     f.readlines()
             return cls(a		,      a		,      a		,      suppress_parsing_errors=a							)


      @classmethod
      def 		_UpperCAmelCase	(       cls		,      a		,      a = None		,      a = "root"		,      a = False							)	-> int:
             lowercase__      :		Optional[Any]          =     string.split('\n'							)
             return cls(a		,      a		,      a		,      suppress_parsing_errors=a							)


      def 		_UpperCAmelCase	(       self		,      a = False							)	-> List[Any]:
             # Skip Tags
             lowercase__      :		Tuple          =     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:
                    lowercase__      :		Optional[int]          =     self.lines[line_count + 1 :]  # Get the last + 1 th item.
             else:
                    lowercase__      :		List[str]          =     self.lines[self.tag_count :]
             super().parse(suppress_parsing_errors=a							)


      def __str__(       self							)	-> Any:
             return str(self.to_dict()							)


      def 		_UpperCAmelCase	(       self		,      a							)	-> str:
             lowercase__      :		Any          =     []
             lowercase__      :		Optional[int]          =     []
             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.
             lowercase__      :		int          =     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
                    lowercase__      :		Dict          =     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
                           lowercase__   ,				lowercase__   ,				lowercase__      :		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

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

