Replace numerous functions which loaded and saved/loaded from/to JSON files with a single handler
This commit is contained in:
parent
97cc80f3b8
commit
12b6fe2acb
|
@ -13,7 +13,7 @@
|
|||
"disable": "disable <name>",
|
||||
"list": "list",
|
||||
"stats": "stats",
|
||||
"rehash": "rehash",
|
||||
"rekey": "rekey",
|
||||
"save": "save <config|keyconf|pool|help|wholist|all>",
|
||||
"load": "load <config|keyconf|pool|help|wholist|all>",
|
||||
"dist": "dist"
|
||||
}
|
||||
|
|
126
threshold
126
threshold
|
@ -22,7 +22,13 @@ IRCPool = {}
|
|||
|
||||
MonitorPool = []
|
||||
|
||||
wholist = {}
|
||||
filemap = {
|
||||
"config": ["config.json", "configuration"],
|
||||
"keyconf": ["keyword.json", "keyword lists"],
|
||||
"pool": ["pool.json", "pool"],
|
||||
"help": ["help.json", "command help"],
|
||||
"wholist": ["wholist.json", "WHO lists"],
|
||||
}
|
||||
|
||||
def log(data):
|
||||
print("[LOG]", data)
|
||||
|
@ -353,51 +359,14 @@ class Helper(object):
|
|||
result[i].append(wholist[i][x])
|
||||
return result
|
||||
|
||||
def getKeywordConfig(self):
|
||||
with open("keyword.json", "r") as f:
|
||||
return load(f)
|
||||
|
||||
def getWholist(self):
|
||||
with open("wholist.json", "r") as f:
|
||||
return load(f)
|
||||
|
||||
def getPool(self):
|
||||
with open("pool.json", "r") as f:
|
||||
data = f.read()
|
||||
if not data == "":
|
||||
pool = loads(data.strip())
|
||||
return pool
|
||||
else:
|
||||
return {}
|
||||
|
||||
def saveConfig(self):
|
||||
global config
|
||||
with open("config.json", "w") as f:
|
||||
dump(config, f, indent=4)
|
||||
def save(self, var):
|
||||
with open(filemap[var][0], "w") as f:
|
||||
dump(globals()[var], f, indent=4)
|
||||
return
|
||||
|
||||
def saveKeywordConfig(self):
|
||||
global keyconf
|
||||
with open("keyword.json", "w") as f:
|
||||
dump(keyconf, f, indent=4)
|
||||
return
|
||||
|
||||
def saveWholist(self):
|
||||
global wholist
|
||||
with open("wholist.json", "w") as f:
|
||||
dump(wholist, f, indent=4)
|
||||
return
|
||||
|
||||
def savePool(self):
|
||||
global pool
|
||||
with open("pool.json", "w") as f:
|
||||
dump(pool, f, indent=4)
|
||||
return
|
||||
|
||||
def getHelp(self):
|
||||
with open("help.json", "r") as f:
|
||||
help = load(f)
|
||||
return help
|
||||
def load(self, var):
|
||||
with open(filemap[var][0], "r") as f:
|
||||
globals()[var] = load(f)
|
||||
|
||||
def incorrectUsage(self, addr, mode):
|
||||
if mode == None:
|
||||
|
@ -507,14 +476,14 @@ class Helper(object):
|
|||
if i in keyword or keyword in i:
|
||||
return "ISIN"
|
||||
keyconf["Keywords"].append(keyword)
|
||||
helper.saveKeywordConfig()
|
||||
self.save("keyconf")()
|
||||
return True
|
||||
|
||||
def delKeyword(self, keyword):
|
||||
if not keyword in keyconf["Keywords"]:
|
||||
return "NOKEY"
|
||||
keyconf["Keywords"].remove(keyword)
|
||||
helper.saveKeywordConfig()
|
||||
self.save("keyconf")()
|
||||
return True
|
||||
|
||||
def parseCommand(self, addr, authed, data):
|
||||
|
@ -546,21 +515,30 @@ class Helper(object):
|
|||
info("\n".join(helpMap))
|
||||
return
|
||||
|
||||
elif cmd == "rehash":
|
||||
global config
|
||||
config = helper.getConfig()
|
||||
log("Configuration rehashed")
|
||||
success("Configuration rehashed successfully")
|
||||
elif cmd == "save":
|
||||
if length == 2:
|
||||
if spl[1] in filemap.keys():
|
||||
self.save(spl[1])
|
||||
success("Saved %s to %s" % (spl[1], filemap[spl[1]][0]))
|
||||
return
|
||||
else:
|
||||
incUsage("save")
|
||||
return
|
||||
else:
|
||||
incUsage("save")
|
||||
return
|
||||
|
||||
elif cmd == "rekey":
|
||||
global keyconf
|
||||
keyconf = helper.getKeywordConfig()
|
||||
log("Keyword configuration rehashed")
|
||||
success("Keyword configuration rehashed successfully")
|
||||
|
||||
elif cmd == "savewho":
|
||||
self.saveWholist()
|
||||
success("Saved WHO info to file")
|
||||
elif cmd == "load":
|
||||
if length == 2:
|
||||
if spl[1] in filemap.keys():
|
||||
self.load(spl[1])
|
||||
success("Loaded %s from %s" % (spl[1], filemap[spl[1]][0]))
|
||||
return
|
||||
else:
|
||||
incUsage("load")
|
||||
return
|
||||
else:
|
||||
incUsage("load")
|
||||
return
|
||||
|
||||
elif cmd == "dist":
|
||||
|
@ -640,7 +618,7 @@ class Helper(object):
|
|||
failure("Name does not exist: %s" % spl[1])
|
||||
return
|
||||
pool[spl[1]]["enabled"] = True
|
||||
helper.savePool()
|
||||
self.save("pool")
|
||||
if not spl[1] in IRCPool.keys():
|
||||
self.addBot(spl[1])
|
||||
else:
|
||||
|
@ -657,7 +635,7 @@ class Helper(object):
|
|||
failure("Name does not exist: %s" % spl[1])
|
||||
return
|
||||
pool[spl[1]]["enabled"] = False
|
||||
helper.savePool()
|
||||
self.save("pool")
|
||||
if spl[1] in IRCPool.keys():
|
||||
if IRCPool[spl[1]].connected == True:
|
||||
IRCPool[spl[1]].transport.loseConnection()
|
||||
|
@ -786,7 +764,7 @@ class Helper(object):
|
|||
keyconf["KeywordsExcept"][spl[2]] = []
|
||||
|
||||
keyconf["KeywordsExcept"][spl[2]].append(spl[3])
|
||||
helper.saveKeywordConfig()
|
||||
self.save("keyconf")()
|
||||
success("Successfully added exception %s for keyword %s" % (spl[3], spl[2]))
|
||||
return
|
||||
elif spl[1] == "master":
|
||||
|
@ -797,7 +775,7 @@ class Helper(object):
|
|||
if not spl[3] in IRCPool[spl[2]].channels:
|
||||
info("Bot not on channel: %s" % spl[3])
|
||||
config["Master"] = [spl[2], spl[3]]
|
||||
helper.saveConfig()
|
||||
self.save("config")
|
||||
success("Master set to %s on %s" % (spl[3], spl[2]))
|
||||
return
|
||||
elif spl[1] == "unexcept":
|
||||
|
@ -810,7 +788,7 @@ class Helper(object):
|
|||
keyconf["KeywordsExcept"][spl[2]].remove(spl[3])
|
||||
if keyconf["KeywordsExcept"][spl[2]] == []:
|
||||
del keyconf["KeywordsExcept"][spl[2]]
|
||||
helper.saveKeywordConfig()
|
||||
self.save("keyconf")()
|
||||
success("Successfully removed exception %s for keyword %s" % (spl[3], spl[2]))
|
||||
return
|
||||
else:
|
||||
|
@ -822,7 +800,7 @@ class Helper(object):
|
|||
failure("No such exception: %s" % spl[2])
|
||||
return
|
||||
del keyconf["KeywordsExcept"][spl[2]]
|
||||
helper.saveKeywordConfig()
|
||||
self.save("keyconf")()
|
||||
success("Successfully removed exception list of %s" % spl[2])
|
||||
return
|
||||
elif spl[1] == "monitor":
|
||||
|
@ -973,7 +951,7 @@ class Helper(object):
|
|||
if config["ConnectOnCreate"] == True:
|
||||
self.addBot(name)
|
||||
success("Successfully created bot")
|
||||
self.savePool()
|
||||
self.save("pool")
|
||||
return
|
||||
|
||||
elif cmd == "del":
|
||||
|
@ -988,7 +966,7 @@ class Helper(object):
|
|||
IRCPool[spl[1]].transport.loseConnection()
|
||||
del IRCPool[spl[1]]
|
||||
success("Successfully removed bot")
|
||||
self.savePool()
|
||||
self.save("pool")
|
||||
return
|
||||
else:
|
||||
incUsage("del")
|
||||
|
@ -1048,7 +1026,7 @@ class Helper(object):
|
|||
spl[2] = []
|
||||
|
||||
config["Default"][spl[1]] = spl[2]
|
||||
self.saveConfig()
|
||||
self.save("config")
|
||||
if toUnset:
|
||||
success("Successfully unset key %s" % spl[1])
|
||||
else:
|
||||
|
@ -1119,7 +1097,7 @@ class Helper(object):
|
|||
pool[spl[1]][spl[2]] = spl[3]
|
||||
if spl[1] in IRCPool.keys():
|
||||
IRCPool[spl[1]].refresh()
|
||||
self.savePool()
|
||||
self.save("pool")
|
||||
if toUnset:
|
||||
success("Successfully unset key %s on %s" % (spl[2], spl[1]))
|
||||
else:
|
||||
|
@ -1149,12 +1127,8 @@ class Helper(object):
|
|||
|
||||
if __name__ == "__main__":
|
||||
helper = Helper()
|
||||
config = helper.getConfig()
|
||||
keyconf = helper.getKeywordConfig()
|
||||
pool = helper.getPool()
|
||||
help = helper.getHelp()
|
||||
|
||||
wholist = helper.getWholist()
|
||||
for i in filemap.keys():
|
||||
helper.load(i)
|
||||
|
||||
for i in pool.keys():
|
||||
if pool[i]["enabled"] == True:
|
||||
|
|
Loading…
Reference in New Issue