-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
85 lines (65 loc) · 1.64 KB
/
utils.py
File metadata and controls
85 lines (65 loc) · 1.64 KB
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
import os
import json
# Some functions
joinPath = os.path.join
isDir = os.path.isdir
isExist = os.path.exists
scriptsDir = os.path.dirname(__file__)
realPath = os.path.realpath
# Simple Implementation class to manage JSON Config Objects
class JSO:
__jso = {}
__path = ""
__ind = None
def __init__(self, path, indent=None):
self.__path = path
self.__ind = indent
self.load()
def store(self):
try:
fo = open(self.__path, "w")
fo.write(json.dumps(self.__jso, indent=self.__ind))
fo.close()
except Exception as e:
print(e)
exit(0)
def load(self):
try:
fo = open(self.__path, "r")
self.__jso = json.load(fo)
fo.close()
except Exception as e:
print(e)
exit(0)
def get(self, attr):
return self.__jso[attr]
def set(self, attr, val):
self.__jso[attr] = val
self.store()
def getAllProducts():
f = open("Data/allProducts.json", "r")
data = json.load(f)
f.close()
return data
def readFile(fname):
f = open(fname, "r")
data = f.read()
f.close()
return data
def outFile(fname, data):
fo = open(fname, "w")
fo.write(data)
fo.close()
def copyFile(old, new):
f = open(old, "r")
outFile(new, f.read())
f.close()
def clearFolder(dirs):
filesToRemove = [joinPath(dirs, f) for f in os.listdir(dirs)]
for f in filesToRemove:
os.remove(f)
def createDir(path, override=False):
if override or not isExist(path):
os.mkdir(path)
return True
return False