5
0
mirror of https://github.com/cwinfo/yggdrasil-map synced 2024-11-26 04:41:34 +00:00
yggdrasil-map/web/graph.py

49 lines
977 B
Python
Raw Normal View History

2014-06-04 18:41:33 +00:00
import re
class Node:
def __init__(self, ip, version=None, label=None):
2014-06-04 18:41:33 +00:00
if not valid_cjdns_ip(ip):
raise ValueError('Invalid IP address')
if not valid_version(version):
raise ValueError('Invalid version')
self.ip = ip
2014-06-04 18:41:33 +00:00
self.version = int(version)
self.label = ip[-4:] if label == None else label
def __lt__(self, b):
return self.ip < b.ip
def __repr__(self):
return 'Node(ip="%s", version=%s, label="%s")' % (
self.ip,
self.version,
self.label)
class Edge:
def __init__(self, a, b):
self.a, self.b = sorted([a, b])
2014-06-02 20:22:37 +00:00
def __eq__(self, that):
return self.a.ip == that.a.ip and self.b.ip == that.b.ip
def __repr__(self):
return 'Edge(a.ip="%s", b.ip="%s")' % (
self.a.ip,
self.b.ip)
2014-06-04 18:41:33 +00:00
_re_cjdns_ip = re.compile(r'^fc[0-9a-f]{2}(:[0-9a-f]{4}){7}$', re.IGNORECASE)
def valid_cjdns_ip(ip):
return _re_cjdns_ip.match(ip) != None
def valid_version(version):
try:
return int(version) < 20
except ValueError:
return False