forked from yaribussi/Blindo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileManaging.py
138 lines (100 loc) · 4.17 KB
/
fileManaging.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import pickle as pk
import os
# from IPython.utils.tests.test_wildcard import obj_t
from fileAudio import FileAudio
import shutil
import StaticParameter as SP
name_file = 'Lista Default'
def change_list(list_name):
global name_file
name_file = list_name
def give_id_button(audio_name):
obj_list = load_list()
for audio in obj_list:
if audio.name == audio_name:
return int(audio.idButton)
# funzione che ritorna la lista delle stringhe dei fileAudio
# presenti in "lista finale" riordinata in base al numero del pulsante
def give_sorted_list():
final_path = os.path.join(SP.path_liste, name_file)
list = []
if os.path.isfile(final_path):
try:
with open(final_path, 'rb') as io:
my_objects = pk.load(io)
my_objects.sort(key=lambda x: int(str(x.idButton)))
for audio in my_objects:
if str(audio.name) != "DEFAULT":
formattedNumber = '{:5s}'.format(str(audio.idButton))
list.append("Pulsante " + formattedNumber + " ---> " + str(audio.name) + "\n")
except Exception as e:
print(e)
return list
# funzione che elimina da lista finale i fileAudio che vengono eliminati tramite la GUI
def delete_element_from_list(nome_file_da_rimuovere, nome_lista): # aggiungere la liste da conytrollare come parametro
# final_path_element = os.path.join(SP.path_liste, nome_file_da_rimuovere)
final_path_list = os.path.join(SP.path_liste, nome_lista)
# print(final_path_list)
my_objects = []
if os.path.isfile(final_path_list):
try:
with open(final_path_list, 'rb') as io:
my_objects = pk.load(io)
for audio in my_objects:
if audio.name == nome_file_da_rimuovere:
my_objects.remove(audio)
save_file_audio_list(my_objects, nome_lista)
except (FileNotFoundError, IOError) as e:
print(e)
def create_list(list_name):
default = FileAudio('DEFAULT', 0)
my_objects = [default]
save_file_audio_list(my_objects, list_name)
def delete_bind(id, audio_name):
obj_list = load_list()
for audio in obj_list:
if audio.idButton == id and audio.name == audio_name:
obj_list.remove(audio)
save_file_audio(obj_list)
# binding between the button and the name of the file audio
def bind(audio_name, id):
# default element needed to pass through the list of file audio if it has only one element
file_audio = FileAudio(audio_name, id)
final_path = os.path.join(SP.path_liste, name_file)
my_objects = []
if os.path.isfile(final_path):
try:
final_path = os.path.join(SP.path_liste, name_file)
with open(final_path, 'rb') as io:
my_objects = pk.load(io)
for audio in my_objects:
if audio.name == audio_name or audio.idButton == id:
my_objects.remove(audio)
my_objects.append(file_audio)
save_file_audio(my_objects)
except (FileNotFoundError, IOError) as e:
print(e)
else:
my_objects.append(file_audio)
save_file_audio(my_objects)
def load_list():
my_objects = []
try:
final_path = os.path.join(SP.path_liste, name_file)
with open(final_path, 'rb') as io:
my_objects = pk.load(io)
except (FileNotFoundError, IOError) as e:
print(e)
return my_objects
# funzione per salvare la lista di fileAudio come un unico oggetto con nome "lista finale" #####
def save_file_audio(my_objects):
final_path = os.path.join(SP.path_liste, name_file)
with open(final_path, 'wb') as output:
pk.dump(my_objects, output, -1)
def save_file_audio_list(my_objects, name_list):
final_path = os.path.join(SP.path_liste, name_list)
with open(final_path, 'wb') as output:
pk.dump(my_objects, output, -1)
# funzione che utilizza la funzione di sitema "shutil.copy" per copiare i file da un path ad un altro
def copy_file_from_path_to_another(initial_path, ending_path):
shutil.copy(initial_path, ending_path)