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 |
|
check_keyboard_type(keyboard) |
40
|
|
|
|
41
|
1 |
|
items_in_keyboard = get_total_items_number(items, keyboard) |
42
|
|
|
|
43
|
1 |
|
check_keyboard_items_limits(items_in_keyboard, items_in_row) |
44
|
|
|
|
45
|
|
|
|
46
|
1 |
|
def check_keyboard_type(keyboard): |
47
|
|
|
""" |
48
|
|
|
:param keyboard: |
49
|
|
|
:return: |
50
|
|
|
""" |
51
|
1 |
|
if keyboard and not isinstance(keyboard, InlineKeyboardMarkup): |
52
|
1 |
|
type_error_message = \ |
53
|
|
|
"Keyboard to which the new items will be added " \ |
54
|
|
|
"should have InlineKeyboardMarkup type. Now it is a %s" % type(keyboard) |
55
|
1 |
|
raise TypeError(type_error_message) |
56
|
|
|
|
57
|
|
|
|
58
|
1 |
|
def check_keyboard_items_limits(items_in_keyboard: int, items_in_row: int) -> None: |
59
|
|
|
""" |
60
|
|
|
:param items_in_keyboard: |
61
|
|
|
:param items_in_row: |
62
|
|
|
:return: |
63
|
|
|
""" |
64
|
|
|
|
65
|
1 |
|
if items_in_keyboard > MAXIMUM_ITEMS_IN_KEYBOARD: |
66
|
1 |
|
value_error_message_keyboard = \ |
67
|
|
|
"Telegram Bot API limit exceeded: The keyboard should have " \ |
68
|
|
|
"from 1 to %s buttons at all. Your total amount is %s." |
69
|
1 |
|
raise ValueError(value_error_message_keyboard % |
70
|
|
|
(MAXIMUM_ITEMS_IN_KEYBOARD, items_in_keyboard)) |
71
|
|
|
|
72
|
1 |
|
if items_in_row is not None and \ |
73
|
|
|
(MINIMUM_ITEMS_IN_LINE > items_in_row or items_in_row > MAXIMUM_ITEMS_IN_LINE): |
74
|
1 |
|
value_error_message_line = \ |
75
|
|
|
"Telegram Bot API limit exceeded: " \ |
76
|
|
|
"The keyboard line should have from 1 to %s buttons. You entered %s." |
77
|
1 |
|
raise ValueError(value_error_message_line % |
78
|
|
|
(MAXIMUM_ITEMS_IN_LINE, items_in_row)) |
79
|
|
|
|
80
|
|
|
|
81
|
1 |
|
def get_total_items_number(items, keyboard) -> int: |
82
|
|
|
""" |
83
|
|
|
:param items: |
84
|
|
|
:param keyboard: |
85
|
|
|
:return: |
86
|
|
|
""" |
87
|
1 |
|
total_items_number = sum( |
88
|
|
|
len(row) if isinstance(row, (list, tuple, set)) else 1 for row in items) |
89
|
|
|
|
90
|
1 |
|
if not keyboard: |
91
|
1 |
|
return total_items_number |
92
|
|
|
|
93
|
1 |
|
keyboard_items = keyboard.__dict__['keyboard'] |
94
|
1 |
|
current_keyboard_items_number = sum(len(row) for row in keyboard_items) |
95
|
|
|
return total_items_number + current_keyboard_items_number |
96
|
|
|
|