make_blueprints_table.create_table()   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 63
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 51
nop 1
dl 0
loc 63
rs 5.8036
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like make_blueprints_table.create_table() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""Generate Blueprints table."""
2
import glob
3
import re
4
5
6
def create_table(directory):
7
    """Create the table header and cells."""
8
    t_header = ''
9
    t_cell = ''
10
11
    bps_rst = []
12
    bps_titles = []
13
    bps_status = []
14
15
    max_len_title = -1
16
    max_len_status = -1
17
    max_len_bps = -1
18
19
    for fp_file in sorted(glob.glob(f'{directory}/EP*.rst')):
20
        split_dir = ''.join(fp_file.split('./blueprints/'))
21
        bp_rst = ''.join(split_dir.split('.rst'))
22
        bps_rst.append(f" :doc:`{bp_rst}<{bp_rst}/>` ")
23
        if max_len_bps < len(bps_rst[-1]):
24
            max_len_bps = len(bps_rst[-1])
25
26
        with open(fp_file) as origin_file:
27
            title = ''
28
            status = ''
29
            for line in origin_file:
30
                if re.findall(r':Title:', line):
31
                    title = ''.join(line.split(':Title:'))
32
                    bps_titles.append(''.join(title.split("\n")))
33
                    if max_len_title < len(title):
34
                        max_len_title = len(title)
35
36
                if re.findall(r':Status:', line):
37
                    status = ''.join(line.split(':Status:'))
38
                    bps_status.append(''.join(status.split("\n")))
39
                    if max_len_status < len(status):
40
                        max_len_status = len(status)
41
                    break
42
43
    th_title_len = max_len_title - len(' Title')
44
    th_status_len = max_len_status - len(' Status')
45
    th_bps_len = max_len_bps - len(' Blueprint')
46
47
    t_header += f"+{'-' * max_len_bps}+"
48
    t_header += f"{'-' * max_len_title}+{'-' * max_len_status}+\n"
49
    t_header += f"|{' Blueprint'}{' ' * th_bps_len}|{' Title'}"
50
    t_header += f"{' ' * th_title_len}|{' Status'}{' ' * th_status_len}|\n"
51
    t_header += f"+{'=' * max_len_bps}+{'=' * max_len_title}+"
52
    t_header += f"{'=' * max_len_status}+\n"
53
54
    for i, _ in enumerate(bps_rst, start=0):
55
        title_space = max_len_title - len(bps_titles[i])
56
        status_space = max_len_status - len(bps_status[i])
57
        bp_space = max_len_bps - len(bps_rst[i])
58
59
        name = bps_rst[i]
60
        title = bps_titles[i]
61
        status = bps_status[i]
62
63
        t_cell += f"|{name}{' ' * bp_space}|{title}{' ' * title_space}|"
64
        t_cell += f"{status}{' ' * status_space}|\n"
65
        t_cell += f"+{'-' * max_len_bps}+{'-' * max_len_title}+"
66
        t_cell += f"{'-' * max_len_status}+\n"
67
68
    return t_header + t_cell
69
70
71
def write_new_index_rst(directory):
72
    """Write table of blueprints in index.rst."""
73
    blueprints_table = create_table(directory)
74
75
    with open(f'{directory}/bp_table.rst', 'w') as fp_file:
76
        fp_file.write(blueprints_table)
77
78
79
if __name__ == '__main__':
80
    write_new_index_rst('./blueprints')
81