]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
add dns.name.Name.parent()
authorBob Halley <halley@dnspython.org>
Thu, 6 Oct 2005 06:25:52 +0000 (06:25 +0000)
committerBob Halley <halley@dnspython.org>
Thu, 6 Oct 2005 06:25:52 +0000 (06:25 +0000)
ChangeLog
dns/name.py
tests/name.py

index 226fa76821430ddddf3b6d06fc66a1bc29eaeb4f..e68b8f6cd94e48393ef5305b708d996e48636585 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2005-10-06  Bob Halley  <halley@dnspython.org>
+
+       * dns/name.py: Added the parent() method, which returns the
+         parent of a name.
+
 2005-10-01  Bob Halley  <halley@dnspython.org>
 
        * dns/resolver.py: Added zone_for_name() helper, which returns
index 3bf5b6ed64eabf2466916fe22809ee4b087c7636..c5e4b9a05270b7d86b805856db637983508c58ad 100644 (file)
@@ -69,6 +69,11 @@ class AbsoluteConcatenation(dns.exception.DNSException):
     empty name to an absolute name."""
     pass
 
+class NoParent(dns.exception.DNSException):
+    """Raised if an attempt is made to get the parent of the root name
+    or the empty name."""
+    pass
+
 _escaped = {
     '"' : True,
     '(' : True,
@@ -490,6 +495,16 @@ class Name(object):
                 return self.derelativize(origin)
         else:
             return self
+
+    def parent(self):
+        """Return the parent of the name.
+        @rtype: dns.name.Name object
+        @raises NoParent: the name is either the root name or the empty name,
+        and thus has no parent.
+        """
+        if self == root or self == empty:
+            raise NoParent
+        return Name(self.labels[1:])
         
 root = Name([''])
 empty = Name([])
index 24abfcd9de2be4c062211688a9527cfdeb9da8cf..cc7ced5f000eb903bd758a2d03e87b034c72089e 100644 (file)
@@ -589,5 +589,27 @@ class NameTestCase(unittest.TestCase):
             (n, cused) = dns.name.from_wire(w, 0)
         self.failUnlessRaises(dns.name.BadLabelType, bad)
 
+    def testParent1(self):
+        n = dns.name.from_text('foo.bar.')
+        self.failUnless(n.parent() == dns.name.from_text('bar.'))
+        self.failUnless(n.parent().parent() == dns.name.root)
+
+    def testParent2(self):
+        n = dns.name.from_text('foo.bar', None)
+        self.failUnless(n.parent() == dns.name.from_text('bar', None))
+        self.failUnless(n.parent().parent() == dns.name.empty)
+
+    def testParent3(self):
+        def bad():
+            n = dns.name.root
+            n.parent()
+        self.failUnlessRaises(dns.name.NoParent, bad)
+
+    def testParent4(self):
+        def bad():
+            n = dns.name.empty
+            n.parent()
+        self.failUnlessRaises(dns.name.NoParent, bad)
+
 if __name__ == '__main__':
     unittest.main()