tiramisu/tiramisu/option/portoption.py

121 lines
4.6 KiB
Python
Raw Normal View History

2017-07-24 19:04:18 +02:00
# -*- coding: utf-8 -*-
2024-07-06 14:33:25 +02:00
# Copyright (C) 2017-2024 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
# ____________________________________________________________
2023-05-11 15:44:48 +02:00
"""PortOption
"""
2017-07-24 19:04:18 +02:00
import re
from ..i18n import _
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]*$")
2023-05-11 15:44:48 +02:00
_port = _('port')
2017-07-24 19:04:18 +02:00
2017-12-13 22:15:34 +01:00
def __init__(self,
*args,
allow_range: bool=False,
allow_zero: bool=False,
allow_wellknown: bool=True,
allow_registred: bool=True,
2022-10-01 19:43:49 +02:00
allow_protocol: bool=False,
allow_private: bool=False,
**kwargs) -> None:
2017-12-13 22:15:34 +01:00
2017-07-24 19:04:18 +02:00
extra = {'_allow_range': allow_range,
2022-10-01 19:43:49 +02:00
'_allow_protocol': allow_protocol,
2017-07-24 19:04:18 +02:00
'_min_value': None,
2022-10-01 19:43:49 +02:00
'_max_value': None,
}
2017-07-24 19:04:18 +02:00
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'))
super().__init__(*args,
extra=extra,
**kwargs)
2017-07-24 19:04:18 +02:00
def validate(self,
value: str) -> None:
super().validate(value)
2023-05-11 15:44:48 +02:00
if self.impl_get_extra('_allow_protocol') and (value.startswith('tcp:') or
value.startswith('udp:')):
2022-10-01 19:43:49 +02:00
value = [value[4:]]
elif self.impl_get_extra('_allow_range') and ":" in str(value):
value = value.split(':')
2017-07-24 19:04:18 +02:00
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()
def second_level_validation(self,
value: str,
warnings_only: bool) -> None:
2023-05-11 15:44:48 +02:00
if self.impl_get_extra('_allow_protocol') and (value.startswith('tcp:') or
value.startswith('udp:')):
2022-10-01 19:43:49 +02:00
value = [value[4:]]
elif ':' in value:
value = value.split(':')
else:
value = [value]
for val in value:
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'):
if warnings_only:
msg = 'should be between {0} and {1}'
else:
msg = 'must be between {0} and {1}'
raise ValueError(_(msg).format(self.impl_get_extra('_min_value'),
self.impl_get_extra('_max_value')))