simplify netmaskoption

This commit is contained in:
Emmanuel Garette 2019-02-25 19:10:20 +01:00
parent 9c459e21b1
commit 0e20318a9b

View file

@ -77,29 +77,28 @@ class NetmaskOption(StrOption):
warnings_only, warnings_only,
context, context,
_cidr=False): _cidr=False):
# opts must be (netmask, ip) options
if context is undefined and len(vals) != 2: if context is undefined and len(vals) != 2:
raise ConfigError(_('ip_netmask needs an IP and a netmask')) raise ConfigError(_('ip_netmask needs an IP and a netmask'))
if None in vals or len(vals) != 2: if None in vals or len(vals) != 2:
return return
msg = None
val_netmask, val_ip = vals val_netmask, val_ip = vals
opt_netmask, opt_ip = opts
ip = ip_interface('{0}/{1}'.format(val_ip, val_netmask)) ip = ip_interface('{0}/{1}'.format(val_ip, val_netmask))
network = ip.network if not _cidr and current_opt == opt_ip
if ip.ip == network.network_address: if ip.ip == ip.network.network_address:
if not _cidr and current_opt == opts[1]: raise ValueError( _('this is a network with netmask "{0}" ("{1}")'
msg = _('this is a network with netmask "{0}" ("{1}")') '').format(val_netmask,
opt_netmask.impl_get_display_name()))
elif ip.ip == ip.network.broadcast_address:
raise ValueError(_('this is a broadcast with netmask "{0}" ("{1}")'
'').format(val_netmask,
opt_netmask.impl_get_display_name()))
else: else:
msg = _('IP "{0}" ("{1}") is the network') if ip.ip == ip.network.network_address:
elif ip.ip == network.broadcast_address: raise ValueError(_('IP "{0}" ("{1}") is the network'
if not _cidr and current_opt == opts[1]: '').format(val_ip,
msg = _('this is a broadcast with netmask "{0}" ("{1}")') opt_ip.impl_get_display_name()))
else: elif ip.ip == ip.network.broadcast_address:
msg = _('IP "{0}" ("{1}") is the broadcast') raise ValueError(_('IP "{0}" ("{1}") is the broadcast'
if msg is not None: '')format(val_ip,
if not _cidr and current_opt == opts[1]: opt_ip.impl_get_display_name()))
raise ValueError(msg.format(val_netmask,
opts[0].impl_get_display_name()))
else:
raise ValueError(msg.format(val_ip,
opts[1].impl_get_display_name()))