From: Bob Halley Date: Thu, 6 Oct 2005 06:25:52 +0000 (+0000) Subject: add dns.name.Name.parent() X-Git-Tag: v1.3.5~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7abb1f23f3c44901c0bcc4aa183c30548e6a1786;p=thirdparty%2Fdnspython.git add dns.name.Name.parent() --- diff --git a/ChangeLog b/ChangeLog index 226fa768..e68b8f6c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2005-10-06 Bob Halley + + * dns/name.py: Added the parent() method, which returns the + parent of a name. + 2005-10-01 Bob Halley * dns/resolver.py: Added zone_for_name() helper, which returns diff --git a/dns/name.py b/dns/name.py index 3bf5b6ed..c5e4b9a0 100644 --- a/dns/name.py +++ b/dns/name.py @@ -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([]) diff --git a/tests/name.py b/tests/name.py index 24abfcd9..cc7ced5f 100644 --- a/tests/name.py +++ b/tests/name.py @@ -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()