Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hooks with register functions/decorators #957

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nengo_gui/components/sim_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from nengo_gui.components.component import Component
import nengo_gui.exec_env
from nengo_gui import hooks
from nengo_gui.server import WebSocketFrame


Expand Down Expand Up @@ -190,11 +191,15 @@ def javascript(self):
def message(self, msg):
if msg == 'pause':
self.paused = True
hooks.on_pause.execute(self.page)
elif msg == 'config':
self.send_config_options = True
elif msg == 'continue':
if self.page.sim is None:
self.page.rebuild = True
hooks.on_start.execute(self.page)
else:
hooks.on_continue.execute(self.page)
self.paused = False
elif msg == 'reset':
self.paused = True
Expand Down
1 change: 1 addition & 0 deletions nengo_gui/guibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ def create_page(self, filename, reset_cfg=False):

def remove_page(self, page):
self._last_access = time.time()
page.close()
self.pages.remove(page)
if (not self._shutting_down and self.settings.auto_shutdown > 0 and
len(self.pages) <= 0):
Expand Down
40 changes: 40 additions & 0 deletions nengo_gui/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from collections import defaultdict


class Context(object):
context_stack = [None]

def __init__(self, page):
self.page = page

def __enter__(self):
self.context_stack.append(self.page)
for hook in (on_step, on_start, on_pause, on_continue, on_close):
# NOTE entering context clears hooks, this might be surprising and
# there might be a better solution to prevent accumulation of hooks
# from multiple code executions.
if self.page in hook.callbacks:
del hook.callbacks[self.page]
return self

def __exit__(self, exc_type, exc_value, tb):
assert self.context_stack.pop() is self.page


class Hook(object):
def __init__(self):
self.callbacks = defaultdict(list) # FIXME page references should be weak

def __call__(self, fn):
self.callbacks[Context.context_stack[-1]].append(fn)

def execute(self, page):
for cb in self.callbacks[page] + self.callbacks[None]:
cb(page.sim)


on_step = Hook()
on_start = Hook()
on_pause = Hook()
on_continue = Hook()
on_close = Hook()
9 changes: 8 additions & 1 deletion nengo_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import nengo

import nengo_gui
from nengo_gui import hooks
import nengo_gui.user_action
import nengo_gui.config
import nengo_gui.seed_generation
Expand Down Expand Up @@ -214,7 +215,7 @@ def execute(self, code):

exec_env = nengo_gui.exec_env.ExecutionEnvironment(self.filename)
try:
with exec_env:
with exec_env, hooks.Context(self):
compiled = compile(code, nengo_gui.exec_env.compiled_filename,
'exec')
exec(compiled, code_locals)
Expand Down Expand Up @@ -501,15 +502,21 @@ def runner(self):
self.sim.run_steps(self.sim.max_steps)
else:
self.sim.step()
hooks.on_step.execute(self)
except Exception as err:
if self.finished:
return
line = nengo_gui.exec_env.determine_line_number()
self.error = dict(trace=traceback.format_exc(), line=line)
self.sim = None
while self.sims_to_close:
hooks.on_close.execute(self)
self.sims_to_close.pop().close()

if self.rebuild:
self.build()
self.sim = None

def close(self):
if self.sim is not None:
hooks.on_close.execute(self)