Add !reloadscripts to modules/scripts.py
This commit is contained in:
parent
fe981f9911
commit
bcb67db3fd
2 changed files with 42 additions and 9 deletions
|
@ -1,28 +1,42 @@
|
|||
|
||||
import glob, os, subprocess
|
||||
import glob, json, os, subprocess
|
||||
from src import Utils
|
||||
|
||||
class Module(object):
|
||||
def __init__(self, bot, events, exports):
|
||||
self.events = events
|
||||
self._directory = os.path.join(bot.directory, "modules", "scripts")
|
||||
self.read()
|
||||
self._hooks = []
|
||||
self._load_scripts()
|
||||
|
||||
def read(self):
|
||||
def _load_scripts(self):
|
||||
for filename in glob.glob(os.path.join(self._directory, "*")):
|
||||
name = os.path.basename(filename)
|
||||
for hashflag, value in Utils.get_hashflags(filename):
|
||||
if hashflag == "name" and value:
|
||||
name = value
|
||||
elif hashflag == "hook" and value:
|
||||
self.events.on(value).hook(
|
||||
hook = self.events.on(value).hook(
|
||||
lambda x: self.call(x, filename, name))
|
||||
self._hooks.append([value, hook])
|
||||
|
||||
@Utils.hook("received.command.reloadscripts", permission="reloadscripts")
|
||||
def reload(self, event):
|
||||
for event_name, hook in self._hooks:
|
||||
self.events.on(event_name).unhook(hook)
|
||||
self._load_scripts()
|
||||
event["stdout"].write("Reloaded all scripts")
|
||||
|
||||
def call(self, event, filename, name):
|
||||
env = {}
|
||||
env["EVENT"] = event.name
|
||||
for key, value in event.kwargs.items():
|
||||
env[key.upper()] = str(value)
|
||||
if isinstance(value, (str,)):
|
||||
env[key.upper()] = value
|
||||
elif isinstance(value, (bool,)):
|
||||
env[key.upper()] = str(int(value))
|
||||
elif isinstance(value, (list, dict)):
|
||||
env[key.upper()] = json.dumps(value)
|
||||
|
||||
proc = subprocess.Popen([filename], stdout=subprocess.PIPE, env=env)
|
||||
try:
|
||||
|
|
|
@ -59,12 +59,16 @@ class EventHookContext(object):
|
|||
self.context = context
|
||||
def hook(self, function, priority=DEFAULT_PRIORITY, replay=False,
|
||||
**kwargs):
|
||||
self._parent._context_hook(self.context, function, priority, replay,
|
||||
kwargs)
|
||||
return self._parent._context_hook(self.context, function, priority,
|
||||
replay, kwargs)
|
||||
def unhook(self, callback):
|
||||
self._parent.unhook(callback)
|
||||
|
||||
def on(self, subevent, *extra_subevents,
|
||||
delimiter=DEFAULT_EVENT_DELIMITER):
|
||||
return self._parent._context_on(self.context, subevent,
|
||||
extra_subevents, delimiter)
|
||||
|
||||
def call_for_result(self, default=None, **kwargs):
|
||||
return self._parent.call_for_result(default, **kwargs)
|
||||
def assure_call(self, **kwargs):
|
||||
|
@ -73,6 +77,7 @@ class EventHookContext(object):
|
|||
return self._parent.call(**kwargs)
|
||||
def call_limited(self, maximum, **kwargs):
|
||||
return self._parent.call_limited(maximum, **kwargs)
|
||||
|
||||
def get_hooks(self):
|
||||
return self._parent.get_hooks()
|
||||
def get_children(self):
|
||||
|
@ -104,9 +109,9 @@ class EventHook(object):
|
|||
|
||||
def hook(self, function, priority=DEFAULT_PRIORITY, replay=False,
|
||||
**kwargs):
|
||||
self._hook(function, None, priority, replay, kwargs)
|
||||
return self._hook(function, None, priority, replay, kwargs)
|
||||
def _context_hook(self, context, function, priority, replay, kwargs):
|
||||
self._hook(function, context, priority, replay, kwargs)
|
||||
return self._hook(function, context, priority, replay, kwargs)
|
||||
def _hook(self, function, context, priority, replay, kwargs):
|
||||
callback = EventCallback(function, priority, kwargs)
|
||||
|
||||
|
@ -121,6 +126,20 @@ class EventHook(object):
|
|||
for kwargs in self._stored_events:
|
||||
self._call(kwargs)
|
||||
self._stored_events = None
|
||||
return callback
|
||||
|
||||
def unhook(self, callback):
|
||||
if callback in self._hooks:
|
||||
self._hooks.remove(callback)
|
||||
|
||||
empty = []
|
||||
for context, hooks in self._context_hooks.items():
|
||||
if callback in hooks:
|
||||
hooks.remove(callback)
|
||||
if not hooks:
|
||||
empty.append(context)
|
||||
for context in empty:
|
||||
del self._context_hooks[context]
|
||||
|
||||
def _make_multiple_hook(self, source, context, events):
|
||||
multiple_event_hook = MultipleEventHook()
|
||||
|
|
Loading…
Reference in a new issue