[augeas-devel] [PATCH 2/2] Add span API call to match C API

Francis Giraldeau francis.giraldeau at gmail.com
Wed Mar 16 00:38:12 UTC 2011


This patch adds support for aug_span API function. The python method is "span"
and returns a tuple with the relevant information in case of success. On
failure, ValueError is raised.

Tests are provided for this new function to verify correct behavior.
---
 augeas.py           |   35 +++++++++++++++++++++++++++++++++++
 test/test_augeas.py |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 0 deletions(-)

diff --git a/augeas.py b/augeas.py
index 3b1d782..e7b14ee 100644
--- a/augeas.py
+++ b/augeas.py
@@ -74,6 +74,7 @@ class Augeas(object):
     SAVE_NOOP = 1 << 4
     NO_LOAD = 1 << 5
     NO_MODL_AUTOLOAD = 1 << 6
+    ENABLE_SPAN = 1 << 7
 
     def __init__(self, root=None, loadpath=None, flags=NONE):
         """Initialize the library.
@@ -323,6 +324,40 @@ class Augeas(object):
 
         return matches
 
+    def span(self, path):
+        """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."""
+        
+        # Sanity checks
+        if not isinstance(path, basestring):
+            raise TypeError("path MUST be a string!")
+        if not self.__handle:
+            raise RuntimeError("The Augeas object has already been closed!")
+
+        filename = ctypes.c_char_p()
+        label_start = ctypes.c_uint()
+        label_end = ctypes.c_uint()
+        value_start = ctypes.c_uint()
+        value_end = ctypes.c_uint()
+        span_start = ctypes.c_uint()
+        span_end = ctypes.c_uint()
+        
+        r = ctypes.byref
+        
+        ret = Augeas._libaugeas.aug_span(self.__handle, path, r(filename),
+                                         r(label_start), r(label_end),
+                                         r(value_start), r(value_end),
+                                         r(span_start), r(span_end))
+        if (ret < 0):
+            raise ValueError("Error during span procedure")
+        
+        return (filename.value, label_start.value, label_end.value,
+                value_start.value, value_end.value,
+                span_start.value, span_end.value)
+
     def save(self):
         """Write all pending changes to disk. Only files that had any changes
         made to them are written.
diff --git a/test/test_augeas.py b/test/test_augeas.py
index 2e9bba4..79d612e 100644
--- a/test/test_augeas.py
+++ b/test/test_augeas.py
@@ -103,6 +103,46 @@ class TestAugeas(unittest.TestCase):
             self.failUnless(a.get(i) == "192.168.1.1")
         del a
 
+    def test08Span(self):
+        "test span"
+        data = [ {"expr": "/files/etc/hosts/1/ipaddr", "f": "hosts",
+                  "ls": 0, "le": 0, "vs": 104, "ve": 113, "ss": 104, "se": 113},
+                 {"expr": "/files/etc/hosts/1", "f": "hosts",
+                  "ls": 0, "le": 0, "vs": 0, "ve": 0, "ss": 104, "se": 155},
+                 {"expr": "/files/etc/hosts/*[last()]", "f": "hosts",
+                  "ls": 0, "le": 0, "vs": 0, "ve": 0, "ss": 155, "se": 202},
+                 {"expr": "/files/etc/hosts/#comment[2]", "f": "hosts",
+                  "ls": 0, "le": 0, "vs": 58, "ve": 103, "ss": 56, "se": 104},
+                 {"expr": "/files/etc/hosts", "f": "hosts",
+                  "ls": 0, "le": 0, "vs": 0, "ve": 0, "ss": 0, "se":202 },
+                ]
+        a = augeas.Augeas(root=MYROOT, flags=augeas.Augeas.ENABLE_SPAN)
+        for d in data:
+            r = a.span(d["expr"])
+            self.assertEquals(os.path.basename(r[0]), d["f"])
+            self.assertEquals(r[1], d["ls"])
+            self.assertEquals(r[2], d["le"])
+            self.assertEquals(r[3], d["vs"])
+            self.assertEquals(r[4], d["ve"])
+            self.assertEquals(r[5], d["ss"])
+            self.assertEquals(r[6], d["se"])
+
+        error = None
+        try:
+            r = a.span("/files")
+        except ValueError, e:
+            error = e
+        self.assertTrue(isinstance(e, ValueError))
+        
+        error = None
+        try:
+            r = a.span("/random")
+        except ValueError, e:
+            error = e
+        self.assertTrue(isinstance(e, ValueError))
+        
+        del a
+
 def getsuite():
     suite = unittest.TestSuite()
     suite = unittest.makeSuite(TestAugeas, 'test')
-- 
1.7.1




More information about the augeas-devel mailing list