simplify netmaskoption

This commit is contained in:
Emmanuel Garette 2019-02-25 19:10:20 +01:00
parent 9c459e21b1
commit 0e20318a9b
1 changed files with 19 additions and 20 deletions

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,
else: opt_netmask.impl_get_display_name()))
msg = _('IP "{0}" ("{1}") is the network') elif ip.ip == ip.network.broadcast_address:
elif ip.ip == network.broadcast_address: raise ValueError(_('this is a broadcast with netmask "{0}" ("{1}")'
if not _cidr and current_opt == opts[1]: '').format(val_netmask,
msg = _('this is a broadcast with netmask "{0}" ("{1}")') opt_netmask.impl_get_display_name()))
else: else:
msg = _('IP "{0}" ("{1}") is the broadcast') if ip.ip == ip.network.network_address:
if msg is not None: raise ValueError(_('IP "{0}" ("{1}") is the network'
if not _cidr and current_opt == opts[1]: '').format(val_ip,
raise ValueError(msg.format(val_netmask, opt_ip.impl_get_display_name()))
opts[0].impl_get_display_name())) elif ip.ip == ip.network.broadcast_address:
else: raise ValueError(_('IP "{0}" ("{1}") is the broadcast'
raise ValueError(msg.format(val_ip, '')format(val_ip,
opts[1].impl_get_display_name())) opt_ip.impl_get_display_name()))