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