##// END OF EJS Templates
Create core.magics.config according to new API.
Fernando Perez -
Show More
@@ -0,0 +1,146 b''
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 #-----------------------------------------------------------------------------
10
11 #-----------------------------------------------------------------------------
12 # Imports
13 #-----------------------------------------------------------------------------
14
15 # Stdlib
16 import re
17
18 # Our own packages
19 from IPython.core.error import UsageError
20 from IPython.core.magic import Magics, register_magics, line_magic
21 from IPython.utils.warn import error
22
23 #-----------------------------------------------------------------------------
24 # Magic implementation classes
25 #-----------------------------------------------------------------------------
26
27 @register_magics
28 class ConfigMagics(Magics):
29
30 def __init__(self, shell):
31 super(ConfigMagics, self).__init__(shell)
32 self.configurables = []
33
34 @line_magic
35 def config(self, s):
36 """configure IPython
37
38 %config Class[.trait=value]
39
40 This magic exposes most of the IPython config system. Any
41 Configurable class should be able to be configured with the simple
42 line::
43
44 %config Class.trait=value
45
46 Where `value` will be resolved in the user's namespace, if it is an
47 expression or variable name.
48
49 Examples
50 --------
51
52 To see what classes are available for config, pass no arguments::
53
54 In [1]: %config
55 Available objects for config:
56 TerminalInteractiveShell
57 HistoryManager
58 PrefilterManager
59 AliasManager
60 IPCompleter
61 PromptManager
62 DisplayFormatter
63
64 To view what is configurable on a given class, just pass the class
65 name::
66
67 In [2]: %config IPCompleter
68 IPCompleter options
69 -----------------
70 IPCompleter.omit__names=<Enum>
71 Current: 2
72 Choices: (0, 1, 2)
73 Instruct the completer to omit private method names
74 Specifically, when completing on ``object.<tab>``.
75 When 2 [default]: all names that start with '_' will be excluded.
76 When 1: all 'magic' names (``__foo__``) will be excluded.
77 When 0: nothing will be excluded.
78 IPCompleter.merge_completions=<CBool>
79 Current: True
80 Whether to merge completion results into a single list
81 If False, only the completion results from the first non-empty
82 completer will be returned.
83 IPCompleter.limit_to__all__=<CBool>
84 Current: False
85 Instruct the completer to use __all__ for the completion
86 Specifically, when completing on ``object.<tab>``.
87 When True: only those names in obj.__all__ will be included.
88 When False [default]: the __all__ attribute is ignored
89 IPCompleter.greedy=<CBool>
90 Current: False
91 Activate greedy completion
92 This will enable completion on elements of lists, results of
93 function calls, etc., but can be unsafe because the code is
94 actually evaluated on TAB.
95
96 but the real use is in setting values::
97
98 In [3]: %config IPCompleter.greedy = True
99
100 and these values are read from the user_ns if they are variables::
101
102 In [4]: feeling_greedy=False
103
104 In [5]: %config IPCompleter.greedy = feeling_greedy
105
106 """
107 from IPython.config.loader import Config
108 # some IPython objects are Configurable, but do not yet have
109 # any configurable traits. Exclude them from the effects of
110 # this magic, as their presence is just noise:
111 configurables = [ c for c in self.shell.configurables
112 if c.__class__.class_traits(config=True) ]
113 classnames = [ c.__class__.__name__ for c in configurables ]
114
115 line = s.strip()
116 if not line:
117 # print available configurable names
118 print "Available objects for config:"
119 for name in classnames:
120 print " ", name
121 return
122 elif line in classnames:
123 # `%config TerminalInteractiveShell` will print trait info for
124 # TerminalInteractiveShell
125 c = configurables[classnames.index(line)]
126 cls = c.__class__
127 help = cls.class_get_help(c)
128 # strip leading '--' from cl-args:
129 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
130 print help
131 return
132 elif '=' not in line:
133 raise UsageError("Invalid config statement: %r, "
134 "should be Class.trait = value" % line)
135
136 # otherwise, assume we are setting configurables.
137 # leave quotes on args when splitting, because we want
138 # unquoted args to eval in user_ns
139 cfg = Config()
140 exec "cfg."+line in locals(), self.shell.user_ns
141
142 for configurable in configurables:
143 try:
144 configurable.update_config(cfg)
145 except Exception as e:
146 error(e)
@@ -2005,7 +2005,7 b' class InteractiveShell(SingletonConfigurable):'
2005 self.register_magic_function = self.magics_manager.register_function
2005 self.register_magic_function = self.magics_manager.register_function
2006 self.define_magic = self.magics_manager.define_magic
2006 self.define_magic = self.magics_manager.define_magic
2007
2007
2008 self.register_magics(m.BasicMagics, m.CodeMagics, mf.ConfigMagics,
2008 self.register_magics(m.BasicMagics, m.CodeMagics, m.ConfigMagics,
2009 mf.ExecutionMagics, mf.NamespaceMagics, mf.AutoMagics,
2009 mf.ExecutionMagics, mf.NamespaceMagics, mf.AutoMagics,
2010 mf.OSMagics, mf.LoggingMagics, mf.ExtensionsMagics,
2010 mf.OSMagics, mf.LoggingMagics, mf.ExtensionsMagics,
2011 mf.PylabMagics, m.HistoryMagics, mf.DeprecatedMagics)
2011 mf.PylabMagics, m.HistoryMagics, mf.DeprecatedMagics)
@@ -9,9 +9,11 b''
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
13 # Imports
14 # Imports
14 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
15 # Stdlib
17 # Stdlib
16 import __builtin__ as builtin_mod
18 import __builtin__ as builtin_mod
17 import bdb
19 import bdb
@@ -63,129 +65,6 b' from IPython.utils.warn import warn, error'
63 #-----------------------------------------------------------------------------
65 #-----------------------------------------------------------------------------
64 # Magic implementation classes
66 # Magic implementation classes
65 #-----------------------------------------------------------------------------
67 #-----------------------------------------------------------------------------
66
67 @register_magics
68 class ConfigMagics(Magics):
69
70 def __init__(self, shell):
71 super(ConfigMagics, self).__init__(shell)
72 self.configurables = []
73
74 @line_magic
75 def config(self, s):
76 """configure IPython
77
78 %config Class[.trait=value]
79
80 This magic exposes most of the IPython config system. Any
81 Configurable class should be able to be configured with the simple
82 line::
83
84 %config Class.trait=value
85
86 Where `value` will be resolved in the user's namespace, if it is an
87 expression or variable name.
88
89 Examples
90 --------
91
92 To see what classes are available for config, pass no arguments::
93
94 In [1]: %config
95 Available objects for config:
96 TerminalInteractiveShell
97 HistoryManager
98 PrefilterManager
99 AliasManager
100 IPCompleter
101 PromptManager
102 DisplayFormatter
103
104 To view what is configurable on a given class, just pass the class
105 name::
106
107 In [2]: %config IPCompleter
108 IPCompleter options
109 -----------------
110 IPCompleter.omit__names=<Enum>
111 Current: 2
112 Choices: (0, 1, 2)
113 Instruct the completer to omit private method names
114 Specifically, when completing on ``object.<tab>``.
115 When 2 [default]: all names that start with '_' will be excluded.
116 When 1: all 'magic' names (``__foo__``) will be excluded.
117 When 0: nothing will be excluded.
118 IPCompleter.merge_completions=<CBool>
119 Current: True
120 Whether to merge completion results into a single list
121 If False, only the completion results from the first non-empty
122 completer will be returned.
123 IPCompleter.limit_to__all__=<CBool>
124 Current: False
125 Instruct the completer to use __all__ for the completion
126 Specifically, when completing on ``object.<tab>``.
127 When True: only those names in obj.__all__ will be included.
128 When False [default]: the __all__ attribute is ignored
129 IPCompleter.greedy=<CBool>
130 Current: False
131 Activate greedy completion
132 This will enable completion on elements of lists, results of
133 function calls, etc., but can be unsafe because the code is
134 actually evaluated on TAB.
135
136 but the real use is in setting values::
137
138 In [3]: %config IPCompleter.greedy = True
139
140 and these values are read from the user_ns if they are variables::
141
142 In [4]: feeling_greedy=False
143
144 In [5]: %config IPCompleter.greedy = feeling_greedy
145
146 """
147 from IPython.config.loader import Config
148 # some IPython objects are Configurable, but do not yet have
149 # any configurable traits. Exclude them from the effects of
150 # this magic, as their presence is just noise:
151 configurables = [ c for c in self.shell.configurables
152 if c.__class__.class_traits(config=True) ]
153 classnames = [ c.__class__.__name__ for c in configurables ]
154
155 line = s.strip()
156 if not line:
157 # print available configurable names
158 print "Available objects for config:"
159 for name in classnames:
160 print " ", name
161 return
162 elif line in classnames:
163 # `%config TerminalInteractiveShell` will print trait info for
164 # TerminalInteractiveShell
165 c = configurables[classnames.index(line)]
166 cls = c.__class__
167 help = cls.class_get_help(c)
168 # strip leading '--' from cl-args:
169 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
170 print help
171 return
172 elif '=' not in line:
173 raise UsageError("Invalid config statement: %r, "
174 "should be Class.trait = value" % line)
175
176 # otherwise, assume we are setting configurables.
177 # leave quotes on args when splitting, because we want
178 # unquoted args to eval in user_ns
179 cfg = Config()
180 exec "cfg."+line in locals(), self.shell.user_ns
181
182 for configurable in configurables:
183 try:
184 configurable.update_config(cfg)
185 except Exception as e:
186 error(e)
187
188
189 @register_magics
68 @register_magics
190 class NamespaceMagics(Magics):
69 class NamespaceMagics(Magics):
191 """Magics to manage various aspects of the user's namespace.
70 """Magics to manage various aspects of the user's namespace.
@@ -15,6 +15,7 b''
15 from IPython.core.magic import Magics, register_magics
15 from IPython.core.magic import Magics, register_magics
16 from .basic import BasicMagics
16 from .basic import BasicMagics
17 from .code import CodeMagics, MacroToEdit
17 from .code import CodeMagics, MacroToEdit
18 from .config import ConfigMagics
18 from .history import HistoryMagics
19 from .history import HistoryMagics
19
20
20 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
@@ -1,4 +1,4 b''
1 """Implementation of basic magic functions.
1 """Implementation of code management magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
General Comments 0
You need to be logged in to leave comments. Login now