-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_mfrNames.py
63 lines (57 loc) · 3.06 KB
/
optimize_mfrNames.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
import os
import sys
from typedef_components import Components_typeDef #класс базы данных компонентов
script_dirName = os.path.dirname(__file__) #адрес папки со скриптом
script_baseName = os.path.splitext(os.path.basename(__file__))[0] #базовое имя модуля
dictionary_address = os.path.join(script_dirName, script_baseName.replace('optimize', 'dict') + os.extsep + 'py') #адрес словаря с именами производителей
#Оптимизирует имена производителей
def optimize(data, **kwargs):
print('INFO >> Manufacturer names optimizer module running.')
#словарь
dict_names = kwargs.get('dict_names', dictionary_address)
print(' ' * 12 + "dictionary: ", end ="", flush = True)
if isinstance(dict_names, str):
#получили строку с адресом словаря
print(f"from module ({os.path.basename(dict_names)}), importing", end ="... ", flush = True)
#импортируем словарь из модуля
import importlib.util
spec = importlib.util.spec_from_file_location("dict_mfrNames", dict_names)
dict_mfrNames = importlib.util.module_from_spec(spec)
sys.modules["dict_mfrNames"] = dict_mfrNames
spec.loader.exec_module(dict_mfrNames)
dict_names = dict_mfrNames.data
print(f"done ({len(dict_names)} entries)")
elif isinstance(dict_names, dict):
#получили словарь как аргумент
print(f"from argument ({len(dict_names)} entries)")
else:
#получили непонятно что
print("invalid")
raise ValueError
#разворачиваем данные
components = data
print('INFO >> Translating names', end ="... ", flush = True)
for component in components.entries:
if component.GENERIC_manufacturer is not None:
entryFound = False
for entry in dict_names:
for word in dict_names[entry]:
if word.casefold() == component.GENERIC_manufacturer.casefold():
component.GENERIC_manufacturer = entry
entryFound = True
break
if entryFound: break
if component.GENERIC_substitute is not None:
for substitute in component.GENERIC_substitute:
if substitute.manufacturer is not None:
entryFound = False
for entry in dict_names:
for word in dict_names[entry]:
if word.casefold() == substitute.manufacturer.casefold():
substitute.manufacturer = entry
entryFound = True
break
if entryFound: break
print('done.')
print("INFO >> Manufacturers names optimization completed.")
return True