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]) od = OptionDescription('od', '', [a, b, c])
warnings.simplefilter("always", ValueWarning) warnings.simplefilter("always", ValueWarning)
cfg = Config(od) cfg = Config(od)
raises(ValueError, "cfg.option('a').value.set('226.94.1.1')") raises(ValueError, "cfg.option('a').value.set('240.94.1.1')")
cfg.option('b').value.set('226.94.1.1') cfg.option('b').value.set('240.94.1.1')
with warnings.catch_warnings(record=True) as w: 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 assert len(w) == 1

View File

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

View File

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

View File

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

View File

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