ButtonCheck.is_callback_data_in_limits()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
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
20 1
    @staticmethod
21 1
    def is_button_data_proper_type(button_data) -> None:
22 1
        if not isinstance(button_data, (tuple, dict, str, int)):
23 1
            type_error_message = (
24
                f"Cannot create {InlineKeyboardButton} from {type(button_data)}. "
25
                f"Please use {InlineButtonData} instead.\n"
26
                "Probably you specified 'auto_alignment' or 'items_in_line' "
27
                "parameter for StructuredSequence."
28
            )
29 1
            raise TypeError(type_error_message)
30
31 1
    @staticmethod
32 1
    def is_callback_proper_type(callback) -> None:
33 1
        if not isinstance(callback, callback_data_types):
34 1
            type_error_message = (
35
                f"Callback cannot be {type(callback)}. "
36
                f"Only {callback_data_types} allowed."
37
            )
38 1
            raise TypeError(type_error_message)
39
40 1
    @staticmethod
41 1
    def is_callback_data_in_limits(callback_data) -> None:
42 1
        if len(callback_data.encode()) > MAXIMUM_CBD_LENGTH:
43 1
            size_error_message = (
44
                "The callback data cannot be more than "
45
                f"64 bytes for one button. Your size is {len(callback_data.encode())}"
46
            )
47
            raise ValueError(size_error_message)
48