Fix aesthetics
This commit is contained in:
parent
95c5edb76b
commit
579cdbe8b0
32
bot.py
32
bot.py
|
@ -1,29 +1,11 @@
|
||||||
import teambot, sys, traceback, plugin, os
|
import teambot, sys, traceback, plugin, os
|
||||||
import importlib, importlib.util
|
import impmod
|
||||||
|
|
||||||
BOTOP = "~minerobber@127.0.0.1"
|
BOTOP = "~minerobber@127.0.0.1"
|
||||||
PREFIX = "!"
|
PREFIX = "!"
|
||||||
PLUGIN_MODULES = dict()
|
PLUGIN_MODULES = dict()
|
||||||
PLUGIN_SPECS = dict()
|
PLUGIN_SPECS = dict()
|
||||||
|
|
||||||
# hacky re-implementation of reload, uses importlib internals and hardcoded stuffs
|
|
||||||
def reloadModule(modname):
|
|
||||||
if modname not in PLUGIN_MODULES:
|
|
||||||
return False
|
|
||||||
if modname not in sys.modules:
|
|
||||||
sys.modules[modname]=PLUGIN_MODULES[modname]
|
|
||||||
# Alright, this needs some explaining.
|
|
||||||
# When you do importlib.reload, it does some juju magic shist to find the spec and calls importlib._bootstrap._exec.
|
|
||||||
# When you dynamically import a module it won't do its magic correctly and it'll error.
|
|
||||||
# Luckily, we can skip all the juju magic since we can just store the spec.
|
|
||||||
try:
|
|
||||||
importlib._bootstrap._exec(PLUGIN_SPECS[modname],PLUGIN_MODULES[modname])
|
|
||||||
except:
|
|
||||||
print("On reloading `{}`:".format(modname))
|
|
||||||
traceback.print_exc()
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
class MinerbotPhoenix(teambot.Handler):
|
class MinerbotPhoenix(teambot.Handler):
|
||||||
def on_connection_established(self,*args):
|
def on_connection_established(self,*args):
|
||||||
self.load_modules()
|
self.load_modules()
|
||||||
|
@ -35,19 +17,11 @@ class MinerbotPhoenix(teambot.Handler):
|
||||||
def load_module(self,modname,path):
|
def load_module(self,modname,path):
|
||||||
if modname in PLUGIN_MODULES:
|
if modname in PLUGIN_MODULES:
|
||||||
print("{} already imported, reloading".format(modname))
|
print("{} already imported, reloading".format(modname))
|
||||||
if reloadModule(modname):
|
PLUGIN_MODULES[modname].reload()
|
||||||
print("{} reloaded".format(modname))
|
|
||||||
else:
|
|
||||||
print("{} reload errored, skipping".format(modname))
|
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
print("importing {}".format(modname))
|
print("importing {}".format(modname))
|
||||||
spec = importlib.util.spec_from_file_location(modname,path)
|
PLUGIN_MODULES[modname]=impmod.Module(modname,path)
|
||||||
PLUGIN_SPECS[modname]=spec
|
|
||||||
module = importlib.util.module_from_spec(spec)
|
|
||||||
spec.loader.exec_module(module)
|
|
||||||
PLUGIN_MODULES[modname]=module
|
|
||||||
sys.modules[modname]=module
|
|
||||||
except:
|
except:
|
||||||
print("Unable to load plugin {}".format(modname))
|
print("Unable to load plugin {}".format(modname))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
import importlib, importlib.util, sys
|
||||||
|
|
||||||
|
class Module:
|
||||||
|
"""A module. Stores module object, spec object, and handles reloading."""
|
||||||
|
def __init__(self,modname,path=None):
|
||||||
|
if path is None:
|
||||||
|
path = modname+".py"
|
||||||
|
self.modname, self.path = modname, path
|
||||||
|
self.spec = importlib.util.spec_from_file_location(modname,path)
|
||||||
|
self.module = importlib.util.module_from_spec(self.spec)
|
||||||
|
self.spec.loader.exec_module(self.module)
|
||||||
|
def reload(self):
|
||||||
|
if self.modname not in sys.modules:
|
||||||
|
sys.modules[self.modname]=self.module
|
||||||
|
# Alright, this needs some explaining.
|
||||||
|
# When you do importlib.reload, it does some juju magic shist to find the spec and calls importlib._bootstrap._exec.
|
||||||
|
# When you dynamically import a module it won't do its magic correctly and it'll error.
|
||||||
|
# Luckily, we can skip all the juju magic since we can just store the spec.
|
||||||
|
importlib._bootstrap._exec(self.spec,self.module)
|
Loading…
Reference in New Issue