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

keyboa.functions_alignment   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 83
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 14

5 Functions

Rating   Name   Duplication   Size   Complexity  
A check_alignment_settings() 0 7 1
A check_is_alignment_iterable() 0 12 4
A check_is_alignment_in_limits() 0 13 3
A get_alignment_range() 0 11 2
A calculate_items_in_row() 0 21 4
1
# -*- coding:utf-8 -*-
2 1
"""
3
Module with functions for work with alignment
4
"""
5 1
from typing import Optional, Iterable
6
7 1
from keyboa.constants import AUTO_ALIGNMENT_RANGE, \
8
    MAXIMUM_ITEMS_IN_LINE, MINIMUM_ITEMS_IN_LINE
9
10
11 1
def calculate_items_in_row(
12
        items, auto_alignment, reverse_alignment_range) -> Optional[int]:
13
    """
14
    :param items:
15
    :param auto_alignment:
16
    :param reverse_alignment_range:
17
    :return:
18
    """
19
20 1
    items_in_row = None
21 1
    alignment_range = get_alignment_range(auto_alignment)
22
23 1
    if reverse_alignment_range:
24 1
        alignment_range = reversed(alignment_range)
25
26 1
    for divider in alignment_range:
27 1
        if not len(items) % divider:
28 1
            items_in_row = divider
29 1
            break
30
31 1
    return items_in_row
32
33
34 1
def get_alignment_range(auto_alignment):
35
    """
36
    :param auto_alignment:
37
    :return:
38
    """
39
40 1
    if isinstance(auto_alignment, bool):
41 1
        return AUTO_ALIGNMENT_RANGE
42
43 1
    check_alignment_settings(auto_alignment)
44 1
    return auto_alignment
45
46
47 1
def check_alignment_settings(auto_alignment):
48
    """
49
    :param auto_alignment:
50
    :return:
51
    """
52 1
    check_is_alignment_iterable(auto_alignment)
53 1
    check_is_alignment_in_limits(auto_alignment)
54
55
56 1
def check_is_alignment_in_limits(auto_alignment):
57
    """
58
    :param auto_alignment:
59
    :return:
60
    """
61 1
    if max(auto_alignment) > MAXIMUM_ITEMS_IN_LINE \
62
            or min(auto_alignment) < MINIMUM_ITEMS_IN_LINE:
63 1
        value_error_message = \
64
            "The auto_alignment's item values should be between " \
65
            "%s and %s. You entered: %s\n" \
66
            "You may define it as 'True' to use AUTO_ALIGNMENT_RANGE." \
67
            % (MINIMUM_ITEMS_IN_LINE, MAXIMUM_ITEMS_IN_LINE, auto_alignment)
68 1
        raise ValueError(value_error_message)
69
70
71 1
def check_is_alignment_iterable(auto_alignment):
72
    """
73
    :param auto_alignment:
74
    :return:
75
    """
76 1
    if not (isinstance(auto_alignment, Iterable)
77
            and all(map(lambda s: isinstance(s, int), auto_alignment))):
78 1
        type_error_message = \
79
            "The auto_alignment variable has not a proper type. " \
80
            "Only Iterable of integers or boolean type allowed.\n" \
81
            "You may define it as 'True' to use AUTO_ALIGNMENT_RANGE."
82
        raise TypeError(type_error_message)
83