summaryrefslogtreecommitdiffstats
path: root/netaddr/contrib
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 17:45:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 17:45:09 +0000
commitda1a8f12d7a38f67f3f464aaaffa851f929ae4ea (patch)
tree677688f3aeab7f324f266d106770165708522c2c /netaddr/contrib
parentInitial commit. (diff)
downloadpython-netaddr-7e869254c654d0b5d5b8673ed3f143942f70296b.tar.xz
python-netaddr-7e869254c654d0b5d5b8673ed3f143942f70296b.zip
Adding upstream version 0.10.1.upstream/0.10.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'netaddr/contrib')
-rw-r--r--netaddr/contrib/__init__.py12
-rw-r--r--netaddr/contrib/subnet_splitter.py46
2 files changed, 58 insertions, 0 deletions
diff --git a/netaddr/contrib/__init__.py b/netaddr/contrib/__init__.py
new file mode 100644
index 0000000..7449cd6
--- /dev/null
+++ b/netaddr/contrib/__init__.py
@@ -0,0 +1,12 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2008 by David P. D. Moss. All rights reserved.
+#
+# Released under the BSD license. See the LICENSE file for details.
+#-----------------------------------------------------------------------------
+"""
+The netaddr.contrib namespace for non-core code contributed by users.
+
+It is a testing ground for new ideas. Depending on the interest in
+functionality found here, code may find its way into the core in various
+ways, either as is or as additions to existing APIs.
+"""
diff --git a/netaddr/contrib/subnet_splitter.py b/netaddr/contrib/subnet_splitter.py
new file mode 100644
index 0000000..875b113
--- /dev/null
+++ b/netaddr/contrib/subnet_splitter.py
@@ -0,0 +1,46 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2008 by David P. D. Moss. All rights reserved.
+#
+# Released under the BSD license. See the LICENSE file for details.
+#-----------------------------------------------------------------------------
+from netaddr.ip import IPNetwork, cidr_exclude, cidr_merge
+
+
+class SubnetSplitter(object):
+ """
+ A handy utility class that takes a single (large) subnet and allows
+ smaller subnet within its range to be extracted by CIDR prefix. Any
+ leaving address space is available for subsequent extractions until
+ all space is exhausted.
+ """
+ def __init__(self, base_cidr):
+ """
+ Constructor.
+
+ :param base_cidr: an IPv4 or IPv6 address with a CIDR prefix.
+ (see IPNetwork.__init__ for full details).
+ """
+ self._subnets = set([IPNetwork(base_cidr)])
+
+ def extract_subnet(self, prefix, count=None):
+ """Extract 1 or more subnets of size specified by CIDR prefix."""
+ for cidr in self.available_subnets():
+ subnets = list(cidr.subnet(prefix, count=count))
+ if not subnets:
+ continue
+ self.remove_subnet(cidr)
+ self._subnets = self._subnets.union(
+ set(
+ cidr_exclude(cidr, cidr_merge(subnets)[0])
+ )
+ )
+ return subnets
+ return []
+
+ def available_subnets(self):
+ """Returns a list of the currently available subnets."""
+ return sorted(self._subnets, key=lambda x: x.prefixlen, reverse=True)
+
+ def remove_subnet(self, ip_network):
+ """Remove a specified IPNetwork from available address space."""
+ self._subnets.remove(ip_network)