o
    8^k                     @   s   d Z ddlmZ ddlmZmZ dZdZedkZ	dZ
e	reZneZdd	 Zd
d ZG dd deZG dd deZG dd deZG dd deZG dd deZddgZdS )a  
Augeas is a library for programmatically editing configuration files.
Augeas parses configuration files into a tree structure, which it exposes
through its public API. Changes made through the API are written back to
the initially read files.

The transformation works very hard to preserve comments and formatting
details. It is controlled by *lens* definitions that describe the file
format and the transformation into a tree.
    )version_info)ffilibz/Nathaniel McCallum <nathaniel@natemccallum.com>zJeff Schroeder <jeffschroeder@computer.org>
Harald Hoyer <harald@redhat.com> - initial python bindings, packaging
Nils Philippsen <nils@redhat.com>
)   utf8c                 C      | r|  tS dS N    )encodeAUGENCst r   </opt/certbot/lib/python3.10/site-packages/augeas/__init__.pyenc3      
r   c                 C   r   r   )decoder   r   r   r   r   dec:   r   r   c                          e Zd Z fddZ  ZS )AugeasIOErrorc                    :   || _ tt| j|g|R   || _|| _|| _|| _d S N)messagesuperr   __init__errormsgminordetailsselfecfullmessager   r   r   args	__class__r   r   r   B      
zAugeasIOError.__init____name__
__module____qualname__r   __classcell__r   r   r$   r   r   A       r   c                       r   )AugeasRuntimeErrorc                    r   r   )r   r   r-   r   r   r   r   r   r   r$   r   r   r   L   r&   zAugeasRuntimeError.__init__r'   r   r   r$   r   r-   K   r,   r-   c                       r   )AugeasValueErrorc                    r   r   )r   r   r.   r   r   r   r   r   r   r$   r   r   r   V   r&   zAugeasValueError.__init__r'   r   r   r$   r   r.   U   r,   r.   c                   @   s>  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdd Zdd ZddefddZdd Zdd Z d d! Z!d"d# Z"d$d% Z#d&d' Z$d(d) Z%d*d+ Z&d,d- Z'd.d/ Z(d0d1 Z)dId3d4Z*d5d6 Z+d7d8 Z,d9d: Z-d;d< Z.d=d> Z/d?d@ Z0dJdBdCZ1dKdEdFZ2dGdH Z3dS )LAugeasz/
    Class wrapper for the Augeas library.
    r                      @      r            	   
               c                 C   s   |t jkrd S tt |S r   )r   NULLr   string)r    cffistrr   r   r   _optffistring   s   
zAugeas._optffistringc           	      G   s   t | j}|tjkrt | t | j}|d | | }| t | j}|r0|d| 7 }| t 	| j}|rA|d| 7 }||||||)Nz: )
r   	aug_error_Augeas__handler/   
AUG_ENOMEMMemoryErrorrD   aug_error_messageaug_error_minor_messageaug_error_details)	r    
errorclasserrmsgr#   r!   r   r"   r   r   r   r   r   _raise_error   s   
zAugeas._raise_errorNc                    s   t |ts|durtdt |ts|durtdt |ts#td|r)t|ntj}|r2t|ntj}tt	||| fdd _
 j
sLtddS )a  
        Initialize the library.

        :param root: the filesystem root. If `root` is :py:obj:`None`, use the
                     value of the environment variable :envvar:`AUGEAS_ROOT`.
                     If that doesn't exist either, use :samp:`/`.
        :type root: str or None

        :param loadpath: a colon-separated list of directories that modules
                         should be searched in. This is in addition to the
                         standard load path and the directories in
                         :envvar:`AUGEAS_LENS_LIB`.
        :type loadpath: str or None

        :param flags: a combination of values of :attr:`SAVE_BACKUP`,
                      :attr:`SAVE_NEWFILE`, :attr:`TYPE_CHECK`,
                      :attr:`NO_STDINC`, :attr:`SAVE_NOOP`, :attr:`NO_LOAD`,
                      :attr:`NO_MODL_AUTOLOAD`, and :attr:`ENABLE_SPAN`.
        :type flags: int or :attr:`NONE`
        Nzroot MUST be a string or None!z"loadpath MUST be a string or None!zflag MUST be a flag!c                    s    j S r   )close)xr    r   r   <lambda>   s    z!Augeas.__init__.<locals>.<lambda>zUnable to create Augeas object!)
isinstancestring_types	TypeErrorintr   r   rA   gcr   aug_initrF   RuntimeError)r    rootloadpathflagsr   rQ   r   r      s   

zAugeas.__init__c                 C   b   t |ts	td| jstdtdd}t| jt	||}|dk r*| 
td | |d S )z
        Lookup the value associated with `path`.
        It is an error if more than one node matches `path`.

        :returns: the value at the path specified
        :rtype: str
        path MUST be a string!*The Augeas object has already been closed!char*[]r0   r   zAugeas.get() failed)rS   rT   rU   rF   rY   r   newr   aug_getr   rN   r.   rD   r    pathvalueretr   r   r   get      

z
Augeas.getc                 C   r]   )z
        Lookup the label associated with `path`.
        It is an error if more than one node matches `path`.

        :returns: the label of the path specified
        :rtype: str
        r^   r_   r`   r0   r   zAugeas.label() failed)rS   rT   rU   rF   rY   r   ra   r   	aug_labelr   rN   r.   rD   )r    rd   labelrf   r   r   r   rj      rh   zAugeas.labelc                 C   sn   t |ts	tdt |ts|durtd| jstdt| jt|t|}|dkr5| t	d dS dS )z
        Set the value associated with `path` to `value`.
        Intermediate entries are created if they don't exist.
        It is an error if more than one node matches `path`.
        r^   Nvalue MUST be a string or None!r_   r   zAugeas.set() failed)
rS   rT   rU   rF   rY   r   aug_setr   rN   r.   rc   r   r   r   set   s   
z
Augeas.setc                 C   s   t |tkr
tdt |tkr|durtdt |tkr&|dur&td| js-tdt| jt|t|t|}|dk rF| t	d |S )a5  
        Set the value of multiple nodes in one operation.
        Find or create a node matching `sub` by interpreting `sub`
        as a path expression relative to each node matching `base`.
        `sub` may be :py:obj:`None`, in which case all the nodes matching
        `base` will be modified.
        zbase MUST be a string!Nzsub MUST be a string or None!rk   r_   r   zAugeas.setm() failed)
typestrrU   rF   rY   r   aug_setmr   rN   r.   )r    basesubre   rf   r   r   r   setm	  s   
zAugeas.setmc                 C   sz   t |ts	tdt |tstdt |tstd| js"tdt| jt|t|t|}|dkr;| t	d |S )z
        Use the value of node `node` as a string and transform it into a tree
        using the lens `lens` and store it in the tree at `path`, which will be
        overwritten. `path` and `node` are path expressions.
        lens MUST be a string!znode MUST be a string!r^   r_   r   zAugeas.text_store() failed)
rS   rT   rU   rF   rY   r   aug_text_storer   rN   r.   )r    lensnoderd   rf   r   r   r   
text_store#  s   


zAugeas.text_storec                 C   s   t |ts	tdt |tstdt |tstdt |ts$td| js+tdt| jt|t|t|t|}|dkrG| t	d |S )a  
        Transform the tree at `path` into a string using lens `lens` and store
        it in the node `node_out`, assuming the tree was initially generated
        using the value of node `node_in`. `path`, `node_in`, and `node_out`
        are path expressions.
        rt   znode_in MUST be a string!r^   znode_out MUST be a string!r_   r   zAugeas.text_retrieve() failed)
rS   rT   rU   rF   rY   r   aug_text_retriever   rN   r.   )r    rv   node_inrd   node_outrf   r   r   r   text_retrieve;  s$   
	


zAugeas.text_retrievec                 C   sn   t |tkr
tdt |tkr|durtd| jstdt| jt|t|}|dk r5| t	d |S )a  
        Define a variable `name` whose value is the result of
        evaluating `expr`. If a variable `name` already exists, its
        name will be replaced with the result of evaluating `expr`.

        If `expr` is :py:obj:`None`, the variable `name` will be removed if it
        is defined.

        Path variables can be used in path expressions later on by
        prefixing them with :samp:`$`.
        name MUST be a string!Nzexpr MUST be a string or None!r_   r   zAugeas.defvar() failed)
rn   ro   rU   rF   rY   r   
aug_defvarr   rN   r.   )r    nameexprrf   r   r   r   defvarW  s   zAugeas.defvarc                 C   s   t |tkr
tdt |tkrtdt |tkrtd| js%tdt| jt|t|t|tj	}|dk r@| 
td |S )a  
        Define a variable `name` whose value is the result of
        evaluating `expr`, which must not be :py:obj:`None` and evaluate to a
        nodeset. If a variable `name` already exists, its name will
        be replaced with the result of evaluating `expr`.

        If `expr` evaluates to an empty nodeset, a node is created,
        equivalent to calling ``set(expr, value)`` and `name` will be the
        nodeset containing that single node.
        r}   zexpr MUST be a string!zvalue MUST be a string!r_   r   zAugeas.defnode() failed)rn   ro   rU   rF   rY   r   aug_defnoder   r   rA   rN   r.   )r    r   r   re   rf   r   r   r   defnoder  s   zAugeas.defnodec                 C   f   t |ts	tdt |tstd| jstdt| jt|t|}|dkr1| t	d dS dS )ap  
        Move the node `src` to `dst`. `src` must match exactly one node
        in the tree. `dst` must either match exactly one node in the
        tree, or may not exist yet. If `dst` exists already, it and all
        its descendants are deleted before moving `src` there. If `dst`
        does not exist yet, it and all its missing ancestors are created.
        src MUST be a string!dst MUST be a string!r_   r   zAugeas.move() failedN)
rS   rT   rU   rF   rY   r   aug_mvr   rN   r.   r    srcdstrf   r   r   r   move     


zAugeas.movec                 C   r   )aq  
        Copy the node `src` to `dst`. `src` must match exactly one node
        in the tree. `dst` must either match exactly one node in the
        tree, or may not exist yet. If `dst` exists already, it and all
        its descendants are deleted before copying `src` there. If `dst`
        does not exist yet, it and all its missing ancestors are created.
        r   r   r_   r   zAugeas.copy() failedN)
rS   rT   rU   rF   rY   r   aug_cpr   rN   r.   r   r   r   r   copy  r   zAugeas.copyc                 C   sb   t |ts	tdt |tstd| jstdt| jt|t|}|dk r/| t	d |S )zH
        Rename the label of all nodes matching `src` to `dst`.
        r   r   r_   r   zAugeas.rename() failed)
rS   rT   rU   rF   rY   r   
aug_renamer   rN   r.   r   r   r   r   rename  s   

zAugeas.renameTc                 C   sp   t |ts	tdt |tstd| jstdt| jt|t||r'dp(d}|dkr6| t	d dS dS )a  
        Create a new sibling `label` for `path` by inserting into the tree
        just before `path` (if `before` is :py:obj:`True`) or just after `path`
        (if `before` is :py:obj:`False`).

        `path` must match exactly one existing node in the tree, and `label`
        must be a label, i.e. not contain a :samp:`/`, :samp:`*` or end with
        a bracketed index :samp:`[N]`.
        r^   zlabel MUST be a string!r_   r0   r   zAugeas.insert() failedN)
rS   rT   rU   rF   rY   r   
aug_insertr   rN   r.   )r    rd   rj   beforerf   r   r   r   insert  s   

zAugeas.insertc                 C   s2   t |ts	td| jstdt| jt|S )z
        Remove `path` and all its children. Returns the number of entries
        removed. All nodes that match `path`, and their descendants, are
        removed.
        r^   r_   )rS   rT   rU   rF   rY   r   aug_rmr   )r    rd   r   r   r   remove  s
   
zAugeas.removec                 C   s   t |ts	td| jstdtd}t| jt	||}|dk r*| 
td| |d }g }t|D ]}|| tjkrRt|| }|t| t||  q4t| |S )a  
        Return the matches of the path expression `path`. The returned paths
        are sufficiently qualified to make sure that they match exactly one
        node in the current tree.

        Path expressions use a very simple subset of XPath: the path `path`
        consists of a number of segments, separated by :samp:`/`; each segment
        can either be a :samp:`*`, matching any tree node, or a string,
        optionally followed by an index in brackets, matching tree nodes
        labelled with exactly that string. If no index is specified, the
        expression matches all nodes with that label; the index can be a
        positive number N, which matches exactly the *N*-th node with that
        label (counting from 1), or the special expression :samp:`last()` which
        matches the last node with the given label. All matches are done in
        fixed positions in the tree, and nothing matches more than one path
        segment.
        r^   r_   zchar***r   zAugeas.match() failed: %s)rS   rT   rU   rF   rY   r   ra   r   	aug_matchr   rN   r-   rangerA   rB   appendr   free)r    rd   parrayrf   arraymatchesiitemr   r   r   match  s(   


zAugeas.matchc                 C   s   t |ts	td| jstdtd}td}td}td}td}td}td}t| jt	||||||||	}	|	dk rM| 
td | |d }
|
t|d t|d t|d t|d t|d t|d fS )a  
        Get the span according to input file of the node associated with
        `path`. If the node is associated with a file, a tuple of 7 elements is
        returned: ``(filename, label_start, label_end, value_start, value_end,
        span_start, span_end)``. If the node associated with `path` doesn't
        belong to a file or is doesn't exists, :py:obj:`ValueError` is raised.

        :rtype: tuple(str, int, int, int, int, int, int)
        r^   r_   zchar **zunsigned int *r   zAugeas.span() failed)rS   rT   rU   rF   rY   r   ra   r   aug_spanr   rN   r.   rD   rV   )r    rd   filenamelabel_start	label_endvalue_start	value_end
span_startspan_endrf   fnamer   r   r   span#  s.   







zAugeas.spanc                 C   6   | j stdt| j }|dkr| td dS dS )a  
        Write all pending changes to disk. Only files that had any changes
        made to them are written.

        If :attr:`SAVE_NEWFILE` is set in the creation `flags`, create changed
        files as new files with the extension :samp:`.augnew`, and leave the
        original file unmodified.

        Otherwise, if :attr:`SAVE_BACKUP` is set in the creation `flags`, move
        the original file to a new file with extension :samp:`.augsave`.

        If neither of these flags is set, overwrite the original file.
        r_   r   zAugeas.save() failedN)rF   rY   r   aug_saverN   r   r    rf   r   r   r   saveI  s   zAugeas.savec                 C   r   )aM  
        Load files into the tree. Which files to load and what lenses to use
        on them is specified under :samp:`/augeas/load` in the tree; each entry
        :samp:`/augeas/load/NAME` specifies a 'transform', by having itself
        exactly one child 'lens' and any number of children labelled 'incl' and
        'excl'. The value of :samp:`NAME` has no meaning.

        The 'lens' grandchild of :samp:`/augeas/load` specifies which lens to
        use, and can either be the fully qualified name of a lens
        :samp:`Module.lens` or :samp:`@Module`. The latter form means that the
        lens from the transform marked for autoloading in MODULE should be
        used.

        The 'incl' and 'excl' grandchildren of :samp:`/augeas/load` indicate
        which files to transform. Their value are used as glob patterns. Any
        file that matches at least one 'incl' pattern and no 'excl' pattern is
        transformed. The order of 'incl' and 'excl' entries is irrelevant.

        When AUG_INIT is first called, it populates :samp:`/augeas/load` with
        the transforms marked for autoloading in all the modules it finds.

        Before loading any files, :func:`load` will remove everything
        underneath :samp:`/augeas/files` and :samp:`/files`, regardless of
        whether any entries have been modified or not.
        r_   r   zAugeas.load() failedN)rF   rY   r   aug_loadrN   r-   r   r   r   r   loada  s   zAugeas.loadc                 C   s   |  d dS )z
        Clear all transforms beneath :samp:`/augeas/load`. If :func:`load` is
        called right after this, there will be no files beneath :samp:`/files`.
        z/augeas/load/*N)r   rQ   r   r   r   clear_transforms  s   zAugeas.clear_transformsr   c                 C   s   |rddl }|jdtdd t|tr|g}t|tr|g}tt|D ]}| ||| d q$tt|D ]}| ||| d q6dS )a  
        Add a transform beneath :samp:`/augeas/load`.

        :param lens: the (file)name of the lens to use
        :type lens: str
        :param incl: one or more glob patterns for the files to transform
        :type incl: str or list(str)
        :param name: deprecated parameter
        :param excl: zero or more glob patterns of files to exclude from
                     transforming
        :type excl: str or list(str)
        r   Nz'name is now deprecated in this functionr1   
stacklevelFT)warningswarnDeprecationWarningrS   rT   r   len	transform)r    rv   inclr   exclr   r   r   r   r   add_transform  s   

zAugeas.add_transformFc                 C   sz   t |ts	tdt |tstdt |tstd| js"tdt| jt|t||}|dkr;| 	t
d dS dS )aB  
        Add a transform for `file` using `lens`.

        `excl` specifies if this the file is to be included (:py:obj:`False`)
        or excluded (:py:obj:`True`) from the `lens`.
        The `lens` may be a module name or a full lens name.
        If a module name is given, then lns will be the lens assumed.
        rt   zfile MUST be a string!zexcl MUST be a boolean!r_   r   zAugeas.transform() failedN)rS   rT   rU   boolrF   rY   r   aug_transformr   rN   r-   )r    rv   filer   rf   r   r   r   r     s   



zAugeas.transformc                 C   s,   | j r	| j tjkrdS t| j  d| _ dS )z
        Close this Augeas instance and free any storage associated with it.
        After this call, this Augeas instance is invalid and can not be used
        for any more operations.
        N)rF   r   rA   r   	aug_closerQ   r   r   r   rO     s   
zAugeas.close)T)Nr   )F)4r(   r)   r*   __doc__NONESAVE_BACKUPSAVE_NEWFILE
TYPE_CHECK	NO_STDINC	SAVE_NOOPNO_LOADNO_MODL_AUTOLOADENABLE_SPANAUG_NOERRORrG   AUG_EINTERNAL
AUG_EPATHXAUG_ENOMATCHAUG_EMMATCHAUG_ESYNTAXAUG_ENOLENS	AUG_EMXFMAUG_ENOSPANAUG_EMVDESCAUG_ECMDRUNAUG_EBADARG
AUG_ELABELAUG_ECPDESCrD   rN   r   rg   rj   rm   rs   rx   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rO   r   r   r   r   r/   _   sd    '
,&#

r/   c                       s    e Zd ZdZ fddZ  ZS )augeaszN
    Compat class, obsolete. Use class Augeas directly.

    :deprecated:
    c                    s2   dd l }|jdtdd tt| j|i | d S )Nr   zuse Augeas instead of augeasr1   r   )r   r   r   r   r   r   )r    pkr   r$   r   r   r     s
   zaugeas.__init__)r(   r)   r*   r   r   r+   r   r   r$   r   r     s    r   N)r   sysr   _pyver
augeas.ffir   r   
__author____credits__PY3r   ro   rT   
basestringr   r   IOErrorr   rY   r-   
ValueErrorr.   objectr/   r   __all__r   r   r   r   <module>   s,     


    w