| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 | from .apikey import apikey_generatorclass validator:    """    This is base validator class. It implements base validator mechanism,     and other validators must implements it.    """    def __init__(self, target: str) -> None:        """        This create new validator, from string.                 Parameters:            target (str): Content to validate        """        self.__target = target        self._failed()    @property    def target(self) -> str:        """ This return content to validate. """        return self.__target    def _failed(self) -> None:        """        This would mark content as invalid.         """        self.__result = False    def _check_all(self) -> None:        """        This would check all marks of the content.        It must be overwrite by target validator.        """        pass    def _contain(self, *args) -> bool:        """        This check that target contain any given phrase or letter.        Args:            (str): Phrase to check that content contain it        Returns:            (bool): True when any of given phrase is in the target        """        for letter in args:            if self.__target.find(letter) != -1:                return True                return False    @property    def result(self) -> bool:        """ Validate content, True when valid or False when not. """        self.__result = True        self._check_all()        return self.__result    @property    def invalid(self) -> bool:        """ Validate, and return True when target is invalid. """                return not self.result    @property    def valid(self) -> bool:        """ It validate target, and return True when valid. """        return self.resultclass nick_validator(validator):    """    This validate nick. It is slow, and best to use only when loading    users from file.    """    def _check_all(self) -> None:                # Minimum 4 characters        if len(self.target) < 4:            return self._failed()        # Maximum 30 characters        if len(self.target) > 30:            return self._failed()        # Could contain only         for letter in self.target:                        # Letters            if letter.isalpha():                continue            # Numbers            if letter.isnumeric():                continue            return self._failed()class password_validator(validator):    """    This validate password. It is slow, and best to use only when    loading users from file.    """    def _check_all(self) -> None:        # Minimum 8 characters        if len(self.target) < 8:            return self._failed()        # Maximum 40 characters        if len(self.target) > 40:            return self._failed()        # Can not contain white chars        for letter in self.target:            if letter.isspace():                return self._failed()class apikey_validator(validator):    """ This is simple ApiKey validator """    def _check_all(self) -> None:                # ApiKey must contain proof size         if len(self.target) != apikey_generator.size() * 2:            return self._failed()class name_validator(validator):    """    This is validator for product name. It check that it is not blank,    and it not contain white chars before and after content. It must have     more or qeual of 4 and less than 40 characters.    """    def _check_all(self) -> None:        # Minimum 4 characters        if len(self.target) < 4:            return self._failed()        # Maximum 40 characters        if len(self.target) > 40:            return self._failed()        # Must be trimed        if len(self.target.strip()) != len(self.target):            return self._failed()class description_validator(validator):    """    This is validator for product description. It could be blank, but can not    have white chars before and after content (must be trimmed), and coud has     only 500 chars.    """    def _check_all(self) -> None:        # Could be blank        if len(self.target) == 0:            return        # Must be trimmed        if len(self.target.strip()) != len(self.target):            return self._failed()        # Must has max 500 characters.        if len(self.target) > 500:            return self._failed()class barcode_validator(validator):    """    This is barcode validator. Barcode must be string, which contain number    with 8, 12 or 13 digits. That is standard EAN.    """    def _check_all(self) -> None:        # Must has only digits        if not self.target.isdigit():            return self._failed()        lenght = len(self.target)        # Must has 8, 12 or 13 chars        if lenght != 8 and lenght != 12 and lenght != 13:            return self._failed()class author_validator(validator):    """    This validate author name. Author can not have less than 3 characters,    and can not have more than 40 characters. It also must be trimmed.    """    def _check_all(self) -> None:        # Must be trimmed        if len(self.target) != len(self.target.strip()):            return self._failed()        # Must have more than 3 characters        if len(self.target) < 3:            return self._failed()        # Must has max 40 characters        if len(self.target) > 40:            return self._failed()class phone_number_validator(validator):    """    This is phone number validator. Phone number must be in format    like +CC XXXXXXXXX. This is standard of E.123 and E.164.    """    def _check_all(self) -> None:                # Separate country code and number        splited = self.target.split(" ")        if len(splited) != 2:            return self._failed()        # Get it        country_code = splited[0]        number = splited[1]                # Must start with "+"        if country_code[0] != "+":            return self._failed()        country_code = country_code[1:]                # Country code must be numeric        if not country_code.isnumeric():            return self._failed()                # Number of course must be numeric        if not number.isnumeric():            return self._failed()        # Country code length must be <1;3>        if len(country_code) == 0 or len(country_code) > 3:            return self._failed()        # All number must have less than 16        if len(country_code) + len(number) > 15:            return self._failed()        # And number mast has minimum 7 length        if len(number) < 7:            return self._failed()class email_validator(validator):    """    This is email validator. This validate email address in the standard    format.    """    def _check_all(self) -> None:                # Split name and domain        splited = self.target.split("@")                if len(splited) != 2:            return self._failed()        # Gat it        name = splited[0]        domain = splited[1]                # Name must be in <1;128>        if len(name) < 1 or len(name) > 128:            return self._failed()        # Domain can not has ".."        if domain.find("..") != -1:            return self._failed()        splited = domain.split(".")                # Must has more or equal 2 parts in domain        if len(splited) < 2:            return self._failed()        # All parts must be in <1;128>        for part in splited:            if len(part) < 1 or len(part) > 128:                return self._failed()
 |