





import html

from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from ...utils import is_bsa_available, logging, requires_backends


if is_bsa_available():
       import bsa
       from bsa import BeautifulSoup


a__: Any							   =						logging.get_logger(__name__)




class        SCREAMING_SNAKE_CASE__					(		UpperCamelCase__ ):



 def __init__(				self,**__lowerCamelCase					):
        requires_backends(self,['''bs4''']					)
        super().__init__(**__lowerCamelCase					)



 def 							UpperCamelCase     (				self,__lowerCamelCase					):
        A__		   =		[]
        A__		   =		[]
        A__		   =		element if element.name else element.parent
        for parent in child.parents:  # type: bs4.element.Tag
               A__		   =		parent.find_all(child.name,recursive=__lowerCamelCase					)
               xpath_tags.append(child.name					)
               xpath_subscripts.append(
                   0 if 1 == len(__lowerCamelCase					) else next(i for i, s in enumerate(__lowerCamelCase,1					) if s is child					)					)
               A__		   =		parent
        xpath_tags.reverse()
        xpath_subscripts.reverse()
        return xpath_tags, xpath_subscripts



 def 							UpperCamelCase     (				self,__lowerCamelCase					):
        A__		   =		BeautifulSoup(__lowerCamelCase,'''html.parser'''					)

        A__		   =		[]
        A__		   =		[]
        A__		   =		[]

        for element in html_code.descendants:
               if type(__lowerCamelCase					) == bsa.element.NavigableString:
                      if type(element.parent					) != bsa.element.Tag:
                             continue

                      A__		   =		html.unescape(__lowerCamelCase					).strip()
                      if not text_in_this_tag:
                             continue

                      all_doc_strings.append(__lowerCamelCase					)

                      A__   ,      A__		   =		self.xpath_soup(__lowerCamelCase					)
                      stringaxtag_seq.append(__lowerCamelCase					)
                      stringaxsubs_seq.append(__lowerCamelCase					)

        if len(__lowerCamelCase					) != len(__lowerCamelCase					):
               raise ValueError('''Number of doc strings and xtags does not correspond'''					)
        if len(__lowerCamelCase					) != len(__lowerCamelCase					):
               raise ValueError('''Number of doc strings and xsubs does not correspond'''					)

        return all_doc_strings, stringaxtag_seq, stringaxsubs_seq



 def 							UpperCamelCase     (				self,__lowerCamelCase,__lowerCamelCase					):
        A__		   =		''''''
        for tagname, subs in zip(__lowerCamelCase,__lowerCamelCase					):
               xpath += f"/{tagname}"
               if subs != 0:
                      xpath += f"[{subs}]"
        return xpath




 def __call__(				self,__lowerCamelCase					):
        A__		   =		False

        # Check that strings has a valid type
        if isinstance(__lowerCamelCase,__lowerCamelCase					):
               A__		   =		True
        elif isinstance(__lowerCamelCase,(list, tuple)					):
               if len(__lowerCamelCase					) == 0 or isinstance(html_strings[0],__lowerCamelCase					):
                      A__		   =		True

        if not valid_strings:
               raise ValueError(
                   '''HTML strings must of type `str`, `List[str]` (batch of examples), '''
                   f"but is of type {type(__lowerCamelCase					)}."					)

        A__		   =		bool(isinstance(__lowerCamelCase,(list, tuple)					) and (isinstance(html_strings[0],__lowerCamelCase					))					)

        if not is_batched:
               A__		   =		[html_strings]

        # Get nodes + xpaths
        A__		   =		[]
        A__		   =		[]
        for html_string in html_strings:
               A__   ,      A__   ,      A__		   =		self.get_three_from_single(__lowerCamelCase					)
               nodes.append(__lowerCamelCase					)
               A__		   =		[]
               for node, tag_list, sub_list in zip(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					):
                      A__		   =		self.construct_xpath(__lowerCamelCase,__lowerCamelCase					)
                      xpath_strings.append(__lowerCamelCase					)
               xpaths.append(__lowerCamelCase					)

        # return as Dict
        A__		   =		{'''nodes''': nodes, '''xpaths''': xpaths}
        A__		   =		BatchFeature(data=__lowerCamelCase,tensor_type=__lowerCamelCase					)

        return encoded_inputs












import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


a__: Union[str, Any]							   =						'hf-internal-testing/tiny-random-bert'
a__: Optional[int]							   =						os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
a__: List[str]							   =						'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'




class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 def 							UpperCamelCase     (				self					):
        A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase					)
        # Should have downloaded the file in here
        self.assertTrue(os.path.isdir(__lowerCamelCase					)					)
        # Cache should contain at least those three subfolders:
        for subfolder in ["blobs", "refs", "snapshots"]:
               self.assertTrue(os.path.isdir(os.path.join(__lowerCamelCase,__lowerCamelCase					)					)					)
        with open(os.path.join(__lowerCamelCase,'''refs''','''main'''					)					) as f:
               A__		   =		f.read()
        self.assertEqual(__lowerCamelCase,os.path.join(__lowerCamelCase,'''snapshots''',__lowerCamelCase,__lowerCamelCase					)					)
        self.assertTrue(os.path.isfile(__lowerCamelCase					)					)

        # File is cached at the same place the second time.
        A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase					)
        self.assertEqual(__lowerCamelCase,__lowerCamelCase					)

        # Using a specific revision to test the full commit hash.
        A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase,revision='''9b8c223'''					)
        self.assertEqual(__lowerCamelCase,os.path.join(__lowerCamelCase,'''snapshots''',__lowerCamelCase,__lowerCamelCase					)					)



 def 							UpperCamelCase     (				self					):
        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid model identifier'''					):
               A__		   =		cached_file('''tiny-random-bert''',__lowerCamelCase					)

        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid git identifier'''					):
               A__		   =		cached_file(__lowerCamelCase,__lowerCamelCase,revision='''aaaa'''					)

        with self.assertRaisesRegex(__lowerCamelCase,'''does not appear to have a file named'''					):
               A__		   =		cached_file(__lowerCamelCase,'''conf'''					)



 def 							UpperCamelCase     (				self					):
        with self.assertRaisesRegex(__lowerCamelCase,'''does not appear to have a file named'''					):
               A__		   =		cached_file(__lowerCamelCase,'''conf'''					)

        with open(os.path.join(__lowerCamelCase,'''refs''','''main'''					)					) as f:
               A__		   =		f.read()
        self.assertTrue(os.path.isfile(os.path.join(__lowerCamelCase,'''.no_exist''',__lowerCamelCase,'''conf'''					)					)					)

        A__		   =		cached_file(__lowerCamelCase,'''conf''',_raise_exceptions_for_missing_entries=__lowerCamelCase					)
        self.assertIsNone(__lowerCamelCase					)

        A__		   =		cached_file(__lowerCamelCase,'''conf''',local_files_only=__lowerCamelCase,_raise_exceptions_for_missing_entries=__lowerCamelCase					)
        self.assertIsNone(__lowerCamelCase					)

        A__		   =		mock.Mock()
        A__		   =		500
        A__		   =		{}
        A__		   =		HTTPError
        A__		   =		{}

        # Under the mock environment we get a 500 error when trying to reach the tokenizer.
        with mock.patch('''requests.Session.request''',return_value=__lowerCamelCase					) as mock_head:
               A__		   =		cached_file(__lowerCamelCase,'''conf''',_raise_exceptions_for_connection_errors=__lowerCamelCase					)
               self.assertIsNone(__lowerCamelCase					)
               # This check we did call the fake head request
               mock_head.assert_called()



 def 							UpperCamelCase     (				self					):
        self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only''',__lowerCamelCase					)					)
        self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''',__lowerCamelCase					)					)
        self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only''',__lowerCamelCase					)					)



 def 							UpperCamelCase     (				self					):
        # `get_file_from_repo` returns None if the file does not exist
        self.assertIsNone(get_file_from_repo('''bert-base-cased''','''ahah.txt'''					)					)

        # The function raises if the repository does not exist.
        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid model identifier'''					):
               get_file_from_repo('''bert-base-case''',__lowerCamelCase					)

        # The function raises if the revision does not exist.
        with self.assertRaisesRegex(__lowerCamelCase,'''is not a valid git identifier'''					):
               get_file_from_repo('''bert-base-cased''',__lowerCamelCase,revision='''ahaha'''					)

        A__		   =		get_file_from_repo('''bert-base-cased''',__lowerCamelCase					)
        # The name is the cached name which is not very easy to test, so instead we load the content.
        A__		   =		json.loads(open(__lowerCamelCase,'''r'''					).read()					)
        self.assertEqual(config['''hidden_size'''],768					)




 def 							UpperCamelCase     (				self					):
        with tempfile.TemporaryDirectory() as tmp_dir:
               A__		   =		Path(__lowerCamelCase					) / '''a.txt'''
               filename.touch()
               self.assertEqual(get_file_from_repo(__lowerCamelCase,'''a.txt'''					),str(__lowerCamelCase					)					)

               self.assertIsNone(get_file_from_repo(__lowerCamelCase,'''b.txt'''					)					)





