diff options
author | Bob Halley <halley@dnspython.org> | 2005-10-06 06:25:52 +0000 |
---|---|---|
committer | Bob Halley <halley@dnspython.org> | 2005-10-06 06:25:52 +0000 |
commit | 7abb1f23f3c44901c0bcc4aa183c30548e6a1786 (patch) | |
tree | 6e877bc759ab7663ffd0611cf1eec40a48917891 | |
parent | 2cf8da1e004f17dca34c6f4eee12a59ab55b37d9 (diff) | |
download | dnspython-7abb1f23f3c44901c0bcc4aa183c30548e6a1786.tar.gz |
add dns.name.Name.parent()
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | dns/name.py | 15 | ||||
-rw-r--r-- | tests/name.py | 22 |
3 files changed, 42 insertions, 0 deletions
@@ -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 diff --git a/dns/name.py b/dns/name.py index 3bf5b6e..c5e4b9a 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 24abfcd..cc7ced5 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() |