Passed
Pull Request — main (#99)
by
unknown
02:57 queued 01:24
created

all_sorter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 50
dl 0
loc 71
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A split_80_space() 0 15 3
A sort_all() 0 27 1
B main() 0 16 7
1
import os
2
from typing import List
3
4
5
def split_80_space(string: str, goal_list: List[str] = None) -> str:
6
    if goal_list is None:
7
        goal_list = []
8
9
    if len(string) < 80:
10
        goal_list.append(string)
11
        return '\n'.join(goal_list)
12
13
    left, right = string[:80], string[80:]
14
    left = left[::-1]
15
    first_space = left.find(' ')
16
    left_left, left_right = left[first_space:], left[:first_space]
17
18
    goal_list.append(left_left[::-1].rstrip())
19
    return split_80_space('    ' + left_right[::-1] + right, goal_list)
20
21
22
def sort_all(file_content: str) -> str:
23
    all_pos = file_content.index('__all__')
24
25
    left_parsed_var = file_content[all_pos:]
26
    end_parenthesis_pos = left_parsed_var.index(')') - 1
27
28
    right_parsed = left_parsed_var[:end_parenthesis_pos]
29
    left_parsed = right_parsed[right_parsed.index('(') + 1:]
30
31
    sorted_parsed = [
32
        f'"{w}"' for w in sorted(
33
            left_parsed.replace('\n', '')
34
                .replace('"', '')
35
                .replace(' ', '')
36
                .split(',')
37
        )
38
    ]
39
40
    joined_string = ', '.join(sorted_parsed)
41
    with_all_name = '__all__ = (\n\t' + joined_string + '\n)'
42
    all_formatted = split_80_space(with_all_name)
43
44
    return (
45
            file_content[:all_pos]
46
            + all_formatted
47
            + file_content[all_pos + end_parenthesis_pos + 2:]
48
    ).replace('\t', '    ')
49
50
51
def main():
52
    for directory, _sub_folders, files in os.walk('pincer'):
53
        if '__' in directory:
54
            continue
55
56
        if '__init__.py' not in files:
57
            continue
58
59
        with open(os.path.join(directory, '__init__.py')) as f:
60
            file_content = f.read()
61
62
        if '__all__' not in file_content:
63
            continue
64
65
        with open(os.path.join(directory, '__init__.py'), 'w') as f:
66
            f.write(sort_all(file_content))
67
68
69
if __name__ == '__main__':
70
    main()
71