Clear the sql cache for dns and dhcp, fixing #42.
[invirt/packages/invirt-dhcp.git] / code / pydhcplib / pydhcplib / type_hw_addr.py
1 # Anemon Dhcp
2 # Copyright (C) 2005 Mathieu Ignacio -- mignacio@april.org
3 #
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.
8 #
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.
13 #
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
17
18
19 from binascii import unhexlify,hexlify
20
21 # Check and convert hardware/nic/mac address type
22 class hwmac:
23     def __init__(self,value="00:00:00:00:00:00") :
24         self._hw_numlist = []
25         self._hw_string = ""
26         hw_type = type(value)
27         if hw_type == str :
28             self._hw_string = value
29             self._StringToNumlist(value)
30             self._CheckNumList()
31         elif hw_type == list :
32             self._hw_numlist = value
33             self._CheckNumList()
34             self._NumlistToString()
35         else : raise TypeError , 'hwmac init : Valid types are  tr and list'
36
37
38
39     # Check if _hw_numlist is valid and raise error if not.
40     def _CheckNumList(self) :
41         if len(self._hw_numlist) != 6 : raise ValueError , "hwmac : wrong list length."
42         for part in self._hw_numlist :
43             if type (part) != int : raise TypeError , "hwmac : each element of list must be int"
44             if part < 0 or part > 255 : raise ValueError , "hwmac : need numbers between 0 and 255."
45         return True
46
47
48     def _StringToNumlist(self,value):
49         self._hw_string = self._hw_string.replace("-",":").replace(".",":")
50         self._hw_string = self._hw_string.lower()
51
52
53         for twochar in self._hw_string.split(":"):
54             self._hw_numlist.append(ord(unhexlify(twochar)))
55             
56     # Convert NumList type ip to String type ip
57     def _NumlistToString(self) :
58         self._hw_string = ":".join(map(hexlify,map(chr,self._hw_numlist)))
59
60     # Convert String type ip to NumList type ip
61     # return ip string
62     def str(self) :
63         return self._hw_string
64     __str__=str
65
66     # return ip list (useful for DhcpPacket class)
67     def list(self) :
68         return self._hw_numlist
69
70     def __hash__(self) :
71         return self._hw_string.__hash__()
72
73     def __repr__(self) :
74         return self._hw_string
75
76     def __cmp__(self,other) :
77         if self._hw_string == other : return 0
78         return 1
79
80     def __nonzero__(self) :
81         if self._hw_string != "00:00:00:00:00:00" : return 1
82         return 0
83
84
85
86