2014-06-04 18:41:33 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
2014-05-30 14:34:00 +00:00
|
|
|
class Node:
|
2015-11-21 11:47:21 +00:00
|
|
|
def __init__(self, ip, version=None, label=None):
|
|
|
|
if not valid_cjdns_ip(ip):
|
|
|
|
raise ValueError('Invalid IP address')
|
|
|
|
if not valid_version(version):
|
|
|
|
raise ValueError('Invalid version')
|
2014-06-04 18:41:33 +00:00
|
|
|
|
2015-11-21 11:47:21 +00:00
|
|
|
self.ip = ip
|
|
|
|
self.version = int(version)
|
|
|
|
self.label = ip[-4:] if label == None else label
|
2014-05-30 14:34:00 +00:00
|
|
|
|
2015-11-21 11:47:21 +00:00
|
|
|
def __lt__(self, b):
|
|
|
|
return self.ip < b.ip
|
2014-05-30 14:34:00 +00:00
|
|
|
|
2015-11-21 11:47:21 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return 'Node(ip="%s", version=%s, label="%s")' % (
|
|
|
|
self.ip,
|
|
|
|
self.version,
|
|
|
|
self.label)
|
2014-05-30 14:34:00 +00:00
|
|
|
|
|
|
|
class Edge:
|
2015-11-21 11:47:21 +00:00
|
|
|
def __init__(self, a, b):
|
|
|
|
self.a, self.b = sorted([a, b])
|
2014-05-30 14:34:00 +00:00
|
|
|
|
2015-11-21 11:47:21 +00:00
|
|
|
def __eq__(self, that):
|
|
|
|
return self.a.ip == that.a.ip and self.b.ip == that.b.ip
|
2014-05-30 14:34:00 +00:00
|
|
|
|
2015-11-21 11:47:21 +00:00
|
|
|
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):
|
2015-11-21 11:47:21 +00:00
|
|
|
return _re_cjdns_ip.match(ip) != None
|
2014-06-04 18:41:33 +00:00
|
|
|
|
|
|
|
def valid_version(version):
|
2015-11-21 11:47:21 +00:00
|
|
|
try:
|
2017-07-02 19:23:27 +00:00
|
|
|
return int(version) < 30
|
2015-11-21 11:47:21 +00:00
|
|
|
except ValueError:
|
|
|
|
return False
|
2014-06-04 18:41:33 +00:00
|
|
|
|