##// END OF EJS Templates
Fix locals collisions with shell variables...
linar-jether -
Show More
@@ -1,158 +1,158 b''
1 1 """Implementation of configuration-related magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import re
17 17
18 18 # Our own packages
19 19 from IPython.core.error import UsageError
20 20 from IPython.core.magic import Magics, magics_class, line_magic
21 21 from logging import error
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Magic implementation classes
25 25 #-----------------------------------------------------------------------------
26 26
27 27 reg = re.compile(r'^\w+\.\w+$')
28 28 @magics_class
29 29 class ConfigMagics(Magics):
30 30
31 31 def __init__(self, shell):
32 32 super(ConfigMagics, self).__init__(shell)
33 33 self.configurables = []
34 34
35 35 @line_magic
36 36 def config(self, s):
37 37 """configure IPython
38 38
39 39 %config Class[.trait=value]
40 40
41 41 This magic exposes most of the IPython config system. Any
42 42 Configurable class should be able to be configured with the simple
43 43 line::
44 44
45 45 %config Class.trait=value
46 46
47 47 Where `value` will be resolved in the user's namespace, if it is an
48 48 expression or variable name.
49 49
50 50 Examples
51 51 --------
52 52
53 53 To see what classes are available for config, pass no arguments::
54 54
55 55 In [1]: %config
56 56 Available objects for config:
57 57 TerminalInteractiveShell
58 58 HistoryManager
59 59 PrefilterManager
60 60 AliasManager
61 61 IPCompleter
62 62 DisplayFormatter
63 63
64 64 To view what is configurable on a given class, just pass the class
65 65 name::
66 66
67 67 In [2]: %config IPCompleter
68 68 IPCompleter options
69 69 -----------------
70 70 IPCompleter.omit__names=<Enum>
71 71 Current: 2
72 72 Choices: (0, 1, 2)
73 73 Instruct the completer to omit private method names
74 74 Specifically, when completing on ``object.<tab>``.
75 75 When 2 [default]: all names that start with '_' will be excluded.
76 76 When 1: all 'magic' names (``__foo__``) will be excluded.
77 77 When 0: nothing will be excluded.
78 78 IPCompleter.merge_completions=<CBool>
79 79 Current: True
80 80 Whether to merge completion results into a single list
81 81 If False, only the completion results from the first non-empty
82 82 completer will be returned.
83 83 IPCompleter.limit_to__all__=<CBool>
84 84 Current: False
85 85 Instruct the completer to use __all__ for the completion
86 86 Specifically, when completing on ``object.<tab>``.
87 87 When True: only those names in obj.__all__ will be included.
88 88 When False [default]: the __all__ attribute is ignored
89 89 IPCompleter.greedy=<CBool>
90 90 Current: False
91 91 Activate greedy completion
92 92 This will enable completion on elements of lists, results of
93 93 function calls, etc., but can be unsafe because the code is
94 94 actually evaluated on TAB.
95 95
96 96 but the real use is in setting values::
97 97
98 98 In [3]: %config IPCompleter.greedy = True
99 99
100 100 and these values are read from the user_ns if they are variables::
101 101
102 102 In [4]: feeling_greedy=False
103 103
104 104 In [5]: %config IPCompleter.greedy = feeling_greedy
105 105
106 106 """
107 107 from traitlets.config.loader import Config
108 108 # some IPython objects are Configurable, but do not yet have
109 109 # any configurable traits. Exclude them from the effects of
110 110 # this magic, as their presence is just noise:
111 111 configurables = sorted(set([ c for c in self.shell.configurables
112 112 if c.__class__.class_traits(config=True)
113 113 ]), key=lambda x: x.__class__.__name__)
114 114 classnames = [ c.__class__.__name__ for c in configurables ]
115 115
116 116 line = s.strip()
117 117 if not line:
118 118 # print available configurable names
119 119 print("Available objects for config:")
120 120 for name in classnames:
121 121 print(" ", name)
122 122 return
123 123 elif line in classnames:
124 124 # `%config TerminalInteractiveShell` will print trait info for
125 125 # TerminalInteractiveShell
126 126 c = configurables[classnames.index(line)]
127 127 cls = c.__class__
128 128 help = cls.class_get_help(c)
129 129 # strip leading '--' from cl-args:
130 130 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
131 131 print(help)
132 132 return
133 133 elif reg.match(line):
134 134 cls, attr = line.split('.')
135 135 return getattr(configurables[classnames.index(cls)],attr)
136 136 elif '=' not in line:
137 137 msg = "Invalid config statement: %r, "\
138 138 "should be `Class.trait = value`."
139 139
140 140 ll = line.lower()
141 141 for classname in classnames:
142 142 if ll == classname.lower():
143 143 msg = msg + '\nDid you mean %s (note the case)?' % classname
144 144 break
145 145
146 146 raise UsageError( msg % line)
147 147
148 148 # otherwise, assume we are setting configurables.
149 149 # leave quotes on args when splitting, because we want
150 150 # unquoted args to eval in user_ns
151 151 cfg = Config()
152 exec("cfg."+line, locals(), self.shell.user_ns)
152 exec("cfg."+line, self.shell.user_ns, locals())
153 153
154 154 for configurable in configurables:
155 155 try:
156 156 configurable.update_config(cfg)
157 157 except Exception as e:
158 158 error(e)
General Comments 0
You need to be logged in to leave comments. Login now