
    Vhu                     (   d dl mZmZmZ eZdZdZd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Z	 d dlmZmZ d	Zd dlmZmZmZ d dlmZmZ d dl m!Z!m"Z" d dl#m$Z$ dZ%e%dz   e%z   dz   e%z   Z&dZ' ejP                  de&z   dz         Z) ejP                  de&z   dz   e'z   dz         Z* ejP                  de&z   dz         Z+ ejP                  de&z   dz   e'z   dz         Z, ejP                  de&z   dz         Z- ejP                  de'z   dz         Z.d Z/d Z0d Z1d Z2d  Z3d! Z4d" Z5d# Z6d$ Z7d% Z8d& Z9d' Z:d( Z;d) Z<d* Z=d+ Z>d, Z?d- Z@d. ZAd/ ZBd0 ZCd1 ZDd8d2ZEd3 ZFd
d4d  eG       fd5ZHd6 ZIeJd7k(  r eI        yy# e$ r  ej0                         Zd
ZY Pw xY w)9    )absolute_importdivisionprint_functiona  
module: xml
short_description: Manage bits and pieces of XML files or strings
description:
  - A CRUD-like interface to managing bits of XML files.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
options:
  path:
    description:
      - Path to the file to operate on.
      - This file must exist ahead of time.
      - This parameter is required, unless O(xmlstring) is given.
    type: path
    aliases: [dest, file]
  xmlstring:
    description:
      - A string containing XML on which to operate.
      - This parameter is required, unless O(path) is given.
    type: str
  xpath:
    description:
      - A valid XPath expression describing the item(s) you want to manipulate.
      - Operates on the document root, V(/), by default.
    type: str
  namespaces:
    description:
      - The namespace C(prefix:uri) mapping for the XPath expression.
      - Needs to be a C(dict), not a C(list) of items.
    type: dict
    default: {}
  state:
    description:
      - Set or remove an xpath selection (node(s), attribute(s)).
    type: str
    choices: [absent, present]
    default: present
    aliases: [ensure]
  attribute:
    description:
      - The attribute to select when using parameter O(value).
      - This is a string, not prepended with V(@).
    type: raw
  value:
    description:
      - Desired state of the selected attribute.
      - Either a string, or to unset a value, the Python V(None) keyword (YAML Equivalent, V(null)).
      - Elements default to no value (but present).
      - Attributes default to an empty string.
    type: raw
  add_children:
    description:
      - Add additional child-element(s) to a selected element for a given O(xpath).
      - Child elements must be given in a list and each item may be either a string (for example C(children=ansible) to add
        an empty C(<ansible/>) child element), or a hash where the key is an element name and the value is the element value.
      - This parameter requires O(xpath) to be set.
    type: list
    elements: raw
  set_children:
    description:
      - Set the child-element(s) of a selected element for a given O(xpath).
      - Removes any existing children.
      - Child elements must be specified as in O(add_children).
      - This parameter requires O(xpath) to be set.
    type: list
    elements: raw
  count:
    description:
      - Search for a given O(xpath) and provide the count of any matches.
      - This parameter requires O(xpath) to be set.
    type: bool
    default: false
  print_match:
    description:
      - Search for a given O(xpath) and print out any matches.
      - This parameter requires O(xpath) to be set.
    type: bool
    default: false
  pretty_print:
    description:
      - Pretty print XML output.
    type: bool
    default: false
  content:
    description:
      - Search for a given O(xpath) and get content.
      - This parameter requires O(xpath) to be set.
    type: str
    choices: [attribute, text]
  input_type:
    description:
      - Type of input for O(add_children) and O(set_children).
    type: str
    choices: [xml, yaml]
    default: yaml
  backup:
    description:
      - Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered
        it incorrectly.
    type: bool
    default: false
  strip_cdata_tags:
    description:
      - Remove CDATA tags surrounding text values.
      - Note that this might break your XML file if text values contain characters that could be interpreted as XML.
    type: bool
    default: false
  insertbefore:
    description:
      - Add additional child-element(s) before the first selected element for a given O(xpath).
      - Child elements must be given in a list and each item may be either a string (for example C(children=ansible) to add
        an empty C(<ansible/>) child element), or a hash where the key is an element name and the value is the element value.
      - This parameter requires O(xpath) to be set.
    type: bool
    default: false
  insertafter:
    description:
      - Add additional child-element(s) after the last selected element for a given O(xpath).
      - Child elements must be given in a list and each item may be either a string (for example C(children=ansible) to add
        an empty C(<ansible/>) child element), or a hash where the key is an element name and the value is the element value.
      - This parameter requires O(xpath) to be set.
    type: bool
    default: false
requirements:
  - lxml >= 2.3.0
notes:
  - Use the C(--check) and C(--diff) options when testing your expressions.
  - The diff output is automatically pretty-printed, so may not reflect the actual file content, only the file structure.
  - This module does not handle complicated xpath expressions, so limit xpath selectors to simple expressions.
  - Beware that in case your XML elements are namespaced, you need to use the O(namespaces) parameter, see the examples.
  - Namespaces prefix should be used for all children of an element where namespace is defined, unless another namespace is
    defined for them.
seealso:
  - name: XML module development community wiki (archived)
    description: More information related to the development of this xml module.
    link: https://github.com/ansible/community/wiki/Module:-xml
  - name: Introduction to XPath
    description: A brief tutorial on XPath (w3schools.com).
    link: https://www.w3schools.com/xml/xpath_intro.asp
  - name: XPath Reference document
    description: The reference documentation on XSLT/XPath (developer.mozilla.org).
    link: https://developer.mozilla.org/en-US/docs/Web/XPath
author:
  - Tim Bielawa (@tbielawa)
  - Magnus Hedemark (@magnus919)
  - Dag Wieers (@dagwieers)
a?  
# Consider the following XML file:
#
# <business type="bar">
#   <name>Tasty Beverage Co.</name>
#     <beers>
#       <beer>Rochefort 10</beer>
#       <beer>St. Bernardus Abbot 12</beer>
#       <beer>Schlitz</beer>
#    </beers>
#   <rating subjective="true">10</rating>
#   <website>
#     <mobilefriendly/>
#     <address>http://tastybeverageco.com</address>
#   </website>
# </business>

- name: Remove the 'subjective' attribute of the 'rating' element
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/rating/@subjective
    state: absent

- name: Set the rating to '11'
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/rating
    value: 11

# Retrieve and display the number of nodes
- name: Get count of 'beers' nodes
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/beers/beer
    count: true
  register: hits

- ansible.builtin.debug:
    var: hits.count

# Example where parent XML nodes are created automatically
- name: Add a 'phonenumber' element to the 'business' element
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/phonenumber
    value: 555-555-1234

- name: Add several more beers to the 'beers' element
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/beers
    add_children:
      - beer: Old Rasputin
      - beer: Old Motor Oil
      - beer: Old Curmudgeon

- name: Add several more beers to the 'beers' element and add them before the 'Rochefort 10' element
  community.general.xml:
    path: /foo/bar.xml
    xpath: '/business/beers/beer[text()="Rochefort 10"]'
    insertbefore: true
    add_children:
      - beer: Old Rasputin
      - beer: Old Motor Oil
      - beer: Old Curmudgeon

# NOTE: The 'state' defaults to 'present' and 'value' defaults to 'null' for elements
- name: Add a 'validxhtml' element to the 'website' element
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/website/validxhtml

- name: Add an empty 'validatedon' attribute to the 'validxhtml' element
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/website/validxhtml/@validatedon

- name: Add or modify an attribute, add element if needed
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/website/validxhtml
    attribute: validatedon
    value: 1976-08-05

# How to read an attribute value and access it in Ansible
- name: Read an element's attribute values
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/website/validxhtml
    content: attribute
  register: xmlresp

- name: Show an attribute value
  ansible.builtin.debug:
    var: xmlresp.matches[0].validxhtml.validatedon

- name: Remove all children from the 'website' element (option 1)
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/website/*
    state: absent

- name: Remove all children from the 'website' element (option 2)
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business/website
    set_children: []

# In case of namespaces, like in below XML, they have to be explicitly stated.
#
# <foo xmlns="http://x.test" xmlns:attr="http://z.test">
#   <bar>
#     <baz xmlns="http://y.test" attr:my_namespaced_attribute="true" />
#   </bar>
# </foo>

# NOTE: There is the prefix 'x' in front of the 'bar' element, too.
- name: Set namespaced '/x:foo/x:bar/y:baz/@z:my_namespaced_attribute' to 'false'
  community.general.xml:
    path: foo.xml
    xpath: /x:foo/x:bar/y:baz
    namespaces:
      x: http://x.test
      y: http://y.test
      z: http://z.test
    attribute: z:my_namespaced_attribute
    value: 'false'

- name: Adding building nodes with floor subnodes from a YAML variable
  community.general.xml:
    path: /foo/bar.xml
    xpath: /business
    add_children:
      - building:
# Attributes
          name: Scumm bar
          location: Monkey island
# Subnodes
          _:
            - floor: Pirate hall
            - floor: Grog storage
            - construction_date: "1990" # Only strings are valid
      - building: Grog factory

# Consider this XML for following example -
#
# <config>
#   <element name="test1">
#     <text>part to remove</text>
#   </element>
#   <element name="test2">
#     <text>part to keep</text>
#   </element>
# </config>

- name: Delete element node based upon attribute
  community.general.xml:
    path: bar.xml
    xpath: /config/element[@name='test1']
    state: absent
a2  
actions:
  description: A dictionary with the original xpath, namespaces and state.
  type: dict
  returned: success
  sample: {xpath: xpath, namespaces: [namespace1, namespace2], state: present}
backup_file:
  description: The name of the backup file that was created.
  type: str
  returned: when O(backup=true)
  sample: /path/to/file.xml.1942.2017-08-24@14:16:01~
count:
  description: The count of xpath matches.
  type: int
  returned: when parameter O(count) is set
  sample: 2
matches:
  description: The xpath matches found.
  type: list
  returned: when parameter O(print_match) is set
msg:
  description: A message related to the performed action(s).
  type: str
  returned: always
xmlstring:
  description: An XML string of the resulting output.
  type: str
  returned: when parameter O(xmlstring) is set
N)BytesIO)LooseVersion)etree	objectifyTF)AnsibleModulejson_dict_bytes_to_unicodemissing_required_lib)	iteritemsstring_types)to_bytes	to_native)MutableMappingz[a-zA-Z-][a-zA-Z0-9_\-\.]*|:z('(?:.*)'|"(?:.*)")z^(.*)/(z)$z)/text\(\)=$z^(.*)/(@(?:z))$z))=z
)\[(.*)\]$z^(.*)/text\(\)=c                     t        j                  t        j                  t        j                  t                          }t        j                  t        j                  t        j                  |                   }||k7  S N)r   tostringr	   
fromstringorig_doc)docorig_objobjs      i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/xml.pyhas_changedr     sP    ~~i225>>(3KLMH
..--ennS.AB
CCO    c                     |j                  ||      }g }|D ]"  }|j                  |j                  |             $ t        j                  |      }d|d|}t        | |||d|       y )N
namespacesz
selector 'z	' match: F)changedmsg)xpathappendgetpathjsondumpsfinish)	moduletreer%   r"   matchmatch_xpathsm	match_strr$   s	            r   do_print_matchr1     sh    JJuJ4EL -DLLO,-

<(I',i
8C
64
EsCr   c                 p    |j                  d|z  |      }d|z  }t        | |||d|t        |             y)z. Return the count of nodes matching the xpath z
count(/%s)r!   zfound %d nodesF)r#   r$   hitcountN)r%   r*   int)r+   r,   r%   r"   hitsr$   s         r   count_nodesr6     s<    ::lU*z:BD
T
!C
64
EsSQUYWr   c                     t        | ||      r1| j                  ||      }t        |d   t        j                        ryy)z Test if a given xpath matches anything and if that match is a node.

    For now we just assume you're only searching for one specific thing.r!   r   TF)xpath_matchesr%   
isinstancer   _Element)r,   r%   r"   r-   s       r   is_noder;     s:     T5*-

5Z
8eAh/r   c                     t        t        dd      }t        | ||      rC| j                  ||      }t	        |d   t        j
                        ry|t	        |d   |      ryy)zu Test if a given xpath matches and that match is an attribute

    An xpath attribute search will only match one item_ElementStringResultNr!   r   TF)getattrr   r8   r%   r9   _ElementUnicodeResult)r,   r%   r"   ElementStringResultr-   s        r   is_attributerA     sc     "%)?FT5*-

5Z
8eAh ; ;< ,E!HFY1Zr   c                 ,    | j                  ||      ryy)z Test if a node exists r!   TF)r%   )r,   r%   r"   s      r   r8   r8     s    zz%Jz/r   c                    d}	 |j                  ||      D ]~  }d}t        |||      r6|j                         }|j                  j	                  |j
                         Ht        |||      r |j                         j                  |       ut        d       	 t        | ||||       y
# t        $ r#}| j                  d|d|d	       Y d
}~y
d
}~ww xY w)z, Delete an attribute or element from a tree Fr!   TzImpossible errorr#   zCouldn't delete xpath target:  ()r$   N)r%   rA   	getparentattribpopattrnamer;   remove	Exceptionr*   	fail_json)r+   r,   r%   r"   r#   resultparentes           r   delete_xpath_targetrR     s    GAjj:j> 	4FGD%4))+ !!&//2uj1  "))&1 233	4$ 	vtUJ@  SqQRRSs   BB* *	C3CCc                 h    t        |      D ]  }|j                  |        |j                  |        y r   )listrL   extend)childrenr-   elements      r   replace_children_ofrX     s-    ; W	LLr   c                    |j                  ||      }t        | ||      }|D cg c]  }t        j                  |       }}d}	|D ]x  }
t	        t        |
            t	        |      k(  rHt        t        |
            D ]0  \  }}t        j                  |      ||   k7  s"t        ||
       d}	 i kt        ||
       d}	z |	S c c}w )Nr!   FT)r%   children_to_nodesr   r   lenrT   	enumeraterX   )r+   r,   r%   r"   rV   in_typematchescchildren_as_stringr#   r-   idxrW   s                r   set_target_children_innerrb     s    jj:j6G !7;H5=>%..+>>G  
tE{s8}, )$u+ 6 W>>'*.@.EE'%8"G	  %0G
 N# ?s   Cc                 D    t        | |||||      }t        | ||||       y )NrD   )rb   r*   )r+   r,   r%   r"   rV   r]   r#   s          r   set_target_childrenrd     s'    'eZSZ[G
64
G<r   c                     t        |||      r\t        | ||      }|s|rt        ||||||       n)|j                  ||      D ]  }	|	j	                  |        t        | |||d       y t        | |||       y )Nr!   TrD   )r;   rZ   insert_target_childrenr%   rU   r*   )
r+   r,   r%   r"   rV   r]   insertbeforeinsertafternew_kidsnodes
             r   add_target_childrenrk     sz    tUJ'$VXw?;"4
HlT_`

5Z
@ &H%&vtUJ=vtUJ/r   c                     | j                  ||      }|rdnd}||   j                         j                  ||         }|d   j                         }	|r|dz  }|D ]  }
|	j                  ||
       |dz  } y)z
    Insert the given children before or after the given xpath. If insertbefore is True, it is inserted before the
    first xpath hit, with insertafter, it is inserted after the last xpath hit.
    r!   r      N)r%   rH   indexinsert)r,   r%   r"   rV   rg   rh   insert_target	loc_indexindex_in_parentrP   childs              r   rf   rf     s    
 JJuJ<M!rI#I.88:@@yAYZO1'')F1 ou-1r   c                     | dd S )Nrn   rm    )gs    r   _extract_xpstrrx   $  s    Qr7Nr   c                    | j                         } t        j                  |       }|r%|j                  d      |j                  d      dfgfS t        j                  |       }|r=|j                  d      |j                  d      t        |j                  d            fgfS t        j                  |       }|r%|j                  d      |j                  d      dfgfS t        j                  |       }|r=|j                  d      |j                  d      t        |j                  d            fgfS t        j                  |       }|rc|j                  d      j                  d      D cg c]  }|j                          }}|j                  d      d|j                  d      z   |fgfS t        j                  |       }|r.|j                  d      dt        |j                  d            fgfS | g fS c c}w )z=split an XPath of the form /foo/bar/baz into /foo/bar and bazrn      N   z and / )strip_RE_SPLITSIMPLELASTr-   group_RE_SPLITSIMPLELASTEQVALUErx   _RE_SPLITSIMPLEATTRLAST_RE_SPLITSIMPLEATTRLASTEQVALUE_RE_SPLITSUBLASTsplit_RE_SPLITONLYEQVALUE)r%   r/   xcontents       r   split_xpath_lastr   (  s   KKME!!%(A
aggaj$/011"((/A
aggaj.*DEFGG%%e,A
aggaj$/011&,,U3A
aggaj.*DEFGGu%A&'ggaj&6&6w&?@1779@@
cAGGAJ.89::""5)A
b."<=>??2; As   *G4c                 `    d| v r)| j                  d      \  }}dj                  ||   |      S | S )Nr   
{{{0}}}{1})r   format)namer"   nsnamerawnames       r   nsnameToClarkr   I  s:    
d{ JJsO"":f#5w?? Kr   c           
      \   t        |      \  }}||k(  s|.| j                  d|dt        j                  |d             yd}t	        |||      st        | |||      }t	        |||      r|r|D ]  \  }}|rd|d   dk7  r\|d   d	k7  rTt        | t        ||      gd
      }	|r|	D ]	  }
||
_         |j                  ||      D ]  }|j                  |	       d} m|rp|d   d	k(  rh|dd  }t        | t        ||      gd
      }	|j                  ||      D ]4  }|j                  |	       |	D ]  }
|D ]  }t        | |
d|z   |         d}6 |dk(  r3|j                  ||      D ]  }|j                  |k7  s||_        d} |rr|d   dk(  rjt        |dd  |      }|j                  ||      D ]C  }||j                  vxs |j                  |   |k7  }|s(|xs |}|d}n|}||j                  |<   E | j                  dt        j                  |d      z          |S )NzCan't process Xpath z" in order to spawn nodes! tree is Tpretty_printrG   Fr   @r|   yamlr!   rn   z./r}   zunknown tree transformation=%s)r   rN   r   r   r;   check_or_make_targetrZ   r   textr%   rU   rI   )r+   r,   r%   r"   inner_xpathchangesr#   eoa	eoa_valueri   nkrj   rW   subexpr	attributechangingvalues                    r   r   r   S  s   -e4['u'/#U^^Dt%LN 	OG4j1&vt[*M t[*-' ' 1	qS)s1v}Q3,VmC6T5UW]^& ,"+, !JJ{zJJ #DKK)"G# Q3ab',VmGZ6X5Y[ab JJ{zJJ #DKK)& Y'0 YG 1TG^ZXYY
 #G#  JJ{zJJ 'D		Y.$-	"&'
 Q3)#ab':>	#zz+*zM 	:G ) ? i7>>R[C\`iCiH")"5X$,$&E$-E49y1	:   %EW[jnHo%o pc1	qf Nr   c                 \    d}t        |||      st        | |||      }t        | ||||       y )NF)r;   r   r*   )r+   r,   r%   r"   r#   s        r   ensure_xpath_existsr     s3    G4
+&vtUJG
64
G4r   c                    d}	 t        |||      st        | |||      }t        |||      s-| j                  d|dt        j                  |d	             |j                  ||      D ]  }	|s+|xs |	j                  |k7  }|	j                  |k7  s(||	_        0|xs |	j                  |      |k7  }d|v r)|j                  d      \  }
}dj                  ||
   |      }|	j                  |      |k7  s|	j                  ||        |S # t        $ rr}d}|j                         j                  rd|vrd}| j                  |d|d|dt        j                  |d	      t        j                         
       Y d }~cd }~ww xY w)NFr}   r   zRXML document has namespace(s) defined, but no namespace prefix(es) used in xpath!
zXpath z causes a failure: z
  -- tree is Tr   r$   	exceptionz$ does not reference a node! tree is rG   r!   r   )r;   r   rM   getrootnsmaprN   r   r   	traceback
format_excr%   r   getr   r   set)r+   r,   r%   r"   r   r   r#   rQ   missing_namespacerW   attr_ns	attr_names               r   set_target_innerr     s   G
GtUJ/*64
KG 4
+#U^^Dt%LN 	O ::e
:; .8',,%"7G||u$$B'++i"8E"AGi%.__S%9"(//
70CYO	{{9%.Iu-. N7  G <<>Cu$4 u/5>>$]a;bdox  pD  pD  pF 	 	G 	GGs   D 	FA'FFc                 B    t        | |||||      }t        | ||||       y r   )r   r*   )r+   r,   r%   r"   r   r   r#   s          r   
set_targetr     s&    vtUJ	5QG
64
G4r   c                    t        |||      s| j                  d|z         g }|j                  ||      D ])  }|j                  |j                  |j
                  i       + t        | |||dt        |      t        |      |       y Nz#Xpath %s does not reference a node!rG   r!   F)r#   r$   r3   r^   )r;   rN   r%   r&   tagr   r*   r[   )r+   r,   r%   r"   elementsrW   s         r   get_element_textr     s    4
+BUJKH::e
:; 5gll345 64
Es8}WZ[cWdnvwr   c                 v   t        |||      s| j                  d|z         g }|j                  ||      D ]Z  }i }|j                         D ]&  }|j	                  |      }|j                  ||i       ( |j                  |j                  |i       \ t        | |||dt        |      t        |      |       y r   )
r;   rN   r%   keysr   updater&   r   r*   r[   )	r+   r,   r%   r"   r   rW   rt   keyr   s	            r   get_element_attrr     s    4
+BUJKH::e
:; .<<> 	'CKK$ELL#u&	' 	e,-. 64
Es8}WZ[cWdnvwr   c                    |dk(  rQt        t        |d            }	 t        j                         }t        j                  ||      }|j                         S |dk(  r0t        |t              rt        j                  |      S t        |t              rt        |      dkD  r| j                  d       t        t        |            \  }}t        |t              rw|j                  d	d       }	t        j                  ||      }|	Kt        |	t               s| j                  d
t#        |	      z         t%        | |	      }
|j'                  |
       |S t        j                  |      }||_        |S | j                  dt#        |      z         y | j                  d|z         y # t        j                  $ r}| j                  d|z         Y d }~y d }~ww xY w)Nxmlsurrogate_or_stricterrorsz%Error while parsing child element: %srG   r   rn   z1Can only create children from hashes with one key_z(Invalid children type: %s, must be list.zBInvalid child type: %s. Children must be either strings or hashes.z>Invalid child input type: %s. Type must be either xml or yaml.)r   r   r   	XMLParserparser   XMLSyntaxErrorrN   r9   r   Elementr   r[   nextr   rJ   rT   typerZ   rU   r   )r+   rt   r]   infileparserrj   rQ   r   r   rV   subnodess              r   child_to_elementr     s   %%0EFG	N__&F;;vv.D<<>! 
F	e\*==''~.5zA~  %X Y	% 01LS%%0 99S$/}}S%0'%h5((-WZ^_gZh-h(i0BHKK) K }}S)!	K!ehlmrhs!st]`ggh9 ## 	N!H1!LMM	Ns   9F# #G6GGc                 N    |g n|}|D cg c]  }t        | ||       c}S c c}w )z8turn a str/hash/list of str&hash into a list of elements)r   )r+   rV   r   rt   s       r   rZ   rZ   
  s,    %r8H?GHeVUD1HHHs   "c                 N   t        j                  |dd| j                  d         }t        d      }| j                  d   r| j                  d   }t	        |d      5 }||j                         k7  rcd|d	<   | j                  sR| j                  d
   r!| j                  | j                  d         |d<   |j                  |dd| j                  d          d d d        n+| j                  d   r||d<   || j                  d   k7  rd|d	<    | j                  di | y # 1 sw Y   xY w)NTUTF-8r   xml_declarationencodingr   FrD   pathrbr#   backupbackup_file	xmlstringrv   )
r   r   paramsdictopenread
check_modebackup_localwrite	exit_json)r+   r,   
xml_stringrO   xml_filexml_contents         r   make_prettyr     s-   dW[a[h[hiw[xyJF }}V==((D! 	}[[--//$(y!((}}X.060C0CFMMRXDY0Z}-JJx^d^k^klz^{J|	} 	} 
{	#({{33 $F9Fv	} 	}s   A7DD$r}   c           	         t        t        ||| j                  d         t        |            }| j                  d   s|r||d<   | j                  d   s|r||d<   |r||d<   |d   r| j                  rBt        t	        j
                  t        d	d
d	      t	        j
                  |d	d
d	            |d<   | j                  d   rk| j                  s_| j                  d   r!| j                  | j                  d         |d<   |j                  | j                  d   d	d
| j                  d          | j                  d   r)t	        j
                  |d	d
| j                  d         |d<    | j                  di | y )Nstate)r%   r"   r   )actionsr#   countprint_matchr^   r$   r#   Tr   r   )beforeafterdiffr   r   r   r   r   rv   )r   r   r   _diffr   r   r   r   r   r   r   )	r+   r,   r%   r"   r#   r$   r3   r^   rO   s	            r   r*   r*   +  sr   !--(

 D!F }}W"w}}]#w#y
ui<<!~~hweijnnT4'`deF6N
 == ):):}}X&(.(;(;FMM&<Q(R}%JJv}}V,dWcicpcpq  dAJ  B}}[!#nnT4RYhnhuhu  wE  iF  G{Fvr   c                      t        t        dCi dt        dddg      dt        d      dt        d      d	t        d
i       dt        ddddgdg      dt        d      dt        d      dt        dd      dt        dd      dt        dd      dt        dd      dt        dd      dt        dddg      d t        dd!d"d!g#      d$t        dd      d%t        dd      d&t        dd      d't        dd      d(t        dgdgdgdgdg)      dd(dggdd(dggd&d(dggd'd(dgggddgg d*gg d+ddgd&d'gg,      } | j                  d   }| j                  d   }| j                  d   }| j                  d	   }| j                  d   }t        | j                  d         }| j                  d   }t        | j                  d         }t        | j                  d         }	| j                  d   }
| j                  d   }| j                  d    }| j                  d   }| j                  d   }| j                  d$   }| j                  d%   }| j                  d&   }| j                  d'   }t        s!| j                  t        d-      t        .       nt        d/j                  d0 t        j                  D                    t        d1      k  r| j                  d23       nLt        d/j                  d4 t        j                  D                    t        d5      k  r| j                  d6       d }	 |rt        t        |d78            }nAt        j                   j#                  |      rt%        |d9      }n| j                  d:|z  3       |	 t        j&                  |       	 t        j,                  |
|?      }t        j.                  ||      }|r|j3                          	 t5        j6                        a|rt;        | |||       |rt=        | |||       |dk(  rt?        | |||       n|dk(  rtA        | |||       |dk(  rtC        | |||       |tE        | |||||       |	rtG        | ||||	|||       |tI        | |||||       |tK        | |||       |
rtM        | |       | j                  dB3       y # t        j(                  $ r$}| j                  d;|d<|d=3       Y d }~Kd }~wt        j*                  $ r$}| j                  d>|d<|d=3       Y d }~d }~ww xY w# t        j0                  $ r(}| j                  d@|xs dAd<|d=3       Y d }~d }~ww xY w# |r|j3                          w w xY w)DNr   destfile)r   aliasesr   str)r   r%   r"   r   )r   defaultr   presentabsentensure)r   r   choicesr   r   rawr   add_childrenrT   )r   r   set_childrenr   boolFr   r   r   r   )r   r   
input_typer   r   )r   r   r   r   strip_cdata_tagsrg   rh   T)r   r   r   r   r   )r   r   r   r   r   r   r   )r   r   r   r   r   r   )argument_specsupports_check_moderequired_byrequired_ifrequired_one_ofmutually_exclusivelxmlr   .c              3   2   K   | ]  }t        |        y wr   r   .0fs     r   	<genexpr>zmain.<locals>.<genexpr>       Hy|H   z2.3.0zTThe xml ansible module requires lxml 2.3.0 or newer installed on the managed machinerG   c              3   2   K   | ]  }t        |        y wr   r  r  s     r   r	  zmain.<locals>.<genexpr>  r
  r  z3.0.0z[Using lxml version lower than 3.0.0 does not guarantee predictable element attribute order.r   r   r   z*The target XML source '%s' does not exist.z"Syntax error in xpath expression: rE   rF   z&Evaluation error in xpath expression: )remove_blank_textstrip_cdatazError while parsing document: r   zDon't know what to dorv   )'r
   r   r   r   HAS_LXMLrN   r   LXML_IMP_ERRr   joinr   LXML_VERSIONwarnr   r   osr   isfiler   XPathXPathSyntaxErrorXPathEvalErrorr   r   r   closecopydeepcopyr   r1   r6   r   r   rR   rd   rk   r   r   r   )r+   r   r   r%   r"   r   r   r   r   r   r   r   r   r   r   r   r   rg   rh   r   rQ   r   r   s                          r   mainr  Q  s    
6FF+;<
&
 E"
 4	

 E9x>S^f]gh
 E"
 &
 6E:
 6E:
 FE2
 &%8
 659
 ek6-BC
 P
 VU3
  "vu=!
" 659#
$ &%8%
( !!iI!)
 dWI&D7),TG9-D7),	
 [!h

 Y[!]+
O,F\ }}V$H{+JMM'"E|,JMM'"E&v}}W'=>Ek*I-fmmN.KLL-fmmN.KLL==0LmmI&G|,J--.KMM'"E]]8$F}}%78==0L--.K 1&9\R	chhHU5G5GHH	ILY`La	ast	chhHU5G5GHH	ILY`La	aqrFXj9NOPFWW^^H%(D)F!MPX!XY cE"	j__|QabF++ff-C LLN }}S!HvsE:6FC
3+eZ8	F	eZ8 FC
; FC
L*U FC
L*Vbdop
 63z9eD FC
; FC 
01} )) _  TY[\%] ^^'' c  X]_`%a bbc ## 	jHLdXdLdfg!hii	j LLN sg    AU8 S 3-T: T7S=7U8 =T7T2,U8 2T77U8 :U5U0*U8 0U55U8 8V__main__)NNr   )K
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr  r(   r  rer   ior   Bansible_collections.community.general.plugins.module_utils.versionr   r  r  r   r	   r  ImportErrorr   ansible.module_utils.basicr
   r   r   ansible.module_utils.sixr   r   +ansible.module_utils.common.text.convertersr   r   /ansible.module_utils.common._collections_compatr   _IDENT_NSIDENT_XPSTRcompiler   r   r   r   r   r   r   r1   r6   r;   rA   r8   rR   rX   rb   rd   rk   rf   rx   r   r   r   r   r   r   r   r   r   rZ   r   tupler*   r  __name__rv   r   r   <module>r1     s    C BWr`D
<   	 	   [%H
 g f < K J	&C<& 3&/ 
! bjjX!5!<= 'RZZ	H(<(NQW(WZ](]^ $"**]X%=%EF !+MH,Du,Lv,UX[,[!\ 2::i(2^CD !rzz"5">"DE DX
$A22=
0 BAH5!H5
xx$iNI4 5:rAW\W^ #LV2r zF k  '9'')LHs   
E5 5FF