+
    h5  c                   s    ^ RI t ^RIHt ^RIHt  ! R R4      t ! R R4      t ! R R	4      tR
 t ! R R]4      t	]	t
R# )i    N)xmlutil)logc                   s*   a  ] tR t^t o RtR tRtV tR# )_XPathSegmenta  
Class representing a single 'segment' of an xpath string. For example,
the xpath:

    ./qemu:foo/bar[1]/baz[@somepro='someval']/@finalprop

will be split into the following segments:

    #1: nodename=., fullsegment=.
    #2: nodename=foo, nsname=qemu, fullsegment=qemu:foo
    #3: nodename=bar, condition_num=1, fullsegment=bar[1]
    #4: nodename=baz, condition_prop=somepro, condition_val=someval,
            fullsegment=baz[@somepro='somval']
    #5: nodename=finalprop, is_prop=True, fullsegment=@finalprop
c                s   Wn         Wn        R V n        R V n        R V n        RV P                  9   d   V P                  P                  R4      P                  R4      w  V n        pRV9   dA   VP                  R4      w  r4VP                  R4      V n        VP                  R4      V n        M&VP                  4       '       d   \        V4      V n        V P                  P                  R4      V n
        V P                  '       d   V P                  R,          V n        R V n        RV P                  9   d+   V P                  P                  R4      w  V n        V n        R # R # )NZ[Z]Z=Z@Z'   NNZ:)fullsegmentnodenamecondition_propcondition_valcondition_numZstripsplitZisdigitZintZ
startswithis_propnsname)selfr   ZcondZcpropZcval   &&   */usr/share/virt-manager/virtinst/xmlapi.py__init__Z_XPathSegment.__init__    s	   &#"!!$--"&--"5"5c":"@"@"EDM4d{ $

3&+kk#&6#%)ZZ_"%(Y"}}//4<<< MM"-DM$--)-)<)<S)A&DK      )r	   r   r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes____classdictcell__Z__classdict__   @r   r   r      s      B Br   r   c                   s@   a  ] tR t^9t o RtR t]R 4       tR tRt	V t
R# )_XPathz]
Helper class for performing manipulations of XPath strings. Splits
the xpath into segments.
c                s6   Wn         . V n        V P                   P                  R 4       FD  pVR8X  d   V P                  RR V n        K   V P                  P                  \	        V4      4       KF  	  V P                  R,          P
                  V n        V P
                  ;'       d&    V P                  R,          P                  ;'       g    RV n        V P
                  '       d   V P                  RR V n        V P                  V P                  4      V n	        R# )/z..N)
	fullxpathsegmentsr
   Zappendr   r   r   propnamejoinxpath)r   r   s   && r   r   Z_XPath.__init__?   s    "%%c*ADy $cr 2MM  q!12 + }}R(00CCr):)C)CKKt<<< MM#2.DMYYt}}-
r   c                s2    R P                  R V  4       4      # )r   c              3   s8   "   T F  qP                   x  K  	  R # 5iN)r   )Z.0r#   s   & r   Z	<genexpr>Z_XPath.join.<locals>.<genexpr>Q   s     8x!xs   )r!   )r      &r   r!   Z_XPath.joinO   s    xx8x888r   c                s>    V P                  V P                  R R 4      # )Nr   )r!   r   r   r&   r   parent_xpathZ_XPath.parent_xpathS   s    yys+,,r   )r   r   r    r   r"   N)r   r   r   r   r   r   Zstaticmethodr!   r(   r   r   r   r   r   r   r   9   s-     
.  9 9- -r   r   c                   s   a  ] tR t^Wt o / t]R 4       tR tR tR t	R t
R tR tR tR	 tR
 tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR tR t R t!Rt"V t#R# )_XMLBasec                s"    W P                   V&   R # r%   )
NAMESPACES)Zclsr   Zuri   &&&r   register_namespaceZ_XMLBase.register_namespaceZ   s    !$vr   c                    \        4       hr%   ZNotImplementedErrorr'   r&   r   copy_apiZ_XMLBase.copy_api^       !##r   c                r-   r%   r.   r   r"      &&r   countZ_XMLBase.counta   r0   r   c                r-   r%   r.   )r   r   r2   r   _findZ_XMLBase._findd   r0   r   c                r-   r%   r.   r   noder2   r   _node_tostringZ_XMLBase._node_tostringg   r0   r   c                r-   r%   r.   r5   r2   r   _node_get_textZ_XMLBase._node_get_textj   r0   r   c                r-   r%   r.   r   r6   setvalr+   r   _node_set_textZ_XMLBase._node_set_textm   r0   r   c                r-   r%   r.   )r   r6   r    r+   r   _node_get_propertyZ_XMLBase._node_get_propertyp   r0   r   c                r-   r%   r.   )r   r6   r    r:      &&&&r   _node_set_propertyZ_XMLBase._node_set_propertys   r0   r   c                r-   r%   r.   )r   xpathseg
parentnoder+   r   	_node_newZ_XMLBase._node_newv   r0   r   c                r-   r%   r.   )r   parentxpathr@   newnoder=   r   _node_add_childZ_XMLBase._node_add_childy   r0   r   c                r-   r%   r.   )r   r@   	childnoder+   r   _node_remove_childZ_XMLBase._node_remove_child|   r0   r   c                r-   r%   r.   )r   r"   rC   r+   r   _node_replace_childZ_XMLBase._node_replace_child   r0   r   c                r-   r%   r.   r   xmlr2   r   _node_from_xmlZ_XMLBase._node_from_xml   r0   r   c                r-   r%   r.   r5   r2   r   _node_has_contentZ_XMLBase._node_has_content   r0   r   c                r-   r%   r.   r5   r2   r   _node_get_nameZ_XMLBase._node_get_name   r0   r   c                r-   r%   r.   r1   r2   r   
node_clearZ_XMLBase.node_clear   r0   r   c                r-   r%   r.   rH   r2   r   _sanitize_xmlZ_XMLBase._sanitize_xml   r0   r   c                sp    V P                  V4      pVf   R# V P                  V P                  V4      4      # )N )r4   rN   r7   )r   r"   r6   r$   r   get_xmlZ_XMLBase.get_xml   s4    zz% <!!$"5"5d";<<r   c                s    V P                  V4      pVf   R # V'       d   R# \        V4      pVP                  '       d   V P                  W4P                  4      # V P                  V4      # )NT)r4   r   r   r<   r    r8   )r   r"   Zis_boolr6   xpathobj   &&&  r   get_xpath_contentZ_XMLBase.get_xpath_content   s[    zz% <%=**41B1BCC""4((r   c                s   V P                  V4      pVR J d   V P                  V4       R# Vf*   Ve   V P                  WR4       V P                  V4       R# Vf   V P	                  V4      pVRJ d   R# V P                  WV4       R# )FNT)r4   node_force_remove_node_set_content_node_remove_empty_node_make_stub)r   r"   r:   r6      &&& r   set_xpath_contentZ_XMLBase.set_xpath_content   s}    zz% U?""5)^&&uD9##E*|++E2~""57r   c                sn    V P                  V4      pV P                  V4      pV P                  W$V4       R # r%   )rJ   rW   rD   )r   rI   r"   rC   r@   rR   r   node_add_xmlZ_XMLBase.node_add_xml   s2    %%c*))%0
U8r   c                sJ    V P                  V4      pV P                  W4       R# )z2
Replace the node at xpath with the passed in xml
N)rJ   rG   )r   r"   rI   rC   rX   r   node_replace_xmlZ_XMLBase.node_replace_xml   s"     %%c*  0r   c                s    \        V4      pV P                  VP                  4       4      pV P                  V4      pVe   Vf   R# V P                  W44       R# )z
Remove the element referenced at the passed xpath, regardless
of whether it has children or not, and then clean up the XML
chain
N)r   r4   r(   rF   )r   r   rQ   r@   rE   r   r   rT   Z_XMLBase.node_force_remove   sO     )$ZZ 5 5 78
JJy)	!2
6r   c                s    V P                  V P                  R 4      4      pW!8X  d   R# \        \        R4      RVRV/,          4      h).NzSXML did not have expected root element name '%(expectname)s', found '%(foundname)s'Z
expectnameZ	foundname)rL   r4   RuntimeErrorZ_)r   Zexpected_root_nameZrootnamer$   r   validate_root_nameZ_XMLBase.validate_root_name   sR    &&tzz#7): /hG	H
 	
r   c                s    \        V4      pVe   \        V4      pVP                  '       d   V P                  W$P                  V4       R # V P                  W#4       R # r%   )r   strr   r>   r    r;   )r   r"   r6   r:   rQ      &&&& r   rU   Z_XMLBase._node_set_content   sJ    %=[F##D*;*;VD-r   c                s   \        V4      pRpV P                  V4      pV'       g   \        P                  ! RV,          4      hVP                  R,           F  pTpVRVP
                  ,          ,          pV P                  V4      pVe   TpK8  V P                  WT4      pV P                  WdV4       TpVP                  '       g   Kq  V P                  WEP                  VP                  4       K  	  V# )a  
Build all nodes for the passed xpath. For example, if XML is <foo/>,
and xpath=./bar/@baz, after this function the XML will be:

  <foo>
    <bar baz=''/>
  </foo>

And the node pointing to @baz will be returned, for the caller to
do with as they please.

There's also special handling to ensure that setting
xpath=./bar[@baz='foo']/frob will create

  <bar baz='foo'>
    <frob></frob>
  </bar>

Even if <bar> didn't exist before. So we fill in the dependent property
expression values
r\   z'Did not find XML root node for xpath=%sr   z/%s)r   r4   r    ZDevErrorr   r   rA   rD   r   r>   r   )	r   r   rQ   rB   r@   r?   ZoldxpathZtmpnoderC   s	   &&       r   rW   Z_XMLBase._node_make_stub   s    , )$ZZ,
""#Ly#XYY ))"--H"H58#7#777Kjj-G"$
nnX:G  w? J &&&''
4K4KXMcMcd! .$ r   c                s6   \        V4      pVP                  R,          pRpV'       dq   \         P                  V4      pVP                  4        TpV P	                  V4      pVf   R# Vf   KL  V P                  V4      '       d   R# V P                  WF4       Kx  R# )z
Walk backwards up the xpath chain, and remove each element
if it doesn't have any children or attributes, so we don't
leave stale elements in the XML
:NNNN)r   r   r!   Zpopr4   rK   rF   )r   r   rQ   r   parentr"   Zchilds   &&     r   rV   Z_XMLBase._node_remove_empty  s     )$$$Q'KK)ELLNEZZ&F~}%%e,,##F2 r    N)$r   r   r   r   r*   Zclassmethodr,   r/   r3   r4   r7   r8   r;   r<   r>   rA   rD   rF   rG   rJ   rK   rL   rM   rN   rP   rS   rY   rZ   r[   rT   r^   rU   rW   rV   r   r   r   r   r   r)   r)   W   s     J% %$$$$$$$$$$$$$$$$$=	)8$9
17

..`3 3r   r)   c                 sF    \        T ;'       d    V P                  R 8H  4      # )Ztext)Zbooltype)nr&   r   node_is_textre   *  s    &&aff&''r   c                   s   a  ] tR tRt o R tR tR tR tR tR t	R t
R	 tR
 tR tR tR tR tR tR tR tR tR tR tRtV tR# )_Libxml2APIi.  c                s   \         P                  V 4       \        P                  ! ^4       \        P                  ! V4      V n        V P
                  P                  4       V n        V P                  P                  V P
                  P                  4       V P                  P                  4        F   w  r#V P                  P                  W#4       K"  	  R# )r   N)r)   r   libxml2ZkeepBlanksDefaultparseDoc_docZxpathNewContext_ctxZsetContextNodechildrenr*   ZitemsZxpathRegisterNs)r   rI   ZkeyZvals   &&  r   r   Z_Libxml2API.__init__/  s    $ 	!!!$$$S)	II--/			  !3!34--/HCII%%c/ 0r   c                s    \        V R 4      '       g   R# V P                  P                  4        RV n        V P                  P	                  4        RV n        R# )ri   N)Zhasattrri   ZfreeDocrj   ZxpathFreeContextr'   r&   r   __del__Z_Libxml2API.__del__>  s@    tV$$					""$	r   c                sT    VP                  R 4      '       g   R V9   d
   VR ,          pV# )
)ZendswithrH   r2   r   rN   Z_Libxml2API._sanitize_xmlG  s%    ||D!!dck4KC
r   c                s\    \        V P                  P                  P                  4       4      # r%   )rf   ri   rk   	serializer'   r&   r   r/   Z_Libxml2API.copy_apiL  s     499--779::r   c                s.   \        V4      P                  p V P                  P                  V4      pT;'       d    T^ ,          ;'       g    R#   \         d;   p\
        P                  ! R YRR7       \        T: R\        T4      : 24      RhRp?ii ; i)z!fullxpath=%s xpath=%s eval failedT)Zexc_infoZ N)	r   r"   rj   	xpathEvalZ	Exceptionr   Zdebugr]   r_   )r   r   r"   r6   Zer   r   r4   Z_Libxml2API._findO  s    y!''	H99&&u-D Q''4'  	HII99VZ[)SV<=4G	Hs   A B5BBc                sJ    \        V P                  P                  V4      4      # r%   )Zlenrj   ro   r1   r2   r   r3   Z_Libxml2API.countX  s    499&&u-..r   c                s"    VP                  4       # r%   )rn   r5   r2   r   r7   Z_Libxml2API._node_tostring[  s    ~~r   c                sB    \         P                  ! V4      P                  # r%   )rg   rh   rk   rH   r2   r   rJ   Z_Libxml2API._node_from_xml^  s    $---r   c                    VP                   # r%   )contentr5   r2   r   r8   Z_Libxml2API._node_get_texta  s    ||r   c                s\    Ve   \         P                  ! V4      pVP                  V4       R # r%   )r    Z
xml_escape
setContentr9   r+   r   r;   Z_Libxml2API._node_set_textd  s$    ''/Fr   c                sP    VP                  V4      pV'       d   VP                  # R # r%   )hasProprq   )r   r6   r    proprX   r   r<   Z_Libxml2API._node_get_propertyi  s"    ||H%<< r   c                s    Vf>   VP                  V4      pV'       d#   VP                  4        VP                  4        R # R # VP                  W#4       R # r%   )rs   
unlinkNodefreeNodeZsetProp)r   r6   r    r:   rt   r`   r   r>   Z_Libxml2API._node_set_propertyn  s>    ><<)D!  LL*r   c                s.  aa \         P                  ! SP                  4      pSP                  '       g   V# VV3R  lpV! 4       pV'       g8   VP	                  V P
                  SP                  ,          SP                  4      pVP                  V4       V# )c                  s   < Sp V '       d]   \         P                  ! V P                  4       4       F"  pVP                  SP                  8X  g   K   Vu # 	  V P                  4       p Kd  R # r%   )r    ZlistifyZnsDefsnamer   Z
get_parent)ra   nsr@   r?   s     r   _find_parent_nsZ._Libxml2API._node_new.<locals>._find_parent_ns|  sN    F!//&--/:Bww(//1!	 ;  **,	 r   )rg   ZnewNoder   r   ZnewNsr*   ZsetNs)r   r?   r@   rC   ry   rx   s   &ff   r   rA   Z_Libxml2API._node_neww  si    //("3"34N	- tx?QBbr   c                s    V P                  V4      pV'       d\   VP                  ;'       g    .  Uu. uF  q3P                  NK  	  ppV F  pVP                  V4       K  	  VP	                  R 4       R # R # u upi r%   )r4   
propertiesrw   Z	unsetProprr   )r   r"   r6   ZpZ	propnamesr   r   rM   Z_Libxml2API.node_clear  sd    zz% *.//*?*?R*?A*?A*?IAq! OOD!	 As   A7c                sp    VP                   R 8H  ;'       d!    VP                  ;'       g    VP                  # )Zelement)rc   rk   rz   r5   r2   r   rK   Z_Libxml2API._node_has_content  s)    yyI%LL4==+K+KDOOLr   c                rp   r%   )rw   r5   r2   r   rL   Z_Libxml2API._node_get_name  s    yyr   c                sd   TpVP                  4       p\        V4      '       d!   VP                  4        VP                  4        VP                  4        VP                  4        \	        VP
                   Uu. uF  p\        V4      NK  	  up4      '       d   VP                  R 4       R # R # u upi r%   )get_prevre   ru   rv   Zallrk   rr   )r   r@   rE   r6   Zwhiterd   s   &&&   r   rF   Z_Libxml2API._node_remove_child  s     NN)<)<=)<AQ)<=>>!!$' ?=s   7B-c                s   Tp\        VP                  4       4      '       gj   VP                  4       p\        V4      '       d"   \        P                  ! VP
                  4      pM\        P                  ! R 4      pVP                  V4       VP                  4       P
                  pVP                  \        P                  ! R4      4       VP                  V4       VP                  \        P                  ! V4      4       R# )rm   z  N)re   Zget_lastr{   rg   ZnewTextrq   ZaddChild)r   rB   r@   rC   ZignoreZprevsibZnewlastZendtexts   &&&&    r   rD   Z_Libxml2API._node_add_child  s    J//122 ))+GG$$!//'//:!//$/(%%'//GOOD12G$GOOG45r   c                sJ    V P                  V4      pVP                  V4       R # r%   )r4   ZreplaceNode)r   r"   rC   ZoldnoderX   r   rG   Z_Libxml2API._node_replace_child  s    **U#G$r   )rj   ri   N)r   r   r   r   r   rl   rN   r/   r4   r3   r7   rJ   r8   r;   r<   r>   rA   rM   rK   rL   rF   rD   rG   r   r   r   r   r   rf   rf   .  sj     0
;(/ . 
 
+&"M(6% %r   rf   )rg   rO   r    Zloggerr   r   r   r)   re   rf   ZXMLAPIrb   r   r   <module>r|      sO      
'B 'BT- -<P3 P3f(I%( I%X 
r   