1 # ldd - DNS implementation in Python
2 # Copyright (C) 2006 Fredrik Tolf <fredrik@dolda2000.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 class DNNotIn(Exception):
19 def __init__(self, a, b):
24 return str(self.a) + " not in " + str(self.b)
27 "A class for abstract representations of domain names"
29 def __init__(self, parts, rooted):
35 if len(self.parts) > 0:
36 for p in self.parts[:-1]:
38 ret = ret + self.parts[-1]
45 raise Exception("cannot append to a rooted domain name")
46 return(domainname(self.parts + y.parts, y.rooted))
48 def __getitem__(self, y):
49 return domainname([self.parts[y]], self.rooted and (y == -1 or y == len(self.parts) - 1))
51 def __getslice__(self, i, j):
52 return domainname(self.parts[i:j], self.rooted and j >= len(self.parts))
55 return len(self.parts)
60 if self.rooted != y.rooted:
62 if len(self.parts) != len(y.parts):
64 for i in range(len(self.parts)):
65 if self.parts[i].lower() != y.parts[i].lower():
70 return not self.__eq__(y)
72 def __contains__(self, y):
73 if len(self) > len(y):
76 return self.rooted == y.rooted
77 return y[-len(self):] == self
81 raise DNNotIn(self, y)
82 return self[:len(self) - len(y)]
86 for part in self.parts:
87 ret = ret ^ hash(part)
100 class DNError(Exception):
103 def __init__(self, kind):
106 return {1: "empty part",
107 2: "illegal character"}[self.kind]
109 def fromstring(name):
112 return domainname([], True)
114 return domainname([], False)
115 while name.find('.') >= 0:
118 raise DNError(DNError.emptypart)
122 raise DNError(DNError.illegalchar)
124 name = name[cur + 1:]
130 return domainname(parts, rooted)