From 092545e6483fd097c775a1422c9bae2b1a3ee521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Tue, 7 Feb 2017 17:00:35 -0500 Subject: [PATCH 1/8] Remove unused function with obscure print statement --- nengo_gui/components/sim_control.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/nengo_gui/components/sim_control.py b/nengo_gui/components/sim_control.py index b05a80c1..d9fdcc36 100644 --- a/nengo_gui/components/sim_control.py +++ b/nengo_gui/components/sim_control.py @@ -138,10 +138,6 @@ def smart_sleep(self, delay_time): else: self.smart_sleep_offset += delay_time - def config_settings(self, data): - for i in data: - print(i) - def update_client(self, client): now = time.time() # send off a ping now and then so we'll notice when connection closes From 953d1ceec7cfaa9d1ac5268c85e0c0477e666910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Tue, 7 Feb 2017 17:29:15 -0500 Subject: [PATCH 2/8] Replace calls to "print" with corresponding logging calls --- nengo_gui/components/ace_editor.py | 8 ++++++-- nengo_gui/components/component.py | 6 +++++- nengo_gui/components/netgraph.py | 15 +++++++++------ nengo_gui/gui.py | 19 ++++++++++++------- nengo_gui/guibackend.py | 4 ++-- nengo_gui/ipython.py | 6 +++++- nengo_gui/layout.py | 9 +++++++-- nengo_gui/main.py | 7 +++++-- nengo_gui/page.py | 11 +++++++---- nengo_gui/password.py | 6 +++++- 10 files changed, 63 insertions(+), 28 deletions(-) diff --git a/nengo_gui/components/ace_editor.py b/nengo_gui/components/ace_editor.py index 53616d03..9ca0ed5f 100644 --- a/nengo_gui/components/ace_editor.py +++ b/nengo_gui/components/ace_editor.py @@ -1,10 +1,14 @@ import json +import logging import os from nengo_gui.components.editor import Editor import nengo_gui.exec_env +logger = logging.getLogger(__name__) + + class AceEditor(Editor): config_defaults = {} @@ -87,8 +91,8 @@ def message(self, msg): with open(self.page.filename, 'w') as f: f.write(self.current_code) except IOError: - print("Could not save %s; permission denied" % - self.page.filename) + logger.exception("Could not save %s; permission denied", + self.page.filename) self.page.net_graph.update_code(self.current_code) else: self.page.net_graph.update_code(self.current_code) diff --git a/nengo_gui/components/component.py b/nengo_gui/components/component.py index fb1d6ff3..fc6635d5 100644 --- a/nengo_gui/components/component.py +++ b/nengo_gui/components/component.py @@ -1,4 +1,8 @@ import json +import logging + + +logger = logging.getLogger(__name__) class Component(object): @@ -60,7 +64,7 @@ def message(self, msg): Any data sent by the client ove the WebSocket will be passed into this method. """ - print('unhandled message', msg) + logger.warning('unhandled message %s', msg) def finish(self): """Close this Component""" diff --git a/nengo_gui/components/netgraph.py b/nengo_gui/components/netgraph.py index 1985c2fa..f9f46eb2 100644 --- a/nengo_gui/components/netgraph.py +++ b/nengo_gui/components/netgraph.py @@ -2,6 +2,7 @@ import os import traceback import collections +import logging import threading import nengo @@ -15,6 +16,10 @@ import nengo_gui.user_action import nengo_gui.layout + +logger = logging.getLogger(__name__) + + class NetGraph(Component): """Handles computations and communications for NetGraph on the JS side. @@ -281,8 +286,7 @@ def _reload(self, code=None): old_component.replace_with = obj obj.original_id = old_component.original_id except: - traceback.print_exc() - print('failed to recreate plot for %s' % obj) + logger.exception('failed to recreate plot for %s', obj) components.append(obj) components.sort(key=lambda x: x.component_order) @@ -398,7 +402,7 @@ def message(self, msg): try: info = json.loads(msg) except ValueError: - print('invalid message', repr(msg)) + logger.warning('invalid message %s', repr(msg)) return action = info.get('act', None) undo = info.get('undo', None) @@ -416,15 +420,14 @@ def message(self, msg): self.page.undo_stack.append([act]) del self.page.redo_stack[:] except: - print('error processing message', repr(msg)) - traceback.print_exc() + logger.exception('error processing message %s', repr(msg)) elif undo is not None: if undo == '1': self.undo() else: self.redo() else: - print('received message', msg) + logger.debug('received message %s', msg) def undo(self): if self.page.undo_stack: diff --git a/nengo_gui/gui.py b/nengo_gui/gui.py index 5245768f..9ad814c4 100644 --- a/nengo_gui/gui.py +++ b/nengo_gui/gui.py @@ -1,6 +1,7 @@ """Classes to instantiate and manage the life cycle of the nengo_gui backend.""" +import logging from __future__ import print_function import select @@ -13,6 +14,9 @@ from nengo_gui.guibackend import GuiServer +logger = logging.getLogger(__name__) + + class ServerShutdown(Exception): """Causes the server to shutdown when raised.""" pass @@ -70,25 +74,26 @@ class InteractiveGUI(BaseGUI): def start(self): protocol = 'https:' if self.server.settings.use_ssl else 'http:' - print("Starting nengo server at %s//%s:%d" % - (protocol, 'localhost', self.server.server_port)) + logger.info("Starting nengo server at %s//%s:%d", + protocol, 'localhost', self.server.server_port) if not sys.platform.startswith('win'): signal.signal(signal.SIGINT, self._confirm_shutdown) try: self.server.serve_forever(poll_interval=0.02) - print("No connections remaining to the nengo_gui server.") + logger.info("No connections remaining to the nengo_gui server.") except ServerShutdown: self.server.shutdown() finally: - print("Shutting down server...") + logger.info("Shutting down server...") self.server.wait_for_shutdown(0.05) n_zombie = sum(thread.is_alive() for thread, _ in self.server.requests) if n_zombie > 0: - print("%d zombie threads will close abruptly" % n_zombie) + logger.warning("%d zombie threads will close abruptly", + n_zombie) def _confirm_shutdown(self, signum, frame): signal.signal(signal.SIGINT, self._immediate_shutdown) @@ -100,9 +105,9 @@ def _confirm_shutdown(self, signum, frame): if line[0].lower() == 'y': raise ServerShutdown() else: - print("Resuming...") + logger.info("Resuming...") else: - print("No confirmation received. Resuming...") + logger.info("No confirmation received. Resuming...") signal.signal(signal.SIGINT, self._confirm_shutdown) def _immediate_shutdown(self, signum, frame): diff --git a/nengo_gui/guibackend.py b/nengo_gui/guibackend.py index 707afb71..12f80c82 100644 --- a/nengo_gui/guibackend.py +++ b/nengo_gui/guibackend.py @@ -252,7 +252,7 @@ def _handle_ws_msg(self, component, msg): component.message(msg.data) return True except: - logging.exception('Error processing: %s', repr(msg.data)) + logger.exception('Error processing: %s', repr(msg.data)) def _handle_config_msg(self, component, msg): cfg = json.loads(msg.data[7:]) @@ -399,6 +399,6 @@ def remove_page(self, page): time.sleep(self.settings.auto_shutdown) earliest_shutdown = self._last_access + self.settings.auto_shutdown if earliest_shutdown < time.time() and len(self.pages) <= 0: - logging.info( + logger.info( "No connections remaining to the nengo_gui server.") self.shutdown() diff --git a/nengo_gui/ipython.py b/nengo_gui/ipython.py index e894b8d1..ca334133 100644 --- a/nengo_gui/ipython.py +++ b/nengo_gui/ipython.py @@ -1,6 +1,7 @@ from __future__ import print_function import atexit +import logging import socket import threading import time @@ -18,6 +19,9 @@ import nengo_gui +logger = logging.getLogger(__name__) + + class ConfigReuseWarning(UserWarning): pass @@ -127,7 +131,7 @@ def _ipython_display_(self): '''.format(url=self.url, id=uuid.uuid4(), height=self.height))) else: - print("Server is not alive.") + logger.error("Server is not alive.") atexit.register(IPythonViz.shutdown_all, timeout=5) diff --git a/nengo_gui/layout.py b/nengo_gui/layout.py index f59a8e05..727dbb16 100644 --- a/nengo_gui/layout.py +++ b/nengo_gui/layout.py @@ -1,3 +1,5 @@ +import logging + from collections import OrderedDict import nengo @@ -5,6 +7,9 @@ from nengo_gui.grandalf.layouts import VertexViewer, SugiyamaLayout +logger = logging.getLogger(__name__) + + class Layout(object): """Generates layouts for nengo Networks""" def __init__(self, model): @@ -35,7 +40,7 @@ def find_parent(self, obj): if len(self.unexamined_networks) == 0: # there are no networks left we haven't looked into # this should not happen in a valid nengo.Network - print("could not find parent of", obj) + logger.error("could not find parent of %s", obj) return None # grab the next network we haven't looked into net = self.unexamined_networks.pop(0) @@ -120,7 +125,7 @@ def make_layout(self, network): if pre is None or post is None: # the connection does not go to a child of this network, # so ignore it. - print('error processing', c) + logger.error('error processing %s', c) else: edges[c] = Edge(vertices[pre], vertices[post], data=c) diff --git a/nengo_gui/main.py b/nengo_gui/main.py index 36c7ccf8..180871e8 100644 --- a/nengo_gui/main.py +++ b/nengo_gui/main.py @@ -10,9 +10,12 @@ from nengo_gui.password import gensalt, hashpw, prompt_pw +logger = logging.getLogger(__name__) + + def old_main(): - print("'nengo_gui' has been renamed to 'nengo'.") - print("Please run 'nengo' in the future to avoid this message!\n") + logger.info("'nengo_gui' has been renamed to 'nengo'.") + logger.info("Please run 'nengo' in the future to avoid this message!\n") main() diff --git a/nengo_gui/page.py b/nengo_gui/page.py index 46372269..9c1e2226 100644 --- a/nengo_gui/page.py +++ b/nengo_gui/page.py @@ -14,6 +14,9 @@ import nengo_gui.config +logger = logging.getLogger(__name__) + + class PageSettings(object): __slots__ = ['backend', 'editor_class', 'filename_cfg'] @@ -259,7 +262,7 @@ def load_config(self): except Exception: # FIXME #if self.gui.interactive: - logging.debug('error parsing config: %s', line) + logger.debug('error parsing config: %s', line) # make sure the required Components exist if '_viz_sim_control' not in self.locals: @@ -313,8 +316,8 @@ def save_config(self, lazy=False, force=False): with open(self.filename_cfg, 'w') as f: f.write(self.config.dumps(uids=self.default_labels)) except IOError: - print("Could not save %s; permission denied" % - self.filename_cfg) + logger.exception("Could not save %s; permission denied" % + self.filename_cfg) def modified_config(self): """Set a flag that the config file should be saved.""" @@ -407,7 +410,7 @@ def remove_uid(self, uid): del self.locals[uid] del self.default_labels[obj] else: - print('WARNING: remove_uid called on unknown uid: %s' % uid) + logger.warning('remove_uid called on unknown uid: %s', uid) def remove_component(self, component): """Remove a component from the layout.""" diff --git a/nengo_gui/password.py b/nengo_gui/password.py index 24f5f821..ec6a7d5f 100644 --- a/nengo_gui/password.py +++ b/nengo_gui/password.py @@ -5,9 +5,13 @@ import binascii from getpass import getpass import hashlib +import logging import os +logger = logging.getLogger(__name__) + + def gensalt(size=16): return binascii.hexlify(os.urandom(size)) @@ -27,4 +31,4 @@ def prompt_pw(): p1 = getpass("Enter password: ") if p0 == p1: return p0 - print("Passwords do not match. Please try again.") + logger.error("Passwords do not match. Please try again.") From 138da742c170f0f09db1308d50f34e48df0168d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Tue, 7 Feb 2017 17:31:37 -0500 Subject: [PATCH 3/8] Set default log level to info, change the logging format string Reason for the change of the format string is not to change the output on the command line. A nice formater may be desirable in the long run. --- nengo_gui/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nengo_gui/main.py b/nengo_gui/main.py index 180871e8..0d83f0f8 100644 --- a/nengo_gui/main.py +++ b/nengo_gui/main.py @@ -49,9 +49,9 @@ def main(): args = parser.parse_args() if args.debug: - logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(format='%(message)s', level=logging.DEBUG) else: - logging.basicConfig() + logging.basicConfig(format='%(message)s', level=logging.INFO) if args.password: if args.password is True: From 04fbdbb1682e49732793bc81ef3eeb8f03fcf57b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Tue, 7 Feb 2017 17:33:44 -0500 Subject: [PATCH 4/8] Change server request log level to debug These log messages are not that important to the average user. Since the default log level has been set to "info", the log level of these messages has to be adapted in order to hide them from the user. --- nengo_gui/guibackend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nengo_gui/guibackend.py b/nengo_gui/guibackend.py index 12f80c82..e9110d0a 100644 --- a/nengo_gui/guibackend.py +++ b/nengo_gui/guibackend.py @@ -295,7 +295,7 @@ def get_session(self): return session def log_message(self, format, *args): - logger.info(format, *args) + logger.debug(format, *args) class ModelContext(object): From 34ce206364f998ecc533f222ab19852f42ea8d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Tue, 7 Feb 2017 17:33:54 -0500 Subject: [PATCH 5/8] Sort imports --- nengo_gui/components/netgraph.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nengo_gui/components/netgraph.py b/nengo_gui/components/netgraph.py index f9f46eb2..da5156ca 100644 --- a/nengo_gui/components/netgraph.py +++ b/nengo_gui/components/netgraph.py @@ -1,13 +1,13 @@ -import time -import os -import traceback import collections +import json import logging +import os import threading +import time +import traceback import nengo from nengo import spa -import json from nengo_gui.components.component import Component from nengo_gui.components.value import Value From c611197d2deb151c5893274a9dd06f8e7f6b0322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Tue, 7 Feb 2017 17:34:24 -0500 Subject: [PATCH 6/8] Remove no longer needed compatibility imports for print --- nengo_gui/conftest.py | 2 -- nengo_gui/gui.py | 2 -- nengo_gui/guibackend.py | 2 -- nengo_gui/ipython.py | 2 -- nengo_gui/password.py | 2 -- nengo_gui/tests/test_basic_functionality.py | 2 -- 6 files changed, 12 deletions(-) diff --git a/nengo_gui/conftest.py b/nengo_gui/conftest.py index 01b0ff45..6150fd09 100644 --- a/nengo_gui/conftest.py +++ b/nengo_gui/conftest.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import os.path import socket import threading diff --git a/nengo_gui/gui.py b/nengo_gui/gui.py index 9ad814c4..f0bd5fe3 100644 --- a/nengo_gui/gui.py +++ b/nengo_gui/gui.py @@ -2,8 +2,6 @@ backend.""" import logging -from __future__ import print_function - import select import signal import sys diff --git a/nengo_gui/guibackend.py b/nengo_gui/guibackend.py index e9110d0a..69188863 100644 --- a/nengo_gui/guibackend.py +++ b/nengo_gui/guibackend.py @@ -1,7 +1,5 @@ """Nengo GUI backend implementation.""" -from __future__ import print_function - import hashlib import json import logging diff --git a/nengo_gui/ipython.py b/nengo_gui/ipython.py index ca334133..50acfbec 100644 --- a/nengo_gui/ipython.py +++ b/nengo_gui/ipython.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import atexit import logging import socket diff --git a/nengo_gui/password.py b/nengo_gui/password.py index ec6a7d5f..cff2dd2c 100644 --- a/nengo_gui/password.py +++ b/nengo_gui/password.py @@ -1,7 +1,5 @@ """Password hashing functions replicating bcrypt API.""" -from __future__ import print_function - import binascii from getpass import getpass import hashlib diff --git a/nengo_gui/tests/test_basic_functionality.py b/nengo_gui/tests/test_basic_functionality.py index ac3cfe82..cf751f1d 100644 --- a/nengo_gui/tests/test_basic_functionality.py +++ b/nengo_gui/tests/test_basic_functionality.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import os import time From cea7db2b32e26ca90b77527b0845d51140710c3a Mon Sep 17 00:00:00 2001 From: Jan Gosmann Date: Wed, 8 Feb 2017 16:06:09 -0500 Subject: [PATCH 7/8] squash! Replace calls to "print" with corresponding logging calls Keep prints where they are in order. --- nengo_gui/gui.py | 7 +++---- nengo_gui/main.py | 6 ++++-- nengo_gui/password.py | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/nengo_gui/gui.py b/nengo_gui/gui.py index f0bd5fe3..198bf4f3 100644 --- a/nengo_gui/gui.py +++ b/nengo_gui/gui.py @@ -90,8 +90,7 @@ def start(self): n_zombie = sum(thread.is_alive() for thread, _ in self.server.requests) if n_zombie > 0: - logger.warning("%d zombie threads will close abruptly", - n_zombie) + logger.debug("%d zombie threads will close abruptly", n_zombie) def _confirm_shutdown(self, signum, frame): signal.signal(signal.SIGINT, self._immediate_shutdown) @@ -103,9 +102,9 @@ def _confirm_shutdown(self, signum, frame): if line[0].lower() == 'y': raise ServerShutdown() else: - logger.info("Resuming...") + sys.stdout.write("Resuming...\n") else: - logger.info("No confirmation received. Resuming...") + sys.stdout.write("No confirmation received. Resuming...\n") signal.signal(signal.SIGINT, self._confirm_shutdown) def _immediate_shutdown(self, signum, frame): diff --git a/nengo_gui/main.py b/nengo_gui/main.py index 0d83f0f8..300125a2 100644 --- a/nengo_gui/main.py +++ b/nengo_gui/main.py @@ -1,6 +1,7 @@ import argparse import logging import os.path +import sys import threading import webbrowser @@ -14,8 +15,9 @@ def old_main(): - logger.info("'nengo_gui' has been renamed to 'nengo'.") - logger.info("Please run 'nengo' in the future to avoid this message!\n") + print("'nengo_gui' has been renamed to 'nengo'.", file=sys.stderr) + print("Please run 'nengo' in the future to avoid this message!\n", + file=sys.stderr) main() diff --git a/nengo_gui/password.py b/nengo_gui/password.py index cff2dd2c..ecd6f763 100644 --- a/nengo_gui/password.py +++ b/nengo_gui/password.py @@ -29,4 +29,4 @@ def prompt_pw(): p1 = getpass("Enter password: ") if p0 == p1: return p0 - logger.error("Passwords do not match. Please try again.") + print("Passwords do not match. Please try again.") From 63e3d4eb7dd9211c4c53803b58092fe5f9675f21 Mon Sep 17 00:00:00 2001 From: Jan Gosmann Date: Wed, 8 Feb 2017 16:35:02 -0500 Subject: [PATCH 8/8] squash! Replace calls to "print" with corresponding logging calls It seems safe to me to replace this `warns` call with a logging call and it makes things consistent with the other logging call for error output. It seems to also give a nicer output in the notebook. --- nengo_gui/ipython.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/nengo_gui/ipython.py b/nengo_gui/ipython.py index 50acfbec..b0e5bc27 100644 --- a/nengo_gui/ipython.py +++ b/nengo_gui/ipython.py @@ -20,10 +20,6 @@ logger = logging.getLogger(__name__) -class ConfigReuseWarning(UserWarning): - pass - - class IPythonViz(object): servers = weakref.WeakValueDictionary() threads = weakref.WeakValueDictionary() @@ -58,9 +54,9 @@ def start_server(cls, cfg, model): server = cls.servers.get(cfg, None) existent = server_thread is not None and server is not None if existent and server_thread.is_alive(): - warnings.warn(ConfigReuseWarning( + logger.warning( "Reusing config. Only the most recent visualization will " - "update the config.")) + "update the config.") for page in server.pages: page.save_config(force=True) page.filename_cfg = get_ipython().mktempfile()