-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubeMX_CPP_Converter.py
101 lines (82 loc) · 4.05 KB
/
CubeMX_CPP_Converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! python3
# CubeMX+CPP_Converter - заменяет расширения указанных файлов в папке
# Использование:
# Скрипт размещается в каталоге с проектом
import os, sys, time
# Имена/окончания заменяемых файлов (без расширения)
fileList = [
"main", # Это имя используется в качестве маркера для динамического определения текущего расширения
"*it",
"usbd_storage_if",
"user_diskio"
]
# Массив заменяемых расширений
fileExtensions = [
".c",
".cpp"
]
# Текущий путь приложения
path = os.path.realpath(os.path.dirname(sys.argv[0]))
class ExtensionConverter(object):
"""Класс конвертера расширения файлов"""
# Текущее расширение файлов
__CurrentExtension = "no_extension"
# Новое расширение файлов
__NewExtension = "no_extension"
def __init__(self, fileList, extensionList, directory):
""" Конструктор класса """
# Список файлов
self.__FileList = fileList
# Список расширений
self.__ExtensionList = extensionList
# Имя файла-маркера
self.__MarkerName = self.__FileList[0]
# Директория
self.__Directory = directory
def __IsNeedToChange(self, filename):
""" Метод проверки необходимости изменения расширения для текущего файла """
if not (filename.lower()).endswith(self.__CurrentExtension):
return False
for item in self.__FileList:
if (item.find("*") == 0):
item = item[1:]
if (filename.lower()).endswith(item + self.__CurrentExtension):
return True
else:
if filename.lower() == (item + self.__CurrentExtension):
return True
return False
def __GetExtensionSettings(self, project_path):
""" Метод автоматического определения настроек конвертации """
for folderName, subfoldersName, fileNames in os.walk(project_path):
for fileName in fileNames:
ind = 0
for item in self.__ExtensionList:
if self.__MarkerName.find("*") == 0:
if (fileName.lower()).endswith(self.__MarkerName + item):
self.__CurrentExtension = item
self.__NewExtension = self.__ExtensionList[(ind + 1) % len(self.__ExtensionList)]
return
else:
if fileName.lower() == (self.__MarkerName + item):
self.__CurrentExtension = item
self.__NewExtension = self.__ExtensionList[(ind + 1) % len(self.__ExtensionList)]
return
ind = ind + 1
def __ChangeFileExtensions(self, project_path):
""" Метод замены расширений файлов """
for folderName, subfoldersName, fileNames in os.walk(project_path):
for fileName in fileNames:
if (self.__IsNeedToChange(fileName)):
source = folderName + '/' + fileName
os.rename(source, os.path.join(folderName, (fileName[:-(len(self.__CurrentExtension))] + self.__NewExtension)))
def Process(self):
""" Метод обработки файлов """
self.__GetExtensionSettings(self.__Directory)
self.__ChangeFileExtensions(self.__Directory)
# Выполнение программы
if __name__ == '__main__':
# Экземпляр класса
converter = ExtensionConverter(fileList, fileExtensions, path)
converter.Process()
print('Выполнено')