aboutsummaryrefslogtreecommitdiff
path: root/uploader/input_validation.py
blob: 9abe7426d888213c26731afeb77b00fdb10554cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Input validation utilities"""
from typing import Any

def is_empty_string(value: str) -> bool:
    """Check whether as string is empty"""
    return (isinstance(value, str) and value.strip() == "")

def is_empty_input(value: Any) -> bool:
    """Check whether user provided an empty value."""
    return (value is None or is_empty_string(value))

def is_integer_input(value: Any) -> bool:
    """
    Check whether user provided a value that can be parsed into an integer.
    """
    def __is_int__(val, base):
        try:
            int(val, base=base)
        except ValueError:
            return False
        return True
    return isinstance(value, int) or (
        (not is_empty_input(value)) and (
            isinstance(value, str) and (
                __is_int__(value, 10)
                or __is_int__(value, 8)
                or __is_int__(value, 16))))