2014-02-21 15:27:19 +01:00
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
class NoneDict:
|
|
|
|
"""
|
|
|
|
A NoneDict acts like None but returns a NoneDict for every item in it.
|
|
|
|
|
|
|
|
This is similar to the behaviour of collections.defaultdict in that even
|
|
|
|
previously inexistent keys can be accessed, but there is nothing stored
|
|
|
|
permanently.
|
|
|
|
"""
|
|
|
|
__repr__ = lambda self: 'NoneDict()'
|
|
|
|
__bool__ = lambda self: False
|
|
|
|
__getitem__ = lambda self, k: NoneDict()
|
|
|
|
__json__ = lambda self: None
|
2014-02-22 13:35:34 +01:00
|
|
|
__float__ = lambda self: float('NaN')
|
2014-02-21 15:27:19 +01:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
raise RuntimeError("NoneDict is readonly")
|
|
|
|
|
|
|
|
class casualdict(defaultdict):
|
|
|
|
"""
|
|
|
|
This special defaultdict returns a NoneDict for inexistent items. Also, its
|
|
|
|
items can be accessed as attributed as well.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(NoneDict)
|
|
|
|
__getattr__ = defaultdict.__getitem__
|
|
|
|
__setattr__ = defaultdict.__setitem__
|
|
|
|
|
|
|
|
class Node(casualdict):
|
2012-05-11 14:12:41 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.name = ""
|
2012-06-04 17:33:23 +02:00
|
|
|
self.id = ""
|
2012-05-11 14:12:41 +02:00
|
|
|
self.macs = set()
|
2012-06-11 23:53:45 +02:00
|
|
|
self.interfaces = dict()
|
2012-06-07 22:21:50 +02:00
|
|
|
self.flags = dict({
|
|
|
|
"online": False,
|
|
|
|
"gateway": False,
|
|
|
|
"client": False
|
|
|
|
})
|
2014-02-21 15:27:19 +01:00
|
|
|
super().__init__()
|
2012-05-11 14:12:41 +02:00
|
|
|
|
|
|
|
def add_mac(self, mac):
|
2012-06-06 03:34:52 +02:00
|
|
|
mac = mac.lower()
|
|
|
|
if len(self.macs) == 0:
|
|
|
|
self.id = mac
|
|
|
|
|
|
|
|
self.macs.add(mac)
|
2012-05-11 14:12:41 +02:00
|
|
|
|
2012-06-11 23:53:45 +02:00
|
|
|
self.interfaces[mac] = Interface()
|
|
|
|
|
2012-05-11 14:12:41 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return self.macs.__repr__()
|
|
|
|
|
2014-02-21 15:27:19 +01:00
|
|
|
def export(self):
|
|
|
|
"""
|
|
|
|
Return a dict that contains all attributes of the Node that are supposed to
|
|
|
|
be exported to other applications.
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"id": self.id,
|
|
|
|
"macs": list(self.macs),
|
|
|
|
"geo": self.geo,
|
|
|
|
"firmware": self.software['firmware']['release'],
|
|
|
|
"flags": self.flags
|
|
|
|
}
|
|
|
|
|
2012-06-11 23:53:45 +02:00
|
|
|
class Interface():
|
|
|
|
def __init__(self):
|
|
|
|
self.vpn = False
|