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

calculate_items_in_row()   A

Complexity

Conditions 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nop 3
dl 0
loc 21
ccs 10
cts 10
cp 1
crap 4
rs 9.85
c 0
b 0
f 0
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