Passed
Push — dev ( 920862...8cd466 )
by Andreas
01:10
created

CMS-cli.ask_paths()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
"""CLI Версия CMS."""
2
import os
3
from pathlib import Path
4
5
vers = '0.3.3с'
6
7
8
def ask_paths():
9
    """Запуск и в интерактивном режиме."""
10
    print('Путь до исходной директории')
11
    input_dir = input()
12
    print('Путь до конечной директории')
13
    output_dir = input()
14
    return input_dir, output_dir
15
16
17
def start_interactive():
18
    """Запуск интерактивного режима."""
19
    answers = ask_paths()
20
    input_dir = answers[0]
21
    output_dir = answers[1]
22
    if input_dir is None or output_dir is None:
23
        print('Не указана начальная или конечная директория')
24
        return
25
    elif input_dir == output_dir:
26
        print('Начальная и конечная директории не должны совпадать')
27
        return
28
    else:
29
        main_process()
30
31
32
def main_process():
33
    """Основной процесс программы."""
34
    pass
35
36
37
if input_dir == '' or output_dir == '':
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable input_dir does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable output_dir does not seem to be defined.
Loading history...
38
    print('Не указана начальная или конечная директория')
39
    exit()
40
elif input_dir == output_dir:
41
    print('Начальная и конечная директории не должны совпадать')
42
    exit()
43
try:
44
    input_dir
45
except Exception:
46
    pass
47
else:
48
    for path, subdirs, files in os.walk(input_dir):
49
        for file in files:
50
            # Перегоняем все MP3 в целевую директорию,
51
            # потом разберемся что с ними делать
52
            # Хотя, лучше искать только нужные (отбрасывать лайвы и ремиксы
53
            # и перегонять уже без них)
54
            filtered = re.search('.*mp3', file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable re does not seem to be defined.
Loading history...
55
            if filtered is not None:
56
                print(f'{path}/{filtered.group(0)}')
57
                shutil.copyfile(f'{path}/{filtered.group(0)}',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable shutil does not seem to be defined.
Loading history...
58
                                f'{output_dir}/{filtered.group(0)}')
59
60
source_file = []
61
# 3.1. Удаление ремиксов и лайвов
62
for files in os.walk(output_dir):
63
    for file in files[2]:
64
        try:
65
            source_file.append(re.search(r'.*\(.*[Rr]emix.*\).*|.*\(.*[Ll]ive.*\).*', file).group(0))
66
        except Exception:
67
            pass
68
for file in source_file:
69
    print('Removing Remix: ', file)
70
    os.remove(f'{output_dir}/{file}')
71
source_file.clear()  # Очищаем список
72
73
# 3.2. Готовим список свежепринесенных файлов с вычищенными ремиксами и лайвами
74
for files in os.walk(output_dir):
75
    for file in files[2]:
76
        try:
77
            source_file.append(file)
78
        except Exception:
79
            pass
80
81
# 3.3. Убираем из имен файлов мусор (номера треков в различном формате)
82
for file in source_file:
83
    new_file = re.sub(r'^[\d{1,2}\s\-\.]*', '', file)
84
    shutil.move(f'{output_dir}/{file}', f'{output_dir}/{new_file}')
85
source_file.clear()
86