|
1
|
|
|
# -*- coding:utf-8 -*- |
|
2
|
1 |
|
""" |
|
3
|
|
|
Module with functions for keyboard precheck |
|
4
|
|
|
""" |
|
5
|
1 |
|
from telebot.types import InlineKeyboardMarkup |
|
6
|
|
|
|
|
7
|
1 |
|
from keyboa.constants import BlockItems, MAXIMUM_ITEMS_IN_KEYBOARD, \ |
|
8
|
|
|
MINIMUM_ITEMS_IN_LINE, MAXIMUM_ITEMS_IN_LINE |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
def _keyboa_pre_check( |
|
12
|
|
|
items: BlockItems = None, |
|
13
|
|
|
items_in_row: int = None, |
|
14
|
|
|
keyboard: InlineKeyboardMarkup = None) -> None: |
|
15
|
|
|
""" |
|
16
|
|
|
This function checks whether the keyboard parameters are beyond Telegram limits or not. |
|
17
|
|
|
|
|
18
|
|
|
:param items: InlineRowItems - Sequence of elements with optional structure, |
|
19
|
|
|
where each top-level item will be a row with one or several buttons. |
|
20
|
|
|
|
|
21
|
|
|
:param items_in_row: int - Desired number of buttons in one row. Should be from 1 to 8. |
|
22
|
|
|
Optional. The default value is None. |
|
23
|
|
|
|
|
24
|
|
|
:param keyboard: InlineKeyboardMarkup object to which we will attach the list items. |
|
25
|
|
|
We need to count the existing buttons so as not to go beyond the general limits. |
|
26
|
|
|
Optional. The default value is None. |
|
27
|
|
|
|
|
28
|
|
|
:return: None if everything is okay. |
|
29
|
|
|
|
|
30
|
|
|
Covered by tests. |
|
31
|
|
|
""" |
|
32
|
|
|
|
|
33
|
1 |
|
if items is None: |
|
34
|
1 |
|
return |
|
35
|
|
|
|
|
36
|
1 |
|
if items and not isinstance(items, list): |
|
37
|
1 |
|
items = [items, ] |
|
38
|
|
|
|
|
39
|
1 |
|
if keyboard and not isinstance(keyboard, InlineKeyboardMarkup): |
|
40
|
1 |
|
type_error_message = \ |
|
41
|
|
|
"Keyboard to which the new items will be added " \ |
|
42
|
|
|
"should have InlineKeyboardMarkup type. Now it is a %s" % type(keyboard) |
|
43
|
1 |
|
raise TypeError(type_error_message) |
|
44
|
|
|
|
|
45
|
|
|
# We need to count existing buttons too if we passed keyboard object to the function |
|
46
|
1 |
|
items_in_keyboard = get_total_items_number(items, keyboard) |
|
47
|
1 |
|
check_keyboard_items_limits(items_in_keyboard, items_in_row) |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
1 |
|
def check_keyboard_items_limits(items_in_keyboard: int, items_in_row: int) -> None: |
|
51
|
|
|
""" |
|
52
|
|
|
:param items_in_keyboard: |
|
53
|
|
|
:param items_in_row: |
|
54
|
|
|
:return: |
|
55
|
|
|
""" |
|
56
|
|
|
|
|
57
|
1 |
|
if items_in_keyboard > MAXIMUM_ITEMS_IN_KEYBOARD: |
|
58
|
1 |
|
value_error_message_keyboard = \ |
|
59
|
|
|
"Telegram Bot API limit exceeded: The keyboard should have " \ |
|
60
|
|
|
"from 1 to %s buttons at all. Your total amount is %s." |
|
61
|
1 |
|
raise ValueError(value_error_message_keyboard % |
|
62
|
|
|
(MAXIMUM_ITEMS_IN_KEYBOARD, items_in_keyboard)) |
|
63
|
|
|
|
|
64
|
1 |
|
if items_in_row is not None and \ |
|
65
|
|
|
(MINIMUM_ITEMS_IN_LINE > items_in_row or items_in_row > MAXIMUM_ITEMS_IN_LINE): |
|
66
|
1 |
|
value_error_message_line = \ |
|
67
|
|
|
"Telegram Bot API limit exceeded: " \ |
|
68
|
|
|
"The keyboard line should have from 1 to %s buttons. You entered %s." |
|
69
|
1 |
|
raise ValueError(value_error_message_line % |
|
70
|
|
|
(MAXIMUM_ITEMS_IN_LINE, items_in_row)) |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
1 |
|
def get_total_items_number(items, keyboard) -> int: |
|
74
|
|
|
""" |
|
75
|
|
|
:param items: |
|
76
|
|
|
:param keyboard: |
|
77
|
|
|
:return: |
|
78
|
|
|
""" |
|
79
|
1 |
|
total_items_number = sum( |
|
80
|
|
|
len(row) if isinstance(row, (list, tuple, set)) else 1 for row in items) |
|
81
|
|
|
|
|
82
|
1 |
|
if not keyboard: |
|
83
|
1 |
|
return total_items_number |
|
84
|
|
|
|
|
85
|
1 |
|
keyboard_items = keyboard.__dict__['keyboard'] |
|
86
|
1 |
|
current_keyboard_items_number = sum(len(row) for row in keyboard_items) |
|
87
|
|
|
return total_items_number + current_keyboard_items_number |
|
88
|
|
|
|