add new option's type: PortOption
This commit is contained in:
@ -277,8 +277,8 @@ class Option(BaseInformation):
|
||||
if value is not None and ((self._validator is not None and
|
||||
not val_validator()) or
|
||||
not self._validate(value)):
|
||||
raise ValueError(_("invalid value {0} for option {1}"
|
||||
"").format(value, self._name))
|
||||
raise ValueError(_("invalid value {0} for option {1} (type {2})"
|
||||
"").format(value, self._name, self.__class__))
|
||||
if context is not None:
|
||||
descr._valid_consistency(self, value, context, None)
|
||||
else:
|
||||
@ -478,6 +478,7 @@ class IPOption(Option):
|
||||
requires=None, multi=False, callback=None,
|
||||
callback_params=None, validator=None, validator_args=None,
|
||||
properties=None, only_private=False):
|
||||
self._only_private = only_private
|
||||
super(IPOption, self).__init__(name, doc, default=default,
|
||||
default_multi=default_multi,
|
||||
callback=callback,
|
||||
@ -487,7 +488,6 @@ class IPOption(Option):
|
||||
validator=validator,
|
||||
validator_args=validator_args,
|
||||
properties=properties)
|
||||
self._only_private = only_private
|
||||
|
||||
def _validate(self, value):
|
||||
try:
|
||||
@ -499,6 +499,76 @@ class IPOption(Option):
|
||||
return False
|
||||
|
||||
|
||||
class PortOption(Option):
|
||||
"""represents the choice of a port
|
||||
The port numbers are divided into three ranges:
|
||||
the well-known ports,
|
||||
the registered ports,
|
||||
and the dynamic or private ports.
|
||||
You can actived this three range.
|
||||
Port number 0 is reserved and can't be used.
|
||||
see: http://en.wikipedia.org/wiki/Port_numbers
|
||||
"""
|
||||
__slots__ = ('_opt_type', '_allow_range', '_allow_zero', '_min_value',
|
||||
'_max_value')
|
||||
_opt_type = 'port'
|
||||
|
||||
def __init__(self, name, doc, default=None, default_multi=None,
|
||||
requires=None, multi=False, callback=None,
|
||||
callback_params=None, validator=None, validator_args=None,
|
||||
properties=None, allow_range=False, allow_zero=False,
|
||||
allow_wellknown=True, allow_registred=True,
|
||||
allow_private=False):
|
||||
self._allow_range = allow_range
|
||||
self._min_value = None
|
||||
self._max_value = None
|
||||
ports_min = [0, 1, 1024, 49152]
|
||||
ports_max = [0, 1023, 49151, 65535]
|
||||
is_finally = False
|
||||
for index, allowed in enumerate([allow_zero, allow_wellknown, allow_registred, allow_private]):
|
||||
if self._min_value is None:
|
||||
if allowed:
|
||||
self._min_value = ports_min[index]
|
||||
elif not allowed:
|
||||
is_finally = True
|
||||
elif allowed and is_finally:
|
||||
raise ValueError(_('inconsistency in allowed range'))
|
||||
if allowed:
|
||||
self._max_value = ports_max[index]
|
||||
|
||||
if self._max_value is None:
|
||||
raise ValueError(_('max value is empty'))
|
||||
|
||||
super(PortOption, self).__init__(name, doc, default=default,
|
||||
default_multi=default_multi,
|
||||
callback=callback,
|
||||
callback_params=callback_params,
|
||||
requires=requires,
|
||||
multi=multi,
|
||||
validator=validator,
|
||||
validator_args=validator_args,
|
||||
properties=properties)
|
||||
|
||||
def _validate(self, value):
|
||||
try:
|
||||
if self._allow_range and ":" in str(value):
|
||||
value = str(value).split(':')
|
||||
if len(value) != 2:
|
||||
return False
|
||||
if not value[0] < value[1]:
|
||||
return False
|
||||
else:
|
||||
value = [value]
|
||||
|
||||
for val in value:
|
||||
if not self._min_value <= int(val) <= self._max_value:
|
||||
return False
|
||||
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
class NetworkOption(Option):
|
||||
"represents the choice of a network"
|
||||
__slots__ = ('_opt_type',)
|
||||
|
Reference in New Issue
Block a user