def fromfile(self, path=None, setpath=True): if path is None: path = self.path if setpath: self.path = path configjsonstr = read_string_from_file(path, "{}") return self.fromjsonstr(configjsonstr)
def hello(): print("request root", request.full_path) if request.full_path == "/?": binid = read_string_from_file("binid.txt", "local") rurl = "/?id=" + binid print("redirecting root to",rurl) return redirect(rurl) return render_template("index.html", randurl = randurl)
def getbinid(): binid = read_string_from_file("binid.txt", "") if binid == "": return None return binid
def wrapped_f(*args): jsonobj = args[0] rjsonobj = {} connected_sids.enqueue(request.sid) prettylog([ "EV <{}> SID <{}>".format(self.ev,request.sid), "ARGS {}".format(args).ljust(160)[:160], "CONNS {}".format(connected_sids.items) ]) if "kind" in jsonobj: try: kind = jsonobj["kind"] if "owner" in jsonobj: rjsonobj["owner"] = jsonobj["owner"] if kind == "cmd": key = jsonobj["key"] commandjsonstr = json.dumps({"command": jsonobj["data"], "key": key}) rjsonobj["status"] = geturl(SIMPLE_SERVER_URL + "/" + quote(commandjsonstr)) rjsonobj["key"] = key elif kind == "storebinid": binid = jsonobj["data"] write_string_to_file("binid.txt", binid) elif kind == "storeconfig": write_string_to_file("localconfig.json", jsonobj["data"]) rjsonobj["kind"] = "configstored" try: print("setting config on firebase") db.child("lichguibotconfig").set(jsonobj["data"]) print("setting config on firebase done") rjsonobj["status"] = "config stored locally and remotely" except: print("setting config on firebase failed") rjsonobj["status"] = "config stored only locally" elif kind == "storedb": try: path = jsonobj["path"] dataobj = jsonobj["dataobj"] rjsonobj["status"] = storedb(path, dataobj) rjsonobj["path"] = path except: traceback.print_exc(file=sys.stderr) rjsonobj["status"] = "! store db failed at {}".format(path) elif kind == "retrievedb": try: path = jsonobj["path"] rjsonobj["dataobj"] , rjsonobj["status"] = retrievedb(path) rjsonobj["path"] = path except: traceback.print_exc(file=sys.stderr) rjsonobj["dataobj"] = None rjsonobj["status"] = "! retrieve db failed at {}".format(path) elif kind == "parsepgn": rjsonobj["historyobj"] = None try: data = jsonobj["data"] rjsonobj["historyobj"] , rjsonobj["status"] = createhistory(data) except: traceback.print_exc(file=sys.stderr) rjsonobj["status"] = "! parse pgn failed" elif kind == "getlocalconfig": rjsonobj["kind"] = "setlocalconfig" try: print("getting config from firebase") rjsonobj["data"] = db.child("lichguibotconfig").get().val() write_string_to_file("localconfig.json", rjsonobj["data"]) print("getting config from firebase done, size", len(rjsonobj["data"])) except: print("getting config from firebase failed, falling back to local config") rjsonobj["data"] = read_string_from_file("localconfig.json", "{}") elif kind == "mainboardmove": try: variantkey = jsonobj["variantkey"] fen = jsonobj["fen"] moveuci = jsonobj["moveuci"] move = chess.Move.from_uci(moveuci) board = get_variant_board(variantkey) board.set_fen(fen) if board.is_legal(move): genboard = board.copy() board.push(move) rjsonobj["kind"] = "setmainboardfen" rjsonobj["fen"] = board.fen() rjsonobj["status"] = "making main board move ok" addpositioninfo(board, rjsonobj, move, genboard) else: rjsonobj["kind"] = "setmainboardfen" rjsonobj["fen"] = fen rjsonobj["status"] = "! making main board move failed, illegal move" addpositioninfo(board, rjsonobj) except: rjsonobj["status"] = "! making main board move failed, fatal" traceback.print_exc(file=sys.stderr) elif kind == "mainboardsetvariant": try: variantkey = jsonobj["variantkey"] board = get_variant_board(variantkey) if variantkey == "chess960": board.set_chess960_pos(random.randint(0, 959)) rjsonobj["kind"] = "setmainboardfen" rjsonobj["fen"] = board.fen() rjsonobj["status"] = "main board variant selected ok" addpositioninfo(board, rjsonobj, "reset") except: rjsonobj["status"] = "! main board variant selection failed" traceback.print_exc(file=sys.stderr) except: rjsonobj["status"] = "! command error" emit('siores', {"request": jsonobj, "response": rjsonobj}) f(*args)
import sys from serverutils.utils import read_string_from_file from serverutils.utils import write_string_to_file acc = sys.argv[1] if acc == "-r": print("restoring") write_string_to_file("config.py", read_string_from_file("config.old.py", "")) write_string_to_file("server.py", read_string_from_file("server.old.py", "")) else: print("doctoring source files in [{}]".format(acc)) config = read_string_from_file("config.py", "") write_string_to_file("config.old.py", config) config = config.replace("lichguibot", acc) write_string_to_file("config.py", config) server = read_string_from_file("server.py", "") write_string_to_file("server.old.py", server) server = server.replace("lichguibotconfig", acc + "config") write_string_to_file("server.py", server)
import sys import json from serverutils.utils import read_string_from_file import pyrebase acc = sys.argv[1] print("set remote", acc) fbcreds = json.loads(open("firebase/fbcreds.json").read()) firebase = pyrebase.initialize_app(fbcreds) db = firebase.database() localconfig = read_string_from_file("localconfig.json","{}") print("setting remote from localconfig {} characters".format(len(localconfig))) db.child(acc).set(localconfig) print("done")