I'm not losing main.py twice -.-
This commit is contained in:
20
backends/README.py
Normal file
20
backends/README.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
def init(): pass
|
||||
|
||||
def load_user(username): pass
|
||||
def save_user(user): pass
|
||||
def delete_user(user): pass
|
||||
|
||||
def load_tokens(user): pass
|
||||
def save_token(token): pass
|
||||
def delete_token(token): pass
|
||||
|
||||
def load_invites(user): pass
|
||||
def save_invite(token): pass
|
||||
def delete_invite(token): pass
|
55
backends/file.py
Normal file
55
backends/file.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from os import path
|
||||
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']:
|
||||
p=path.join(config['path'],n+'.csv')
|
||||
globals()[n]=p
|
||||
if not path.exists(p): open(p,'w').write('\n')
|
||||
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()
|
||||
except PermissionError: raise Exception(f"Backend file can't be written: {p}")
|
||||
|
||||
def load_user(username):
|
||||
for line in linegen(users):
|
||||
split=line.split(',')
|
||||
if split[0]==username: return models.User(*split)
|
||||
def save_user(user):
|
||||
gen=linegen(users)
|
||||
for line in gen:
|
||||
split=line.split(',')
|
||||
if split[0]==user.username: update(users,{gen.send(True):user.csv})
|
||||
def delete_user(user): pass
|
||||
|
||||
def load_tokens(user): pass
|
||||
def save_token(token): pass
|
||||
def delete_token(token): pass
|
||||
|
||||
def load_invites(user): pass
|
||||
def save_invite(token): pass
|
||||
def delete_invite(token): pass
|
||||
|
||||
def linegen(path):
|
||||
i=0
|
||||
file=open(path)
|
||||
line=file.readline()
|
||||
while line:
|
||||
i+=1
|
||||
response=yield line
|
||||
if response is not None: yield i
|
||||
line=file.readline()
|
||||
file.close()
|
||||
|
||||
def update(path,lines):
|
||||
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)
|
||||
line=file.readline()
|
||||
file.close()
|
||||
write.close()
|
Reference in New Issue
Block a user