




"""simple docstring"""







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      =							logging.get_logger(__name__)






class       SCREAMING_SNAKE_CASE__					(					_a					):


						def __init__(							self				:			Optional[int]						,						**lowerCAmelCase				:			List[str]					):
										requires_backends(self						,						["""bs4"""]					)
										super().__init__(**lowerCAmelCase					)


						def   __lowercase (							self				:			Dict						,						lowerCAmelCase				:			Union[str, Any]					):
										lowerCAmelCase  =				[]
										lowerCAmelCase  =				[]
										lowerCAmelCase  =				element if element.name else element.parent
										for parent in child.parents:  # type: bs4.element.Tag
														lowerCAmelCase  =				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					)					)
														lowerCAmelCase  =				parent
										xpath_tags.reverse()
										xpath_subscripts.reverse()
										return xpath_tags, xpath_subscripts


						def   __lowercase (							self				:			Optional[int]						,						lowerCAmelCase				:			Optional[int]					):
										lowerCAmelCase  =				BeautifulSoup(lowerCAmelCase						,						"""html.parser"""					)

										lowerCAmelCase  =				[]
										lowerCAmelCase  =				[]
										lowerCAmelCase  =				[]

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

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

																		all_doc_strings.append(lowerCAmelCase					)

																		lowerCAmelCase ,						lowerCAmelCase  =				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   __lowercase (							self				:			Tuple						,						lowerCAmelCase				:			Union[str, Any]						,						lowerCAmelCase				:			List[str]					):
										lowerCAmelCase  =				""""""
										for tagname, subs in zip(lowerCAmelCase						,						lowerCAmelCase					):
														xpath += f'''/{tagname}'''
														if subs != 0:
																		xpath += f'''[{subs}]'''
										return xpath




						def __call__(							self				:			Dict						,						lowerCAmelCase				:			Optional[Any]					):
										lowerCAmelCase  =				False

										# Check that strings has a valid type
										if isinstance(lowerCAmelCase						,						lowerCAmelCase					):
														lowerCAmelCase  =				True
										elif isinstance(lowerCAmelCase						,						(list, tuple)					):
														if len(lowerCAmelCase					) == 0 or isinstance(html_strings[0]						,						lowerCAmelCase					):
																		lowerCAmelCase  =				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					)}.'''					)

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

										if not is_batched:
														lowerCAmelCase  =				[html_strings]

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

										# return as Dict
										lowerCAmelCase  =				{"""nodes""": nodes, """xpaths""": xpaths}
										lowerCAmelCase  =				BatchFeature(data=lowerCAmelCase						,						tensor_type=lowerCAmelCase					)

										return encoded_inputs











"""simple docstring"""







import os
import string
import sys


a      =							1 << 8

a      =							{
    'tab': ord('\t'),
    'newline': ord('\r'),
    'esc': 2_7,
    'up': 6_5 + ARROW_KEY_FLAG,
    'down': 6_6 + ARROW_KEY_FLAG,
    'right': 6_7 + ARROW_KEY_FLAG,
    'left': 6_8 + ARROW_KEY_FLAG,
    'mod_int': 9_1,
    'undefined': sys.maxsize,
    'interrupt': 3,
    'insert': 5_0,
    'delete': 5_1,
    'pg_up': 5_3,
    'pg_down': 5_4,
}

a      =							KEYMAP['up']
a      =							KEYMAP['left']

if sys.platform == "win32":
		a      =							[]
		a      =							{
		    b'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG,
		    b'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG,
		    b'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG,
		    b'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG,
		    b'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG,
		    b'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG,
		    b'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG,
		    b'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG,
		}

for i in range(1_0):
		a      =							ord(str(i))





def 							lowercase					()     ->  str:


				'''simple docstring'''






				if os.name == "nt":
								import msvcrt

								lowerCAmelCase  =				"""mbcs"""
								# Flush the keyboard buffer
								while msvcrt.kbhit():
												msvcrt.getch()
								if len(snake_case__ ) == 0:
												# Read the keystroke
												lowerCAmelCase  =				msvcrt.getch()

												# If it is a prefix char, get second part
												if ch in (b"\x00", b"\xe0"):
																lowerCAmelCase  =				ch + msvcrt.getch()
																# Translate actual Win chars to bullet char types
																try:
																				lowerCAmelCase  =				chr(WIN_KEYMAP[cha] )
																				WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) )
																				WIN_CH_BUFFER.append(snake_case__ )
																				if ord(snake_case__ ) in (
																				    KEYMAP["insert"] - 1 << 9,
																				    KEYMAP["delete"] - 1 << 9,
																				    KEYMAP["pg_up"] - 1 << 9,
																				    KEYMAP["pg_down"] - 1 << 9,
																				):
																								WIN_CH_BUFFER.append(chr(126 ) )
																				lowerCAmelCase  =				chr(KEYMAP["""esc"""] )
																except KeyError:
																				lowerCAmelCase  =				cha[1]
												else:
																lowerCAmelCase  =				ch.decode(snake_case__ )
								else:
												lowerCAmelCase  =				WIN_CH_BUFFER.pop(0 )
				elif os.name == "posix":
								import termios
								import tty

								lowerCAmelCase  =				sys.stdin.fileno()
								lowerCAmelCase  =				termios.tcgetattr(snake_case__ )
								try:
												tty.setraw(snake_case__ )
												lowerCAmelCase  =				sys.stdin.read(1 )
								finally:
												termios.tcsetattr(snake_case__      ,       termios.TCSADRAIN      ,       snake_case__ )
				return ch






def 							lowercase					()     ->  List[str]:


				'''simple docstring'''






				lowerCAmelCase  =				get_raw_chars()
				if ord(snake_case__ ) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
								return char

				elif ord(snake_case__ ) == KEYMAP["esc"]:
								lowerCAmelCase  =				get_raw_chars()
								if ord(snake_case__ ) == KEYMAP["mod_int"]:
												lowerCAmelCase  =				get_raw_chars()
												if ord(snake_case__ ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(snake_case__ ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
																return chr(ord(snake_case__ ) + ARROW_KEY_FLAG )
												else:
																return KEYMAP["undefined"]
								else:
												return get_raw_chars()

				else:
								if char in string.printable:
												return char
								else:
												return KEYMAP["undefined"]





