tiramisu/tiramisu/option/portoption.py

125 lines
5.0 KiB
Python
Raw Normal View History

2017-07-24 19:04:18 +02:00
# -*- coding: utf-8 -*-
2018-01-26 07:33:47 +01:00
# Copyright (C) 2017-2018 Team tiramisu (see AUTHORS for all contributors)
2017-07-24 19:04:18 +02:00
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
import re
import sys
2018-11-15 18:35:14 +01:00
from typing import Union
2017-07-24 19:04:18 +02:00
2018-11-15 18:35:14 +01:00
from ..setting import undefined, Undefined, OptionBag
2017-07-24 19:04:18 +02:00
from ..i18n import _
2017-07-24 20:39:01 +02:00
from .option import Option
from .stroption import StrOption
2017-07-24 19:04:18 +02:00
class PortOption(StrOption):
2017-07-24 19:04:18 +02:00
"""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__ = tuple()
port_re = re.compile(r"^[0-9]*$")
_display_name = _('port')
2017-12-13 22:15:34 +01:00
def __init__(self,
name,
doc,
default=None,
default_multi=None,
requires=None,
multi=False,
callback=None,
callback_params=None,
validator=None,
validator_params=None,
properties=None,
allow_range=False,
allow_zero=False,
allow_wellknown=True,
allow_registred=True,
allow_private=False,
warnings_only=False):
2017-07-24 19:04:18 +02:00
extra = {'_allow_range': allow_range,
'_min_value': None,
'_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 extra['_min_value'] is None:
if allowed:
extra['_min_value'] = ports_min[index]
elif not allowed:
is_finally = True
elif allowed and is_finally:
raise ValueError(_('inconsistency in allowed range'))
if allowed:
extra['_max_value'] = ports_max[index]
if extra['_max_value'] is None:
raise ValueError(_('max value is empty'))
2017-12-13 22:15:34 +01:00
super(PortOption, self).__init__(name,
doc,
default=default,
2017-07-24 19:04:18 +02:00
default_multi=default_multi,
callback=callback,
callback_params=callback_params,
requires=requires,
multi=multi,
validator=validator,
validator_params=validator_params,
properties=properties,
warnings_only=warnings_only,
extra=extra)
2017-12-13 22:15:34 +01:00
def _validate(self,
2018-11-15 18:35:14 +01:00
value: Union[int,str],
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
2018-11-15 16:17:39 +01:00
if not isinstance(value, str):
raise ValueError(_('invalid string'))
2018-09-30 11:36:09 +02:00
if self.impl_get_extra('_allow_range') and ":" in str(value):
2017-07-24 19:04:18 +02:00
value = str(value).split(':')
if len(value) != 2:
2017-12-13 22:15:34 +01:00
raise ValueError(_('range must have two values only'))
2017-07-24 19:04:18 +02:00
if not value[0] < value[1]:
2017-12-13 22:15:34 +01:00
raise ValueError(_('first port in range must be'
' smaller than the second one'))
2017-07-24 19:04:18 +02:00
else:
value = [value]
for val in value:
if not self.port_re.search(val):
2017-12-13 22:15:34 +01:00
raise ValueError()
2017-07-24 19:04:18 +02:00
val = int(val)
2018-09-30 11:36:09 +02:00
if not self.impl_get_extra('_min_value') <= val <= self.impl_get_extra('_max_value'):
2017-12-13 22:15:34 +01:00
raise ValueError(_('must be an integer between {0} '
2018-09-30 11:36:09 +02:00
'and {1}').format(self.impl_get_extra('_min_value'),
self.impl_get_extra('_max_value')))