yuuki/libs/data.py

180 lines
5.7 KiB
Python
Raw Normal View History

2019-08-22 18:33:10 +08:00
#!/usr/bin/python3
# coding=UTF-8
2019-08-24 17:22:23 +08:00
import os, time, json
from .core.ttypes import OpType
2019-08-24 14:43:16 +08:00
2019-08-22 18:33:10 +08:00
class Yuuki_Data:
def __init__(self):
2019-08-24 17:22:23 +08:00
# Data
self.Data = {}
self.DataType = {
"Global":{
2019-08-25 08:47:47 +08:00
"LastResetLimitTime":None,
2019-08-24 17:22:23 +08:00
},
"Group": {},
2019-08-25 01:46:03 +08:00
"LimitInfo":{},
2019-08-24 17:22:23 +08:00
"BlackList":[]
}
self.GroupType = {
"SEGroup":None,
2019-08-27 13:33:04 +08:00
"Ext_Admin":[],
"GroupTicket":{}
2019-08-24 17:22:23 +08:00
}
2019-08-25 02:04:49 +08:00
self.LimitType = {
"KickLimit":{},
"CancelLimit":{}
2019-08-25 01:46:03 +08:00
}
2019-08-24 17:22:23 +08:00
self.SEGrouptype = {
OpType.NOTIFIED_UPDATE_GROUP:False,
OpType.NOTIFIED_INVITE_INTO_GROUP:False,
OpType.NOTIFIED_ACCEPT_GROUP_INVITATION:False,
OpType.NOTIFIED_KICKOUT_FROM_GROUP:False
}
2019-08-27 14:42:05 +08:00
self.initType = {
"Group":self.GroupType,
"LimitInfo":self.LimitType,
"SEGroup":self.SEGrouptype
}
2019-08-24 17:22:23 +08:00
self.DataPath = "data/"
self.DataName = "{}.json"
if not os.path.isdir(self.DataPath):
os.mkdir(self.DataPath)
for Type in self.DataType:
name = self.DataPath + self.DataName.format(Type)
if not os.path.isfile(name):
with open(name, "w") as f:
f.write("")
2019-08-24 20:40:17 +08:00
Type = 0
2019-08-24 17:22:23 +08:00
else:
with open(name, "r") as f:
2019-08-24 20:31:58 +08:00
try:
json.loads(f.read())
Type = 0
except ValueError:
Type = 1
assert Type == 0, "{}\nJson Test Error".format(name)
2019-08-24 17:22:23 +08:00
# Data Initialize
for Type in self.DataType:
name = self.DataPath + self.DataName.format(Type)
with open(name, "r+") as f:
2019-08-24 20:40:17 +08:00
text = f.read()
if text != "":
self.Data[Type] = json.loads(text)
2019-08-24 17:22:23 +08:00
else:
self.Data[Type] = self.DataType[Type]
f.write(json.dumps(self.Data[Type]))
# Log
2019-08-24 14:43:16 +08:00
self.LogType = {
2019-08-25 13:00:39 +08:00
"JoinGroup":"<li>%s: %s(%s) -> Inviter: %s</li>",
2019-08-25 13:21:49 +08:00
"KickEvent":"<li>%s: %s(%s) -(%s)> Kicker: %s | Kicked: %s | Status: %s</li>",
"CancelEvent":"<li>%s: %s(%s) -(%s)> Inviter: %s | Canceled: %s</li>",
2019-08-25 13:00:39 +08:00
"BlackList":"<li>%s: %s(%s)</li>"
2019-08-24 14:43:16 +08:00
}
self.LogPath = "logs/"
self.LogName = "{}.html"
self.initHeader = "<title>{} - SYB</title>" \
"<meta charset='utf-8' />"
2019-08-24 15:05:00 +08:00
if not os.path.isdir(self.LogPath):
os.mkdir(self.LogPath)
2019-08-24 14:43:16 +08:00
for Type in self.LogType:
name = self.LogPath + self.LogName.format(Type)
2019-08-24 15:02:26 +08:00
if not os.path.isfile(name):
2019-08-24 14:43:16 +08:00
with open(name, "w") as f:
f.write(self.initHeader.format(Type))
2019-08-24 23:06:28 +08:00
def file(self, Type, Mode, Format):
if Format == "Data":
2019-08-24 23:50:09 +08:00
return open(self.DataPath + self.DataName.format(Type), Mode)
2019-08-24 23:06:28 +08:00
elif Format == "Log":
return open(self.LogPath + self.LogName.format(Type), Mode)
2019-08-24 14:43:16 +08:00
2019-08-24 23:06:28 +08:00
def syncData(self):
for Type in self.DataType:
with self.file(Type, "w", "Data") as f:
f.write(json.dumps(self.Data[Type]))
def updateData(self, Object, Input, Data):
if type(Object) == list:
2019-08-24 17:22:23 +08:00
if Input:
2019-08-24 23:06:28 +08:00
Object.append(Data)
2019-08-24 17:22:23 +08:00
else:
2019-08-24 23:06:28 +08:00
Object.remove(Data)
elif type(Object) == dict:
Object[Input] = Data
self.syncData()
2019-08-24 17:22:23 +08:00
2019-08-24 14:43:16 +08:00
def updateLog(self, Type, Data):
2019-08-24 23:06:28 +08:00
with self.file(Type, "a", "Log") as f:
2019-08-24 14:43:16 +08:00
f.write(self.LogType[Type] % Data)
def getTime(self, format="%b %d %Y %H:%M:%S %Z"):
Time = time.localtime(time.time())
return time.strftime(format, Time)
2019-08-24 17:22:23 +08:00
2019-08-27 15:22:35 +08:00
def getData(self, Type, Query=None):
if type(Query) == list:
if len(Query) == 1:
2019-08-27 14:42:05 +08:00
if Query not in self.Data[Type]:
2019-08-27 15:29:12 +08:00
if Type in self.initType:
self.Data[Type][Query] = self.initType[Type]
else:
self.Data[Type][Query] = {}
2019-08-27 15:22:35 +08:00
return self.Data[Type][Query]
elif len(Query) == 2:
if Query[0] not in self.Data[Type]:
2019-08-27 15:29:12 +08:00
if Type in self.initType:
self.Data[Type][Query[0]] = self.initType[Type]
else:
2019-08-27 15:38:09 +08:00
self.Data[Type][Query[0]] = {}
if Query[1] not in self.Data[Type][Query[0]]:
2019-08-27 15:29:12 +08:00
if Type in self.initType:
self.Data[Type][Query[0]][Query[1]] = self.initType[Query[0]]
else:
self.Data[Type][Query[0]][Query[1]] = {}
2019-08-27 15:22:35 +08:00
return self.Data[Type][Query[0]][Query[1]]
2019-08-27 14:42:05 +08:00
else:
assert "Error Query Level"
else:
return self.Data[Type]
2019-08-24 17:22:23 +08:00
2019-08-25 02:04:49 +08:00
def getLimit(self, Type):
if Type == "Kick":
2019-08-25 11:24:26 +08:00
Limit = {}
2019-08-27 15:22:35 +08:00
for userId in self.getData("LimitInfo", "KickLimit"):
Limit[userId] = int(self.getData("LimitInfo", ["KickLimit", userId], 3))
2019-08-25 02:04:49 +08:00
elif Type == "Cancel":
2019-08-25 11:24:26 +08:00
Limit = {}
2019-08-27 15:22:35 +08:00
for userId in self.getData("LimitInfo", "CancelLimit"):
Limit[userId] = int(self.getData("LimitInfo", ["CancelLimit", userId], 3))
2019-08-25 11:24:26 +08:00
else:
Limit = None
return Limit
2019-08-25 02:04:49 +08:00
2019-08-24 17:22:23 +08:00
def getSEGroup(self, GroupID):
2019-08-27 14:55:22 +08:00
SEMode = self.getData("Group", GroupID)["SEGroup"]
2019-08-25 02:44:17 +08:00
if SEMode == None:
return None
2019-08-25 02:38:38 +08:00
SEMode_ = {}
2019-08-25 02:33:12 +08:00
for Mode in SEMode:
2019-08-25 02:38:38 +08:00
SEMode_[int(Mode)] = SEMode[Mode]
2019-08-25 02:36:10 +08:00
return SEMode_