-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfile_util.py
209 lines (172 loc) · 6.37 KB
/
file_util.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
from os.path import abspath, join, pardir, basename, dirname, isdir
import shutil
import ntpath
import GPIO_Init
import time
# from GPIO_Init import getAnyKeyEvent, displayImage, getFont, getKeyStroke, getSmallFont, displayPng
from PIL import Image, ImageDraw
__author__ = "Hsuan Han Lai (Edward Lai)"
__date__ = "2019-04-02"
workDir = os.path.dirname(os.path.realpath(__file__))
# =================== Helper Tools===================
def copytree(src, dst, symlinks=False, ignore=shutil.ignore_patterns('.*', '_*')):
"""
Copy Entire Folder
:param src: source path
:param dst: destination path
:param symlinks: optional
:param ignore: pass shutil.ignore_patterns('.*', '_*')
:return:
"""
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
def copyfile(src, dst, with_remove=False):
print("copy " + src + " to " + dst)
shutil.copy(src, dst)
if (with_remove):
os.remove(src)
def forcedir(path):
"""
This creat necessary folders to the path if not already exists
:param path: path to folder existence check
:return: NA
"""
if not os.path.isdir(path):
os.makedirs(path)
# ======================Copy Files ======================
def listOfPathsToDir(lst, target_Dir):
for i in lst:
parentfolder = ntpath.basename(os.path.dirname(i))
filename = ntpath.basename(i)
# Ensure the Dir is There
forcedir(target_Dir + "/" + parentfolder)
dstBuilder = target_Dir + "/" + parentfolder + filename
shutil.copyfile(i, dstBuilder)
# [synth pack 1, synth pack 2, synth pack 3, synth pack 4... ], op1/synth
def copyListOfDirs(lst, target_Dir):
for i in lst:
foldername = ntpath.basename(i)
forcedir(target_Dir + "/" + foldername)
copytree(i, target_Dir + "/" + foldername)
# =======================List Files======================
def get_visible_folders(dirPath=""):
"""
Get visible folder from given path
:param dir: path to directory
:return: list of directories found
"""
lst = []
try:
lst = list(filter(lambda x: os.path.isdir(os.path.join(dirPath, x)), os.listdir(dirPath)))
except:
pass
return lst
def getDirFileList(d):
return list(filter(lambda x: x[0] != '.', os.listdir(d)))
def fileTransferHelper(srclist, dest):
"""
Pass in list of paths to file, and copy to root destination
It will create patch's parent folder if not already exist in the destination folder
For example:
fileTransferHelper(["..../OP1_File_Organizer/NotUsed/..../patch.aif"], dest = "/..../synth")
:param srclist: ["pwd/1.aif", "pwd/2.aif", "pwd/3.aif",....., "pwd/n.aif"]
:param dest: Root of the synth and drum destination folder
:return: NA
"""
for i in srclist:
srcParentFolderName = abspath(join(i, pardir)).split("/")[-1:][0]
srcBaseName = basename(i)
distParentFolderName = dest + "/" + str(srcParentFolderName)
print(distParentFolderName)
forcedir(distParentFolderName)
image = Image.new('1', (128, 64))
if workDir in srclist[0]:
# Local to OP1
image.paste(Image.open(workDir + "/Assets/Img/UploadPatches.png").convert("1"))
else:
# OP1 to Local
image.paste(Image.open(workDir + "/Assets/Img/DownloadPatches.png").convert("1"))
draw = ImageDraw.Draw(image)
draw.text((20, 63), srcBaseName, font=GPIO_Init.getFont(), fill="white")
GPIO_Init.displayImage(image)
print(i, distParentFolderName + "/" + srcBaseName)
shutil.copy2(i, distParentFolderName + "/" + srcBaseName)
GPIO_Init.displayPng(workDir + "/Assets/Img/Done.png")
GPIO_Init.getAnyKeyEvent() # Press any key to proceed
return
def deleteHelper(srclist):
"""
Takes in a list of full walk path(str) for deletion.
And render the delete on the display while deleting.
For Example:
[filePath1, filePath2, filePath3.......filePath n]
or
[DirPath1, DirPath2, DirPath3,...., DirPath n]
:param srclist: list of paths to file or directory
:return: NA
"""
GPIO_Init.displayPng(workDir + "/Assets/Img/Deleting.png")
time.sleep(0.5)
for f in srclist:
if isdir(f):
# If given element in a list is a directory
shutil.rmtree(srclist[0])
else:
folder = dirname(f)
if os.path.exists(f):
# Check for file existence
os.remove(f)
if len(os.listdir(folder)) == 0:
# If nothing is in the folder, remove the parent folder
os.rmdir(folder)
GPIO_Init.displayPng(workDir + "/Assets/Img/Done.png")
GPIO_Init.getAnyKeyEvent() # Press any key to proceed
return
def recursive_overwrite(src, dest, ignore=None):
print(src, dest)
if os.path.isdir(src):
if not os.path.isdir(dest):
os.makedirs(dest)
files = os.listdir(src)
if ignore is not None:
ignored = ignore(src, files)
else:
ignored = set()
for f in files:
if f not in ignored:
recursive_overwrite(os.path.join(src, f),
os.path.join(dest, f),
ignore)
else:
shutil.copyfile(src, dest)
def moveFilesToFolder(lst, destFolderPath):
for f in lst:
base = basename(f)
dest = os.path.join(destFolderPath, base)
print("Moving: ", f, " To: ", dest)
shutil.move(f, dest)
def createEmptyFolder(path, foldername):
newFolderPath = join(path, foldername)
if not os.path.exists(newFolderPath):
os.makedirs(newFolderPath)
def clearUnderFolder(path):
for root, dirs, files in os.walk(path):
for f in files:
print("remove", f)
os.unlink(os.path.join(root, f))
for d in dirs:
print("remove Dir", d)
shutil.rmtree(os.path.join(root, d))
def createImportantFolders():
from config import savePaths
forcedir(savePaths["Local_Dir"])
forcedir(savePaths["Local_Projects"])
forcedir(savePaths["Local_Patches"])
forcedir(savePaths["Local_Synth"])
forcedir(savePaths["Local_Drum"])