Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/core/magics/config.py: 22%
46 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1"""Implementation of configuration-related magic functions.
2"""
3#-----------------------------------------------------------------------------
4# Copyright (c) 2012 The IPython Development Team.
5#
6# Distributed under the terms of the Modified BSD License.
7#
8# The full license is in the file COPYING.txt, distributed with this software.
9#-----------------------------------------------------------------------------
11#-----------------------------------------------------------------------------
12# Imports
13#-----------------------------------------------------------------------------
15# Stdlib
16import re
18# Our own packages
19from IPython.core.error import UsageError
20from IPython.core.magic import Magics, magics_class, line_magic
21from logging import error
23#-----------------------------------------------------------------------------
24# Magic implementation classes
25#-----------------------------------------------------------------------------
27reg = re.compile(r'^\w+\.\w+$')
28@magics_class
29class ConfigMagics(Magics):
31 def __init__(self, shell):
32 super(ConfigMagics, self).__init__(shell)
33 self.configurables = []
35 @line_magic
36 def config(self, s):
37 """configure IPython
39 %config Class[.trait=value]
41 This magic exposes most of the IPython config system. Any
42 Configurable class should be able to be configured with the simple
43 line::
45 %config Class.trait=value
47 Where `value` will be resolved in the user's namespace, if it is an
48 expression or variable name.
50 Examples
51 --------
53 To see what classes are available for config, pass no arguments::
55 In [1]: %config
56 Available objects for config:
57 AliasManager
58 DisplayFormatter
59 HistoryManager
60 IPCompleter
61 LoggingMagics
62 MagicsManager
63 OSMagics
64 PrefilterManager
65 ScriptMagics
66 TerminalInteractiveShell
68 To view what is configurable on a given class, just pass the class
69 name::
71 In [2]: %config LoggingMagics
72 LoggingMagics(Magics) options
73 ---------------------------
74 LoggingMagics.quiet=<Bool>
75 Suppress output of log state when logging is enabled
76 Current: False
78 but the real use is in setting values::
80 In [3]: %config LoggingMagics.quiet = True
82 and these values are read from the user_ns if they are variables::
84 In [4]: feeling_quiet=False
86 In [5]: %config LoggingMagics.quiet = feeling_quiet
88 """
89 from traitlets.config.loader import Config
90 # some IPython objects are Configurable, but do not yet have
91 # any configurable traits. Exclude them from the effects of
92 # this magic, as their presence is just noise:
93 configurables = sorted(set([ c for c in self.shell.configurables
94 if c.__class__.class_traits(config=True)
95 ]), key=lambda x: x.__class__.__name__)
96 classnames = [ c.__class__.__name__ for c in configurables ]
98 line = s.strip()
99 if not line:
100 # print available configurable names
101 print("Available objects for config:")
102 for name in classnames:
103 print(" ", name)
104 return
105 elif line in classnames:
106 # `%config TerminalInteractiveShell` will print trait info for
107 # TerminalInteractiveShell
108 c = configurables[classnames.index(line)]
109 cls = c.__class__
110 help = cls.class_get_help(c)
111 # strip leading '--' from cl-args:
112 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
113 print(help)
114 return
115 elif reg.match(line):
116 cls, attr = line.split('.')
117 return getattr(configurables[classnames.index(cls)],attr)
118 elif '=' not in line:
119 msg = "Invalid config statement: %r, "\
120 "should be `Class.trait = value`."
122 ll = line.lower()
123 for classname in classnames:
124 if ll == classname.lower():
125 msg = msg + '\nDid you mean %s (note the case)?' % classname
126 break
128 raise UsageError( msg % line)
130 # otherwise, assume we are setting configurables.
131 # leave quotes on args when splitting, because we want
132 # unquoted args to eval in user_ns
133 cfg = Config()
134 exec("cfg."+line, self.shell.user_ns, locals())
136 for configurable in configurables:
137 try:
138 configurable.update_config(cfg)
139 except Exception as e:
140 error(e)