def validate_password(answers, current): if len(current) < 8: raise ValidationError( '', reason='Senha precisa ter no mínimo 8 caracateres') if len(current) > 16: raise ValidationError('', reason='Senha pode ter no máximo 16 caracateres') return True
def language_validator(self, response): """Match language code in libraries/locale. Arguments --------- response: "String containing current answer" Raises ------ ValidationError: Display a short description with available formats" Returns ------- boolean: True """ language_list = open('libraries/locale').read() if ('{response}\n'.format(response=response) not in language_list) or \ (response == ''): raise ValidationError( '', reason=self.trad('Invalid language code: {response} (e.q., fr_FR)' ).format(response=response)) return True
def validate_birthday(answers, current): try: datetime.strptime(current, '%d/%m/%Y') except: raise ValidationError( '', reason='Data inválida, o formato deve ser "dd/mm/YYYY"') return True
def timezone_validator(self, response): """Match timezone code in libraries/timezone. Arguments --------- response: "String containing current answer" Raises ------ ValidationError: "Display a short description with available formats" Returns ------- boolean: True """ timezone_list = open('libraries/timezone').read() if ('{response}\n'.format(response=response) not in timezone_list) or \ (response == ''): raise ValidationError( '', reason=self.trad( 'Invalid timezone: {response} (e.q., Europe/Paris)').format( response=response)) return True
def size_validator(self, user, response): """Match regex, current partition min/max size and remaining disk space. Arguments --------- user: "******" response: "String containing current answer" Modules ------- re: "Regular expression matching operations" humanfriendly: "Parse human readable data libraries" inquirer.errors: "Common base class for all non-exit exceptions" Functions --------- `size_counter`: "Returns integer of the current disk space usage" `size_index`: "Returns integer of the current partition index" Raises ------ ValidationError: "Display a short description with available formats" Returns ------- boolean: True """ name = ['boot', 'root', 'swap', 'home'] min_size = ['100M', '5G', '1G', '4G'] max_size = ['2G', '16T', '32G', '16T'] eq_size = ['512M', '25G', '2G', '100G'] valid_size = r'^[1-9]{1}[0-9]{0,2}((,|\.)[0-9]{1,2}){0,1}(M|G|T){1}$' msg_error = self.trad('Invalid size for {name}: {response} (e.q., {eq})') msg_status = self.trad( 'Minimum [{min}] Maximum [{max}] Remaining [{free}]') error = '{msg} {status}'.format(msg=msg_error, status=msg_status) if (not re.match(valid_size, response)) or \ ((size_counter(user) + parse_size(response.replace(',', '.'))) > parse_size(user['drive'].split()[1].replace(',', '.'))) or \ (parse_size(response.replace(',', '.')) < parse_size(min_size[size_index(user)])) or \ (parse_size(response.replace(',', '.')) > parse_size(max_size[size_index(user)])): raise ValidationError( '', reason=error.format( name=name[size_index(user)], response=response, eq=eq_size[size_index(user)], min=min_size[size_index(user)], max=max_size[size_index(user)], free=format_size( parse_size(user['drive'].split()[1].replace(',', '.')) - size_counter(user)))) return True
def validate_sources(answers, current): """ Makes sure that there is at least one selected source """ if not current: raise ValidationError("", reason="You must choose at last one source") return True
def validate_other_source(answers, current): """ If "Other" is within selected sources, it will be mandatory to provide a valid one, validated by the fact that the importer can make an instance of it. """ try: importer.source_from_string_id(current) except KeroError as e: raise ValidationError("", reason=f"{e}") return True
def validate_backup(answers, current): """ Validates that a backup string can be instantiated into a real backup. """ if not current: return True try: importer.backup_from_string_id(current) except KeroError as e: raise ValidationError("", reason=f"{e}") return True
def passwd_validator(self, response): """Match UNIX password regex. Arguments --------- response: "String containing current answer" Raises ------ ValidationError: "Display a short description with available formats" Returns ------- boolean: True """ info = self.trad('Password should be at least') valid = self.trad('8 chars long with one letter and one digit !') message = '{info} {valid}'.format(info=info, valid=valid) if not re.match(r'^(?=.*[A-Za-z])(?=.*\d)[\S]{8,}$', response): raise ValidationError('', reason=message) return True
def username_validator(self, response): """Match UNIX username regex. Arguments --------- response: "String containing current answer" Raises ------ ValidationError: "Display a short description with available formats" Returns ------- boolean: True """ if not re.match(r'^[a-z_]{1}[a-z0-9_-]{1,31}$', response): raise ValidationError( '', reason=self.trad('Invalid username: {response} (e.q., JohnDoe)' ).format(response=response)) return True
def validate_empty(answers, s): if not s: raise ValidationError('', reason='Must not be empty') return True
def validate_url(answers, url): rx = r'^https://[\S-]+\.awsapps\.com/start/$' if re.match(rx, url) is None: raise ValidationError('', reason=f'URL must match {rx}') return True
def validate_name(answers, current): if not re.match(r'^([^\W\d_]+[ ]*)+$', current, re.UNICODE): raise ValidationError('', reason='Nome só pode conter letras') return True
def validate_email(answers, current): if not re.match(r'^(\w+[.|\w])*@(\w+[.])*\w+$', current, re.IGNORECASE): raise ValidationError('', reason='E-mail inválido') return True
def validate_username(answers, current): if len(current) == 0: raise ValidationError('', reason='O nome de usuário é obrigatório') return True