tiramisu/tiramisu/option/domainnameoption.py

147 lines
5.8 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
from IPy import IP
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 DomainnameOption(StrOption):
2017-07-24 19:04:18 +02:00
"""represents the choice of a domain name
netbios: for MS domain
hostname: to identify the device
domainname:
fqdn: with tld, not supported yet
"""
__slots__ = tuple()
_display_name = _('domain name')
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_ip=False,
type_='domainname',
warnings_only=False,
allow_without_dot=False):
2017-07-24 19:04:18 +02:00
if type_ not in ['netbios', 'hostname', 'domainname']:
raise ValueError(_('unknown type_ {0} for hostname').format(type_))
extra = {'_dom_type': type_}
if allow_ip not in [True, False]:
raise ValueError(_('allow_ip must be a boolean'))
if allow_without_dot not in [True, False]:
raise ValueError(_('allow_without_dot must be a boolean'))
extra['_allow_ip'] = allow_ip
extra['_allow_without_dot'] = allow_without_dot
if type_ == 'domainname':
if allow_without_dot:
min_time = 0
else:
min_time = 1
regexp = r'((?!-)[a-z0-9-]{{{1},{0}}}\.){{{1},}}[a-z0-9-]{{1,{0}}}'.format(self._get_len(type_), min_time)
else:
regexp = r'((?!-)[a-z0-9-]{{1,{0}}})'.format(self._get_len(type_))
if allow_ip:
regexp = r'^(?:{0}|(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){{3}}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))$'.format(regexp)
else:
regexp = r'^{0}$'.format(regexp)
extra['_domain_re'] = re.compile(regexp)
extra['_has_upper'] = re.compile('[A-Z]')
2017-12-13 22:15:34 +01:00
super(DomainnameOption, 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)
def _get_len(self, type_):
if type_ == 'netbios':
return 15
else:
return 63
2017-12-13 22:15:34 +01:00
def _validate(self,
2018-11-15 18:35:14 +01:00
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
2017-07-24 19:04:18 +02:00
def _valid_length(val):
if len(val) < 1:
2017-12-13 22:15:34 +01:00
raise ValueError(_("invalid length (min 1)"))
2017-07-24 19:04:18 +02:00
if len(val) > part_name_length:
2017-12-13 22:15:34 +01:00
raise ValueError(_("invalid length (max {0})"
2017-07-24 19:04:18 +02:00
"").format(part_name_length))
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_ip') is True:
2017-07-24 19:04:18 +02:00
try:
IP('{0}/32'.format(value))
except ValueError:
pass
2017-12-13 22:15:34 +01:00
else:
return
2017-07-24 19:04:18 +02:00
else:
try:
IP('{0}/32'.format(value))
except ValueError:
pass
else:
2017-12-13 22:15:34 +01:00
raise ValueError(_('must not be an IP'))
2018-09-30 11:36:09 +02:00
part_name_length = self._get_len(self.impl_get_extra('_dom_type'))
if self.impl_get_extra('_dom_type') == 'domainname':
if not self.impl_get_extra('_allow_without_dot') and not "." in value:
2017-12-13 22:15:34 +01:00
raise ValueError(_("must have dot"))
2017-07-24 19:04:18 +02:00
if len(value) > 255:
2017-12-13 22:15:34 +01:00
raise ValueError(_("invalid length (max 255)"))
2017-07-24 19:04:18 +02:00
for dom in value.split('.'):
2017-12-13 22:15:34 +01:00
_valid_length(dom)
2017-07-24 19:04:18 +02:00
else:
2017-12-13 22:15:34 +01:00
_valid_length(value)
2017-07-24 19:04:18 +02:00
def _second_level_validation(self, value, warnings_only):
2018-09-30 11:36:09 +02:00
if self.impl_get_extra('_has_upper').search(value):
2017-12-13 22:15:34 +01:00
raise ValueError(_('some characters are uppercase'))
2018-09-30 11:36:09 +02:00
if not self.impl_get_extra('_domain_re').search(value):
2017-07-24 19:04:18 +02:00
if warnings_only:
2017-12-13 22:15:34 +01:00
raise ValueError(_('some characters may cause problems'))
2017-07-24 19:04:18 +02:00
else:
2017-12-13 22:15:34 +01:00
raise ValueError()