Passed
Push — master ( 48245f...6bb124 )
by torrua
01:22
created

keyboa.functions_callback   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 67
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Functions

Rating   Name   Duplication   Size   Complexity  
A get_callback() 0 11 2
A get_checked_marker() 0 16 3
A get_callback_data() 0 26 3
1
# -*- coding:utf-8 -*-
2 1
"""
3
Module with functions for work with callback
4
"""
5
6 1
from keyboa.constants import CallbackDataMarker, \
7
    MAXIMUM_CBD_LENGTH, callback_data_types
8
9
10 1
def get_callback_data(
11
        raw_callback: CallbackDataMarker,
12
        front_marker: CallbackDataMarker = str(),
13
        back_marker: CallbackDataMarker = str()) -> str:
14
    """
15
    :param raw_callback:
16
    :param front_marker:
17
    :param back_marker:
18
    :return:
19
    """
20
21 1
    front_marker = get_checked_marker(front_marker)
22 1
    back_marker = get_checked_marker(back_marker)
23
24 1
    callback_data = "%s%s%s" % (front_marker, raw_callback, back_marker)
25
26 1
    if not callback_data:
27 1
        raise ValueError("The callback data cannot be empty.")
28
29 1
    if len(callback_data.encode()) > MAXIMUM_CBD_LENGTH:
30 1
        size_error_message = "The callback data cannot be more than " \
31
                             "64 bytes for one button. Your size is %s" \
32
                             % len(callback_data.encode())
33 1
        raise ValueError(size_error_message)
34
35 1
    return callback_data
36
37
38 1
def get_checked_marker(marker: CallbackDataMarker) -> CallbackDataMarker:
39
    """
40
    :param marker:
41
    :return:
42
    """
43
44 1
    if marker is None:
45 1
        marker = str()
46
47 1
    if not isinstance(marker, callback_data_types):
48 1
        type_error_message = \
49
            "Marker could not have %s type. Only %s allowed." \
50
            % (type(marker), CallbackDataMarker)
51 1
        raise TypeError(type_error_message)
52
53 1
    return marker
54
55
56 1
def get_callback(button_data: tuple) -> str:
57
    """
58
    :param button_data:
59
    :return:
60
    """
61 1
    callback = button_data[1]
62 1
    if not isinstance(callback, callback_data_types):
63 1
        type_error_message = "Callback cannot be %s. Only %s allowed." \
64
                             % (type(callback), callback_data_types)
65 1
        raise TypeError(type_error_message)
66
    return callback
67