yuuki/libs/data.py

160 lines
4.6 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,
"Ext_Admin":[]
}
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
}
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 = {
"JoinGroup":"<li>%s: %s(%s) -> Inviter: %s</li>"
}
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
def getData(self, Type):
return self.Data[Type]
2019-08-25 02:04:49 +08:00
def getLimit(self, Type):
LimitInfo = self.getData("LimitInfo")
if len(LimitInfo) != 2:
LimitInfo = self.LimitType
if Type == "Kick":
2019-08-25 11:24:26 +08:00
Limit = {}
for Mode in LimitInfo["KickLimit"]:
Limit[Mode] = int(LimitInfo["KickLimit"][Mode])
2019-08-25 02:04:49 +08:00
elif Type == "Cancel":
2019-08-25 11:24:26 +08:00
Limit = {}
for Mode in LimitInfo["CancelLimit"]:
Limit[Mode] = int(LimitInfo["CancelLimit"][Mode])
else:
Limit = None
return Limit
2019-08-25 02:04:49 +08:00
2019-08-24 17:22:23 +08:00
def getGroup(self, GroupID):
Groups = self.getData("Group")
if len(Groups) > 0:
2019-08-24 23:42:58 +08:00
GroupIDs = [Group for Group in Groups]
2019-08-24 23:31:59 +08:00
if GroupID not in GroupIDs:
2019-08-24 23:42:58 +08:00
Groups[GroupID] = self.GroupType
2019-08-24 23:35:49 +08:00
else:
2019-08-24 23:42:58 +08:00
Groups[GroupID] = self.GroupType
2019-08-24 23:37:14 +08:00
return Groups[GroupID]
2019-08-24 17:22:23 +08:00
def getSEGroup(self, GroupID):
2019-08-25 02:33:12 +08:00
SEMode = self.getGroup(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_