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