Passed
Push — master ( a8cf2f...4ab9d5 )
by torrua
02:00 queued 12s
created

ButtonCheck.is_callback_data_in_limits()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
# -*- coding:utf-8 -*-
2 1
"""
3
This module contains all checks for Keyboa Button class parameters
4
"""
5
# pylint: disable = C0116
6
7 1
from keyboa.constants import (
8
    InlineKeyboardButton,
9
    InlineButtonData,
10
    callback_data_types,
11
    MAXIMUM_CBD_LENGTH,
12
)
13
14
15 1
class ButtonCheck:
16
    """
17
    This class contains all checks for Keyboa Button class parameters
18
    """
19 1
    @staticmethod
20 1
    def is_button_data_proper_type(button_data) -> None:
21 1
        if not isinstance(button_data, (tuple, dict, str, int)):
22 1
            type_error_message = (
23
                "Cannot create %s from %s. Please use %s instead.\n"
24
                "Probably you specified 'auto_alignment' or 'items_in_line' "
25
                "parameter for StructuredSequence."
26
                % (InlineKeyboardButton, type(button_data), InlineButtonData)
27
            )
28 1
            raise TypeError(type_error_message)
29
30 1
    @staticmethod
31 1
    def is_callback_proper_type(callback) -> None:
32 1
        if not isinstance(callback, callback_data_types):
33 1
            type_error_message = "Callback cannot be %s. Only %s allowed." % (
34
                type(callback),
35
                callback_data_types,
36
            )
37 1
            raise TypeError(type_error_message)
38
39 1
    @staticmethod
40 1
    def is_callback_data_in_limits(callback_data) -> None:
41 1
        if len(callback_data.encode()) > MAXIMUM_CBD_LENGTH:
42 1
            size_error_message = (
43
                "The callback data cannot be more than "
44
                "64 bytes for one button. Your size is %s" % len(callback_data.encode())
45
            )
46
            raise ValueError(size_error_message)
47