-
Notifications
You must be signed in to change notification settings - Fork 1
/
FolderManager.py
79 lines (68 loc) · 2.93 KB
/
FolderManager.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
import os, shutil
class FolderManager:
tldName = "UserFolders"
@staticmethod
def setup():
if not os.path.isdir(os.path.join(os.getcwd(), FolderManager.tldName)):
try:
os.mkdir(os.path.join(os.getcwd(), FolderManager.tldName))
except Exception as e:
return "ERROR: Failed to create top-level directory; error: {}".format(e)
return "Success"
@staticmethod
def registerFolder(accID):
if os.path.isdir(os.path.join(os.getcwd(), FolderManager.tldName, accID)):
return True
try:
os.mkdir(os.path.join(os.getcwd(), FolderManager.tldName, accID))
print("FM: Registered folder for account {}".format(accID))
except Exception as e:
return "ERROR: Failed to register folder; error: {}".format(e)
return True
@staticmethod
def checkIfFolderIsRegistered(accID):
if os.path.isdir(os.path.join(os.getcwd(), FolderManager.tldName, accID)):
return True
else:
return False
@staticmethod
def deleteFolder(accID):
if os.path.isdir(os.path.join(os.getcwd(), FolderManager.tldName, accID)):
try:
shutil.rmtree(os.path.join(os.getcwd(), FolderManager.tldName, accID), ignore_errors=True)
return True
except Exception as e:
return "ERROR: Failed to delete folder; error: {}".format(e)
else:
return "ERROR: No such folder registered."
@staticmethod
def getFilenames(accID):
if os.path.isdir(os.path.join(os.getcwd(), FolderManager.tldName, accID)):
filenames = [f for f in os.listdir(os.path.join(os.getcwd(), FolderManager.tldName, accID)) if os.path.isfile(os.path.join(os.getcwd(), FolderManager.tldName, accID, f))]
if filenames == []:
filenames = []
return filenames
else:
return "ERROR: No such folder exists."
@staticmethod
def deleteFile(accID, filename):
if os.path.isdir(os.path.join(os.getcwd(), FolderManager.tldName, accID)):
if os.path.isfile(os.path.join(os.getcwd(), FolderManager.tldName, accID, filename)):
try:
os.remove(os.path.join(os.getcwd(), FolderManager.tldName, accID, filename))
return True
except Exception as e:
return "ERROR: An unknown error occured in deleting the file."
else:
return "ERROR: No such file exists in the folder registered under that account ID."
else:
return "ERROR: No such folder exists."
@staticmethod
def getFileExtension(filename):
extension = ''
for char in reversed(filename):
if char == '.':
break
else:
extension = char + extension
return extension