1
|
|
|
# -*- coding:utf-8 -*- |
2
|
1 |
|
""" |
3
|
|
|
This module contains all checks for Keyboa Base class parameters |
4
|
|
|
""" |
5
|
|
|
# pylint: disable = C0116 |
6
|
|
|
|
7
|
1 |
|
from typing import Iterable |
8
|
|
|
|
9
|
1 |
|
from telebot.types import InlineKeyboardMarkup |
10
|
|
|
|
11
|
1 |
|
from keyboa.constants import ( |
12
|
|
|
MAXIMUM_ITEMS_IN_KEYBOARD, |
13
|
|
|
MINIMUM_ITEMS_IN_LINE, |
14
|
|
|
MAXIMUM_ITEMS_IN_LINE, |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
class BaseCheck: |
19
|
|
|
""" |
20
|
|
|
This class contains all checks for Keyboa Base class parameters |
21
|
|
|
""" |
22
|
|
|
|
23
|
1 |
|
@staticmethod |
24
|
1 |
|
def is_all_items_in_limits(items) -> None: |
25
|
1 |
|
items_in_keyboard = sum( |
26
|
|
|
len(row) if isinstance(row, list) else 1 for row in items |
27
|
|
|
) |
28
|
1 |
|
if items_in_keyboard > MAXIMUM_ITEMS_IN_KEYBOARD: |
29
|
1 |
|
value_error_message_keyboard = ( |
30
|
|
|
"Telegram Bot API limit exceeded: The keyboard should have " |
31
|
|
|
"from 1 to %s buttons at all. Your total amount is %s." |
32
|
|
|
) |
33
|
1 |
|
raise ValueError( |
34
|
|
|
value_error_message_keyboard |
35
|
|
|
% (MAXIMUM_ITEMS_IN_KEYBOARD, items_in_keyboard) |
36
|
|
|
) |
37
|
|
|
|
38
|
1 |
|
@classmethod |
39
|
1 |
|
def is_row_in_limits(cls, items) -> None: |
40
|
1 |
|
if all(isinstance(line, list) for line in items): |
41
|
1 |
|
for line in items: |
42
|
1 |
|
cls.is_items_in_row_limits(len(line)) |
43
|
|
|
|
44
|
1 |
|
@staticmethod |
45
|
1 |
|
def is_items_in_row_limits(items_in_row) -> None: |
46
|
1 |
|
if items_in_row is not None and ( |
47
|
|
|
MINIMUM_ITEMS_IN_LINE > items_in_row or items_in_row > MAXIMUM_ITEMS_IN_LINE |
48
|
|
|
): |
49
|
1 |
|
value_error_message_line = ( |
50
|
|
|
"Telegram Bot API limit exceeded: " |
51
|
|
|
"The keyboard line should have from 1 to %s buttons. You entered %s." |
52
|
|
|
) |
53
|
1 |
|
raise ValueError( |
54
|
|
|
value_error_message_line % (MAXIMUM_ITEMS_IN_LINE, items_in_row) |
55
|
|
|
) |
56
|
|
|
|
57
|
1 |
|
@staticmethod |
58
|
1 |
|
def is_alignment_in_limits(auto_alignment) -> None: |
59
|
|
|
""" |
60
|
|
|
:param auto_alignment: |
61
|
|
|
:return: |
62
|
|
|
""" |
63
|
1 |
|
if ( |
64
|
|
|
max(auto_alignment) > MAXIMUM_ITEMS_IN_LINE |
65
|
|
|
or min(auto_alignment) < MINIMUM_ITEMS_IN_LINE |
66
|
|
|
): |
67
|
1 |
|
value_error_message = ( |
68
|
|
|
"The auto_alignment's item values should be between " |
69
|
|
|
"%s and %s. You entered: %s\n" |
70
|
|
|
"You may define it as 'True' to use AUTO_ALIGNMENT_RANGE." |
71
|
|
|
% (MINIMUM_ITEMS_IN_LINE, MAXIMUM_ITEMS_IN_LINE, auto_alignment) |
72
|
|
|
) |
73
|
1 |
|
raise ValueError(value_error_message) |
74
|
|
|
|
75
|
1 |
|
@staticmethod |
76
|
1 |
|
def is_alignment_iterable(auto_alignment) -> None: |
77
|
|
|
""" |
78
|
|
|
:param auto_alignment: |
79
|
|
|
:return: |
80
|
|
|
""" |
81
|
1 |
|
if not ( |
82
|
|
|
isinstance(auto_alignment, Iterable) |
83
|
|
|
and all(map(lambda s: isinstance(s, int), auto_alignment)) |
84
|
|
|
): |
85
|
1 |
|
type_error_message = ( |
86
|
|
|
"The auto_alignment variable has not a proper type. " |
87
|
|
|
"Only Iterable of integers or boolean type allowed.\n" |
88
|
|
|
"You may define it as 'True' to use AUTO_ALIGNMENT_RANGE." |
89
|
|
|
) |
90
|
1 |
|
raise TypeError(type_error_message) |
91
|
|
|
|
92
|
1 |
|
@staticmethod |
93
|
1 |
|
def is_keyboard_proper_type(keyboard) -> None: |
94
|
1 |
|
if keyboard and not isinstance(keyboard, InlineKeyboardMarkup): |
95
|
1 |
|
type_error_message = ( |
96
|
|
|
"Keyboard to which the new items will be added " |
97
|
|
|
"should have InlineKeyboardMarkup type. Now it is a %s" % type(keyboard) |
98
|
|
|
) |
99
|
|
|
raise TypeError(type_error_message) |
100
|
|
|
|