Beispiel #1
0
    def validate(self, document):
        valid = True  #os.path.isabs(document.text)
        if not valid:
            raise ValidationError(
                message='Please enter the directory from the root:',
                cursor_position=len(document.text))  # Move cursor to end'
        global WORKDIR
        wdir = WORKDIR + '/' if WORKDIR else WORKDIR
        exists = os.path.exists(wdir + document.text)
        if not exists:
            raise ValidationError(
                message=
                'The file path does not exist! Please enter a valid path:',
                cursor_position=len(document.text))  # Move cursor to end'

        files = os.listdir(wdir + document.text)
        for file in files:
            file = wdir + document.text + '/' + file
            is_file = os.path.isfile(file)  # check file
            doc = os.path.basename(file)  #check file type
            f_type = doc.split('.')
            is_img = True if f_type[1] == 'pdf' or f_type[
                1] == 'jpg' or f_type[1] == 'png' else False
            if not (is_file and is_img):
                raise ValidationError(
                    message=
                    'The file path does not lead to an image! Please enter a valid path:',
                    cursor_position=len(document.text))  # Move cursor to end'
Beispiel #2
0
    def validate_title(self, title: str) -> bool:
        """Checks if title is not empty and not already taken.

        Args:
            title:
                A title of a new notebook.

        Returns:
            True, if validation succeeded.  Never returns False.

        Raises:
            PyInquirer.ValidationError:
                An error when validation failed.
        """
        title = title.strip()

        if len(title) == 0:
            raise ValidationError(message='Title must not be empty.')

        if '/' in title:
            raise ValidationError(message="Please, do not use '/' in title.")

        if self._db.notebook_exists(title):
            raise ValidationError(message=f"Notebook '{title}' exists")

        pages_dir = os.path.expanduser(f'~/.local/share/smth/pages/{title}')
        if os.path.exists(pages_dir):
            raise ValidationError(message=f"'{pages_dir}' already exists")

        return True
 def validate(self, document):
     if len(document.text) == 0:
         raise ValidationError(message='Enter value')
     try:
         int(document.text)
     except:
         raise ValidationError(message='Enter valid integer value')
Beispiel #4
0
    def validate(self, document):
        valid = True  # os.path.isabs(document.text)
        if not valid:
            raise ValidationError(
                message='Please enter the directory from the root:',
                cursor_position=len(document.text))  # Move cursor to end'
        global WORKDIR
        wdir = WORKDIR + '/' if WORKDIR else WORKDIR
        exists = os.path.exists(wdir + document.text)
        if not exists:
            raise ValidationError(
                message=
                'The file path does not exist! Please enter a valid path:',
                cursor_position=len(document.text))  # Move cursor to end'

        is_folder = os.path.isdir(
            wdir + document.text)  #checking if it is a directory

        if not is_folder:
            raise ValidationError(
                message=
                'The file path does not lead to a folder! Please enter a valid path:',
                cursor_position=len(document.text))  # Move cursor to end'

        contents = len(os.listdir(
            wdir +
            document.text))  #and making sure it has at last 1000 photos in it
        if contents < 1000:
            raise ValidationError(
                message=
                'There is not enough files to properly train in this folder! Please try again:',
                cursor_position=len(document.text))  # Move cursor to end'
Beispiel #5
0
    def validate(self, document):
        user_values = re.findall(',?\d+,?', str(document.text))
        index = re.findall('\d+', str(document.text))
        other = re.findall('[^,\d\s]+', str(document.text))
        if not user_values:
            raise ValidationError(
                message=
                f"Please enter valid index (ex 1 or 1,2) (index < {playlist_len})",
                cursor_position=len(str(document.text)))  # Move cursor to end

        if other and user_values:
            raise ValidationError(
                message=
                f"Please enter valid index (ex 1 or 1,2) (index < {playlist_len})",
                cursor_position=len(str(document.text)))  # Move cursor to end

        elif index and user_values:
            greater_index = ""
            for i in index:
                if int(i) > playlist_len or int(i) == 0:
                    greater_index = greater_index + str(i)

                    raise ValidationError(
                        message=
                        f"Please enter valid index ex 1 or 1,2 (index < {playlist_len})",
                        cursor_position=len(str(
                            document.text)))  # Move cursor to end
                    break
def validate_balance(player, answer, deposit=True):
    money = player.data["money"]
    money_in_bank = player.data["bank_account"]
    if check_float(answer):
        float_answer = float(answer)
        if float_answer == 0:
            raise ValidationError(
                message=f"You aren't allowed to {'deposit' if deposit else 'withdraw'} $0!"
            )
        elif deposit:
            if money >= float_answer:
                return True
            else:
                raise ValidationError(
                    message=f"You don't have that much money! (You have ${money})",
                    cursor_position=len(answer)
                )
        else:
            if money_in_bank >= float_answer:
                return True
            else:
                raise ValidationError(
                    message=f"You don't have that much money in your bank! (You have ${money_in_bank})",
                    cursor_position=len(answer)
                )
    else:
        raise ValidationError(
            message="Please enter a valid number!",
            cursor_position=len(answer)
        )
Beispiel #7
0
    def validate_path(self, path_str: str) -> bool:
        """Checks if path is not empty and not already taken.

        Args:
            path_str:
                A path to PDF file for a new notebook.

        Returns:
            True, if validation succeeded.  Never returns False.

        Raises:
            PyInquirer.ValidationError:
                An error when validation failed.
        """
        path_str = path_str.strip()

        if len(path_str) == 0:
            raise ValidationError(message='Path must not be empty')

        path = pathlib.Path(os.path.expandvars(path_str))
        path = path.expanduser().resolve()

        if not path.is_dir() and path.exists():
            raise ValidationError(message=f"'{path}' already exists")

        notebook = self._db.get_notebook_by_path(str(path))

        if notebook.title != 'Untitled':
            message = f"'{path}' already taken by notebook '{notebook.title}'"
            raise ValidationError(message=message)

        return True
 def validate(self, document):
     nok = re.findall("[/@]", document.text)
     if nok:
         raise ValidationError(message="Password can't contain / or @ ")
     if len(document.text) < 8 or len(document.text) > 30:
         raise ValidationError(
             message='Password must be length between 8 to 30 characters')
Beispiel #9
0
 def validate(self, document):
     if not document.text.isdigit():
         raise ValidationError(message='Введите число 1 - 100',
                               cursor_position=len(document.text))
     elif int(document.text) < 1:
         raise ValidationError(message='Введите число 1 - 100',
                               cursor_position=len(document.text))
Beispiel #10
0
    def validate_number_of_pages_to_append(self, number: str) -> bool:  # pylint: disable=no-self-use  # noqa: E501
        """Checks if number is an integer from 0 to 100.

        Args:
            number:
                A number of pages to append to a notebook.  An empty value is
                allowed.

        Returns:
            True, if validation succeeded.  Never returns False.

        Raises:
            PyInquirer.ValidationError:
                An error when validation failed.
        """
        if len(number.strip()) == 0:
            return True

        if not number.isnumeric():
            raise ValidationError(
                message='Please, enter a number from 0 to 100 or leave empty.')

        if len(number) > 3:
            raise ValidationError(
                message='Please, enter a number from 0 to 100 or leave empty.')

        if int(number) > 100:
            raise ValidationError(
                message='Please, enter a number from 0 to 100 or leave empty.')

        return True
    def validate(self, value):

        if len(value.text):
            try:
                ipaddress.ip_address(value.text)
            except ValueError as e:
                raise ValidationError(message=str(e),
                                      cursor_position=len(value.text))

            hosts = config.items("HOSTS")
            ip = []

            for host in hosts:
                data = json.loads(host[1].replace("'", '"'))
                ip.append(data["ip"])

            if value.text in ip:
                raise ValidationError(
                    message=("A host with that IP address already exists. "
                             "To replace it, please remove host first."),
                    cursor_position=len(value.text),
                )
        else:
            raise ValidationError(message="You can't leave this blank",
                                  cursor_position=len(value.text))
Beispiel #12
0
    def validate_page_size(self, size: str) -> bool:  # pylint: disable=no-self-use  # noqa: E501
        """Checks if given input is an integer from 10 to 1000.

        Args:
            size:
                A page width or height in millimeters.

        Returns:
            True, if validation succeeded.  Never returns False.

        Raises:
            PyInquirer.ValidationError:
                An error when validation failed.
        """
        size = size.strip()

        if not size.isnumeric():
            raise ValidationError(
                message='Please, enter a number from 10 to 1000')

        if int(size) < 10 or int(size) > 1000:
            raise ValidationError(
                message='Please, enter a number from 10 to 1000')

        return True
Beispiel #13
0
    def validate_first_page_number(self, number: str) -> bool:  # pylint: disable=no-self-use  # noqa: E501
        """Checks if number is an integer from 0 to 100.

        Args:
            number:
                The 1st page number of a new notebook.

        Returns:
            True, if validation succeeded.  Never returns False.

        Raises:
            PyInquirer.ValidationError:
                An error when validation failed.
        """
        number = number.strip()

        if not number.isnumeric():
            raise ValidationError(
                message='Please, enter a number from 0 to 1000.')

        if len(number) > 4:
            raise ValidationError(
                message='Please, enter a number from 0 to 1000.')

        if int(number) > 1000:
            raise ValidationError(
                message='Please, enter a number from 0 to 1000.')

        return True
Beispiel #14
0
 def validate(self, document):
     try:
         value = int(document.text)
         if value < 1:
             raise ValidationError(message=s.number_lower_error,
                                   cursor_position=len(document.text))
     except ValueError:
         raise ValidationError(message=s.not_a_number_error,
                               cursor_position=len(document.text))
Beispiel #15
0
 def validate(self, document):
     try:
         p = float(document.text)
         if p < 0 or p > 1:
             raise ValidationError(
                 message='Please enter a number between 0 and 1.',
                 cursor_position=len(document.text))
     except ValueError:
         raise ValidationError(message='Please enter a valid number.',
                               cursor_position=len(document.text))
Beispiel #16
0
 def validate(self, value):
     if len(value.text):
         if os.path.isfile(value.text):
             return True
         else:
             raise ValidationError(message="File not found",
                                   cursor_position=len(value.text))
     else:
         raise ValidationError(message="Please enter a proper file path",
                               cursor_position=len(value.text))
Beispiel #17
0
 def validate(self, value):
     if len(value.text):
         if os.path.isfile(value.text):
             return True
         else:
             raise ValidationError(message="File not found", cursor_position=len(value.text))
     else:
         raise ValidationError(
             message="You can't leave this blank", cursor_position=len(value.text)
         )
Beispiel #18
0
 def validate(self, value):
     if len(value.text):
         if os.path.exists(value.text):
             return True
         else:
             raise ValidationError(message=_("Path not found"),
                                   cursor_position=len(value.text))
     else:
         raise ValidationError(message=_("You can't leave this blank"),
                               cursor_position=len(value.text))
Beispiel #19
0
 def validate(self, email):
     if len(email.text):
         if re.match(self.pattern, email.text):
             return True
         else:
             raise ValidationError(message=_("Invalid email"),
                                   cursor_position=len(email.text))
     else:
         raise ValidationError(message=_("You can't leave this blank"),
                               cursor_position=len(email.text))
Beispiel #20
0
 def validate(self, document):
     name = document.text
     if len(name) == 0:
         raise ValidationError(
             message="tmux session name must not be an empty string",
             cursor_position=0)
     elif "." in name:
         raise ValidationError(
             message="tmux session names may not contain '.'",
             cursor_position=len(name))  # Move cursor to end
Beispiel #21
0
 def validate(self, document):
     if len(document.text) == 0:
         raise ValidationError(message='Enter value')
     if int(document.text) not in range(1150, 65535):
         raise ValidationError(
             message='Enter valid port number in range 1150-65535')
     try:
         int(document.text)
     except:
         raise ValidationError(message='Enter valid integer value')
Beispiel #22
0
 def validate(self, numbers):
     if len(numbers.text):
         if re.match(self.num_pattern, numbers.text):
             return True
         else:
             raise ValidationError(message="Please provide the numbers",
                                   cursor_position=len(numbers.text))
     else:
         raise ValidationError(message="You can't leave this blank",
                               cursor_position=len(numbers.text))
Beispiel #23
0
 def validate(self, document):
     query = document.text
     if len(query) >= 55:
         raise ValidationError(
             message='Please be concise. Query should be less than 50 characters.',
             cursor_position=len(document.text))  # Move cursor to end
     elif query == "":
         raise ValidationError(
             message='Please enter something.',
             cursor_position=len(document.text))  # Move cursor to end
 def validate(self, value):
     if len(value.text):
         if not os.path.isfile(value.text):
             raise ValidationError(
                 message=("File " + value.text + " could not be found"),
                 cursor_position=len(value.text),
             )
     else:
         raise ValidationError(message="You can't leave this blank",
                               cursor_position=len(value.text))
Beispiel #25
0
 def validate(self, document):
     if not os.path.isfile(document.text):
         raise ValidationError(
             message='Please enter a path to an image that exists',
             cursor_position=len(document.text))  # pragma: no cover
     if not document.text.endswith('.img') and not document.text.endswith(
             '.qcow2'):
         raise ValidationError(
             message='Image must be either a .img or a .qcow2 image',
             cursor_position=len(document.text))  # pragma: no cover
 def validate(self, document):
     if not os.path.isdir(document.text):
         raise ValidationError(message='Please enter a valid folder path.',
                               cursor_position=len(document.text))
     files = os.listdir(document.text)
     files = list(filter(lambda x: x.endswith('.csv'), files))
     if len(files) == 0:
         raise ValidationError(
             message='Please enter folder path with csv files.',
             cursor_position=len(document.text))
Beispiel #27
0
 def validate(self, document):
     ok = regex.match('^[a-z_]*$', document.text)
     if not ok:
         raise ValidationError(
             message='Nazwa musi zawierać tylko małe litery oraz "_"',
             cursor_position=len(document.text))
     if os.path.exists(document.text):
         raise ValidationError(
             message='Taki katalog już istnieje, wybierz inną nazwę.',
             cursor_position=len(document.text))
Beispiel #28
0
 def validate(self, doc):
     try:
         if not os.path.exists(str("./" + doc.text)):
             raise ValidationError(
                 message='The directory at this location does not exist',
                 cursor_position=len(doc.text))  # Move cursor to end
     except ValueError:
         raise ValidationError(
             message='Please enter a valid directory path',
             cursor_position=len(doc.text))  # Move cursor to end
Beispiel #29
0
    def validate(self, document: document.Document):
        value = document.text
        try:
            value = int(value)
        except Exception:
            raise ValidationError(message = 'Value should be greater than 0', cursor_position = len(document.text))

        if value <= 0:
            raise ValidationError(message = 'Value should be greater than 0', cursor_position = len(document.text))
        return True
Beispiel #30
0
 def validate(self, document):
     try:
         int(document.text)
     except ValueError:
         raise ValidationError(
             message='Please enter a number',
             cursor_position=len(document.text))  # Move cursor to end
     if not int(document.text) <= 50:
         raise ValidationError(
             message="Please enter a number  <= 50",
             cursor_position=len(document.text))