HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/__pycache__/xmlutil.cpython-310.pyc
o

�N�ge7�@s�dZddlZddlZddlmZddlZdd�Zdd�Zdd	�Z	d!dd�Z
d
d�Zdd�Zd"dd�Z
dd�Zd"dd�Zd#dd�Zd"dd�Zdd�Zdd�Zdd �ZdS)$z
Various XML utilities
�N)�ElementTreecCs"d|vr|�d�}|d}|S|S)z�
    If this XML tree has an xmlns attribute, then etree will add it
    to the beginning of the tag, like: "{http://path}tag".
    �}�)�split)�x�comps�name�r	�F/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/xmlutil.py�
_conv_names

rcCs�|st|j�}||jiSi}|D]1}t|j�}||vr*|r$t|�||<q|j||<qt||t�s8||g||<||�t|��q|S)z�
    Converts an XML ElementTree to a dictionary that only contains items.
    This is the default behavior in version 2017.7. This will default to prevent
    unexpected parsing issues on modules dependent on this.
    )r�tag�text�_to_dict�
isinstance�list�append)�xmltreer�xmldict�itemr	r	r
rs


rcCs�i}|j��D]\}}|||<q|s"|s|jS|jr"|j|t|j�<|D])}t|j�}||vr6t|�||<q$t||t�sD||g||<||�t|��q$|S)z?
    Returns the full XML dictionary including attributes.
    )	�attrib�itemsr
rr�
_to_full_dictrrr)rrZattrNameZ	attrValuerrr	r	r
r7s 

rFcCs|rt|�St|�S)z�
    Convert an XML tree into a dict. The tree that is passed in must be an
    ElementTree object.
    Args:
        xmltree: An ElementTree object.
        attr: If true, attributes will be parsed. If false, they will be ignored.

    )rr)r�attrr	r	r
�to_dict[s	rcCs�|�d�s
d|��}|�|�}|dur[|d|�d��}|�|�}|dur*t||�}||�d�dd�}t�d|�}|�d�rO|�d�rO|�d�|�d�ini}t�||�d�|�}|S)	a'
    Get an XML node using a path (super simple xpath showing complete node ancestry).
    This also creates the missing nodes.

    The supported XPath can contain elements filtering using [@attr='value'].

    Args:
        node: an Element object
        xpath: simple XPath to look for.
    �./N�/rz>(?P<tag>[^[]+)(?:\[@(?P<attr>\w+)=["'](?P<value>[^"']+)["']])?r�valuer)	�
startswith�find�rfind�get_xml_node�re�match�grouprZ
SubElement)�node�xpath�resZparent_xpath�parentZsegment�matcherrr	r	r
r js*




����r cCst|�|_dS)z�
    Function to use in the ``set`` value in the :py:func:`change_xml` mapping items to set the text.
    This is the default.

    :param node: the node to set the text to
    :param value: the value to set
    N)�strr
)r$rr	r	r
�
set_node_text�sr*cCsz|jduo	|j��}|�|�}d}t|j��|pg�dkr-t|�s-|s-|r-|�|�d}|dur;t|||�}|p:|}|S)a�
    Remove the node from its parent if it has no attribute but the ignored ones, no text and no child.
    Recursively called up to the document root to ensure no empty node is left.

    :param parent_map: dictionary mapping each node to its parent
    :param node: the node to clean
    :param ignored: a list of ignored attributes.
    :return: True if anything has been removed, False otherwise
    NFrT)	r
�strip�get�lenr�keysr�remove�
clean_node)�
parent_mapr$�ignoredZhas_textr'ZremovedZparent_cleanedr	r	r
r0�s 

���
r0cCs ||}|�|�t||�dS)z�
    Function to use as ``del`` value in the :py:func:`change_xml` mapping items to remove the text.
    This is the default function.
    Calls :py:func:`clean_node` before returning.
    T)r/r0)r1r$r'r	r	r
�del_text�s

r3cs��fdd�}|S)a�
    Helper returning a function to use as ``del`` value in the :py:func:`change_xml` mapping items to
    remove an attribute.

    The generated function calls :py:func:`clean_node` before returning.

    :param attribute: the name of the attribute to remove
    :param ignored: the list of attributes to ignore during the cleanup

    :return: the function called by :py:func:`change_xml`.
    cs,�|��vrdS|j���t||��dS)NFT)r.r�popr0)r1r$��	attributer2r	r
�
_do_delete�s
z!del_attribute.<locals>._do_deleter	)r6r2r7r	r5r
�
del_attribute�s
r8cs<||�fdd��fdd�tjj��|�d�}|r||d<|S)az
    Helper function creating a change_xml mapping entry for a text XML attribute.

    :param path: the path to the value in the data
    :param xpath: the xpath to the node holding the attribute
    :param attr_name: the attribute name
    :param ignored: the list of attributes to ignore when cleaning up the node
    :param convert: a function used to convert the value
    cs
|���S�N�r,��n��	attr_namer	r
�<lambda>�s
zattribute.<locals>.<lambda>c�|��t|��Sr9��setr)�r<�vr=r	r
r?����pathr%r,rB�del�convert��salt�utilsZxmlutilr8)rGr%r>r2rI�entryr	r=r
r6�s

�r6cs,||�fdd��fdd�tjj��|�d�S)aI
    Helper function creating a change_xml mapping entry for a text XML integer attribute.

    :param path: the path to the value in the data
    :param xpath: the xpath to the node holding the attribute
    :param attr_name: the attribute name
    :param ignored: the list of attributes to ignore when cleaning up the node
    cs|���rt|����SdSr9)r,�intr;r=r	r
r?��zint_attribute.<locals>.<lambda>cr@r9rArCr=r	r
r?�rErFrJ)rGr%r>r2r	r=r
�
int_attribute�s


�rPcsd}|D�]}tjj�||dg�}|d���d�s d����dd�|d�d�D�}dd	�|D�}t����|�}t	�
d
d|�}t�}	|D]���d}
t|
t
�sWt|
t�rXqG|
d
ur��fdd	�|D����fdd	�|D�}t����|�}t||�}|	�|�|�ddd��}
|�dt�}|
|�}|�d�}|r�||
�}
|�ddd��}|||
�s�|||
�d}qG|�|�}|�dt�}dd	�|��D�}|D]}|||�}|p�|}q�qG|�r
dd�|D��r
t|�|��}||	}|�dt�}dd	�|��D�}|D]}|||�}|�p|}q�q|S)a�
    Change an XML ElementTree document according.

    :param doc: the ElementTree parsed XML document to modify
    :param data: the dictionary of values used to modify the XML.
    :param mapping: a list of items describing how to modify the XML document.
        Each item is a dictionary containing the following keys:

        .. glossary::
            path
                the path to the value to set or remove in the ``data`` parameter.
                See :py:func:`salt.utils.data.get_value <salt.utils.data.get_value>` for the format
                of the value.

            xpath
                Simplified XPath expression used to locate the change in the XML tree.
                See :py:func:`get_xml_node` documentation for details on the supported XPath syntax

            get
                function gettin the value from the XML.
                Takes a single parameter for the XML node found by the XPath expression.
                Default returns the node text value.
                This may be used to return an attribute or to perform value transformation.

            set
                function setting the value in the XML.
                Takes two parameters for the XML node and the value to set.
                Default is to set the text value.

            del
                function deleting the value in the XML.
                Takes two parameters for the parent node and the node matched by the XPath.
                Returns True if anything was removed, False otherwise.
                Default is to remove the text value.
                More cleanup may be performed, see the :py:func:`clean_node` function for details.

            convert
                function modifying the user-provided value right before comparing it with the one from the XML.
                Takes the value as single parameter.
                Default is to apply no conversion.

    :return: ``True`` if the XML has been modified, ``False`` otherwise.
    FrGr%rcSs,g|]}|�d�r|�d�r|dd��qS)�{rr���)r�endswith)�.0�sr	r	r
�
<listcomp>5s��
�zchange_xml.<locals>.<listcomp>�:cSsi|]}|d�qS)z$$$r	�rT�placeholderr	r	r
�
<dictcomp>;szchange_xml.<locals>.<dictcomp>z"(?:=['"]\$\$\$["'])|(?:\[\$\$\$\])�rNcs.i|]}|d|�d��vrdd�ndd��qS)z[$�]cSs|dS)Nrr	r;r	r	r
r?Nsz'change_xml.<locals>.<dictcomp>.<lambda>cSs|Sr9r	r;r	r	r
r?Nsr	)rT�p)r%r	r
rZMs ��cs"i|]
}|�|��|d���qS)r[r:rX)�
converters�
value_itemr	r
rZQs�
��r,cSs|jSr9)r
r;r	r	r
r?\szchange_xml.<locals>.<lambda>rBrI�equalscSst|�t|�kSr9)r))�or<r	r	r
r?frETrHcS�i|]
}|D]}||�qqSr	r	�rTr]�cr	r	r
rZmrOcSsg|]}|�d�gkr|�qS)rr:)rTrDr	r	r
rVsscSrbr	r	rcr	r	r
rZwrO)rKrL�data�	get_valuerr�string�Template�
substituter!�subrBrr�dictr �addr,r*�findallr3�iter)�docre�mappingZneed_update�param�valuesZplaceholders�ctxZall_nodes_xpathZ
kept_nodesZ	new_valueZ
node_xpathr$Zget_fnZset_fnZ
current_valueZ
convert_fnZ
compare_fnZnodesZdel_fnr1ZdeletedZ	all_nodes�	to_remover	)r^r_r%r
�
change_xml�sr,


��
��




�


�
�rucCs`|jdur|j�d�|_|jdur|j�d�|_z|D]}t|�qW|Sty/td��w)z�
    Remove all spaces and line breaks before and after nodes.
    This helps comparing XML trees.

    :param node: the XML node to remove blanks from
    :return: the node
    Nz 	
zFailed to recurse on the node)�tailr+r
�strip_spaces�RecursionError�	Exception)r$�childr	r	r
rw~s
	

���rwcCstjj�t�|��S)z-
    Serialize an XML node into a string
    )rKrLZstringutilsZto_strrZtostring)r$r	r	r
�element_to_str�sr{)Fr9)NN)�__doc__r!rgZ	xml.etreerZsalt.utils.datarKrrrrr r*r0r3r8r6rPrurwr{r	r	r	r
�<module>s(
$"