o
    PjK                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZmZmZmZ d dl m!Z! d dl"m#Z#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d d	l*m+Z+ d d
l,m-Z- erd dl m.Z. d dl/m0Z0m1Z1m2Z2m3Z3 ee4ee4 ee4ge5f f Z6ee4ee4 edgee5ee5 f f f Z7ee4ee4 edgee5ee5 f f f Z8eedgef eddgef f Z9edgef Z:ed Z;ed Z<ed Z=ed Z>ed Z?ed Z@ed ZAed ZBed ZCed ZDed ZEG dd deddZFG d d! d!eddZGd"ed#ee4ef fd$d%ZHG d&d' d'eZIG d(d) d)eddZJG d*d+ d+eZKG d,d- d-eZLee4ee4eMe5f f ZN	dxd.ee4 d/e4d0ee6 d1e5d#e5f
d2d3ZO	dxd.ee4 d4e4d1e5d#e4fd5d6ZPd.ee4 d#ee4 fd7d8ZQd.ee4 d0e4d#e4fd9d:ZRd.ee4 d;e4d#ee4ee4 f fd<d=ZSd>e4d#efd?d@ZTG dAdB dBeddZUdCZVdDZWdEZXG dFdG dGZYdHeZdIee
 d#eFfdJdKZ[dxdLeFdMee4 d#e#fdNdOZ\dPe4d#e4fdQdRZ]dSed#efdTdUZ^d#e_fdVdWZ`G dXdY dYZaG dZd[ d[Zbecd\Zdd]e4d#e4fd^d_Zed`eee4f d#dfdadbZfdcee4ef ddee4egf d#dfdedfZhdcee4ef d#egfdgdhZiediZjdjejd#ejfdkdlZkdjee d#eee!  fdmdnZldoee d#e5fdpdqZmecdrZndse4d#e5fdtduZodoeeMejpdf d#eeM fdvdwZqdS )y    N)Path)TracebackType)TYPE_CHECKINGAny	AwaitableCallableDictListLiteralOptionalPatternSetTuple	TypedDictTypeVarUnioncast)ParseResulturljoinurlparse
urlunparse)	NameValue)ErrorTargetClosedErrorTimeoutErroris_target_closed_errorrewrite_error)glob_to_regex_pattern)RouteGreenlet)escape_regex_flags)HeadersArray)RequestResponseRouteWebSocketRouter!   r"   r#   r$   )darklightno-preferencenull)activenoner(   )morer'   r(   )r'   r(   reduce)commitdomcontentloadedloadnetworkidle)AltControlControlOrMetaMetaShift)leftmiddleright)allowblock)fullminimal)attachembedomit)abortfallbackc                   @   s2   e Zd ZU eed< eed< eed< ee ed< dS )ErrorPayloadmessagenamestackvalueN)__name__
__module____qualname__str__annotations__r   r    rL   rL   M/home/thesage/.local/lib/python3.10/site-packages/playwright/_impl/_helper.pyrB   S   s
   
 rB   F)totalc                   @   s"   e Zd ZU eed< ee ed< dS )HarRecordingMetadatapathcontentN)rG   rH   rI   rJ   rK   r   HarContentPolicyrL   rL   rL   rM   rO   Z   s   
 rO   paramsreturnc                 C   s   dt | d i}d| v r,| d }t|t r||d< t|tr)|j|d< t||d< | d= d| v r9| d |d< | d= d }d }d	| v rH| d	 }| d	= d
| v rS| d
 }| d
= |pZ|rYdnd }|ra||d< |S )NrP   recordHarPathrecordHarUrlFilterurlGloburlRegexSourceurlRegexFlagsrecordHarModemoderecordHarContentrecordHarOmitContentr?   rQ   )rJ   
isinstancer   patternr   )rS   
out_paramsoptnew_content_apiold_content_apirQ   rL   rL   rM   prepare_record_har_options_   s0   


rd   c                   @   s&   e Zd ZU eed< eed< eed< dS )ParsedMessageParamstypeguidinitializerN)rG   rH   rI   rJ   rK   r   rL   rL   rL   rM   re   |   s   
 re   c                   @   sR   e Zd ZU eed< eed< eed< eed< eed< eed< eed< e	e ed< d	S )
ParsedMessagePayloadidrg   methodrS   resulterrorerrorDetailslogN)
rG   rH   rI   intrK   rJ   re   r   rB   r	   rL   rL   rL   rM   ri      s   
 ri   c                   @   s   e Zd ZU ee ed< dS )DocumentrequestN)rG   rH   rI   r   r   rK   rL   rL   rL   rM   rq      s   
 rq   c                   @   s6   e Zd ZU eed< eed< ee ed< ee ed< dS )FrameNavigatedEventurlrD   newDocumentrm   N)rG   rH   rI   rJ   rK   r   rq   rL   rL   rL   rM   rs      s
   
 rs   base_url
url_stringmatchwebsocket_urlc                 C   sD   |sdS t |trtt| ||}t |trt||S ||S )NT)r^   rJ   recompileresolve_glob_to_regex_patternr   boolsearch)rv   rw   rx   ry   rL   rL   rM   url_matches   s   


r   globc                 C   s   |rt | } t| |}t|S N)to_websocket_base_urlresolve_glob_baser   )rv   r   ry   rL   rL   rM   r|      s   
r|   c                 C   s&   | d urt d| rt dd| } | S )Nz
^https?://z^httpws)rz   rx   sub)rv   rL   rL   rM   r      s   r   c                    s  |d dkr|S i  dt dt dt f fdd}|dd	}|d
s6|ds6|ds6|ds6|dr8|S g }t|dD ]e\}}|dv rO|| qA|dkro|drod|v s`d|v ri|||d n|| qA|d	}|dkr|||d| d qA||d | d| d}|||d  d| d}|||  qAd|}	t	| |	\}
} 
 D ]\}}|o||v }|
||r| n|d}
q|
S )Nr   *originalreplacementrT   c                    s   t | dkrdS |  |< |S )Nr    )len)r   r   	token_maprL   rM   	map_token   s   z$resolve_glob_base.<locals>.map_tokenz\\??zabout:zdata:zchrome:zedge:zfile:/).z..r   :{zhttp:z$_z_$z?$_   )rJ   replace
startswith	enumeratesplitappendendswithfindjoinresolve_base_urlitemslower)rv   rx   r   processed_partsindextokenquestion_index
new_prefix
new_suffixrelative_pathresolvedcase_insensitive_partr   	normalizerL   r   rM   r      sP   



r   	given_urlc                 C   sx   z.t t| d ur	| nd|}t|}|jpd}|jr"|d|j 7 }|j d| }||fW S  ty;   |d f Y S w )Nr   r   z://)nodelike_urlparser   r   hostnameportscheme	Exception)rv   r   rt   r   hostname_portcase_insensitive_prefixrL   rL   rM   r      s   
r   rt   c                 C   s4   t | dd}|jdv }|r|jdkr|jdd}|S )NT)allow_fragments)httphttpsr   wssftpfiler   r   rP   )r   r   rP   _replace)rt   parsedis_special_urlrL   rL   rM   r     s   

r   c                   @   sV   e Zd ZU ed ed< ee ed< ee ed< ee ed< ed ed< ee ed< d	S )
HarLookupResult)rm   redirectfulfillnoentryactionrC   redirectURLstatusr    headersbodyN)rG   rH   rI   r
   rK   r   rJ   rp   rL   rL   rL   rM   r     s   
 r   i0u  i  ic                   @   s   e Zd Zeddee defddZded  ddfddZdee ddfd	d
ZddedefddZ	dee ddfddZ
dee fddZdee fddZddedefddZdS )TimeoutSettingsNtimeoutrT   c                 C   s   | d ur| S t S r   )1DEFAULT_PLAYWRIGHT_LAUNCH_TIMEOUT_IN_MILLISECONDS)r   rL   rL   rM   launch_timeout*  s
   zTimeoutSettings.launch_timeoutparentc                 C   s   || _ d | _d | _d S r   )_parent_default_timeout_default_navigation_timeout)selfr   rL   rL   rM   __init__2  s   
zTimeoutSettings.__init__c                 C   
   || _ d S r   r   r   r   rL   rL   rM   set_default_timeout7  s   
z#TimeoutSettings.set_default_timeoutc                 C   s0   |d ur|S | j d ur| j S | jr| j S tS r   )r   r   r   *DEFAULT_PLAYWRIGHT_TIMEOUT_IN_MILLISECONDSr   rL   rL   rM   r   :  s   

zTimeoutSettings.timeoutnavigation_timeoutc                 C   r   r   r   )r   r   rL   rL   rM   set_default_navigation_timeoutC  s   
z.TimeoutSettings.set_default_navigation_timeoutc                 C      | j S r   r   r   rL   rL   rM   default_navigation_timeoutH     z*TimeoutSettings.default_navigation_timeoutc                 C   r   r   r   r   rL   rL   rM   default_timeoutK  r   zTimeoutSettings.default_timeoutc                 C   s@   |d ur|S | j d ur| j S | jd ur| jS | jr| j S tS r   )r   r   r   r   r   r   rL   rL   rM   r   N  s   


z"TimeoutSettings.navigation_timeoutr   )rG   rH   rI   staticmethodr   floatr   r   r   r   r   r   r   r   rL   rL   rL   rM   r   (  s    	
r   extbc                 C   s   t t| ddt|dS )Nr   r   )rC   rD   rE   )rB   rJ   r   	traceback	format_tb)r   r   rL   rL   rM   serialize_errorZ  s   r   rm   ro   c                 C   s\   t }| ddkrt}| ddkrt}|sd}|t| d | }| d |_| d |_|S )NrD   r   r   r   rC   rE   )r   getr   r   patch_error_message_name_stack)rm   ro   base_error_classexcrL   rL   rM   parse_error`  s   

r   rC   c                 C   s8   t d| }|rt|d|d } | dd} | S )Nz(\w+)(: expected .*)r      zPass { acceptDownloads: true }zPass 'accept_downloads=True')rz   rx   to_snake_casegroupr   )rC   rx   rL   rL   rM   r   n  s   r   argsc                 C   sN   i }| D ] }|dkrq| | d ur$t | | ts| | nt| | ||< q|S )Nr   )r^   r   locals_to_params)r   copykeyrL   rL   rM   r   x  s   

r   c                   C   s   t t d S )Ni  )mathfloortime	monotonicrL   rL   rL   rM   monotonic_time  s   r   c                   @   s(   e Zd ZU ded< ded< d	ddZdS )
RouteHandlerInvocationasyncio.Futurecompleter#   routerT   Nc                 C   s   || _ || _d S r   )r   r   )r   r   r   rL   rL   rM   r     s   
zRouteHandlerInvocation.__init__)r   r   r   r#   rT   N)rG   rH   rI   rK   r   rL   rL   rL   rM   r     s   
 r   c                   @   s   e Zd Z	ddee dedededee f
ddZ	d	ed
efddZ
ddd
efddZddd
efddZded d
dfddZed
efddZeded  d
eeeef  fddZdS )RouteHandlerNrv   rt   handleris_synctimesc                 C   s@   || _ || _|| _|r|ntj| _d| _|| _d| _t	 | _
d S )Nr   F)	_base_urlrt   r   r   inf_times_handled_count_is_sync_ignore_exceptionset_active_invocations)r   rv   rt   r   r   r   rL   rL   rM   r     s   zRouteHandler.__init__request_urlrT   c                 C   s   t | j|| jS r   )r   r   rt   )r   r  rL   rL   rM   matches     zRouteHandler.matchesr   r#   c              
      s   t t  |}| j| zUz| |I d H W W |jd  | j	| S  t
ye } z2| jrFW Y d }~W |jd  | j	| dS t|r_| jsOdnd}t|dt| d| d|d }~ww |jd  | j	| w )NFzawait r   "z3" while running route callback.
Consider awaiting `zkpage.unroute_all(behavior='ignoreErrors')`
before the end of the test to ignore remaining routes in flight.)r   asyncioget_running_loopcreate_futurer  add_handle_internalr   
set_resultremover   r  r   r  r   rJ   )r   r   handler_invocationeoptional_async_prefixrL   rL   rM   handle  s2   zRouteHandler.handlec                    s     } jd7  _jr+j  d fdd}t|}|   I d H  nj}|r<t	
|I d H  |I d H S )Nr   rT   c               
      sP   z j  d  W d S  ty' }  z |  W Y d } ~ d S d } ~ ww r   )r   rr   r  r   set_exception)r  handler_finished_futurer   r   rL   rM   _handler  s   z/RouteHandler._handle_internal.<locals>._handlerrT   N)_start_handlingr  r  _loopr  r   switchr   rr   r
  ensure_future)r   r   handled_futurer  gcoro_or_futurerL   r  rM   r    s   
	
zRouteHandler._handle_internalbehavior)ignoreErrorswaitc                    sL   |dkr
d| _ d S g }| jD ]}|jjs||j qtj| I d H  d S )Nr"  T)r  r  r   
_did_throwr   r   r
  gather)r   r!  tasks
activationrL   rL   rM   stop  s   

zRouteHandler.stopc                 C   s   | j d | jkS )Nr   )r  r   r   rL   rL   rM   will_expire  s   zRouteHandler.will_expirehandlersc                 C   sp   g }d}| D ](}t |jtr|d|ji qt |jtjr,||jjt|jd qd}q|r6ddigS |S )NFr   )regexSource
regexFlagsTz**/*)r^   rt   rJ   r   rz   r   r_   r   )r*  patternsallr   rL   rL   rM   prepare_interception_patterns  s   
z*RouteHandler.prepare_interception_patternsr   )rG   rH   rI   r   rJ   URLMatchRouteHandlerCallbackr}   rp   r   r  r  r  r
   r(  propertyr)  r   r	   r   r/  rL   rL   rL   rM   r     s2    
r   z(((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))rD   c                 C   s   t d|  S )Nz_\1)to_snake_case_regexr   r   )rD   rL   rL   rM   r     r  r   rP   c                 C   s2   t j| st |  } t jt j| dd d S )NT)exist_ok)osrP   isabsr   cwdmakedirsdirnamer   rL   rL   rM   make_dirs_for_file
  s   r:  r   datac                    s0   d fdd}t  }|d |I d H  d S )NrT   c                     sH   t t tr	dnd} |   W d    d S 1 sw   Y  d S )Nwwb)openr^   rJ   writefhr;  r   rL   rM   inner  s   "zasync_writefile.<locals>.innerr  )r
  r  run_in_executor)r   r;  rC  looprL   rB  rM   async_writefile  s   rF  c                    s.   dt f fdd}t }|d |I d H S )NrT   c                     s6   t  d} |  W  d    S 1 sw   Y  d S )Nrb)r>  readr@  r   rL   rM   rC    s   $zasync_readfile.<locals>.inner)bytesr
  r  rD  )r   rC  rE  rL   rI  rM   async_readfile  s   rK  Tobjc                 C   s   t | drtt| jS | S )N	_impl_obj)hasattrr   r   rN  )rM  rL   rL   rM   to_impl%  s   
rP  c                 C   s8   | sd S g }|   D ]\}}|t|t|d q
|S )N)rD   rF   )r   r   r   rJ   )rM  rl   r   rF   rL   rL   rM   object_to_array+  s   rQ  rF   c                 C   s"   t | tod| v od| v od| v S )NrD   mimeTypebuffer)r^   dictrF   rL   rL   rM   is_file_payload4  s   
rV  z^(text\/.*?|application\/(json|(x-)?javascript|xml.*?|ecmascript|graphql|x-www-form-urlencoded)|image\/svg(\+xml)?|application\/.*?(\+json|\+xml))(;\s*charset=.*)?$	mime_typec                 C   s   t t| S r   )r}   TEXTUAL_MIME_TYPErx   )rW  rL   rL   rM   is_textual_mime_typeB  s   rY  c                 C   s,   | d u rd S t | tjr| tjdd S | S )Nr   )milliseconds)r^   datetime	timedeltarU  rL   rL   rM   to_millisecondsF  s
   r]  r   )rr
  r[  r   r5  rz   r   r   pathlibr   typesr   typingr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   urllib.parser   r   r   r    playwright._impl._api_structuresr   playwright._impl._errorsr   r   r   r   r   playwright._impl._globr   playwright._impl._greenletsr   playwright._impl._str_utilsr   r    playwright._impl._networkr!   r"   r#   r$   rJ   r}   r0  URLMatchRequestURLMatchResponser1  WebSocketRouteHandlerCallbackColorSchemeForcedColorsContrastReducedMotionDocumentLoadStateKeyboardModifierMouseButtonServiceWorkersPolicyHarModerR   RouteFromHarNotFoundPolicyrB   rO   rd   re   ri   rq   rs   r   Envr   r|   r   r   r   r   r   r   r   PLAYWRIGHT_MAX_DEADLINEr   r   r   r   r   r   rp   r   r   r   r{   r3  r   r:  rJ  rF  rK  rL  rP  rQ  rV  rX  rY  r\  r]  rL   rL   rL   rM   <module>   s   D""

	=
	2
	
p&				