IPy => ipaddress

This commit is contained in:
Emmanuel Garette 2019-02-24 11:53:30 +01:00
parent 715289a25a
commit 7c710d50b9
6 changed files with 73 additions and 64 deletions

View file

@ -52,10 +52,10 @@ def test_ip_reserved():
od = OptionDescription('od', '', [a, b, c])
warnings.simplefilter("always", ValueWarning)
cfg = Config(od)
raises(ValueError, "cfg.option('a').value.set('226.94.1.1')")
cfg.option('b').value.set('226.94.1.1')
raises(ValueError, "cfg.option('a').value.set('240.94.1.1')")
cfg.option('b').value.set('240.94.1.1')
with warnings.catch_warnings(record=True) as w:
cfg.option('c').value.set('226.94.1.1')
cfg.option('c').value.set('240.94.1.1')
assert len(w) == 1

View file

@ -18,7 +18,7 @@
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from IPy import IP
from ipaddress import ip_address, IPv4Address, ip_network
from ..error import ConfigError
from ..setting import undefined, Undefined, OptionBag
@ -42,7 +42,8 @@ class BroadcastOption(Option):
if val.startswith("0") and len(val) > 1:
raise ValueError()
try:
IP('{0}/32'.format(value))
if not isinstance(ip_address(value), IPv4Address):
raise ValueError()
except ValueError:
raise ValueError()
@ -57,7 +58,7 @@ class BroadcastOption(Option):
if None in vals:
return
broadcast, network, netmask = vals
if IP('{0}/{1}'.format(network, netmask)).broadcast() != IP(broadcast):
if ip_network('{0}/{1}'.format(network, netmask)).broadcast_address != ip_address(broadcast):
raise ValueError(_('broadcast "{4}" invalid with network {0}/{1} ("{2}"/"{3}")'
'').format(network,
netmask,

View file

@ -19,7 +19,7 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
import re
from IPy import IP
from ipaddress import ip_address, IPv4Address
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
@ -113,14 +113,16 @@ class DomainnameOption(StrOption):
raise ValueError(_('invalid string'))
if self.impl_get_extra('_allow_ip') is True:
try:
IP('{0}/32'.format(value))
if not isinstance(ip_address(value), IPv4Address):
raise ValueError()
except ValueError:
pass
else:
return
else:
try:
IP('{0}/32'.format(value))
if not isinstance(ip_address(value), IPv4Address):
raise ValueError()
except ValueError:
pass
else:

View file

@ -18,7 +18,7 @@
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from IPy import IP
from ipaddress import ip_address, ip_network, IPv4Address
from ..error import ConfigError
from ..setting import undefined, Undefined, OptionBag
@ -78,21 +78,22 @@ class IPOption(StrOption):
raise ValueError()
# 'standard' validation
try:
IP('{0}/32'.format(value))
if not isinstance(ip_address(value), IPv4Address):
raise ValueError()
except ValueError:
raise ValueError()
def _second_level_validation(self,
value,
warnings_only):
ip = IP('{0}/32'.format(value))
if not self.impl_get_extra('_allow_reserved') and ip.iptype() == 'RESERVED':
ip = ip_address(value)
if not self.impl_get_extra('_allow_reserved') and ip.is_reserved:
if warnings_only:
msg = _("shouldn't in reserved class")
else:
msg = _("mustn't be in reserved class")
raise ValueError(msg)
if self.impl_get_extra('_private_only') and ip.iptype() != 'PRIVATE':
if self.impl_get_extra('_private_only') and not ip.is_private:
if warnings_only:
msg = _("should be in private class")
else:
@ -110,8 +111,8 @@ class IPOption(StrOption):
if len(vals) != 3 or None in vals:
return
ip, network, netmask = vals
if IP(ip) not in IP('{0}/{1}'.format(network,
netmask)):
if ip_address(ip) not in ip_network('{0}/{1}'.format(network,
netmask)):
msg = _('"{4}" is not in network "{0}"/"{1}" ("{2}"/"{3}")')
raise ValueError(msg.format(network,
netmask,

View file

@ -18,7 +18,7 @@
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from IPy import IP
from ipaddress import ip_interface, ip_network
from ..error import ConfigError
from ..setting import undefined, OptionBag, Undefined
@ -44,7 +44,7 @@ class NetmaskOption(StrOption):
if val.startswith("0") and len(val) > 1:
raise ValueError()
try:
IP('0.0.0.0/{0}'.format(value))
ip_network('0.0.0.0/{0}'.format(value))
except ValueError:
raise ValueError()
@ -59,13 +59,22 @@ class NetmaskOption(StrOption):
raise ConfigError(_('network_netmask needs a network and a netmask'))
if None in vals or len(vals) != 2:
return
return self.__cons_netmask(current_opt,
opts,
vals[0],
vals[1],
False,
warnings_only,
'network')
msg = None
val_netmask, val_ipnetwork = vals
try:
ip_network('{0}/{1}'.format(val_ipnetwork, val_netmask))
except ValueError:
if current_opt == opts[1]:
raise ValueError(_('with netmask "{0}" ("{1}")').format(val_netmask, opts[0].impl_get_display_name()))
else:
raise ValueError(_('with network "{0}" ("{1}")').format(val_ipnetwork, opts[1].impl_get_display_name()))
if msg is not None:
self.raise_err(msg,
val_netmask,
val_ipnetwork,
current_opt,
opts,
'network')
def _cons_ip_netmask(self,
current_opt,
@ -78,50 +87,45 @@ class NetmaskOption(StrOption):
raise ConfigError(_('ip_netmask needs an IP and a netmask'))
if None in vals or len(vals) != 2:
return
self.__cons_netmask(current_opt,
opts,
vals[0],
vals[1],
True,
warnings_only,
'ip')
def __cons_netmask(self,
current_opt,
opts,
val_netmask,
val_ipnetwork,
make_net,
warnings_only,
typ):
msg = None
val_netmask, val_ipnetwork = vals
try:
ip = IP('{0}/{1}'.format(val_ipnetwork, val_netmask), make_net=make_net)
#if cidr == 32, ip same has network
if make_net and ip.prefixlen() != 32:
val_ip = IP(val_ipnetwork)
if ip.net() == val_ip:
ip = ip_interface('{0}/{1}'.format(val_ipnetwork, val_netmask))
network = ip.network
#if not ip same has network
if str(network.netmask) != '255.255.255.255':
if ip.ip == network.network_address:
if current_opt == opts[1]:
msg = _('this is a network with netmask "{0}" ("{1}")')
else:
msg = _('this is a network with {2} "{0}" ("{1}")')
elif ip.broadcast() == val_ip:
elif ip.ip == network.broadcast_address:
if current_opt == opts[1]:
msg = _('this is a broadcast with netmask "{0}" ("{1}")')
else:
msg = _('this is a broadcast with {2} "{0}" ("{1}")')
except ValueError:
if not make_net:
if current_opt == opts[1]:
raise ValueError(_('with netmask "{0}" ("{1}")').format(val_netmask, opts[0].impl_get_display_name()))
else:
raise ValueError(_('with {2} "{0}" ("{1}")').format(val_ipnetwork, opts[1].impl_get_display_name(), typ))
pass
if msg is not None:
if current_opt == opts[1]:
raise ValueError(msg.format(val_netmask,
opts[1].impl_get_display_name()))
else:
raise ValueError(msg.format(val_ipnetwork,
opts[0].impl_get_display_name(),
typ))
self.raise_err(msg,
val_netmask,
val_ipnetwork,
current_opt,
opts,
'IP')
def raise_err(self,
msg,
val_netmask,
val_ipnetwork,
current_opt,
opts,
typ):
if current_opt == opts[1]:
raise ValueError(msg.format(val_netmask,
opts[1].impl_get_display_name()))
else:
raise ValueError(msg.format(val_ipnetwork,
opts[0].impl_get_display_name(),
typ))

View file

@ -18,7 +18,7 @@
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from IPy import IP
from ipaddress import ip_address, IPv4Address
from ..setting import undefined
from ..i18n import _
@ -42,15 +42,16 @@ class NetworkOption(Option):
if val.startswith("0") and len(val) > 1:
raise ValueError()
try:
IP(value)
if not isinstance(ip_address(value), IPv4Address):
raise ValueError()
except ValueError:
raise ValueError()
def _second_level_validation(self,
value,
warnings_only):
ip = IP(value)
if ip.iptype() == 'RESERVED':
ip = ip_address(value)
if ip.is_reserved:
if warnings_only:
msg = _("shouldn't be in reserved class")
else: