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