5353 Same as 2/all, but also adds any new objects in the module. See
5454 unit test at IPython/extensions/tests/test_autoreload.py::test_autoload_newly_added_objects
5555
56+ Adding ``--print`` or ``-p`` to the ``%autoreload`` line will print autoreload activity to
57+ standard out. ``--log`` or ``-l`` will do it to the log at INFO level; both can be used
58+ simultaneously.
59+
5660``%aimport``
5761
5862 List modules which are to be automatically imported or not to be imported.
6973
7074 Mark module 'foo' to not be autoreloaded.
7175
72- ``%averbose off``
73-
74- Perform autoreload tasks quietly
75-
76- ``%averbose on``
77-
78- Report activity with `print` statements.
79-
80- ``%averbose log``
81-
82- Report activity with the logger.
83-
8476Caveats
8577=======
8678
113105- Reloading a module, or importing the same module by a different name, creates new Enums. These may look the same, but are not.
114106"""
115107
108+ from IPython .core import magic_arguments
116109from IPython .core .magic import Magics , magics_class , line_magic
117110
118111__skip_doctest__ = True
@@ -525,7 +518,28 @@ def __init__(self, *a, **kw):
525518 self .loaded_modules = set (sys .modules )
526519
527520 @line_magic
528- def autoreload (self , parameter_s = "" ):
521+ @magic_arguments .magic_arguments ()
522+ @magic_arguments .argument ('mode' , type = str , default = 'now' , nargs = '?' ,
523+ help = """
524+ blank or 'now' - Reload all modules (except those excluded by
525+ %%aimport) automaticallynow.
526+
527+ '0' or 'off' - Disable automatic reloading.
528+
529+ '1' or 'explicit' - Reload only modules imported with %%aimport every
530+ time before executing the Python code typed.
531+
532+ '2' or 'all' - Reload all modules (except those excluded by %%aimport)
533+ every time before executing the Python code typed.
534+
535+ '3' or 'complete' - Same as 2/all, but also but also adds any new
536+ objects in the module.
537+ """ )
538+ @magic_arguments .argument ('-p' , '--print' , action = 'store_true' , default = False ,
539+ help = 'Show autoreload activity using `print` statements' )
540+ @magic_arguments .argument ('-l' , '--log' , action = 'store_true' , default = False ,
541+ help = 'Show autoreload activity using the logger' )
542+ def autoreload (self , line = "" ):
529543 r"""%autoreload => Reload modules automatically
530544
531545 %autoreload or %autoreload now
@@ -547,6 +561,10 @@ def autoreload(self, parameter_s=""):
547561 Same as 2/all, but also but also adds any new objects in the module. See
548562 unit test at IPython/extensions/tests/test_autoreload.py::test_autoload_newly_added_objects
549563
564+ The optional arguments --print and --log control display of autoreload activity. The default
565+ is to act silently; --print (or -p) will print out the names of modules that are being
566+ reloaded, and --log (or -l) outputs them to the log at INFO level.
567+
550568 Reloading Python modules in a reliable way is in general
551569 difficult, and unexpected things may occur. %autoreload tries to
552570 work around common pitfalls by replacing function code objects and
@@ -573,23 +591,45 @@ def autoreload(self, parameter_s=""):
573591 autoreloaded.
574592
575593 """
576- parameter_s_lower = parameter_s .lower ()
577- if parameter_s == "" or parameter_s_lower == "now" :
594+ args = magic_arguments .parse_argstring (self .autoreload , line )
595+ mode = args .mode .lower ()
596+
597+ def p (msg ):
598+ print (msg )
599+
600+ def l (msg ):
601+ logging .getLogger ("autoreload" ).info (msg )
602+
603+ def pl (msg ):
604+ p (msg )
605+ l (msg )
606+
607+ if args .print is False and args .log is False :
608+ self ._reloader ._report = lambda msg : None
609+ elif args .print is True :
610+ if args .log is True :
611+ self ._reloader ._report = lambda msg : pl (msg )
612+ else :
613+ self ._reloader ._report = lambda msg : p (msg )
614+ elif args .log is True :
615+ self ._reloader ._report = lambda msg : l (msg )
616+
617+ if mode == "" or mode == "now" :
578618 self ._reloader .check (True )
579- elif parameter_s == "0" or parameter_s_lower == "off" :
619+ elif mode == "0" or mode == "off" :
580620 self ._reloader .enabled = False
581- elif parameter_s == "1" or parameter_s_lower == "explicit" :
621+ elif mode == "1" or mode == "explicit" :
582622 self ._reloader .check_all = False
583623 self ._reloader .enabled = True
584- elif parameter_s == "2" or parameter_s_lower == "all" :
624+ elif mode == "2" or mode == "all" :
585625 self ._reloader .check_all = True
586626 self ._reloader .enabled = True
587- elif parameter_s == "3" or parameter_s_lower == "complete" :
627+ elif mode == "3" or mode == "complete" :
588628 self ._reloader .check_all = True
589629 self ._reloader .enabled = True
590630 self ._reloader .autoload_obj = True
591631 else :
592- raise ValueError (f'Unrecognized parameter " { parameter_s } ".' )
632+ raise ValueError (f'Unrecognized autoreload mode " { mode } ".' )
593633
594634 @line_magic
595635 def aimport (self , parameter_s = "" , stream = None ):
@@ -630,31 +670,6 @@ def aimport(self, parameter_s="", stream=None):
630670 # Inject module to user namespace
631671 self .shell .push ({top_name : top_module })
632672
633- @line_magic
634- def averbose (self , parameter_s = "" ):
635- r"""%averbose => Turn verbosity on/off for autoreloading.
636-
637- %averbose 0 or %averbose off
638- Turn off any reporting during autoreload.
639-
640- %averbose 1 or %averbose on
641- Report autoreload activity via print statements.
642-
643- %averbose 2 or %averbose log
644- Report autoreload activity via logging.
645- """
646-
647- if parameter_s == "0" or parameter_s .lower () == "off" :
648- self ._reloader ._report = lambda msg : None
649- elif parameter_s == "1" or parameter_s .lower () == "on" :
650- self ._reloader ._report = lambda msg : print (msg )
651- elif parameter_s == "2" or parameter_s .lower () == "log" :
652- self ._reloader ._report = lambda msg : logging .getLogger ("autoreload" ).info (
653- msg
654- )
655- else :
656- raise ValueError (f'Unrecognized parameter "{ parameter_s } ".' )
657-
658673 def pre_run_cell (self ):
659674 if self ._reloader .enabled :
660675 try :
0 commit comments