Show More
@@ -1,363 +1,363 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Magic functions for InteractiveShell. |
|
2 | """Magic functions for InteractiveShell. | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
6 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
7 | # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu> |
|
7 | # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu> | |
8 | # Copyright (C) 2008 The IPython Development Team |
|
8 | # Copyright (C) 2008 The IPython Development Team | |
9 |
|
9 | |||
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Stdlib |
|
17 | # Stdlib | |
18 | import os |
|
18 | import os | |
19 | import re |
|
19 | import re | |
20 | import sys |
|
20 | import sys | |
21 | import types |
|
21 | import types | |
22 | from getopt import getopt, GetoptError |
|
22 | from getopt import getopt, GetoptError | |
23 |
|
23 | |||
24 | # Our own |
|
24 | # Our own | |
25 | from IPython.config.configurable import Configurable |
|
25 | from IPython.config.configurable import Configurable | |
26 | from IPython.core import oinspect |
|
26 | from IPython.core import oinspect | |
27 | from IPython.core.error import UsageError |
|
27 | from IPython.core.error import UsageError | |
28 | from IPython.core.prefilter import ESC_MAGIC |
|
28 | from IPython.core.prefilter import ESC_MAGIC | |
29 | from IPython.external.decorator import decorator |
|
29 | from IPython.external.decorator import decorator | |
30 | from IPython.utils.ipstruct import Struct |
|
30 | from IPython.utils.ipstruct import Struct | |
31 | from IPython.utils.process import arg_split |
|
31 | from IPython.utils.process import arg_split | |
32 | from IPython.utils.traitlets import Bool, Dict, Instance |
|
32 | from IPython.utils.traitlets import Bool, Dict, Instance | |
33 | from IPython.utils.warn import error |
|
33 | from IPython.utils.warn import error | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 | # Globals |
|
36 | # Globals | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
39 | # A dict we'll use for each class that has magics, used as temporary storage to |
|
39 | # A dict we'll use for each class that has magics, used as temporary storage to | |
40 | # pass information between the @line/cell_magic method decorators and the |
|
40 | # pass information between the @line/cell_magic method decorators and the | |
41 | # @register_magics class decorator, because the method decorators have no |
|
41 | # @register_magics class decorator, because the method decorators have no | |
42 | # access to the class when they run. See for more details: |
|
42 | # access to the class when they run. See for more details: | |
43 | # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class |
|
43 | # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class | |
44 |
|
44 | |||
45 | magics = dict(line={}, cell={}) |
|
45 | magics = dict(line={}, cell={}) | |
46 |
|
46 | |||
47 | magic_types = ('line', 'cell') |
|
47 | magic_types = ('line', 'cell') | |
48 |
|
48 | |||
49 | #----------------------------------------------------------------------------- |
|
49 | #----------------------------------------------------------------------------- | |
50 | # Utility classes and functions |
|
50 | # Utility classes and functions | |
51 | #----------------------------------------------------------------------------- |
|
51 | #----------------------------------------------------------------------------- | |
52 |
|
52 | |||
53 | class Bunch: pass |
|
53 | class Bunch: pass | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | def on_off(tag): |
|
56 | def on_off(tag): | |
57 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
57 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" | |
58 | return ['OFF','ON'][tag] |
|
58 | return ['OFF','ON'][tag] | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | def compress_dhist(dh): |
|
61 | def compress_dhist(dh): | |
62 | head, tail = dh[:-10], dh[-10:] |
|
62 | head, tail = dh[:-10], dh[-10:] | |
63 |
|
63 | |||
64 | newhead = [] |
|
64 | newhead = [] | |
65 | done = set() |
|
65 | done = set() | |
66 | for h in head: |
|
66 | for h in head: | |
67 | if h in done: |
|
67 | if h in done: | |
68 | continue |
|
68 | continue | |
69 | newhead.append(h) |
|
69 | newhead.append(h) | |
70 | done.add(h) |
|
70 | done.add(h) | |
71 |
|
71 | |||
72 | return newhead + tail |
|
72 | return newhead + tail | |
73 |
|
73 | |||
74 |
|
74 | |||
75 | def needs_local_scope(func): |
|
75 | def needs_local_scope(func): | |
76 | """Decorator to mark magic functions which need to local scope to run.""" |
|
76 | """Decorator to mark magic functions which need to local scope to run.""" | |
77 | func.needs_local_scope = True |
|
77 | func.needs_local_scope = True | |
78 | return func |
|
78 | return func | |
79 |
|
79 | |||
80 | #----------------------------------------------------------------------------- |
|
80 | #----------------------------------------------------------------------------- | |
81 | # Class and method decorators for registering magics |
|
81 | # Class and method decorators for registering magics | |
82 | #----------------------------------------------------------------------------- |
|
82 | #----------------------------------------------------------------------------- | |
83 |
|
83 | |||
84 | def register_magics(cls): |
|
84 | def register_magics(cls): | |
85 | cls.registered = True |
|
85 | cls.registered = True | |
86 | cls.magics = dict(line = magics['line'], |
|
86 | cls.magics = dict(line = magics['line'], | |
87 | cell = magics['cell']) |
|
87 | cell = magics['cell']) | |
88 | magics['line'] = {} |
|
88 | magics['line'] = {} | |
89 | magics['cell'] = {} |
|
89 | magics['cell'] = {} | |
90 | return cls |
|
90 | return cls | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | def validate_type(magic_type): |
|
93 | def validate_type(magic_type): | |
94 | if magic_type not in magic_types: |
|
94 | if magic_type not in magic_types: | |
95 | raise ValueError('magic_type must be one of %s, %s given' % |
|
95 | raise ValueError('magic_type must be one of %s, %s given' % | |
96 | magic_types, magic_type) |
|
96 | magic_types, magic_type) | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | def _magic_marker(magic_type): |
|
99 | def _magic_marker(magic_type): | |
100 | validate_type(magic_type) |
|
100 | validate_type(magic_type) | |
101 |
|
101 | |||
102 | # This is a closure to capture the magic_type. We could also use a class, |
|
102 | # This is a closure to capture the magic_type. We could also use a class, | |
103 | # but it's overkill for just that one bit of state. |
|
103 | # but it's overkill for just that one bit of state. | |
104 | def magic_deco(arg): |
|
104 | def magic_deco(arg): | |
105 | call = lambda f, *a, **k: f(*a, **k) |
|
105 | call = lambda f, *a, **k: f(*a, **k) | |
106 |
|
106 | |||
107 | if callable(arg): |
|
107 | if callable(arg): | |
108 | # "Naked" decorator call (just @foo, no args) |
|
108 | # "Naked" decorator call (just @foo, no args) | |
109 | func = arg |
|
109 | func = arg | |
110 | name = func.func_name |
|
110 | name = func.func_name | |
111 | func.magic_name = name |
|
111 | func.magic_name = name | |
112 | retval = decorator(call, func) |
|
112 | retval = decorator(call, func) | |
113 | magics[magic_type][name] = name |
|
113 | magics[magic_type][name] = name | |
114 | elif isinstance(arg, basestring): |
|
114 | elif isinstance(arg, basestring): | |
115 | # Decorator called with arguments (@foo('bar')) |
|
115 | # Decorator called with arguments (@foo('bar')) | |
116 | name = arg |
|
116 | name = arg | |
117 | def mark(func, *a, **kw): |
|
117 | def mark(func, *a, **kw): | |
118 | func.magic_name = name |
|
118 | func.magic_name = name | |
119 | magics[magic_type][name] = func.func_name |
|
119 | magics[magic_type][name] = func.func_name | |
120 | return decorator(call, func) |
|
120 | return decorator(call, func) | |
121 | retval = mark |
|
121 | retval = mark | |
122 | else: |
|
122 | else: | |
123 | raise ValueError("Decorator can only be called with " |
|
123 | raise ValueError("Decorator can only be called with " | |
124 | "string or function") |
|
124 | "string or function") | |
125 |
|
125 | |||
126 | return retval |
|
126 | return retval | |
127 |
|
127 | |||
128 | return magic_deco |
|
128 | return magic_deco | |
129 |
|
129 | |||
130 |
|
130 | |||
131 | line_magic = _magic_marker('line') |
|
131 | line_magic = _magic_marker('line') | |
132 | cell_magic = _magic_marker('cell') |
|
132 | cell_magic = _magic_marker('cell') | |
133 |
|
133 | |||
134 | #----------------------------------------------------------------------------- |
|
134 | #----------------------------------------------------------------------------- | |
135 | # Core Magic classes |
|
135 | # Core Magic classes | |
136 | #----------------------------------------------------------------------------- |
|
136 | #----------------------------------------------------------------------------- | |
137 |
|
137 | |||
138 | class MagicsManager(Configurable): |
|
138 | class MagicsManager(Configurable): | |
139 | """Object that handles all magic-related functionality for IPython. |
|
139 | """Object that handles all magic-related functionality for IPython. | |
140 | """ |
|
140 | """ | |
141 | # Non-configurable class attributes |
|
141 | # Non-configurable class attributes | |
142 | magics = Dict |
|
142 | magics = Dict | |
143 |
|
143 | |||
144 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
144 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
145 |
|
145 | |||
146 | auto_magic = Bool |
|
146 | auto_magic = Bool | |
147 |
|
147 | |||
148 | _auto_status = [ |
|
148 | _auto_status = [ | |
149 | 'Automagic is OFF, % prefix IS needed for magic functions.', |
|
149 | 'Automagic is OFF, % prefix IS needed for magic functions.', | |
150 | 'Automagic is ON, % prefix IS NOT needed for magic functions.'] |
|
150 | 'Automagic is ON, % prefix IS NOT needed for magic functions.'] | |
151 |
|
151 | |||
152 | user_magics = Instance('IPython.core.magic_functions.UserMagics') |
|
152 | user_magics = Instance('IPython.core.magic_functions.UserMagics') | |
153 |
|
153 | |||
154 | def __init__(self, shell=None, config=None, user_magics=None, **traits): |
|
154 | def __init__(self, shell=None, config=None, user_magics=None, **traits): | |
155 |
|
155 | |||
156 | super(MagicsManager, self).__init__(shell=shell, config=config, |
|
156 | super(MagicsManager, self).__init__(shell=shell, config=config, | |
157 | user_magics=user_magics, **traits) |
|
157 | user_magics=user_magics, **traits) | |
158 | self.magics = dict(line={}, cell={}) |
|
158 | self.magics = dict(line={}, cell={}) | |
159 |
|
159 | |||
160 | def auto_status(self): |
|
160 | def auto_status(self): | |
161 | """Return descriptive string with automagic status.""" |
|
161 | """Return descriptive string with automagic status.""" | |
162 | return self._auto_status[self.auto_magic] |
|
162 | return self._auto_status[self.auto_magic] | |
163 |
|
163 | |||
164 | def lsmagic(self): |
|
164 | def lsmagic(self): | |
165 | """Return a dict of currently available magic functions. |
|
165 | """Return a dict of currently available magic functions. | |
166 |
|
166 | |||
167 | The return dict has the keys 'line' and 'cell', corresponding to the |
|
167 | The return dict has the keys 'line' and 'cell', corresponding to the | |
168 | two types of magics we support. Each value is a list of names. |
|
168 | two types of magics we support. Each value is a list of names. | |
169 | """ |
|
169 | """ | |
170 | return self.magics |
|
170 | return self.magics | |
171 |
|
171 | |||
172 | def register(self, *magic_objects): |
|
172 | def register(self, *magic_objects): | |
173 | """Register one or more instances of Magics. |
|
173 | """Register one or more instances of Magics. | |
174 | """ |
|
174 | """ | |
175 | # Start by validating them to ensure they have all had their magic |
|
175 | # Start by validating them to ensure they have all had their magic | |
176 | # methods registered at the instance level |
|
176 | # methods registered at the instance level | |
177 | for m in magic_objects: |
|
177 | for m in magic_objects: | |
178 | if not m.registered: |
|
178 | if not m.registered: | |
179 | raise ValueError("Class of magics %r was constructed without " |
|
179 | raise ValueError("Class of magics %r was constructed without " | |
180 | "the @register_macics class decorator") |
|
180 | "the @register_macics class decorator") | |
181 | if type(m) is type: |
|
181 | if type(m) is type: | |
182 | # If we're given an uninstantiated class |
|
182 | # If we're given an uninstantiated class | |
183 | m = m(self.shell) |
|
183 | m = m(self.shell) | |
184 |
|
184 | |||
185 | for mtype in magic_types: |
|
185 | for mtype in magic_types: | |
186 | self.magics[mtype].update(m.magics[mtype]) |
|
186 | self.magics[mtype].update(m.magics[mtype]) | |
187 |
|
187 | |||
188 | def define_magic(self, magic_name, func, magic_type='line'): |
|
188 | def define_magic(self, magic_name, func, magic_type='line'): | |
189 | """Expose own function as magic function for ipython |
|
189 | """Expose own function as magic function for ipython | |
190 |
|
190 | |||
191 | Example:: |
|
191 | Example:: | |
192 |
|
192 | |||
193 | def foo_impl(self, parameter_s=''): |
|
193 | def foo_impl(self, parameter_s=''): | |
194 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
194 | 'My very own magic!. (Use docstrings, IPython reads them).' | |
195 | print 'Magic function. Passed parameter is between < >:' |
|
195 | print 'Magic function. Passed parameter is between < >:' | |
196 | print '<%s>' % parameter_s |
|
196 | print '<%s>' % parameter_s | |
197 | print 'The self object is:', self |
|
197 | print 'The self object is:', self | |
198 |
|
198 | |||
199 | ip.define_magic('foo', foo_impl) |
|
199 | ip.define_magic('foo', foo_impl) | |
200 | """ |
|
200 | """ | |
201 | # Create the new method in the user_magics and register it in the |
|
201 | # Create the new method in the user_magics and register it in the | |
202 | # global table |
|
202 | # global table | |
203 | self.user_magics.new_magic(magic_name, func, magic_type) |
|
203 | self.user_magics.new_magic(magic_name, func, magic_type) | |
204 | self.magics[magic_type][magic_name] = \ |
|
204 | self.magics[magic_type][magic_name] = \ | |
205 | self.user_magics.magics[magic_type][magic_name] |
|
205 | self.user_magics.magics[magic_type][magic_name] | |
206 |
|
206 | |||
207 | # Key base class that provides the central functionality for magics. |
|
207 | # Key base class that provides the central functionality for magics. | |
208 |
|
208 | |||
209 | class Magics(object): |
|
209 | class Magics(object): | |
210 | """Base class for implementing magic functions. |
|
210 | """Base class for implementing magic functions. | |
211 |
|
211 | |||
212 | Shell functions which can be reached as %function_name. All magic |
|
212 | Shell functions which can be reached as %function_name. All magic | |
213 | functions should accept a string, which they can parse for their own |
|
213 | functions should accept a string, which they can parse for their own | |
214 | needs. This can make some functions easier to type, eg `%cd ../` |
|
214 | needs. This can make some functions easier to type, eg `%cd ../` | |
215 | vs. `%cd("../")` |
|
215 | vs. `%cd("../")` | |
216 |
|
216 | |||
217 | Classes providing magic functions need to subclass this class, and they |
|
217 | Classes providing magic functions need to subclass this class, and they | |
218 | MUST: |
|
218 | MUST: | |
219 |
|
219 | |||
220 | - Use the method decorators `@line_magic` and `@cell_magic` to decorate |
|
220 | - Use the method decorators `@line_magic` and `@cell_magic` to decorate | |
221 | individual methods as magic functions, AND |
|
221 | individual methods as magic functions, AND | |
222 |
|
222 | |||
223 | - Use the class decorator `@register_magics` to ensure that the magic |
|
223 | - Use the class decorator `@register_magics` to ensure that the magic | |
224 | methods are properly registered at the instance level upon instance |
|
224 | methods are properly registered at the instance level upon instance | |
225 | initialization. |
|
225 | initialization. | |
226 |
|
226 | |||
227 | See :mod:`magic_functions` for examples of actual implementation classes. |
|
227 | See :mod:`magic_functions` for examples of actual implementation classes. | |
228 | """ |
|
228 | """ | |
229 | # Dict holding all command-line options for each magic. |
|
229 | # Dict holding all command-line options for each magic. | |
230 | options_table = None |
|
230 | options_table = None | |
231 |
|
231 | # Dict for the mapping of magic names to methods, set by class decorator | ||
232 | # Non-configurable class attributes |
|
232 | magics = None | |
233 | magics = Dict |
|
233 | # Flag to check that the class decorator was properly applied | |
234 |
|
||||
235 | registered = False |
|
234 | registered = False | |
236 |
|
235 | |||
237 | def __init__(self, shell): |
|
236 | def __init__(self, shell): | |
238 | if not(self.__class__.registered): |
|
237 | if not(self.__class__.registered): | |
239 | raise ValueError('unregistered Magics') |
|
238 | raise ValueError('unregistered Magics') | |
240 | self.shell = shell |
|
239 | self.shell = shell | |
|
240 | self.options_table = {} | |||
241 | mtab = dict(line={}, cell={}) |
|
241 | mtab = dict(line={}, cell={}) | |
242 | for mtype in magic_types: |
|
242 | for mtype in magic_types: | |
243 | tab = mtab[mtype] |
|
243 | tab = mtab[mtype] | |
244 | for magic_name, meth_name in self.magics[mtype].iteritems(): |
|
244 | for magic_name, meth_name in self.magics[mtype].iteritems(): | |
245 | if isinstance(meth_name, basestring): |
|
245 | if isinstance(meth_name, basestring): | |
246 | tab[magic_name] = getattr(self, meth_name) |
|
246 | tab[magic_name] = getattr(self, meth_name) | |
247 | self.magics.update(mtab) |
|
247 | self.magics.update(mtab) | |
248 |
|
248 | |||
249 | def arg_err(self,func): |
|
249 | def arg_err(self,func): | |
250 | """Print docstring if incorrect arguments were passed""" |
|
250 | """Print docstring if incorrect arguments were passed""" | |
251 | print 'Error in arguments:' |
|
251 | print 'Error in arguments:' | |
252 | print oinspect.getdoc(func) |
|
252 | print oinspect.getdoc(func) | |
253 |
|
253 | |||
254 | def format_latex(self,strng): |
|
254 | def format_latex(self, strng): | |
255 | """Format a string for latex inclusion.""" |
|
255 | """Format a string for latex inclusion.""" | |
256 |
|
256 | |||
257 | # Characters that need to be escaped for latex: |
|
257 | # Characters that need to be escaped for latex: | |
258 | escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE) |
|
258 | escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE) | |
259 | # Magic command names as headers: |
|
259 | # Magic command names as headers: | |
260 | cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC, |
|
260 | cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC, | |
261 | re.MULTILINE) |
|
261 | re.MULTILINE) | |
262 | # Magic commands |
|
262 | # Magic commands | |
263 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC, |
|
263 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC, | |
264 | re.MULTILINE) |
|
264 | re.MULTILINE) | |
265 | # Paragraph continue |
|
265 | # Paragraph continue | |
266 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
266 | par_re = re.compile(r'\\$',re.MULTILINE) | |
267 |
|
267 | |||
268 | # The "\n" symbol |
|
268 | # The "\n" symbol | |
269 | newline_re = re.compile(r'\\n') |
|
269 | newline_re = re.compile(r'\\n') | |
270 |
|
270 | |||
271 | # Now build the string for output: |
|
271 | # Now build the string for output: | |
272 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) |
|
272 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) | |
273 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', |
|
273 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', | |
274 | strng) |
|
274 | strng) | |
275 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) |
|
275 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) | |
276 | strng = par_re.sub(r'\\\\',strng) |
|
276 | strng = par_re.sub(r'\\\\',strng) | |
277 | strng = escape_re.sub(r'\\\1',strng) |
|
277 | strng = escape_re.sub(r'\\\1',strng) | |
278 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
278 | strng = newline_re.sub(r'\\textbackslash{}n',strng) | |
279 | return strng |
|
279 | return strng | |
280 |
|
280 | |||
281 | def parse_options(self, arg_str, opt_str, *long_opts, **kw): |
|
281 | def parse_options(self, arg_str, opt_str, *long_opts, **kw): | |
282 | """Parse options passed to an argument string. |
|
282 | """Parse options passed to an argument string. | |
283 |
|
283 | |||
284 | The interface is similar to that of getopt(), but it returns back a |
|
284 | The interface is similar to that of getopt(), but it returns back a | |
285 | Struct with the options as keys and the stripped argument string still |
|
285 | Struct with the options as keys and the stripped argument string still | |
286 | as a string. |
|
286 | as a string. | |
287 |
|
287 | |||
288 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
288 | arg_str is quoted as a true sys.argv vector by using shlex.split. | |
289 | This allows us to easily expand variables, glob files, quote |
|
289 | This allows us to easily expand variables, glob files, quote | |
290 | arguments, etc. |
|
290 | arguments, etc. | |
291 |
|
291 | |||
292 | Options: |
|
292 | Options: | |
293 | -mode: default 'string'. If given as 'list', the argument string is |
|
293 | -mode: default 'string'. If given as 'list', the argument string is | |
294 | returned as a list (split on whitespace) instead of a string. |
|
294 | returned as a list (split on whitespace) instead of a string. | |
295 |
|
295 | |||
296 | -list_all: put all option values in lists. Normally only options |
|
296 | -list_all: put all option values in lists. Normally only options | |
297 | appearing more than once are put in a list. |
|
297 | appearing more than once are put in a list. | |
298 |
|
298 | |||
299 | -posix (True): whether to split the input line in POSIX mode or not, |
|
299 | -posix (True): whether to split the input line in POSIX mode or not, | |
300 | as per the conventions outlined in the shlex module from the |
|
300 | as per the conventions outlined in the shlex module from the | |
301 | standard library.""" |
|
301 | standard library.""" | |
302 |
|
302 | |||
303 | # inject default options at the beginning of the input line |
|
303 | # inject default options at the beginning of the input line | |
304 |
caller = sys._getframe(1).f_code.co_name |
|
304 | caller = sys._getframe(1).f_code.co_name | |
305 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
305 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) | |
306 |
|
306 | |||
307 | mode = kw.get('mode','string') |
|
307 | mode = kw.get('mode','string') | |
308 | if mode not in ['string','list']: |
|
308 | if mode not in ['string','list']: | |
309 | raise ValueError,'incorrect mode given: %s' % mode |
|
309 | raise ValueError,'incorrect mode given: %s' % mode | |
310 | # Get options |
|
310 | # Get options | |
311 | list_all = kw.get('list_all',0) |
|
311 | list_all = kw.get('list_all',0) | |
312 | posix = kw.get('posix', os.name == 'posix') |
|
312 | posix = kw.get('posix', os.name == 'posix') | |
313 | strict = kw.get('strict', True) |
|
313 | strict = kw.get('strict', True) | |
314 |
|
314 | |||
315 | # Check if we have more than one argument to warrant extra processing: |
|
315 | # Check if we have more than one argument to warrant extra processing: | |
316 | odict = {} # Dictionary with options |
|
316 | odict = {} # Dictionary with options | |
317 | args = arg_str.split() |
|
317 | args = arg_str.split() | |
318 | if len(args) >= 1: |
|
318 | if len(args) >= 1: | |
319 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
319 | # If the list of inputs only has 0 or 1 thing in it, there's no | |
320 | # need to look for options |
|
320 | # need to look for options | |
321 | argv = arg_split(arg_str, posix, strict) |
|
321 | argv = arg_split(arg_str, posix, strict) | |
322 | # Do regular option processing |
|
322 | # Do regular option processing | |
323 | try: |
|
323 | try: | |
324 | opts,args = getopt(argv,opt_str,*long_opts) |
|
324 | opts,args = getopt(argv,opt_str,*long_opts) | |
325 | except GetoptError,e: |
|
325 | except GetoptError,e: | |
326 | raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, |
|
326 | raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, | |
327 | " ".join(long_opts))) |
|
327 | " ".join(long_opts))) | |
328 | for o,a in opts: |
|
328 | for o,a in opts: | |
329 | if o.startswith('--'): |
|
329 | if o.startswith('--'): | |
330 | o = o[2:] |
|
330 | o = o[2:] | |
331 | else: |
|
331 | else: | |
332 | o = o[1:] |
|
332 | o = o[1:] | |
333 | try: |
|
333 | try: | |
334 | odict[o].append(a) |
|
334 | odict[o].append(a) | |
335 | except AttributeError: |
|
335 | except AttributeError: | |
336 | odict[o] = [odict[o],a] |
|
336 | odict[o] = [odict[o],a] | |
337 | except KeyError: |
|
337 | except KeyError: | |
338 | if list_all: |
|
338 | if list_all: | |
339 | odict[o] = [a] |
|
339 | odict[o] = [a] | |
340 | else: |
|
340 | else: | |
341 | odict[o] = a |
|
341 | odict[o] = a | |
342 |
|
342 | |||
343 | # Prepare opts,args for return |
|
343 | # Prepare opts,args for return | |
344 | opts = Struct(odict) |
|
344 | opts = Struct(odict) | |
345 | if mode == 'string': |
|
345 | if mode == 'string': | |
346 | args = ' '.join(args) |
|
346 | args = ' '.join(args) | |
347 |
|
347 | |||
348 | return opts,args |
|
348 | return opts,args | |
349 |
|
349 | |||
350 | def default_option(self, fn, optstr): |
|
350 | def default_option(self, fn, optstr): | |
351 | """Make an entry in the options_table for fn, with value optstr""" |
|
351 | """Make an entry in the options_table for fn, with value optstr""" | |
352 |
|
352 | |||
353 | if fn not in self.lsmagic(): |
|
353 | if fn not in self.lsmagic(): | |
354 | error("%s is not a magic function" % fn) |
|
354 | error("%s is not a magic function" % fn) | |
355 | self.options_table[fn] = optstr |
|
355 | self.options_table[fn] = optstr | |
356 |
|
356 | |||
357 | def new_magic(self, magic_name, func, magic_type='line'): |
|
357 | def new_magic(self, magic_name, func, magic_type='line'): | |
358 | """TODO |
|
358 | """TODO | |
359 | """ |
|
359 | """ | |
360 | validate_type(magic_type) |
|
360 | validate_type(magic_type) | |
361 | meth = types.MethodType(func, self) |
|
361 | meth = types.MethodType(func, self) | |
362 | setattr(self, magic_name, meth) |
|
362 | setattr(self, magic_name, meth) | |
363 | self.magics[magic_type][magic_name] = meth |
|
363 | self.magics[magic_type][magic_name] = meth |
General Comments 0
You need to be logged in to leave comments.
Login now