Add model class
This commit is contained in:
parent
85d23c8c95
commit
819a92bedd
1
model.json
Normal file
1
model.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"bar": "foo", "foo": 3, "test": 4}
|
64
model.py
64
model.py
@ -0,0 +1,64 @@
|
|||||||
|
import random
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class Model:
|
||||||
|
def __init__(self, filename = "model.json"):
|
||||||
|
self.filename = filename
|
||||||
|
self.sessions = None
|
||||||
|
if os.path.isfile(filename):
|
||||||
|
with open(filename) as f:
|
||||||
|
try:
|
||||||
|
self.sessions = json.load(f)
|
||||||
|
except:
|
||||||
|
self.sessions = {}
|
||||||
|
else:
|
||||||
|
self.sessions = {}
|
||||||
|
|
||||||
|
def handle_post(self, data):
|
||||||
|
pass
|
||||||
|
# handle post request data
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exception_type, exception_value, traceback):
|
||||||
|
if not (exception_type or exception_value or traceback):
|
||||||
|
with open(self.filename, "w") as f:
|
||||||
|
json.dump(self.sessions, f)
|
||||||
|
if not os.path.isdir("backups"):
|
||||||
|
try:
|
||||||
|
os.mkdir("backups")
|
||||||
|
except FileExistsError:
|
||||||
|
print("backups is a file, no directory. Please delete yourself")
|
||||||
|
datestring = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d-%H%M%S")
|
||||||
|
with open(f"backups/{datestring}_{self.filename}", "w") as f:
|
||||||
|
json.dump(self.sessions, f)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def create_session(self) -> str:
|
||||||
|
sessionname = base64.b32encode(bytearray(random.randint(0, 0xFF) for _ in range(10)))[:16].decode().lower()
|
||||||
|
self.sessions[sessionname] = {}
|
||||||
|
return sessionname
|
||||||
|
|
||||||
|
def subscribe(self, clientid, socket):
|
||||||
|
pass
|
||||||
|
# todo subscribe socket, match with id
|
||||||
|
|
||||||
|
|
||||||
|
def unsubscribe(self, socket):
|
||||||
|
pass
|
||||||
|
# remove socket from all clients
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
new session : ()
|
||||||
|
add player : uuid
|
||||||
|
"""
|
Loading…
Reference in New Issue
Block a user