
    XT\                         d Z dZdZddlZddlZddlZddlmZ ddl	m
Z
 edk\  ZdZereZneZd	 Zd
 Zd Z G d de      Z G d de      Zy)a  Pure python bindings for the augeas library

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.

z/Nathaniel McCallum <nathaniel@natemccallum.com>zJeff Schroeder <jeffschroeder@computer.org>
Harald Hoyer <harald@redhat.com> - initial python bindings, packaging
Nils Philippsen <nils@redhat.com>
    N)version_info)reduce)   utf8c                 2    | r| j                  t              S y N)encodeAUGENCsts    (/usr/lib/python3/dist-packages/augeas.pyencr   :       	yy   
    c                 2    | r| j                  t              S y r   )decoder
   r   s    r   decr   ?   r   r   c                      | D cg c]!  }t         j                  j                  |      # c}D cg c]  }|s|	 }}t        d |d      }|st	        d| d   z        |S c c}w c c}w )z]Search for one of the libraries given as arguments and load it.
    Returns the library.
    c                 H    | xs t         j                  j                  |      S r   )ctypescdllLoadLibrary)xys     r   <lambda>z_dlopen.<locals>.<lambda>I   s    q>FKK$;$;A$> r   NzUnable to import lib%s!r   )r   utilfind_libraryr   ImportError)argsallibslibs        r   _dlopenr$   D   si     ?CD11!4DJ!AJDJ>dKC3d1g=>>J	 EJs   &A!A&A&c                      e Zd ZdZ ed      Zej                  ej                  _	        dZ
dZdZdZdZdZd	Zd
ZdZdde
f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 Z d Z!d Z"d Z#d Z$d Z%d$d Z&d%d!Z'd" Z(y)&Augeasz$Class wrapper for the augeas libraryaugeasr                      @      Nc                    t        |t              s|dk7  rt        d      t        |t              s|dk7  rt        d      t        |t              st        d      t        j
                  j                  t        |      t        |      |      | _        | j                  st        d      t        j                  | j                        | _        y)a  Initialize the library.

        Use 'root' as the filesystem root. If 'root' is None, use the value of
        the environment variable AUGEAS_ROOT. If that doesn't exist either,
        use "/".

        'loadpath' is a colon-spearated list of directories that modules
        should be searched in. This is in addition to the standard load path
        and the directories in AUGEAS_LENS_LIB.

        'flags' is a bitmask made up of values from AUG_FLAGS.Nzroot MUST be a string or None!z"loadpath MUST be a string or None!zflag MUST be a flag!zUnable to create Augeas object!)
isinstancestring_types	TypeErrorintr&   
_libaugeasaug_initr   _Augeas__handleRuntimeErrorr   c_void_p)selfrootloadpathflagss       r   __init__zAugeas.__init__`   s     $-$$,<==(L1h$6F@AA%%233 ))223t9c(mUS}}@AA6r   c                 $    | j                          y r   )closer:   s    r   __del__zAugeas.__del__|   s    

r   c                 f   t        |t              st        d      | j                  st	        d      t        j                         }t        j                  j                  | j                  t        |      t        j                  |            }|dkD  rt        d      t        |j                        S )zLookup the value associated with 'path'.
        Returns the value at the path specified.
        It is an error if more than one node matches 'path'.path MUST be a string!*The Augeas object has already been closed!r(   $path specified had too many matches!)r1   r2   r3   r7   r8   r   c_char_pr&   r5   aug_getr   byref
ValueErrorr   valuer:   pathrK   rets       r   getz
Augeas.get   s     $-455}}KLL ! ''s4y(.U(;=7CDD5;;r   c                 f   t        |t              st        d      | j                  st	        d      t        j                         }t        j                  j                  | j                  t        |      t        j                  |            }|dkD  rt        d      t        |j                        S )zLookup the label associated with 'path'.
        Returns the label of the path specified.
        It is an error if more than one node matches 'path'.rD   rE   r(   rF   )r1   r2   r3   r7   r8   r   rG   r&   r5   	aug_labelr   rI   rJ   r   rK   )r:   rM   labelrN   s       r   rR   zAugeas.label   s     $-455}}KLL ! ))$--T*0,,u*=?7CDD5;;r   c                 f   t        |t              st        d      t        |t              s"t        |      t        d      k7  rt        d      | j                  st        d      t        j                  j                  | j                  t        |      t        |            }|dk7  rt        d      y)zSet 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'.rD   Nzvalue MUST be a string or None!rE   r   Unable to set value to path!)r1   r2   r3   typer7   r8   r&   r5   aug_setr   rJ   rL   s       r   setz
Augeas.set   s     $-455%.4;$t*3L=>>}}KLL ''s4y#e*M!8;<< r   c                    t        |      t        k7  rt        d      t        |      t        k7  r|dk7  rt        d      t        |      t        k7  rt        d      | j                  st	        d      t
        j                  j                  | j                  t        |      t        |      t        |            }|dk  rt        d      |S )a  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 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!value MUST be a string!rE   r   rT   )
rU   strr3   r7   r8   r&   r5   aug_setmr   rJ   )r:   basesubrK   rN   s        r   setmzAugeas.setm   s     :4559t;<<;#566}}KLL ((MM3t9c#hE
<7;<<
r   c                    t        |t              st        d      t        |t              st        d      t        |t              st        d      | j                  st	        d      t
        j                  j                  | j                  t        |      t        |      t        |            }|dk7  rt        d      |S )zUse 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!rD   rE   r   Unable to store text at node!)
r1   r2   r3   r7   r8   r&   r5   aug_text_storer   rJ   )r:   lensnoderM   rN   s        r   
text_storezAugeas.text_store   s     $-455$-455$-455}}KLL ..MM3t9c$iT<!8<==
r   c           	         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                  j                  | j                  t        |      t        |      t        |      t        |            }|dk7  rt        d      |S )zTransform 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.r`   znode_in MUST be a string!rD   znode_out MUST be a string!rE   r   ra   )
r1   r2   r3   r7   r8   r&   r5   aug_text_retriever   rJ   )r:   rc   node_inrM   node_outrN   s         r   text_retrievezAugeas.text_retrieve   s     $-455'<0788$-455(L1899}}KLL 11MM3t9c'lCIs8}N!8<==
r   c                 L   t        |      t        k7  rt        d      t        |      t        k7  r|dk7  rt        d      | j                  st	        d      t
        j                  j                  | j                  t        |      t        |            }|dk  r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 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 '$'.name MUST be a string!Nzexpr MUST be a string or None!rE   r   zUnable to register variable!)
rU   rZ   r3   r7   r8   r&   r5   
aug_defvarr   rJ   )r:   nameexprrN   s       r   defvarzAugeas.defvar  s     :455:<==}}KLL **4==#d)SYO7;<<
r   c                    t        |      t        k7  rt        d      t        |      t        k7  rt        d      t        |      t        k7  rt        d      | j                  st	        d      t
        j                  j                  | j                  t        |      t        |      t        |      d      }|dk  rt        d      |S )a  Define a variable 'name' whose value is the result of
        evaluating 'expr', which must not be 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.rl   zexpr MUST be a string!rY   rE   Nr   zUnable to register node!)
rU   rZ   r3   r7   r8   r&   r5   aug_defnoder   rJ   )r:   rn   ro   rK   rN   s        r   defnodezAugeas.defnode  s     :455:455;#566}}KLL ++MM3t9c$iUTC7788
r   c                 8   t        |t              st        d      t        |t              st        d      | j                  st	        d      t
        j                  j                  | j                  t        |      t        |            }|dk7  rt        d      y)aj  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!rE   r   zUnable to move src to dst!N)
r1   r2   r3   r7   r8   r&   r5   aug_mvr   rJ   r:   srcdstrN   s       r   movezAugeas.move7  s     #|,344#|,344}}KLL &&t}}c#hCI!89:: r   c                 :   t        |t              st        d      t        |t              st        d      | j                  st	        d      t
        j                  j                  | j                  t        |      t        |            }|dk  rt        d      |S )z6Rename the label of all nodes matching 'src' to 'lbl'.ru   rv   rE   r   zUnable to rename src as dst!)
r1   r2   r3   r7   r8   r&   r5   
aug_renamer   rJ   rx   s       r   renamezAugeas.renameK  s     #|,344#|,344}}KLL **4==#c(CHM7;<<
r   c                 J   t        |t              st        d      t        |t              st        d      | j                  st	        d      t
        j                  j                  | j                  t        |      t        |      |xr dxs d      }|dk7  rt        d      y)aX  Create a new sibling 'label' for 'path' by inserting into the tree
        just before 'path' (if 'before' is True) or just after 'path'
        (if 'before' is False).

        'path' must match exactly one existing node in the tree, and 'label'
        must be a label, i.e. not contain a '/', '*' or end with a bracketed
        index '[N]'.rD   zlabel MUST be a string!rE   r(   r   zUnable to insert label!N)
r1   r2   r3   r7   r8   r&   r5   
aug_insertr   rJ   )r:   rM   rR   beforerN   s        r   insertzAugeas.insert\  s     $-455%.566}}KLL **4==#d)+.u:v|!7HqJ!8677 r   c                     t        |t              st        d      | j                  st	        d      t
        j                  j                  | j                  t        |            S )zRemove 'path' and all its children. Returns the number of entries
        removed. All nodes that match 'path', and their descendants, are
        removed.rD   rE   )	r1   r2   r3   r7   r8   r&   r5   aug_rmr   )r:   rM   s     r   removezAugeas.removes  sP     $-455}}KLL   ''s4yAAr   c           	         t        |t              st        d      | j                  st	        d       t        j                  t
        j                               }t        j                  j                  | j                  t        |      t        j                  |            }|dk  rt	        d|      g }t        |      D ]  }||   s	|j                  t        t        j                   ||   t
        j"                        j$                               t
        j&                  j)                  t        j                  ||                 t
        j&                  j)                  |       |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 '/'; each segment can
        either be a '*', 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 Nth node with that label (counting from 1), or the
        special expression '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.rD   rE   r   zError during match procedure!)r1   r2   r3   r7   r8   r   POINTERr9   r&   r5   	aug_matchr   rI   rangeappendr   castrG   rK   	pythonapi
PyMem_Free)r:   rM   arrayrN   matchesis         r   matchzAugeas.match  s   " $-455}}KLL 0v/1 ))$--T*0,,u*=?7>EE s 	GAQxs6;;uQx/5$@@E G H
   ++FOOE!H,EF	G 	##E*r   c                 6   t        |t              st        d      | j                  st	        d      t        j                         }t        j                         }t        j                         }t        j                         }t        j                         }t        j                         }t        j                         }t
        j                  }	t        j                  j                  | j                  t        |       |	|       |	|       |	|       |	|       |	|       |	|       |	|      	      }
|
dk  rt        d      t        |j                        |j                  |j                  |j                  |j                  |j                  |j                  fS )ai  Get the span according to input file of the node associated with
        PATH. If the node is associated with a file, un tuple of 5 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, ValueError is raised.rD   rE   r   zError during span procedure)r1   r2   r3   r7   r8   r   rG   c_uintrI   r&   r5   aug_spanr   rJ   r   rK   )r:   rM   filenamelabel_start	label_endvalue_start	value_end
span_startspan_endrrN   s              r   spanzAugeas.span  s+    $-455}}KLL??$mmoMMO	mmoMMO	]]_
==?LL((D	1X;)*;9)*;9)*:(E !G:;;HNN#[%6%6	!!9??  (..2 	2r   c                     | j                   st        d      t        j                  j	                  | j                         }|dk7  rt        d      y)a  Write all pending changes to disk. Only files that had any changes
        made to them are written.

        If SAVE_NEWFILE is set in the creation 'flags', create changed files as
        new files with the extension ".augnew", and leave the original file
        unmodified.

        Otherwise, if SAVE_BACKUP is set in the creation 'flags', move the
        original file to a new file with extension ".augsave".

        If neither of these flags is set, overwrite the original file.rE   r   zUnable to save to file!N)r7   r8   r&   r5   aug_saveIOErrorr:   rN   s     r   savezAugeas.save  sK     }}KLL ((7!8344 r   c                     | j                   st        d      t        j                  j	                  | j                         }|dk7  rt        d      y)a  Load files into the tree. Which files to load and what lenses to use
        on them is specified under /augeas/load in the tree; each entry
        /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 NAME has no meaning.

        The 'lens' grandchild of /augeas/load specifies which lens to use, and
        can either be the fully qualified name of a lens 'Module.lens' or
        '@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 /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 /augeas/load with the
        transforms marked for autoloading in all the modules it finds.

        Before loading any files, AUG_LOAD will remove everything underneath
        /augeas/files and /files, regardless of whether any entries have been
        modified or not.rE   r   zaug_load() failed!N)r7   r8   r&   r5   aug_loadr   s     r   loadzAugeas.load  sI    2 }}KLL((7!8344 r   c                 &    | j                  d       y)zClear all transforms beneath /augeas/load. If load() is called right
        after this, there will be no files beneath /files.z/augeas/load/*N)r   rA   s    r   clear_transformszAugeas.clear_transforms
  s     	$%r   c                 H   |rddl }|j                  dt        d       t        |t              r|g}t        |t              r|g}t        t        |            D ]  }| j                  |||   d        t        t        |            D ]  }| j                  |||   d        y)a  Add a transform beneath /augeas/load.

        lens: the (file)name of the lens to use
        incl: one or more glob patterns for the files to transform
        name: deprecated parameter
        excl: zero or more glob patterns of files to exclude from transforming
        r   Nz'name is now deprecated in this functionr)   
stacklevelFT)warningswarnDeprecationWarningr1   r2   r   len	transform)r:   rc   inclrn   exclr   r   s          r   add_transformzAugeas.add_transform  s     MMCEW%&  (t\*6Dt\*6Ds4y! 	1ANN4a%0	1s4y! 	0ANN4a$/	0r   c                 p   t        |t              st        d      t        |t              st        d      t        |t              st        d      | j                  st        d      t        j                  j                  | j                  t        |      t        |      |      }|dk7  rt        d      y)a$  Add a transform for 'file' using 'lens'.
        'excl' specifies if this the file is to be included (False)
        or excluded (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.
        r`   zfile MUST be a string!zexcl MUST be a boolean!rE   r   zUnable to add transform!N)
r1   r2   r3   boolr7   r8   r&   r5   aug_transformr   )r:   rc   filer   rN   s        r   r   zAugeas.transform&  s     $-455$-455$%566}}KLL--dmmSYD	SWX!89:: r   c                 ~    | j                   syt        j                  j                  | j                          d| _         y)zClose 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)r7   r&   r5   	aug_closerA   s    r   r@   zAugeas.close;  s1     }} 	##DMM2 r   )T)N )F))__name__
__module____qualname____doc__r$   r5   r   r9   r6   restypeNONESAVE_BACKUPSAVE_NEWFILE
TYPE_CHECK	NO_STDINC	SAVE_NOOPNO_LOADNO_MODL_AUTOLOADENABLE_SPANr>   rB   rO   rR   rW   r^   re   rj   rp   rs   r{   r~   r   r   r   r   r   r   r   r   r   r@   r   r   r   r&   r&   N   s    * "J"(//J DKLJIIGK 4t 78 , ,=$0,026;("8.B/b 2D5,5@&
0.;*r   r&   c                   "     e Zd ZdZ fdZ xZS )r'   z2Compat class, obsolete. Use class Augeas directly.c                 d    dd l }|j                  dt        d       t        t        |   |i | y )Nr   zuse Augeas instead of augeasr)   r   )r   r   r   superr'   r>   )r:   pkr   	__class__s       r   r>   zaugeas.__init__O  s4    46H 	 	fd$a-1-r   )r   r   r   r   r>   __classcell__)r   s   @r   r'   r'   L  s    8. .r   r'   )r   
__author____credits__typesr   ctypes.utilsysr   _pyver	functoolsr   PY3r
   rZ   r2   
basestringr   r   r$   objectr&   r'   r   r   r   <module>r      ss   D ?

    &  n	 LL!
!
zV z|.V .r   