forked from FastestMolasses/Vouch-Pro-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
99 lines (72 loc) · 2.32 KB
/
data.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
#cython: language_level=3
import os
import sys
import glob
import json
DATABASE_FILENAME = 'database.json'
def popTextFile(filename: str) -> str:
with open(getFileName(filename), 'r') as f:
lines = f.readlines()
# If we have no lines, then return none
if len(lines) == 0:
return None
popped = lines[0]
# Save the new file without the first one
with open(getFileName(filename), 'w') as f:
# If we have more than one line, then write the rest of them to the file
if len(lines) > 1:
for i in lines[1:]:
f.write(i)
return popped
def updateJson(path: str, data: dict):
try:
d = loadJSON(path)
except FileNotFoundError:
d = {}
d.update(data)
saveJSON(path, d)
def getLineFromTextFile(filename: str) -> str:
with open(getFileName(filename), 'r') as f:
return f.readline().strip()
def deleteLineFromTextFile(line: str, filename: str):
with open(getFileName(filename), 'r') as f:
lines = f.readlines()
with open(getFileName(filename), 'w') as f:
for i in lines:
if line in i:
continue
f.write(i)
def saveToTextFile(data: str, path: str):
data = data.strip()
with open(getFileName(path), 'a') as f:
if isFileEmpty(path):
f.write(data)
else:
f.write('\n' + data)
def getFileName(path: str):
directory = ''
if getattr(sys, 'frozen', False):
if os.name == 'nt':
directory = sys.executable[:sys.executable.rfind('\\')]
else:
directory = sys.executable[:sys.executable.rfind('/')]
return os.path.join(directory, path)
def saveListToTextFile(data: list, path: str):
with open(getFileName(path), 'a') as f:
fileEmpty = isFileEmpty(path)
for i in data:
if fileEmpty:
f.write(i.strip())
fileEmpty = False
else:
f.write('\n' + i.strip())
def isFileEmpty(fileName: str) -> bool:
return os.stat(fileName).st_size == 0
def loadJSON(fileName: str) -> dict:
with open(getFileName(fileName), 'r') as f:
return json.load(f)
def saveJSON(path: str, data: dict):
path = getFileName(path)
with open(path, 'w') as f:
json.dump(data, f, indent=4)
return True