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}); break else: update(users,{-1: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() if -1 in lines: write.write(lines[-1]) file.close() write.close()