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

Added hooks registered per model #965

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions nengo_gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .version import version as __version__
from .namefinder import NameFinder
from .main import main, old_main
from . import hooks
8 changes: 8 additions & 0 deletions nengo_gui/components/sim_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from nengo_gui.components.component import Component
import nengo_gui.exec_env
from nengo_gui.server import WebSocketFrame
import nengo_gui.hooks


class SimControl(Component):
Expand Down Expand Up @@ -190,11 +191,18 @@ def javascript(self):
def message(self, msg):
if msg == 'pause':
self.paused = True
nengo_gui.hooks.on_pause.trigger(self.page.model,
self.page.sim)
elif msg == 'config':
self.send_config_options = True
elif msg == 'continue':
if self.page.sim is None:
self.page.rebuild = True
nengo_gui.hooks.on_start.trigger(self.page.model,
self.page.sim)
else:
nengo_gui.hooks.on_continue.trigger(self.page.model,
self.page.sim)
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
22 changes: 22 additions & 0 deletions nengo_gui/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import weakref

class Hook(object):
def __init__(self):
self.callbacks = weakref.WeakKeyDictionary()
def trigger(self, model, sim):
items = self.callbacks.get(model, [])
for callback in items:
callback(sim)
def __call__(self, model):
def register(fn, model=model, callbacks=self.callbacks):
if model not in callbacks:
callbacks[model] = [fn]
else:
callbacks[model].append(fn)
return register

on_step = Hook()
on_start = Hook()
on_pause = Hook()
on_continue = Hook()
on_close = Hook()
7 changes: 7 additions & 0 deletions nengo_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import nengo_gui
import nengo_gui.user_action
import nengo_gui.config
import nengo_gui.hooks
import nengo_gui.seed_generation


Expand Down Expand Up @@ -513,15 +514,21 @@ def runner(self):
self.sim.run_steps(self.sim.max_steps)
else:
self.sim.step()
nengo_gui.hooks.on_step.trigger(self.model, self.sim)
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:
nengo_gui.hooks.on_close.trigger(self.model, self.sim)
self.sims_to_close.pop().close()

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

def close(self):
if self.sim is not None:
nengo_gui.hooks.on_close.trigger(self.model, self.sim)