
    M/e*                        d 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 ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  ej6                  e      Z G d d      Z G d d      Zdede fdZ! G d d e	e         Z"d!e#de"fd"Z$d#ee   de"fd$Z%d%e"de#fd&Z&d%e"d#ee   ddfd'Z'y)(z6Very low-level nginx config parser based on pyparsing.    N)Any)IO)Iterable)Iterator)List)overload)SupportsIndex)Tuple)Union)Combine)Forward)Group)Literal)Optional)ParseResults)QuotedString)Regex)
restOfLine)	stringEnd)White)
ZeroOrMorec                   :   e Zd ZdZ e e             j                         Z e       j                         Z e	d      j                         Ze e	d      j                         z   Z e	d      j                         Z edddd	      Z ed
ddd	      Zeez  Z ed      Z ed      Z ee ee      z         Z ee e	d      z    ee      z         Zeez  ez  Zeez    eeez         z   ez   Zeez   Ze e	d      z   ez   Z e       Z e e       e e      z   e e      z  Z! e e      Z" e  ee!      ez         j                         Z#ee"ez   e#z   ez   z    ee!      ez   e$z   Z%e%jM                         j                          de'ddfdZ(de)fdZ*de+e,   fdZ-y)RawNginxParserz7A class that parses nginx configuration with pyparsing.{};"TF\)	multilineunquoteResultsescChar'z(\$\{)|[^{};\s'\"]z(\$\{)|[^{;\s])#sourcereturnNc                     || _         y Nr%   )selfr%   s     E/usr/lib/python3/dist-packages/certbot_nginx/_internal/nginxparser.py__init__zRawNginxParser.__init__H   	        c                 L    | j                   j                  | j                        S )zReturns the parsed tree.)scriptparseStringr%   r*   s    r+   parsezRawNginxParser.parseK   s    {{&&t{{33r.   c                 >    | j                         j                         S )z"Returns the parsed tree as a list.)r3   asListr2   s    r+   as_listzRawNginxParser.as_listO   s    zz|""$$r.   ).__name__
__module____qualname____doc__r   r   leaveWhitespacespacerequired_spacer   suppressleft_bracketright_bracket	semicolonr   dquotedsquotedquotedr   head_tokencharstail_tokencharsr   r   
tokencharsparen_quote_extendtokenwhitespace_token_group
assignmentr   commentr   blockr   contentsblock_beginblock_innardsr   r0   parseWithTabsstrr,   r   r3   r   r   r6    r.   r+   r   r   !   s   A UW--/EW,,.N3<((*LGCL1133M%%'I3$udSG3$udSGwF12O-.O:o+FFGJ '#,!6O9T!TU +f4E"U]Z8N-OORWW')3Jgcl"Z/GIE W~e,uZ/@@H./K*X.67GGIM	[<'-7-GG!E)I5F
**,s t 4| 4%c %r.   r   c                   j    e Zd ZdZdee   ddfdZddej                  ee      de	e
   fdZde
fdZy)	RawNginxDumperz>A class that dumps nginx configuration from the provided tree.blocksr&   Nc                     || _         y r(   rV   )r*   rV   s     r+   r,   zRawNginxDumper.__init__V   r-   r.   c              #   <  K   |xs | j                   }|D ]  }t        |t              r| t        j                  |      }t        |d         r|j                  d       |sRt        |d   t              r[dj                  |j                  d            dz    |j                  d      D ]  }| j                  |g      D ]  }|   d d}t        |d   t              r|d   j                         dk(  rd}dj                  |      |z     yw)z"Iterates the dumped nginx content.r    r   r   r   r$   N)rV   
isinstancerR   copydeepcopyspaceypoplistjoin__iter__strip)r*   rV   b0item	parameterlinerA   s          r+   rb   zRawNginxDumper.__iter__Y   s    &4;; 	0B"c"==$Dd1ghhqk!$q'4(ggdhhqk*S00!%! #I $yk : #"
## 		d1gs+Q30F "Iggdmi//)	0s   DDc                 $    dj                  |       S )z$Return the parsed block as a string.rZ   )ra   r2   s    r+   __str__zRawNginxDumper.__str__r   s    wwt}r.   r(   )r7   r8   r9   r:   r   r   r,   typingr   r   rR   rb   ri   rS   r.   r+   rU   rU   T   sL    HtCy T 0vtCy9 0Xc] 02 r.   rU   xr&   c                 T    t        | t              xr | j                         xs | dk(  S )z#Is x an empty string or whitespace?rZ   )r[   rR   isspace)rk   s    r+   r^   r^   w   s#    q#.199;:17:r.   c                       e Zd ZdZdee   ddf fdZeddded   fd       Z	ede
dee
e
f   fd	       Z	edee   ded ee   f   fd
       Z	dedeeef   fdZ	dddeddf fdZdeddf fdZdeddf fdZdee   dd fdZdededdfdZdededdfdZd%dZdededdfdZdededdfdZdedef   deddf fdZdedef   ddf fdZd edd fd!Zdefd"Zd#ddefd$Z xZS )&UnspacedListzIWrap a list [of lists], making any whitespace entries magically invisiblelist_sourcer&   Nc                    t        j                  t        |            | _        d| _        t
        |   |       t        t        t        |                   D ]m  \  }}t        |t              r5t        |      }t
        | -  ||       |j                  | j                  |<   Kt        |      sWd| d | vs_t
        | 5  |       o y )NFr$   )r\   r]   r`   spaceddirtysuperr,   reversed	enumerater[   ro   __setitem__r^   __delitem__)r*   rp   ientrysublist	__class__s        r+   r,   zUnspacedList.__init__   s    mmD$56
 	% io!67 	+HAu%&&u-#Aw/!(Ad2Ah&G'*	+r.   inbound)NNc                      y r(   rS   r*   r}   s     r+   _coercezUnspacedList._coerce   s    ;>r.   c                      y r(   rS   r   s     r+   r   zUnspacedList._coerce   s    8;r.   c                      y r(   rS   r   s     r+   r   zUnspacedList._coerce   s    ORr.   c                 t    t        |t              s||fS t        |d      st        |      }||j                  fS )z
        Coerce some inbound object to be appropriately usable in this object

        :param inbound: string or None or list or UnspacedList
        :returns: (coerced UnspacedList or string or None, spaced equivalent)
        :rtype: tuple

        rr   )r[   r`   hasattrro   rr   r   s     r+   r   zUnspacedList._coerce   s<     '4(G##7H-&w/GNN**r.   ry   r	   rk   c                 <   t        j                  |      }| j                  |      \  }}|t        |       k  r| j	                  |      nt        | j
                        }| j
                  j                  ||       t        |      st        |   ||       d| _	        y)zInsert object before index.TN)
operatorindexr   len_spaced_positionrr   insertr^   rt   rs   )r*   ry   rk   idxre   spaced_itemsliceposr|   s          r+   r   zUnspacedList.insert   sx    nnQ LLOk14s4y4((-c$++FV8[1d|GN3%
r.   c                     | j                  |      \  }}| j                  j                  |       t        |      st        | 	  |       d| _        y)z%Append object to the end of the list.TN)r   rr   appendr^   rt   rs   r*   rk   re   r   r|   s       r+   r   zUnspacedList.append   sA     LLOk;'d|GN4 
r.   c                     | j                  |      \  }}| j                  j                  |       t        | 	  |       d| _        y)z4Extend list by appending elements from the iterable.TN)r   rr   extendrt   rs   r   s       r+   r   zUnspacedList.extend   s:     LLOk;'t
r.   otherc                 `    t        j                  |       }|j                  |       d|_        |S )NT)r\   r]   r   rs   )r*   r   new_lists      r+   __add__zUnspacedList.__add__   s(    ==&r.   argskwargsc                     t        d      )z2Function pop() is not implemented for UnspacedListz&UnspacedList.pop() not yet implementedNotImplementedErrorr*   r   r   s      r+   r_   zUnspacedList.pop   s    !"JKKr.   c                     t        d      )z5Function remove() is not implemented for UnspacedListz)UnspacedList.remove() not yet implementedr   r   s      r+   removezUnspacedList.remove   s    !"MNNr.   c                     t        d      )z6Function reverse() is not implemented for UnspacedListz*UnspacedList.reverse() not yet implementedr   r2   s    r+   reversezUnspacedList.reverse   s    !"NOOr.   _args_kwargsc                     t        d      )z3Function sort() is not implemented for UnspacedListz'UnspacedList.sort() not yet implementedr   )r*   r   r   s      r+   sortzUnspacedList.sort   s    !"KLLr.   c                     t        d      )N5Slice operations on UnspacedLists not yet implementedr   r   s      r+   __setslice__zUnspacedList.__setslice__   s    !"YZZr.   valuec                     t        |t              rt        d      | j                  |      \  }}| j                  j                  | j                  |      |       t        |      st        |   ||       d| _	        y Nr   T)
r[   slicer   r   rr   rw   r   r^   rt   rs   )r*   ry   r   re   r   r|   s        r+   rw   zUnspacedList.__setitem__   sh    a%&]^^ LL/k 5 5a 8+Fd|G4(
r.   c                     t        |t              rt        d      | j                  j	                  | j                  |             t        |   |       d| _        y r   )r[   r   r   rr   rx   r   rt   rs   )r*   ry   r|   s     r+   rx   zUnspacedList.__delitem__   sJ    a%&]^^ 5 5a 89A
r.   memoc                     t        j                  | j                  |      }t        |      }| j                  |_        |S )N)r   )r\   r]   rr   ro   rs   )r*   r   
new_spacedr   s       r+   __deepcopy__zUnspacedList.__deepcopy__   s0    ]]4;;T:

+r.   c                 @    | j                   ryt        d | D              S )zFRecurse through the parse tree to figure out if any sublists are dirtyTc              3   `   K   | ]&  }t        |t              xr |j                          ( y wr(   )r[   ro   is_dirty).0rk   s     r+   	<genexpr>z(UnspacedList.is_dirty.<locals>.<genexpr>   s%     OQJq,/@AJJL@Os   ,.)rs   anyr2   s    r+   r   zUnspacedList.is_dirty   s    ::O$OPPr.   r   c                 $   t        j                  |      }dx}}|dk  rt        |       |z   }d|cxk  rt        |       k  st        d       t        d      |}|dk7  r.t	        | j
                  |         r|dz  }n|dz  }|dz  }|dk7  r.||z   S )zHConvert from indexes in the unspaced list to positions in the spaced oner   zlist index out of range   )r   r   r   
IndexErrorr^   rr   )r*   r   int_idxposspacesint_idx0s         r+   r   zUnspacedList._spaced_position   s    ..%fQ;$i')GG'c$i'677 (677mdkk#&'!11HC m &  r.   )r&   N)r7   r8   r9   r:   r   r   r,   r   r
   r   rR   r   r   r   r   r   r_   r   r   r   r   r   r   rw   rx   r   boolr   intr   __classcell__)r|   s   @r+   ro   ro   |   s   S+HSM +d +$ >t>j(9> >;s;uS#X; ;RtCyRU>493L-MR R+s +uS#X +  C D     T#Y > L L L LOC O3 O4 OPM3 M3 M4 M[# [ [ [U?E#9: 3 4 U?E#9: t   Q$ Q!O ! !r.   ro   r%   c                 F    t        t        |       j                               S )zvParses from a string.

    :param str source: The string to parse
    :returns: The parsed tree
    :rtype: list

    )ro   r   r6   r)   s    r+   loadsr     s     v.66899r.   file_c                 4    t        | j                               S )zrParses from a file.

    :param file file_: The file to parse
    :returns: The parsed tree
    :rtype: list

    )r   read)r   s    r+   loadr     s     r.   rV   c                 >    t        t        | j                              S )zjDump to a Unicode string.

    :param UnspacedList blocks: The parsed tree
    :rtype: six.text_type

    )rR   rU   rr   rX   s    r+   dumpsr   %  s     ~fmm,--r.   c                 8    |j                  t        |              y)zDump to a file.

    :param UnspacedList blocks: The parsed tree
    :param IO[Any] file_: The file stream to dump to. It must be opened with
                          Unicode encoding.
    :rtype: None

    N)writer   )rV   r   s     r+   dumpr   /  s     
KKfr.   )(r:   r\   loggingr   rj   r   r   r   r   r   r   r	   r
   r   	pyparsingr   r   r   r   r   r   r   r   r   r   r   r   	getLoggerr7   loggerr   rU   r   r^   ro   rR   r   r   r   r   rS   r.   r+   <module>r      s    <                    " "       			8	$0% 0%f   F;c ;d ;
M!49 M!f:# :, :3 L ., .3 .	 	bg 	$ 	r.   