From baf9bc19fb5cb2c6482830858b646a31ba65633e Mon Sep 17 00:00:00 2001 From: SuperSonic Date: Fri, 28 Feb 2020 18:13:08 +0800 Subject: [PATCH] Update --- libs/data.py | 26 +++---- libs/data_mds.py | 175 ++++++++++++++++++++--------------------------- libs/yuuki.py | 3 +- 3 files changed, 88 insertions(+), 116 deletions(-) diff --git a/libs/data.py b/libs/data.py index e06e1bb..c0beaf8 100644 --- a/libs/data.py +++ b/libs/data.py @@ -14,7 +14,7 @@ import time from tornado.httpclient import HTTPClient, HTTPRequest from yuuki_core.ttypes import OpType -from .data_mds import listen as mds_listen +from .data_mds import PythonMDS from .thread_control import Yuuki_Multiprocess from .thread_control import Yuuki_Thread @@ -79,37 +79,37 @@ class Yuuki_Data: if not os.path.isdir(self.DataPath): os.mkdir(self.DataPath) - for Type in self.DataType: - name = self.DataPath + self.DataName.format(Type) + for data_type in self.DataType: + name = self.DataPath + self.DataName.format(data_type) + + test_result = 0 if not os.path.isfile(name): with open(name, "w") as f: f.write("") - Type = 0 else: with open(name, "r") as f: try: json.load(f) - Type = 0 except ValueError: - Type = 1 - assert Type == 0, "{}\nJson Test Error".format(name) + test_result = 1 + assert test_result == 0, "{}\nJson Test Error".format(name) - for Type in self.DataType: - name = self.DataPath + self.DataName.format(Type) with open(name, "r+") as f: text = f.read() if text != "": - self.Data[Type] = json.loads(text) + self.Data[data_type] = json.loads(text) else: - self.Data[Type] = self.DataType[Type] - f.write(json.dumps(self.Data[Type])) + self.Data[data_type] = self.DataType[data_type] + f.write(json.dumps(self.Data[data_type])) + return self._MDS_Initialize() def _MDS_Initialize(self): if self.threading: + mds = PythonMDS() self.mdsHost = "http://localhost:2019/" self.mdsCode = "{}.{}".format(random.random(), time.time()) - self.MdsThreadControl.add(mds_listen, (self.mdsCode,)) + self.MdsThreadControl.add(mds.listen, (self.mdsCode,)) # MDS Sync diff --git a/libs/data_mds.py b/libs/data_mds.py index 45459f5..6e2430d 100644 --- a/libs/data_mds.py +++ b/libs/data_mds.py @@ -10,103 +10,23 @@ # Initializing import json +from abc import ABC from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.web import Application, RequestHandler -switch_data = {} +# Works +_work = {} auth_code = 0 -# Functions -def update(data): - global switch_data - # noinspection PyBroadException - try: - if type(data["path"]) is list: - over = query({"path": data["path"]}) - over.get("data").update(data["data"]) - return {"status": 200} - return {"status": 400} - except: - return {"status": 500} - - -def delete(data): - global switch_data - # noinspection PyBroadException - try: - if type(data["path"]) is list: - over = query({"path": data["path"]}) - over.get("data").pop(data["data"]) - return {"status": 200} - return {"status": 400} - except: - return {"status": 500} - - -def query(data): - global switch_data - query_data = data["path"] - # noinspection PyBroadException - try: - if type(switch_data) is dict and type(query_data) is list: - result = switch_data - query_len = len(query_data) - 1 - for count, key in enumerate(query_data): - if key in result: - if count < query_len: - if type(result.get(key)) is not dict: - result = 1 # "unknown_type" + type(source_data.get(key)) - break - result = result.get(key) - else: - result = 2 # "unknown_key" - break - - return {"status": 200, "data": result} - return {"status": 400} - except: - return {"status": 500} - - -def sync(data): - global switch_data - # noinspection PyBroadException - try: - switch_data = data["path"] - return {"status": 200} - except: - return {"status": 500} - - -def yuukiLimitDecrease(data): - global switch_data - # noinspection PyBroadException - try: - switch_data["LimitInfo"][data["path"]][data["userId"]] -= 1 - return {"status": 200} - except: - return {"status": 500} - - -# Works -_work = { - "UPT": update, - "DEL": delete, - "GET": query, - "SYC": sync, - "YLD": yuukiLimitDecrease -} - - -class IndexHandler(RequestHandler): +class IndexHandler(RequestHandler, ABC): def get(self): self.write(''' Python MDS Server
To switch data in multiprocessing.
- (c)2019 Star Inc. + (c)2020 Star Inc. ''') def post(self): @@ -128,23 +48,76 @@ class IndexHandler(RequestHandler): self.write(json.dumps(result)) -# Main -app = Application([ - ('/', IndexHandler) -]) -server = HTTPServer(app) -async_lock = IOLoop.current() +class PythonMDS: + switch_data = {} + # Main + app = Application([ + ('/', IndexHandler) + ]) + server = HTTPServer(app) + async_lock = IOLoop.current() -def listen(code): - global auth_code - auth_code = code - server.listen(2019) - async_lock.start() + def __init__(self): + _work["UPT"] = self._update + _work["DEL"] = self._delete + _work["GET"] = self._query + _work["SYC"] = self._sync + _work["YLD"] = self._yuuki_limit_decrease + _work["EXT"] = self.mds_exit + def _query(self, data): + query_data = data["path"] + if type(self.switch_data) is dict and type(query_data) is list: + result = self.switch_data + query_len = len(query_data) - 1 + for count, key in enumerate(query_data): + if key in result: + if count < query_len: + if type(result.get(key)) is not dict: + result = 1 # "unknown_type" + type(source_data.get(key)) + break + result = result.get(key) + else: + result = 2 # "unknown_key" + break -def mds_exit(): - server.stop() - yield server.close_all_connections() - async_lock.stop() - async_lock.close() + return {"status": 200, "data": result} + return {"status": 400} + + def _update(self, data): + if type(data["path"]) is list: + over = self._query({"path": data["path"]}) + over.get("data").update(data["data"]) + return {"status": 200} + return {"status": 400} + + def _delete(self, data): + if type(data["path"]) is list: + over = self._query({"path": data["path"]}) + over.get("data").pop(data["data"]) + return {"status": 200} + return {"status": 400} + + def _sync(self, data): + self.switch_data = data["path"] + return {"status": 200} + + def _yuuki_limit_decrease(self, data): + self.switch_data["LimitInfo"][data["path"]][data["userId"]] -= 1 + return {"status": 200} + + def listen(self, code): + global auth_code + auth_code = code + self.server.listen(2019) + self.async_lock.start() + + def mds_exit(self, data): + if data: + pass + self.server.stop() + yield {"status": 200} + self.server.close_all_connections() + self.async_lock.stop() + self.async_lock.close() diff --git a/libs/yuuki.py b/libs/yuuki.py index e0c7458..c71f937 100644 --- a/libs/yuuki.py +++ b/libs/yuuki.py @@ -16,7 +16,6 @@ from yuuki_core.TalkService import * from .connection import Yuuki_Connect from .data import Yuuki_Data -from .data_mds import mds_exit from .events import Yuuki_Command, Yuuki_JoinGroup, Yuuki_Security from .i18n import Yuuki_LangSetting from .poll import Yuuki_Poll @@ -130,7 +129,7 @@ class Yuuki: while self.data.getData(["Global", "Power"]): self.data.updateData(["Global", "Power"], False) if self.Threading: - mds_exit() + # mds_exit() if self.YuukiConfigs.get("WebAdmin"): self.webAdmin.stop() if restart: