-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
157 lines (113 loc) · 5.74 KB
/
test_main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#alle files kopieren
#quell laufwerk auswählen
#doppelte erkennen und neu neue files kopieren
#ordner anlegen für jeden tag, anhand bilder meta daten
#ausgabe wieviele files kopiert wurden
#ausgabe wieviele files erkannt wurden, bestätigung ob kopiert werden soll, kopieren und überschreiben? kopieren nur neue?
#sync der daten nach cloud (an mit mobilen daten, an mit wlan, aus)
#mobile daten verwenden, rest volumen mobile daten anzeigen
#wlan anzeigen und einloggen
#schnittstelle zum handy für eingabe von wlan daten (WIFI?)
#anzeige vom festplatten speicher
#einstellbarmachen welche dateien kopiert werden sollen.
#status leiste: akku stand, festplatten stand, wifi, uhrzeit / datum
#progressbar kopieren
#logging output des ganzen prozesses,
#wenn foto nicht in ordner einsortiert werden kann, dann in dummy ordner packen
#Signal Anzeige ob alle Daten in die Clound synchronisiert worden sind.
#gerät soll per usb auslesbar sein, also usb host
#--------------------------Done------------------------------
#datum von file auslesen ordner erstellen mit dem datum.
#zuerst sortieren welche files wohin sollen und dann am ende alle files am stück kopieren, für progress bar
#-------------------------Verworfen--------------------------
#wenn gerät unter 30% akku warnung, wenn gerät unter 10% stop, unter 5% kein anschalten -> Betrieb nur mit Netzstecker
from pathlib import Path
import os
from datetime import datetime
import shutil
def list_files_walk(search_path:Path,list_of_allowed_file_names):
"""Function used to iterate over files under a given dir
Keyword arguments:
search_path -- path to look for files/pictures
list_of_allowed_file_names -- list with the file suffixes to look for
"""
found_files_dict = {}
for root, dirs, files in os.walk(search_path):
for file in files:
for file_subfix in list_of_allowed_file_names:
if file.endswith(file_subfix):
found_files_dict[file]=os.path.join(root, file)
return found_files_dict
def create_folders(target_dir:Path,list_of_dates):
"""Function used to create folders based on the creation date of a file
Keyword arguments:
target_dir -- location where the folders are created
list_of_dates -- list containing the dates as strings to create folders from
"""
for dir_element in list_of_dates:
temp_dir_path = Path(target_dir,dir_element)
if os.path.isdir(temp_dir_path):
print(f"{temp_dir_path} is already there skipping")
else:
os.mkdir(temp_dir_path)
print(f"{temp_dir_path} has been created.")
def check_if_file_already_exists(file_name:str, date_folder:str, target_path:Path):
"""Function used to create folders based on the creation date of a file
Keyword arguments:
file_name -- file name as string to check
date_folder -- name of the date folder
target_path -- path of the target directory
"""
file_path_target = Path(target_path,date_folder,file_name)
if file_path_target.is_file():
return True
else:
return False
def copy_batched_files(dict_with_batched_files:dict[Path,Path],device_name:str):
"""Function used to create folders based on the creation date of a file
Keyword arguments:
dict_with_batched_files -- dict with two paths containing the files to copy [source_path, target_path]
device_name -- name of the device, used for logging
"""
number_of_files_to_copy : int = len(dict_with_batched_files)
print(f"Found {number_of_files_to_copy} files in total to copy.")
file_counter = 0
for source_path, target_path in dict_with_batched_files.items():
shutil.copyfile(source_path,target_path)
file_counter += 1
print(f"{file_counter}.) {device_name} copied {target_path}.")
print(f"Copied a total number of {file_counter} files.")
if __name__ == "__main__":
sd_card_reader_path : Path = Path("E:\\")
target_path : Path = "D:\\test_target_path"
list_of_allowed_file_names = [".ORF"]
batched_files_to_copy = {}
FLAG_ALWAYS_COPY_REPLACE = False
FLAG_ALWAYS_COPY = False
MY_DEVICE = "BackupBox"
found_files = list_files_walk(sd_card_reader_path,list_of_allowed_file_names)
file_date_dict = {}
for file_element in found_files:
unix_timestamp = os.path.getctime(found_files[file_element])
date_d_m_y = datetime.utcfromtimestamp(unix_timestamp).strftime('%d-%m-%Y')
file_date_dict[file_element] = date_d_m_y
folders_to_create_with_doubles = file_date_dict.values()
folders_to_create = list(set(folders_to_create_with_doubles))
create_folders(target_path,folders_to_create)
for file_name in file_date_dict.keys():
file_date_folder_name = file_date_dict[file_name]
if (not check_if_file_already_exists(file_name,file_date_folder_name,target_path)) or FLAG_ALWAYS_COPY_REPLACE:
file_full_target_path = Path(target_path,file_date_folder_name,file_name)
batched_files_to_copy[found_files[file_name]] = file_full_target_path
print(f"{file_full_target_path} added to the copy list.")
elif FLAG_ALWAYS_COPY:
file_full_target_path_renamed = Path(target_path,file_date_folder_name,"new_"+file_name)
batched_files_to_copy[found_files[file_name]] = file_full_target_path_renamed
print(f"{file_name} is already there. Renaming")
else:
print(f"{file_name} is already there. Skipping...")
if len(batched_files_to_copy) == 0:
print("There are no files to copy")
else:
copy_batched_files(batched_files_to_copy,MY_DEVICE)
#print(next(scan_dir_object)) datetime.utcfromtimestamp(unix_timestamp).strftime('%d-%m-%Y')