[augeas-devel] [PATCH] Add setm, defvar and defnode Add tests for all three methods

Raphael Pinson raphink at gmail.com
Sun Nov 21 23:53:59 UTC 2010


The following patch adds support for the new setm, defvar and
defnode calls.
The defnode call has been simplified for now, in that it does not
support the 'created' argument. This should not be a problem as
'created' could default to None when it is added, thus not breaking
the API set by this patch.

---
 augeas.py           |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test_augeas.py |   28 +++++++++++++++++++
 2 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/augeas.py b/augeas.py
index 778d200..3b1d782 100644
--- a/augeas.py
+++ b/augeas.py
@@ -144,6 +144,80 @@ class Augeas(object):
         if ret != 0:
             raise ValueError("Unable to set value to path!")
 
+    def setm(self, base, sub, value):
+        """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."""
+
+        # Sanity checks
+        if type(base) != str:
+            raise TypeError, "base MUST be a string!"
+        if type(sub) != str and sub != None:
+            raise TypeError, "sub MUST be a string or None!"
+        if type(value) != str:
+            raise TypeError, "value MUST be a string!"
+        if not self.__handle:
+            raise RuntimeError, "The Augeas object has already been closed!"
+
+        # Call the function
+        ret = Augeas._libaugeas.aug_setm(self.__handle, base, sub, value)
+        if ret < 0:
+            raise ValueError, "Unable to set value to path!"
+        return ret
+
+    def defvar(self, name, expr):
+        """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 '$'."""
+
+        # Sanity checks
+        if type(name) != str:
+            raise TypeError, "name MUST be a string!"
+        if type(expr) != str and expr != None:
+            raise TypeError, "expr MUST be a string or None!"
+        if not self.__handle:
+            raise RuntimeError, "The Augeas object has already been closed!"
+
+        # Call the function
+        ret = Augeas._libaugeas.aug_defvar(self.__handle, name, expr)
+        if ret < 0:
+            raise ValueError, "Unable to register variable!"
+        return ret
+
+    def defnode(self, name, expr, value):
+        """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."""
+
+        # Sanity checks
+        if type(name) != str:
+            raise TypeError, "name MUST be a string!"
+        if type(expr) != str:
+            raise TypeError, "expr MUST be a string!"
+        if type(value) != str:
+            raise TypeError, "value MUST be a string!"
+        if not self.__handle:
+            raise RuntimeError, "The Augeas object has already been closed!"
+
+        # Call the function
+        ret = Augeas._libaugeas.aug_defnode(self.__handle, name, expr, value, None)
+        if ret < 0:
+            raise ValueError, "Unable to register node!"
+        return ret
+
     def move(self, src, dst):
         """Move the node 'src' to 'dst'. 'src' must match exactly one node
            in the tree. 'dst' must either match exactly one node in the
diff --git a/test/test_augeas.py b/test/test_augeas.py
index 60cf662..bf93503 100644
--- a/test/test_augeas.py
+++ b/test/test_augeas.py
@@ -71,7 +71,35 @@ class TestAugeas(unittest.TestCase):
         self.failUnless(default == 1)
         a.set("/files/etc/grub.conf/default", str(0))
         a.save()
+
+    def test05Defvar(self):
+        "test defvar"
+        a = augeas.Augeas(root=MYROOT)
+        a.defvar("hosts", "/files/etc/hosts")
+        matches = a.match("$hosts/*")
+        self.failUnless(matches)
+        for i in matches:
+            for attr in a.match(i+"/*"):
+                self.failUnless(a.get(attr) != None)
+        del a
         
+    def test06Defnode(self):
+        "test defnode"
+        a = augeas.Augeas(root=MYROOT)
+        a.defnode("bighost", "/files/etc/hosts/50/ipaddr", "192.168.1.1")
+        value = a.get("$bighost")
+        self.failUnless(value == "192.168.1.1")
+        del a
+
+    def test07Setm(self):
+        "test setm"
+        a = augeas.Augeas(root=MYROOT)
+        matches = a.match("/files/etc/hosts/*/ipaddr")
+        self.failUnless(matches)
+        a.setm("/files/etc/hosts", "*/ipaddr", "192.168.1.1")
+        for i in matches:
+            self.failUnless(a.get(i) == "192.168.1.1")
+        del a
 
 def getsuite():
     suite = unittest.TestSuite()
-- 
1.7.0.4




More information about the augeas-devel mailing list