Beispiel #1
0
    async def _split_command_string(self, command_string):
        """
        Splits a command string into a list which contains individual
        command identifiers and parameters

        command_string  : str   - The command string to be split
        Returns         : list  - A list containing the individual command identifiers and parameters
        """

        result = []
        char_ptr = -1

        current_string = ''

        within_quotes = False

        escape_next_char = False
        escape_char = False

        while True:
            char_ptr += 1
            if char_ptr == len(command_string):
                if current_string != '':
                    result.append(current_string)
                break

            escape_char = escape_next_char
            escape_next_char = False

            char = command_string[char_ptr]

            if char == ' ' and within_quotes == False and escape_char == False:
                if current_string == '':
                    continue
                result.append(current_string)
                current_string = ''
                continue

            if char == '\\' and escape_char == False:
                escape_next_char = True
                continue

            if char == '"' and escape_char == False:
                if within_quotes == False and current_string != '':
                    raise exceptions.Value_Error(
                        'A quote used to start escaping text must follow a space'
                    )
                if within_quotes == True and char_ptr + 1 < len(
                        command_string) and command_string[char_ptr +
                                                           1] != ' ':
                    raise exceptions.Value_Error(
                        'A quote used to end escaping text must be followed by a space'
                    )
                within_quotes = not within_quotes
                continue

            current_string += char

        return result
Beispiel #2
0
async def async_validate_bounds(value, min, max, include_min, include_max):
    if min is not None:
        if include_min == False and value <= min:
            raise exceptions.Value_Error('must be greater than {}'.format(min))
        if value < min:
            raise exceptions.Value_Error('cannot be less than {}'.format(min))
    
    if max is not None:
        if include_max == False and value >= max:
            raise exceptions.Value_Error('must be less than {}'.format(max))
        if value > max:
            raise exceptions.Value_Error('cannot be greater than {}'.format(max))
Beispiel #3
0
def validate_command_name(string):
    """
    Checks if the input is a string of non-zero length that contains only letters and underscores

    string  : str   - The string to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if string is not of the correct format
    """

    validate_string(string)
    if re.match('.*[^a-zA-Z_].*', string):
        raise exceptions.Value_Error('must only contain letters and underscores')
Beispiel #4
0
def validate_word(string):
    """
    Checks if the input is a string of non-zero length that contains only letters

    string  : str   - The string to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if string is not of the correct format
    """

    validate_string(string)
    if re.match('.*[^a-zA-Z].*', string):
        raise exceptions.Value_Error('must represent a word')
Beispiel #5
0
def validate_letter(string):
    """
    Checks if the input is a string of length 1 that contains only letters

    string  : str   - The string to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if string is not of the correct format
    """

    validate_word(string)
    if len(string) != 1:
        raise exceptions.Value_Error('must have length 1')
Beispiel #6
0
def validate_role_mention(string):
    """
    Checks if the input is a string that represents a discord role mention

    string  : str   - The string to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if string is not of the correct format
    """
    
    validate_string(string)
    if not re.match('<@&\d+>', string):
        raise exceptions.Value_Error('must represent a role mention')
Beispiel #7
0
def validate_time(string):
    """
    Checks if the input is a string that represents a time in the form '%H:%M:%S'

    string  : str   - The string to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if string is not of the correct format
    """

    validate_string(string)
    if not re.match('\d\d:\d\d:\d\d', string):
        raise exceptions.Value_Error('must represent a time')
Beispiel #8
0
def validate_string(string):
    """
    Checks if the input is a string of non-zero length

    string  : str   - The string to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if string is not of the correct format
    """

    if not isinstance(string, str):
        raise exceptions.Type_Error('expected str instance, {} found'.format(string.__class__.__name__))
    if len(string) == 0:
        raise exceptions.Value_Error('must not have 0 length')
Beispiel #9
0
def validate_float(obj):
    """
    Checks if the input can be converted to a float

    obj : object    - The object to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if object cannot be converted to a float
    """

    try:
        float(obj)
    except ValueError:
        raise exceptions.Value_Error('must represent a float')
Beispiel #10
0
def validate_integer(obj):
    """
    Checks if the input can be converted to a integer

    string  : object    - The object to be checked

    Raises discord_cli.exceptions.Discord_CLI_Error if object cannot be converted to a integer
    """
    
    try:
        int(obj)
    except ValueError:
        raise exceptions.Value_Error('must represent an integer')
Beispiel #11
0
def validate_bounds(value, min, max, include_min, include_max):
    """
    Checks if a value lies within an interval defined by a min and a max

    min         : object    - The lower bound of the interval (if None, assumed to be negative infinity)
    max         : object    - The upper bound of the interval (if None, assumed to be positive infinity)
    include_min : object    - Whether the lower bound is not a strict inequality
    include_max : object    - Whether the upper bound is not a strict inequality

    Raises discord_cli.exceptions.Discord_CLI_Error if the value lies outside the interval
    """

    if min is not None:
        if include_min == False and value <= min:
            raise exceptions.Value_Error('must be greater than {}'.format(min))
        if value < min:
            raise exceptions.Value_Error('cannot be less than {}'.format(min))
    
    if max is not None:
        if include_max == False and value >= max:
            raise exceptions.Value_Error('must be less than {}'.format(max))
        if value > max:
            raise exceptions.Value_Error('cannot be greater than {}'.format(max))
Beispiel #12
0
async def async_validate_user_mention(string):
    await async_validate_string(string)
    if not re.match('<@!?\d+>', string):
        raise exceptions.Value_Error('must represent a user mention')
Beispiel #13
0
async def async_validate_command_name(string):
    await async_validate_string(string)
    if re.match('.*[^a-zA-Z_].*', string):
        raise exceptions.Value_Error('must only contain letters and underscores')
Beispiel #14
0
async def async_validate_string(string):
    if not isinstance(string, str):
        raise exceptions.Type_Error('expected str instance, {} found'.format(string.__class__.__name__))
    if len(string) == 0:
        raise exceptions.Value_Error('must not have 0 length')
Beispiel #15
0
async def async_validate_float(string):
    await async_validate_string(string)
    try:
        float(string)
    except ValueError:
        raise exceptions.Value_Error('must represent a float')
Beispiel #16
0
async def async_validate_word(string):
    await async_validate_string(string)
    if re.match('.*[^a-zA-Z].*', string):
        raise exceptions.Value_Error('must represent a word')
Beispiel #17
0
async def async_validate_integer(string):
    await async_validate_string(string)
    try:
        int(string)
    except ValueError:
        raise exceptions.Value_Error('must represent an integer')
Beispiel #18
0
async def async_validate_time(string):
    await async_validate_string(string)
    if not re.match('\d\d:\d\d:\d\d', string):
        raise exceptions.Value_Error('must represent a time')
Beispiel #19
0
async def async_validate_role_mention(string):
    await async_validate_string(string)
    if not re.match('<@&\d+>', string):
        raise exceptions.Value_Error('must represent a role mention')
Beispiel #20
0
async def async_validate_channel_mention(string):
    await async_validate_string(string)
    if not re.match('<#\d+>', string):
        raise exceptions.Value_Error('must represent a channel mention')
Beispiel #21
0
async def async_validate_letter(string):
    await async_validate_word(string)
    if len(string) > 1:
        raise exceptions.Value_Error('must have length 1')