o XT\@sdZdZdZddlZddlZddlZddlmZddl m Z edkZ dZ e r)e ZneZd d Zd d Zd dZGdddeZGdddeZdS)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)utf8cC|r|tSdSN)encodeAUGENCstr (/usr/lib/python3/dist-packages/augeas.pyenc: rcCrr)decoder r r r r dec?rrcGs@dddd|DD}tdd|d}|std|d|S) z]Search for one of the libraries given as arguments and load it. Returns the library. cSsg|]}|r|qSr r ).0lr r r Hsz_dlopen..cSsg|]}tj|qSr )ctypesutil find_library)rar r r rHscSs|ptj|Sr)rcdll LoadLibrary)xyr r r Isz_dlopen..NzUnable to import lib%s!r)r ImportError)argslibslibr r r _dlopenDs r"c@seZdZdZedZejej_ dZ dZ dZ dZ dZdZd Zd Zd Zd d e fd dZddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zdd8d9Z'd:d;Z(d S)?Augeasz$Class wrapper for the augeas libraryaugeasr @NcCst|ts |dkr tdt|ts|dkrtdt|ts#tdtjt|t|||_|js7t dt |j|_dS)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)selfrootloadpathflagsr r r __init__`s zAugeas.__init__cCs |dSr)closer6r r r __del__|s zAugeas.__del__cC^t|ts td|jstdt}tj |jt |t |}|dkr*t dt |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!)r-r.r/r3r4rc_char_pr#r1aug_getrbyref ValueErrorrvaluer6pathrFretr r r get  z Augeas.getcCr>)zLookup the label associated with 'path'. Returns the label of the path specified. It is an error if more than one node matches 'path'.r?r@r%rA)r-r.r/r3r4rrBr#r1 aug_labelrrDrErrF)r6rHlabelrIr r r rMrKz Augeas.labelcCspt|ts tdt|tst|tdkrtd|js!tdtj|jt |t |}|dkr6t ddS)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'.r?Nzvalue MUST be a string or None!r@rUnable to set value to path!) r-r.r/typer3r4r#r1aug_setrrErGr r r sets z Augeas.setcCst|tkr tdt|tkr|dkrtdt|tkr"td|js)tdtj|jt|t|t|}|dkrAt d|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!r@rrN) rOstrr/r3r4r#r1aug_setmrrE)r6basesubrFrIr r r setms  z Augeas.setmcCsxt|ts tdt|tstdt|tstd|js"tdtj|jt|t|t|}|dkr: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!znode MUST be a string!r?r@rUnable to store text at node!) r-r.r/r3r4r#r1aug_text_storerrE)r6lensnoderHrIr r r text_stores   zAugeas.text_storecCst|ts tdt|tstdt|tstdt|ts$td|js+tdtj|jt|t|t|t|}|dkrFt 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.rXznode_in MUST be a string!r?znode_out MUST be a string!r@rrY) r-r.r/r3r4r#r1aug_text_retrieverrE)r6r[node_inrHnode_outrIr r r text_retrieves    zAugeas.text_retrievecCslt|tkr tdt|tkr|dkrtd|jstdtj|jt|t|}|dkr4t 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!Nzexpr MUST be a string or None!r@rzUnable to register variable!) rOrSr/r3r4r#r1 aug_defvarrrE)r6nameexprrIr r r defvars z Augeas.defvarcCst|tkr tdt|tkrtdt|tkrtd|js%tdtj|jt|t|t|d}|dkr>t d|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.rbzexpr MUST be a string!rRr@NrzUnable to register node!) rOrSr/r3r4r#r1 aug_defnoderrE)r6rdrerFrIr r r defnodes   zAugeas.defnodecCs`t|ts tdt|tstd|jstdtj|jt|t|}|dkr.t ddS)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!r@rzUnable to move src to dst!N) r-r.r/r3r4r#r1aug_mvrrEr6srcdstrIr r r move7s  z Augeas.movecCs`t|ts tdt|tstd|jstdtj|jt|t|}|dkr.t d|S)z6Rename the label of all nodes matching 'src' to 'lbl'.rirjr@rzUnable to rename src as dst!) r-r.r/r3r4r#r1 aug_renamerrErlr r r renameKs  z Augeas.renameTcCsjt|ts tdt|tstd|jstdtj|jt|t||r(dp)d}|dkr3t ddS)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]'.r?zlabel MUST be a string!r@r%rzUnable to insert label!N) r-r.r/r3r4r#r1 aug_insertrrE)r6rHrMbeforerIr r r insert\s  z Augeas.insertcCs4t|ts td|jstdtj|jt|S)zRemove 'path' and all its children. Returns the number of entries removed. All nodes that match 'path', and their descendants, are removed.r?r@) r-r.r/r3r4r#r1aug_rmr)r6rHr r r removess z Augeas.removecCst|ts td|jstdttj}tj |jt |t |}|dkr.td|g}t |D] }||rT|tt||tjjtjt||q4tj||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.r?r@rzError during match procedure!)r-r.r/r3r4rPOINTERr5r#r1 aug_matchrrDrangeappendrcastrBrF pythonapi PyMem_Free)r6rHarrayrImatchesir r r matchs,    z Augeas.matchc Cst|ts td|jstdt}t}t}t}t}t}t}tj} t j |jt || || || || || || || | } | dkrVt dt|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.r?r@rzError during span procedure)r-r.r/r3r4rrBc_uintrDr#r1aug_spanrrErrF) r6rHfilename label_start label_end value_start value_end span_startspan_endrrIr r r spans.    z Augeas.spancCs0|jstdtj|j}|dkrtddS)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.r@rzUnable to save to file!N)r3r4r#r1aug_saveIOErrorr6rIr r r saves z Augeas.savecCs0|jstdtj|j}|dkrtddS)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.r@rzaug_load() failed!N)r3r4r#r1aug_loadrr r r loads z Augeas.loadcCs|ddS)zClear all transforms beneath /augeas/load. If load() is called right after this, there will be no files beneath /files.z/augeas/load/*N)rvr<r r r clear_transforms szAugeas.clear_transformsr cCs|rddl}|jdtddt|tr|g}t|tr|g}tt|D] }||||dq$tt|D] }||||dq6dS)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)warningswarnDeprecationWarningr-r.rylen transform)r6r[inclrdexclrrr r r add_transforms   zAugeas.add_transformFcCstt|ts tdt|tstdt|tstd|js"tdtj|jt |t ||}|dkr8tddS)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. rXzfile MUST be a string!zexcl MUST be a boolean!r@rzUnable to add transform!N) r-r.r/boolr3r4r#r1 aug_transformr)r6r[filerrIr r r r&s   zAugeas.transformcCs"|jsdStj|jd|_dS)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)r3r#r1 aug_closer<r r r r;;s z Augeas.close)T)Nr )F))__name__ __module__ __qualname____doc__r"r1rr5r2restypeNONE SAVE_BACKUP SAVE_NEWFILE TYPE_CHECK NO_STDINC SAVE_NOOPNO_LOADNO_MODL_AUTOLOAD ENABLE_SPANr:r=rJrMrQrWr]rarfrhrorqrtrvrrrrrrrr;r r r r r#NsF  1"   r#cs eZdZdZfddZZS)r$z2Compat class, obsolete. Use class Augeas directly.cs2ddl}|jdtddtt|j|i|dS)Nrzuse Augeas instead of augeasr&r)rrrsuperr$r:)r6pkr __class__r r r:Os zaugeas.__init__)rrrrr: __classcell__r r rr r$Lsr$)r __author__ __credits__typesr ctypes.utilsysr_pyver functoolsrPY3r rSr. basestringrrr"objectr#r$r r r r s,"