tiramisu/tiramisu/option/domainnameoption.py

146 lines
5.7 KiB
Python
Raw Normal View History

2017-07-24 19:04:18 +02:00
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Team tiramisu (see AUTHORS for all contributors)
#
# 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
from ..setting import undefined
from ..i18n import _
2017-07-24 20:39:01 +02:00
from .option import Option
2017-07-24 19:04:18 +02:00
class DomainnameOption(Option):
"""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,
value,
2017-12-19 23:11:45 +01:00
*args,
**kwargs):
2017-12-13 22:15:34 +01:00
self._impl_valid_string(value)
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))
if self._get_extra('_allow_ip') is True:
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'))
2017-07-24 19:04:18 +02:00
part_name_length = self._get_len(self._get_extra('_dom_type'))
if self._get_extra('_dom_type') == 'domainname':
if not self._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):
if self._get_extra('_has_upper').search(value):
2017-12-13 22:15:34 +01:00
raise ValueError(_('some characters are uppercase'))
2017-07-24 19:04:18 +02:00
if not self._get_extra('_domain_re').search(value):
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()