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