XT\dZdZdZddlZddlZddlZddlmZddl m Z edk\Z dZ e re ZneZd Zd Zd ZGd d eZGddeZy)aPure 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 zJeff Schroeder Harald Hoyer - initial python bindings, packaging Nils Philippsen N) version_info)reduce)utf8c2|r|jtSyN)encodeAUGENCsts (/usr/lib/python3/dist-packages/augeas.pyencr: yy   c2|r|jtSyr)decoder r s r decr?rrc|Dcgc]!}tjj|#c}Dcgc]}|s| }}td|d}|st d|dz|Scc}wcc}w)z]Search for one of the libraries given as arguments and load it. Returns the library. cH|xstjj|Sr)ctypescdll LoadLibrary)xys r z_dlopen..Isq>FKK$;$;A$>rNzUnable to import lib%s!r)rutil find_libraryr ImportError)argsallibslibs r _dlopenr$Dsi?CD11!4D J!A JD J >d KC 3d1g=>> J E Js&A!A&A&c eZdZdZedZejej_ dZ dZ dZ dZ dZdZd Zd Zd Zd d e fd ZdZdZdZdZdZdZdZdZdZdZdZd#dZdZ dZ!dZ"dZ#dZ$dZ%d$d Z&d%d!Z'd"Z(y )&Augeasz$Class wrapper for the augeas libraryaugeasr @Nct|ts|dk7r tdt|ts|dk7r tdt|ts tdtj j t|t|||_|js tdtj|j|_y)aInitialize 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.Nzroot MUST be a string or None!z"loadpath MUST be a string or None!zflag MUST be a flag!zUnable to create Augeas object!) isinstance string_types TypeErrorintr& _libaugeasaug_initr_Augeas__handle RuntimeErrorrc_void_p)selfrootloadpathflagss r __init__zAugeas.__init__`s$ -$$,<= =(L1h$6F@A A%%23 3))223t9c(mUS }}@A A 6 rc$|jyr)closer:s r __del__zAugeas.__del__|s  rcft|ts td|js t dt j }tjj|jt|t j|}|dkDr tdt|jS)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!)r1r2r3r7r8rc_char_pr&r5aug_getrbyref ValueErrorrvaluer:pathrKrets r getz Augeas.gets $ -45 5}}KL L!'' s4y(. U(;= 7CD D5;;rcft|ts td|js t dt j }tjj|jt|t j|}|dkDr tdt|jS)zLookup the label associated with 'path'. Returns the label of the path specified. It is an error if more than one node matches 'path'.rDrEr(rF)r1r2r3r7r8rrGr&r5 aug_labelrrIrJrrK)r:rMlabelrNs r rRz Augeas.labels $ -45 5}}KL L!))$--T*0,,u*=? 7CD D5;;rcft|ts tdt|ts"t|tdk7r td|js t dt jj|jt|t|}|dk7r tdy)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'.rDNzvalue MUST be a string or None!rErUnable to set value to path!) r1r2r3typer7r8r&r5aug_setrrJrLs r setz Augeas.sets $ -45 5%.4;$t*3L=> >}}KL L'' s4y#e*M !8;< < rct|tk7r tdt|tk7r|dk7r tdt|tk7r td|js t dt j j|jt|t|t|}|dkr td|S)aSet 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.zbase MUST be a string!Nzsub MUST be a string or None!value MUST be a string!rErrT) rUstrr3r7r8r&r5aug_setmrrJ)r:basesubrKrNs r setmz Augeas.setms : 45 5 9 t ;< < ;# 56 6}}KL L(( MM3t9c#hE < 7;< < rct|ts tdt|ts tdt|ts td|js t dt j j|jt|t|t|}|dk7r td|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!znode MUST be a string!rDrErUnable to store text at node!) r1r2r3r7r8r&r5aug_text_storerrJ)r:lensnoderMrNs r text_storezAugeas.text_stores $ -45 5$ -45 5$ -45 5}}KL L.. MM3t9c$iT< !8<= = rc t|ts tdt|ts tdt|ts tdt|ts td|js t dt j j|jt|t|t|t|}|dk7r td|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`znode_in MUST be a string!rDznode_out MUST be a string!rErra) r1r2r3r7r8r&r5aug_text_retrieverrJ)r:rcnode_inrMnode_outrNs r text_retrievezAugeas.text_retrieves $ -45 5'<078 8$ -45 5(L189 9}}KL L11 MM3t9c'lCIs8}N !8<= = rcLt|tk7r tdt|tk7r|dk7r td|js t dt j j|jt|t|}|dkr td|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!Nzexpr MUST be a string or None!rErzUnable to register variable!) rUrZr3r7r8r&r5 aug_defvarrrJ)r:nameexprrNs r defvarz Augeas.defvars : 45 5 : <= =}}KL L**4==#d)SYO 7;< < rct|tk7r tdt|tk7r tdt|tk7r td|js t dt j j|jt|t|t|d}|dkr td|S)aDefine 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.rlzexpr MUST be a string!rYrENrzUnable to register node!) rUrZr3r7r8r&r5 aug_defnoderrJ)r:rnrorKrNs r defnodezAugeas.defnodes : 45 5 : 45 5 ;# 56 6}}KL L++ MM3t9c$iUTC 778 8 rc8t|ts tdt|ts td|js t dt j j|jt|t|}|dk7r tdy)ajMove 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!rErzUnable to move src to dst!N) r1r2r3r7r8r&r5aug_mvrrJr:srcdstrNs r movez Augeas.move7s#|,34 4#|,34 4}}KL L&&t}}c#hCI !89: : rc:t|ts tdt|ts td|js t dt j j|jt|t|}|dkr td|S)z6Rename the label of all nodes matching 'src' to 'lbl'.rurvrErzUnable to rename src as dst!) r1r2r3r7r8r&r5 aug_renamerrJrxs r renamez Augeas.renameKs#|,34 4#|,34 4}}KL L**4==#c(CHM 7;< < rcJt|ts tdt|ts td|js t dt j j|jt|t||xrdxsd}|dk7r tdy)aXCreate 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]'.rDzlabel MUST be a string!rEr(rzUnable to insert label!N) r1r2r3r7r8r&r5 aug_insertrrJ)r:rMrRbeforerNs r insertz Augeas.insert\s$ -45 5%.56 6}}KL L**4==#d)+.u:v|!7HqJ !867 7 rct|ts td|js t dt j j|jt|S)zRemove 'path' and all its children. Returns the number of entries removed. All nodes that match 'path', and their descendants, are removed.rDrE) r1r2r3r7r8r&r5aug_rmr)r:rMs r removez Augeas.removessP $ -45 5}}KL L  '' s4yAArc t|ts td|js t dt j t j}tjj|jt|t j|}|dkr t d|g}t|D]}||s |jtt j ||t j"j$t j&j)t j||t j&j)||S)aReturn 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.rDrErzError during match procedure!)r1r2r3r7r8rPOINTERr9r&r5 aug_matchrrIrangeappendrcastrGrK pythonapi PyMem_Free)r:rMarrayrNmatchesis r matchz Augeas.matchs"$ -45 5}}KL L0v/1))$--T*0,,u*=? 7>E Es GAQxs6;;uQx/5$@@E GH   ++FOOE!H,EF G ##E*rc 6t|ts td|js t dt j }t j}t j}t j}t j}t j}t j}t j} tjj|jt|| || || || || || || | } | dkr tdt|j|j|j|j|j|j|jfS)aiGet 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.rDrErzError during span procedure)r1r2r3r7r8rrGc_uintrIr&r5aug_spanrrJrrK) r:rMfilename label_start label_end value_start value_end span_startspan_endrrNs r spanz Augeas.spans+$ -45 5}}KL L??$mmo MMO mmo MMO ]]_ ==? LL((D 1X;)*;9)*;9)*:( E !G:; ;HNN#[%6%6 !!9??  (..2 2rc|js tdtjj |j}|dk7r t dy)aWrite 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.rErzUnable to save to file!N)r7r8r&r5aug_saveIOErrorr:rNs r savez Augeas.savesK}}KL L((7 !834 4 rc|js tdtjj |j}|dk7r tdy)aLoad 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.rErzaug_load() failed!N)r7r8r&r5aug_loadrs r loadz Augeas.loadsI2}}KL L((7 !834 4 rc&|jdy)zClear all transforms beneath /augeas/load. If load() is called right after this, there will be no files beneath /files.z/augeas/load/*N)rrAs r clear_transformszAugeas.clear_transforms s $%rcH|rddl}|jdtdt|tr|g}t|tr|g}t t |D]}|j|||dt t |D]}|j|||dy)aAdd 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 rNz'name is now deprecated in this functionr) stacklevelFT)warningswarnDeprecationWarningr1r2rlen transform)r:rcinclrnexclrrs r add_transformzAugeas.add_transforms   MMCEW%&  ( t\ *6D t\ *6Ds4y! 1A NN4a% 0 1s4y! 0A NN4a$ / 0rcpt|ts tdt|ts tdt|ts td|js t dt jj|jt|t||}|dk7r t dy)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`zfile MUST be a string!zexcl MUST be a boolean!rErzUnable to add transform!N) r1r2r3boolr7r8r&r5 aug_transformr)r:rcfilerrNs r rzAugeas.transform&s$ -45 5$ -45 5$%56 6}}KL L--dmmSYD SWX !89: : rc~|jsytjj|jd|_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)r7r&r5 aug_closerAs r r@z Augeas.close;s1 }}  ##DMM2 r)T)N)F))__name__ __module__ __qualname____doc__r$r5rr9r6restypeNONE SAVE_BACKUP SAVE_NEWFILE TYPE_CHECK NO_STDINC SAVE_NOOPNO_LOADNO_MODL_AUTOLOAD ENABLE_SPANr>rBrOrRrWr^rerjrprsr{r~rrrrrrrrrr@rrr r&r&Ns*"J"(//J DKLJIIGK 4t78 , ,=$0,026;("8. B/b 2D5,5@& 0.;* rr&c"eZdZdZfdZxZS)r'z2Compat class, obsolete. Use class Augeas directly.cdddl}|jdtdtt||i|y)Nrzuse Augeas instead of augeasr)r)rrrsuperr'r>)r:pkr __class__s r r>zaugeas.__init__Os4 46H   fd$a-1-r)rrrrr> __classcell__)rs@r r'r'Ls8..rr')r __author__ __credits__typesr ctypes.utilsysr_pyver functoolsrPY3r rZr2 basestringrrr$objectr&r'rrr rss D?   & n LL! ! zVz|.V.r