aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2010-07-08 16:24:37 +0000
committerRob Austein <sra@hactrn.net>2010-07-08 16:24:37 +0000
commit9d67afc3819ff737f3ad4be2a71983943a59c13e (patch)
tree8546170acb34b7a4615e8de2fd12e7368610bb82
parent5272332c6f1ae1b4cb526e98facdd73dcf0b4e38 (diff)
.from_strings() methods
svn path=/rpkid/rpki/resource_set.py; revision=3366
-rw-r--r--rpkid/rpki/resource_set.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/rpkid/rpki/resource_set.py b/rpkid/rpki/resource_set.py
index 3755706f..4a82c85e 100644
--- a/rpkid/rpki/resource_set.py
+++ b/rpkid/rpki/resource_set.py
@@ -65,7 +65,8 @@ class resource_range(object):
"""
Initialize and sanity check a resource_range.
"""
- assert min <= max, "Mis-ordered range: %s before %s" % (str(min), str(max))
+ assert min.__class__ is max.__class__, "Type mismatch, %r doesn't match %r" % (a.__class__, b.__class__)
+ assert min <= max, "Mis-ordered range: %s before %s" % (min, max)
self.min = min
self.max = max
@@ -118,6 +119,15 @@ class resource_range_as(resource_range):
else:
return cls(long(x), long(x))
+ @classmethod
+ def from_strings(cls, a, b = None):
+ """
+ Construct ASN range from strings.
+ """
+ if b is None:
+ b = a
+ return cls(long(a), long(b))
+
class resource_range_ip(resource_range):
"""
Range of (generic) IP addresses.
@@ -217,6 +227,26 @@ class resource_range_ip(resource_range):
result.append(self.make_prefix(min, self.datum_type.bits - bits))
min = self.datum_type(min + mask + 1)
+ @classmethod
+ def from_strings(cls, a, b = None):
+ """
+ Construct IP address range from strings.
+ """
+ if b is None:
+ b = a
+ a = rpki.ipaddrs.parse(a)
+ b = rpki.ipaddrs.parse(b)
+ if a.__class__ is not b.__class__:
+ raise TypeError
+ if cls is resource_range_ip:
+ if isinstance(a, rpki.ipaddrs.v4addr):
+ return resource_range_ipv4(a, b)
+ if isinstance(a, rpki.ipaddrs.v6addr):
+ return resource_range_ipv6(a, b)
+ elif isinstance(a, cls.datum_type):
+ return cls(a, b)
+ raise TypeError
+
class resource_range_ipv4(resource_range_ip):
"""
Range of IPv4 addresses.