def func(self, args):
"""Evaluate an expression of adding and subtracting networks."""
expr = args.expression
accum = [_network_address(expr.pop(0))]
while len(expr) >= 2:
operator = expr.pop(0)
# right-hand side of the expression
rhs = _network_address(expr.pop(0))
if operator in ("+", "add", "merge"):
# add (merge) in a new network
accum = netaddr.cidr_merge(accum + [rhs])
elif operator in ("-", "sub", "remove"):
# subtract (remove) a network
# for each network in accum, remove the RHS from it, then
# chain everything into a single flat generator sequence
# (RHS may be partially contained in more than one accum
# network)
minus_rhs = itertools.chain.from_iterable(
netaddr.cidr_exclude(network, rhs)
for network in accum
)
accum = netaddr.cidr_merge(minus_rhs)
else:
raise CommandParseError("invalid operator '%s'" % operator)
if expr:
self.warn("ignoring extra argument '%s'" % ' '.join(expr))
for i in accum:
print(i)
评论列表
文章目录