From 6a51158f4ce22c0319b2443dbde225d5beec71f5 2017-05-11 21:07:54 From: Joshua Storck Date: 2017-05-11 21:07:54 Subject: [PATCH] Adding a -q option to the logstart magic to suppress output of log state. Also added a command line option --- diff --git a/IPython/core/magics/logging.py b/IPython/core/magics/logging.py index 9683c3f..b6b8d8a 100644 --- a/IPython/core/magics/logging.py +++ b/IPython/core/magics/logging.py @@ -19,6 +19,7 @@ import sys # Our own packages from IPython.core.magic import Magics, magics_class, line_magic from warnings import warn +from traitlets import Bool #----------------------------------------------------------------------------- # Magic implementation classes @@ -28,11 +29,17 @@ from warnings import warn class LoggingMagics(Magics): """Magics related to all logging machinery.""" + quiet = Bool(False, help= + """ + Suppress output of log state when logging is enabled + """ + ).tag(config=True) + @line_magic def logstart(self, parameter_s=''): """Start logging anywhere in a session. - %logstart [-o|-r|-t] [log_name [log_mode]] + %logstart [-o|-r|-t|-q] [log_name [log_mode]] If no name is given, it defaults to a file named 'ipython_log.py' in your current directory, in 'rotate' mode (see below). @@ -82,12 +89,16 @@ class LoggingMagics(Magics): -t put timestamps before each input line logged (these are put in comments). + + -q + suppress output of logstate message when logging is invoked """ - opts,par = self.parse_options(parameter_s,'ort') + opts,par = self.parse_options(parameter_s,'ortq') log_output = 'o' in opts log_raw_input = 'r' in opts timestamp = 't' in opts + quiet = 'q' in opts logger = self.shell.logger @@ -145,9 +156,10 @@ class LoggingMagics(Magics): # re-enable timestamping logger.timestamp = True - print ('Activating auto-logging. ' - 'Current session state plus future input saved.') - logger.logstate() + if not (self.quiet or quiet): + print ('Activating auto-logging. ' + 'Current session state plus future input saved.') + logger.logstate() @line_magic def logstop(self, parameter_s=''): diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index dd26ba9..288a4b3 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -6,6 +6,7 @@ Needs to be run by nose (to make ipython session available). import io import os +import re import sys import warnings from unittest import TestCase @@ -20,7 +21,7 @@ from IPython.core.error import UsageError from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, register_line_magic, register_cell_magic) -from IPython.core.magics import execution, script, code +from IPython.core.magics import execution, script, code, logging from IPython.testing import decorators as dec from IPython.testing import tools as tt from IPython.utils import py3compat @@ -977,3 +978,35 @@ def test_strip_initial_indent(): nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2") nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc") nt.assert_equal(sii("a\n b"), "a\n b") + +def test_logging_magic_quiet_from_arg(): + _ip.config.LoggingMagics.quiet = False + lm = logging.LoggingMagics(shell=_ip) + with TemporaryDirectory() as td: + try: + with tt.AssertNotPrints(re.compile("Activating.*")): + lm.logstart('-q {}'.format( + os.path.join(td, "quiet_from_arg.log"))) + finally: + _ip.logger.logstop() + +def test_logging_magic_quiet_from_config(): + _ip.config.LoggingMagics.quiet = True + lm = logging.LoggingMagics(shell=_ip) + with TemporaryDirectory() as td: + try: + with tt.AssertNotPrints(re.compile("Activating.*")): + lm.logstart(os.path.join(td, "quiet_from_config.log")) + finally: + _ip.logger.logstop() + +def test_logging_magic_not_quiet(): + _ip.config.LoggingMagics.quiet = False + lm = logging.LoggingMagics(shell=_ip) + with TemporaryDirectory() as td: + try: + with tt.AssertPrints(re.compile("Activating.*")): + lm.logstart(os.path.join(td, "not_quiet.log")) + finally: + _ip.logger.logstop() + diff --git a/IPython/terminal/ipapp.py b/IPython/terminal/ipapp.py index 19f4199..0434f1e 100755 --- a/IPython/terminal/ipapp.py +++ b/IPython/terminal/ipapp.py @@ -25,7 +25,9 @@ from IPython.core.history import HistoryManager from IPython.core.application import ( ProfileDir, BaseIPythonApplication, base_flags, base_aliases ) -from IPython.core.magics import ScriptMagics +from IPython.core.magics import ( + ScriptMagics, LoggingMagics +) from IPython.core.shellapp import ( InteractiveShellApp, shell_flags, shell_aliases ) @@ -202,6 +204,7 @@ class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): PlainTextFormatter, IPCompleter, ScriptMagics, + LoggingMagics, StoreMagics, ]