Users can be saved and loaded
This commit is contained in:
@@ -3,7 +3,7 @@ Here are the headers that every .py in this folder should implement.
|
||||
Each file is free to define its own set of config options that will be passed in AFTER the file is imported, under the name "config". It is generally assumed that this will be a dict, but there is no strict requirement, other than "it must be a valid json value".
|
||||
The init() function will be called with no arguments after config is passed in.
|
||||
the *token functions are about access tokens, as distinct from invite tokens.
|
||||
The backend will also have access to utils.py, under the name utils.
|
||||
The backend will also have access to utils.py, under the name utils and models.py under the name models.
|
||||
"""
|
||||
def init(): pass
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from os import path
|
||||
from os import path,rename
|
||||
def init():
|
||||
if not path.exists(config['path']): raise Exception(f"Backend directory doesn't exist: {config['path']}")
|
||||
for n in ['users','invite_tokens','access_tokens']:
|
||||
@@ -8,7 +8,7 @@ def init():
|
||||
if path.isdir(p): raise Exception(f"Backend file is a directory: {p}")
|
||||
try: open(p).close()
|
||||
except PermissionError: raise Exception(f"Backend file can't be read: {p}")
|
||||
try: open(p,'w').close()
|
||||
try: open(p,'a').close()
|
||||
except PermissionError: raise Exception(f"Backend file can't be written: {p}")
|
||||
|
||||
def load_user(username):
|
||||
@@ -21,7 +21,11 @@ def save_user(user):
|
||||
split=line.split(',')
|
||||
if split[0]==user.username: update(users,{gen.send(True):user.csv}); break
|
||||
else: update(users,{-1:user.csv})
|
||||
def delete_user(user): pass
|
||||
def delete_user(user):
|
||||
gen=linegen(users)
|
||||
for line in gen:
|
||||
split=line.split(',')
|
||||
if split[0]==user.username: remove(users,[gen.send(True)]); break
|
||||
|
||||
def load_tokens(user): pass
|
||||
def save_token(token): pass
|
||||
@@ -43,15 +47,28 @@ def linegen(path):
|
||||
file.close()
|
||||
|
||||
def update(path,lines):
|
||||
def _action(file,i,line):
|
||||
if i in lines: file.write(lines[i]+'\n')
|
||||
else: file.write(line)
|
||||
def _post(file):
|
||||
if -1 in lines: file.write(lines[-1]+'\n')
|
||||
writefile(path,_action,_post)
|
||||
|
||||
def remove(path,lines):
|
||||
def _action(file,i,line):
|
||||
if i not in lines: file.write(line)
|
||||
writefile(path,_action)
|
||||
|
||||
def writefile(path,action,post=None):
|
||||
i=0
|
||||
file=open(path)
|
||||
write=open(f'{path}.tmp','w')
|
||||
line=file.readline()
|
||||
while line:
|
||||
i+=1
|
||||
if i in lines: write.write(lines[i]+'\n')
|
||||
else: write.write(line)
|
||||
action(write,i,line)
|
||||
line=file.readline()
|
||||
if -1 in lines: write.write(lines[-1])
|
||||
if post is not None: post(write)
|
||||
file.close()
|
||||
write.close()
|
||||
write.close()
|
||||
rename(f'{path}.tmp',path)
|
Reference in New Issue
Block a user