Skip to content

Commit

Permalink
Implement hooks with register functions/decorators
Browse files Browse the repository at this point in the history
Addresses #953.
  • Loading branch information
jgosmann committed Apr 30, 2018
1 parent 56405ee commit c688afa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
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
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)

0 comments on commit c688afa

Please sign in to comment.