
    (<id                         U d Z ddlZddlmZ ddlmZmZ ddddddddddd	d	d
Zee	e	f   e
d<    edg      Zee	   e
d<   de	de	fdZde	dee	   fdZde	defdZde	dee	ef   fdZy)zSUtility to convert DevTools "Copy as cURL" commands to Scrapling Fetcher arguments.    N)Optional)_CookieParser_ParseHeadersheadercookiemethoddataproxy)z-Hz--headerz-bz--cookiez-Xz	--requestz-dz--dataz
--data-rawz--data-binaryz-xz--proxy_VALUE_FLAGSz--compressed_IGNORED_FLAGScurl_commandreturnc                 &    | j                  dd      S )z2Join backslash-continued lines into a single line.z\
 )replace)r   s    H/home/jay/workspace/.worktrees/task-2057-dev2/scripts/curl_to_fetcher.py_normalize_multiliner      s    ,,    c                 B    t        |       }t        j                  |      S )z0Tokenize a curl command string, handling quotes.)r   shlexsplit)r   
normalizeds     r   	_tokenizer   !   s    %l3J;;z""r   tokenc                 $    | j                  d      S )z/Return True if the token looks like a CLI flag.-)
startswith)r   s    r   _is_flagr   '   s    C  r   c                    | j                         }|st        d      	 t        |      }|r|d   dk7  r |rt        |d         nd}t        d|       g }g }d}d}d}	g }
d}|t	        |      k  rl||   }|t
        v r|dz  }"|t        v r|dz   t	        |      k\  rt        d	|d
      ||dz      }t        |   }|dk(  r|j                  |       n<|dk(  r|j                  |       n%|dk(  r|j                         }n|dk(  r|}n|dk(  r|}	|dz  }d|v rt        |      r|j                  d      \  }}}|t        v rct        |   }|dk(  r|j                  |       n<|dk(  r|j                  |       n%|dk(  r|j                         }n|dk(  r|}n|dk(  r|}	|dz  }Ct        |      r|dz  }U|
j                  |       |dz  }|t	        |      k  rl|
st        d      |
d   }t        |d      \  }}i }|D ]%  }|j                  t        t        |                   ' i ||}||}n|d}nd}||||d}|||d<   |	|	|d<   |S # t        $ r}t        d|       |d}~ww xY w)a  Parse a curl command string into Scrapling Fetcher call arguments.

    Args:
        curl_command: A curl command string, optionally with backslash line
            continuations (as produced by browser DevTools "Copy as cURL").

    Returns:
        A dict with keys:
            - ``url`` (str)
            - ``method`` (str): "GET", "POST", "PUT", or "DELETE"
            - ``headers`` (dict[str, str]): HTTP headers (Cookie header excluded)
            - ``cookies`` (dict[str, str]): parsed cookies
            - ``data`` (str | None): request body, present only when provided
            - ``proxy`` (str | None): proxy URL, present only when provided

    Raises:
        ValueError: If the command is empty, does not start with ``curl``,
            or contains no URL.
    zcurl command must not be emptyz!Failed to tokenize curl command: Nr   curlz	'(empty)'z%Command must start with 'curl', got:    zFlag z requires a value but got noner   r   r   r	   r
      =zNo URL found in curl commandT)parse_cookiesPOSTGET)urlr   headerscookies)strip
ValueErrorr   reprlenr   r   appendupperr   	partitionr   updatedictr   )r   strippedtokensexcfirstraw_header_linesraw_cookie_stringsexplicit_methodraw_datar
   url_candidatesidxr   valuekeyflag_r'   r(   cookies_from_headerscookies_from_b
cookie_strr)   r   results                            r   
parse_curlrE   ,   s   ( !!#H9::M8$ VAY&(#)VAY{@HII #%$&%)O"HE "N
C
F
sN"1HCL Qw#f+% 5	1O!PQQ37OEu%Ch ''."))%0"'++- 1HC %<HUO"__S1ND!U|#"4((?$++E2H_&--e4H_&+kkmOF]$HG^!Eq E?1HC 	e$qi F
l 788

C %22BRV$W!G! &(N( ?
d=#<=>? I!5HHG " 		 	!F !vwMW  M<SEBCLMs   I 	I1I,,I1)__doc__r   typingr   scrapling.core.utils._shellr   r   r   r2   str__annotations__	frozensetr   r   listr   boolr   objectrE    r   r   <module>rP      s    Y   D 



 d38n   "+N+;!<	# <-s -s -
#C #DI #!C !D !
ES ET#v+%6 Er   