##// END OF EJS Templates
Merge branch 'kernelmanager' of git://github.com/ellisonbg/ipython into qtfrontend...
epatters -
r2757:c7455244 merge
parent child Browse files
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -0,0 +1,139 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 A base class for objects that are configurable.
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2010 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 from copy import deepcopy
24 import datetime
25 from weakref import WeakValueDictionary
26
27 from IPython.utils.importstring import import_item
28 from loader import Config
29 from IPython.utils.traitlets import HasTraits, Instance
30
31
32 #-----------------------------------------------------------------------------
33 # Helper classes for Configurables
34 #-----------------------------------------------------------------------------
35
36
37 class ConfigurableError(Exception):
38 pass
39
40
41 #-----------------------------------------------------------------------------
42 # Configurable implementation
43 #-----------------------------------------------------------------------------
44
45
46 class Configurable(HasTraits):
47
48 config = Instance(Config,(),{})
49 created = None
50
51 def __init__(self, **kwargs):
52 """Create a conigurable given a config config.
53
54 Parameters
55 ----------
56 config : Config
57 If this is empty, default values are used. If config is a
58 :class:`Config` instance, it will be used to configure the
59 instance.
60
61 Notes
62 -----
63 Subclasses of Configurable must call the :meth:`__init__` method of
64 :class:`Configurable` *before* doing anything else and using
65 :func:`super`::
66
67 class MyConfigurable(Configurable):
68 def __init__(self, config=None):
69 super(MyConfigurable, self).__init__(config)
70 # Then any other code you need to finish initialization.
71
72 This ensures that instances will be configured properly.
73 """
74 config = kwargs.pop('config', None)
75 if config is not None:
76 # We used to deepcopy, but for now we are trying to just save
77 # by reference. This *could* have side effects as all components
78 # will share config. In fact, I did find such a side effect in
79 # _config_changed below. If a config attribute value was a mutable type
80 # all instances of a component were getting the same copy, effectively
81 # making that a class attribute.
82 # self.config = deepcopy(config)
83 self.config = config
84 # This should go second so individual keyword arguments override
85 # the values in config.
86 super(Configurable, self).__init__(**kwargs)
87 self.created = datetime.datetime.now()
88
89 #-------------------------------------------------------------------------
90 # Static trait notifiations
91 #-------------------------------------------------------------------------
92
93 def _config_changed(self, name, old, new):
94 """Update all the class traits having ``config=True`` as metadata.
95
96 For any class trait with a ``config`` metadata attribute that is
97 ``True``, we update the trait with the value of the corresponding
98 config entry.
99 """
100 # Get all traits with a config metadata entry that is True
101 traits = self.traits(config=True)
102
103 # We auto-load config section for this class as well as any parent
104 # classes that are Configurable subclasses. This starts with Configurable
105 # and works down the mro loading the config for each section.
106 section_names = [cls.__name__ for cls in \
107 reversed(self.__class__.__mro__) if
108 issubclass(cls, Configurable) and issubclass(self.__class__, cls)]
109
110 for sname in section_names:
111 # Don't do a blind getattr as that would cause the config to
112 # dynamically create the section with name self.__class__.__name__.
113 if new._has_section(sname):
114 my_config = new[sname]
115 for k, v in traits.items():
116 # Don't allow traitlets with config=True to start with
117 # uppercase. Otherwise, they are confused with Config
118 # subsections. But, developers shouldn't have uppercase
119 # attributes anyways! (PEP 6)
120 if k[0].upper()==k[0] and not k.startswith('_'):
121 raise ConfigurableError('Configurable traitlets with '
122 'config=True must start with a lowercase so they are '
123 'not confused with Config subsections: %s.%s' % \
124 (self.__class__.__name__, k))
125 try:
126 # Here we grab the value from the config
127 # If k has the naming convention of a config
128 # section, it will be auto created.
129 config_value = my_config[k]
130 except KeyError:
131 pass
132 else:
133 # print "Setting %s.%s from %s.%s=%r" % \
134 # (self.__class__.__name__,k,sname,k,config_value)
135 # We have to do a deepcopy here if we don't deepcopy the entire
136 # config object. If we don't, a mutable config_value will be
137 # shared by all instances, effectively making it a class attribute.
138 setattr(self, k, deepcopy(config_value))
139
@@ -0,0 +1,124 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Tests for IPython.config.configurable
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez (design help)
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2010 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 from unittest import TestCase
24
25 from IPython.config.configurable import Configurable, ConfigurableError
26 from IPython.utils.traitlets import (
27 TraitError, Int, Float, Str
28 )
29 from IPython.config.loader import Config
30
31
32 #-----------------------------------------------------------------------------
33 # Test cases
34 #-----------------------------------------------------------------------------
35
36
37 class MyConfigurable(Configurable):
38 a = Int(1, config=True)
39 b = Float(1.0, config=True)
40 c = Str('no config')
41
42
43 class Foo(Configurable):
44 a = Int(0, config=True)
45 b = Str('nope', config=True)
46
47
48 class Bar(Foo):
49 b = Str('gotit', config=False)
50 c = Float(config=True)
51
52
53 class TestConfigurableConfig(TestCase):
54
55 def test_default(self):
56 c1 = Configurable()
57 c2 = Configurable(config=c1.config)
58 c3 = Configurable(config=c2.config)
59 self.assertEquals(c1.config, c2.config)
60 self.assertEquals(c2.config, c3.config)
61
62 def test_custom(self):
63 config = Config()
64 config.foo = 'foo'
65 config.bar = 'bar'
66 c1 = Configurable(config=config)
67 c2 = Configurable(config=c1.config)
68 c3 = Configurable(config=c2.config)
69 self.assertEquals(c1.config, config)
70 self.assertEquals(c2.config, config)
71 self.assertEquals(c3.config, config)
72 # Test that copies are not made
73 self.assert_(c1.config is config)
74 self.assert_(c2.config is config)
75 self.assert_(c3.config is config)
76 self.assert_(c1.config is c2.config)
77 self.assert_(c2.config is c3.config)
78
79 def test_inheritance(self):
80 config = Config()
81 config.MyConfigurable.a = 2
82 config.MyConfigurable.b = 2.0
83 c1 = MyConfigurable(config=config)
84 c2 = MyConfigurable(config=c1.config)
85 self.assertEquals(c1.a, config.MyConfigurable.a)
86 self.assertEquals(c1.b, config.MyConfigurable.b)
87 self.assertEquals(c2.a, config.MyConfigurable.a)
88 self.assertEquals(c2.b, config.MyConfigurable.b)
89
90 def test_parent(self):
91 config = Config()
92 config.Foo.a = 10
93 config.Foo.b = "wow"
94 config.Bar.b = 'later'
95 config.Bar.c = 100.0
96 f = Foo(config=config)
97 b = Bar(config=f.config)
98 self.assertEquals(f.a, 10)
99 self.assertEquals(f.b, 'wow')
100 self.assertEquals(b.b, 'gotit')
101 self.assertEquals(b.c, 100.0)
102
103 def test_override1(self):
104 config = Config()
105 config.MyConfigurable.a = 2
106 config.MyConfigurable.b = 2.0
107 c = MyConfigurable(a=3, config=config)
108 self.assertEquals(c.a, 3)
109 self.assertEquals(c.b, config.MyConfigurable.b)
110 self.assertEquals(c.c, 'no config')
111
112 def test_override2(self):
113 config = Config()
114 config.Foo.a = 1
115 config.Bar.b = 'or' # Up above b is config=False, so this won't do it.
116 config.Bar.c = 10.0
117 c = Bar(config=config)
118 self.assertEquals(c.a, config.Foo.a)
119 self.assertEquals(c.b, 'gotit')
120 self.assertEquals(c.c, config.Bar.c)
121 c = Bar(a=2, b='and', c=20.0, config=config)
122 self.assertEquals(c.a, 2)
123 self.assertEquals(c.b, 'and')
124 self.assertEquals(c.c, 20.0)
@@ -0,0 +1,125 b''
1 # encoding: utf-8
2 """A class for managing IPython extensions.
3
4 Authors:
5
6 * Brian Granger
7 """
8
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2010 The IPython Development Team
11 #
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
19
20 import os
21 import sys
22
23 from IPython.config.configurable import Configurable
24 from IPython.utils.traitlets import Instance
25
26 #-----------------------------------------------------------------------------
27 # Main class
28 #-----------------------------------------------------------------------------
29
30 class ExtensionManager(Configurable):
31 """A class to manage IPython extensions.
32
33 An IPython extension is an importable Python module that has
34 a function with the signature::
35
36 def load_ipython_extension(ipython):
37 # Do things with ipython
38
39 This function is called after your extension is imported and the
40 currently active :class:`InteractiveShell` instance is passed as
41 the only argument. You can do anything you want with IPython at
42 that point, including defining new magic and aliases, adding new
43 components, etc.
44
45 The :func:`load_ipython_extension` will be called again is you
46 load or reload the extension again. It is up to the extension
47 author to add code to manage that.
48
49 You can put your extension modules anywhere you want, as long as
50 they can be imported by Python's standard import mechanism. However,
51 to make it easy to write extensions, you can also put your extensions
52 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
53 is added to ``sys.path`` automatically.
54 """
55
56 shell = Instance('IPython.core.iplib.InteractiveShellABC')
57
58 def __init__(self, shell=None, config=None):
59 super(ExtensionManager, self).__init__(shell=shell, config=config)
60 self.shell.on_trait_change(
61 self._on_ipython_dir_changed, 'ipython_dir'
62 )
63
64 def __del__(self):
65 self.shell.on_trait_change(
66 self._on_ipython_dir_changed, 'ipython_dir', remove=True
67 )
68
69 @property
70 def ipython_extension_dir(self):
71 return os.path.join(self.shell.ipython_dir, u'extensions')
72
73 def _on_ipython_dir_changed(self):
74 if not os.path.isdir(self.ipython_extension_dir):
75 os.makedirs(self.ipython_extension_dir, mode = 0777)
76
77 def load_extension(self, module_str):
78 """Load an IPython extension by its module name.
79
80 If :func:`load_ipython_extension` returns anything, this function
81 will return that object.
82 """
83 from IPython.utils.syspathcontext import prepended_to_syspath
84
85 if module_str not in sys.modules:
86 with prepended_to_syspath(self.ipython_extension_dir):
87 __import__(module_str)
88 mod = sys.modules[module_str]
89 return self._call_load_ipython_extension(mod)
90
91 def unload_extension(self, module_str):
92 """Unload an IPython extension by its module name.
93
94 This function looks up the extension's name in ``sys.modules`` and
95 simply calls ``mod.unload_ipython_extension(self)``.
96 """
97 if module_str in sys.modules:
98 mod = sys.modules[module_str]
99 self._call_unload_ipython_extension(mod)
100
101 def reload_extension(self, module_str):
102 """Reload an IPython extension by calling reload.
103
104 If the module has not been loaded before,
105 :meth:`InteractiveShell.load_extension` is called. Otherwise
106 :func:`reload` is called and then the :func:`load_ipython_extension`
107 function of the module, if it exists is called.
108 """
109 from IPython.utils.syspathcontext import prepended_to_syspath
110
111 with prepended_to_syspath(self.ipython_extension_dir):
112 if module_str in sys.modules:
113 mod = sys.modules[module_str]
114 reload(mod)
115 self._call_load_ipython_extension(mod)
116 else:
117 self.load_extension(module_str)
118
119 def _call_load_ipython_extension(self, mod):
120 if hasattr(mod, 'load_ipython_extension'):
121 return mod.load_ipython_extension(self.shell)
122
123 def _call_unload_ipython_extension(self, mod):
124 if hasattr(mod, 'unload_ipython_extension'):
125 return mod.unload_ipython_extension(self.shell)
@@ -0,0 +1,51 b''
1 # encoding: utf-8
2 """IPython plugins.
3
4 Authors:
5
6 * Brian Granger
7 """
8
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2010 The IPython Development Team
11 #
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
19
20 from IPython.config.configurable import Configurable
21 from IPython.utils.traitlets import Dict
22
23 #-----------------------------------------------------------------------------
24 # Main class
25 #-----------------------------------------------------------------------------
26
27 class PluginManager(Configurable):
28 """A manager for IPython plugins."""
29
30 plugins = Dict({})
31
32 def __init__(self, config=None):
33 super(PluginManager, self).__init__(config=config)
34
35 def register_plugin(self, name, plugin):
36 if not isinstance(plugin, Plugin):
37 raise TypeError('Expected Plugin, got: %r' % plugin)
38 if self.plugins.has_key(name):
39 raise KeyError('Plugin with name already exists: %r' % name)
40 self.plugins[name] = plugin
41
42 def unregister_plugin(self, name):
43 del self.plugins[name]
44
45 def get_plugin(self, name, default=None):
46 return self.plugins.get(name, default)
47
48
49 class Plugin(Configurable):
50 """Base class for IPython plugins."""
51 pass
@@ -0,0 +1,46 b''
1 """Tests for plugin.py"""
2
3 #-----------------------------------------------------------------------------
4 # Imports
5 #-----------------------------------------------------------------------------
6
7 from unittest import TestCase
8
9 from IPython.core.plugin import Plugin, PluginManager
10
11 #-----------------------------------------------------------------------------
12 # Tests
13 #-----------------------------------------------------------------------------
14
15 class FooPlugin(Plugin):
16 pass
17
18
19 class BarPlugin(Plugin):
20 pass
21
22
23 class BadPlugin(object):
24 pass
25
26
27 class PluginTest(TestCase):
28
29 def setUp(self):
30 self.manager = PluginManager()
31
32 def test_register_get(self):
33 self.assertEquals(None, self.manager.get_plugin('foo'))
34 foo = FooPlugin()
35 self.manager.register_plugin('foo', foo)
36 self.assertEquals(foo, self.manager.get_plugin('foo'))
37 bar = BarPlugin()
38 self.assertRaises(KeyError, self.manager.register_plugin, 'foo', bar)
39 bad = BadPlugin()
40 self.assertRaises(TypeError, self.manager.register_plugin, 'bad')
41
42 def test_unregister(self):
43 foo = FooPlugin()
44 self.manager.register_plugin('foo', foo)
45 self.manager.unregister_plugin('foo')
46 self.assertEquals(None, self.manager.get_plugin('foo'))
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,66 +1,66 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 IPython.
4 IPython.
5
5
6 IPython is a set of tools for interactive and exploratory computing in Python.
6 IPython is a set of tools for interactive and exploratory computing in Python.
7 """
7 """
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2009 The IPython Development Team
9 # Copyright (C) 2008-2009 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 from __future__ import absolute_import
18 from __future__ import absolute_import
19
19
20 import os
20 import os
21 import sys
21 import sys
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Setup everything
24 # Setup everything
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 if sys.version[0:3] < '2.5':
27 if sys.version[0:3] < '2.6':
28 raise ImportError('Python Version 2.5 or above is required for IPython.')
28 raise ImportError('Python Version 2.6 or above is required for IPython.')
29
29
30
30
31 # Make it easy to import extensions - they are always directly on pythonpath.
31 # Make it easy to import extensions - they are always directly on pythonpath.
32 # Therefore, non-IPython modules can be added to extensions directory.
32 # Therefore, non-IPython modules can be added to extensions directory.
33 # This should probably be in ipapp.py.
33 # This should probably be in ipapp.py.
34 sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
34 sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
35
35
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37 # Setup the top level names
37 # Setup the top level names
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39
39
40 from .config.loader import Config
40 from .config.loader import Config
41 from .core import release
41 from .core import release
42 from .core.application import Application
42 from .core.application import Application
43 from .core.ipapp import IPythonApp
43 from .core.ipapp import IPythonApp
44 from .core.embed import embed
44 from .core.embed import embed
45 from .core.error import TryNext
45 from .core.error import TryNext
46 from .core.iplib import InteractiveShell
46 from .core.iplib import InteractiveShell
47 from .testing import test
47 from .testing import test
48
48
49 from .lib import (
49 from .lib import (
50 enable_wx, disable_wx,
50 enable_wx, disable_wx,
51 enable_gtk, disable_gtk,
51 enable_gtk, disable_gtk,
52 enable_qt4, disable_qt4,
52 enable_qt4, disable_qt4,
53 enable_tk, disable_tk,
53 enable_tk, disable_tk,
54 set_inputhook, clear_inputhook,
54 set_inputhook, clear_inputhook,
55 current_gui, spin,
55 current_gui, spin,
56 appstart_qt4, appstart_wx,
56 appstart_qt4, appstart_wx,
57 appstart_gtk, appstart_tk
57 appstart_gtk, appstart_tk
58 )
58 )
59
59
60 # Release data
60 # Release data
61 __author__ = ''
61 __author__ = ''
62 for author, email in release.authors.values():
62 for author, email in release.authors.values():
63 __author__ += author + ' <' + email + '>\n'
63 __author__ += author + ' <' + email + '>\n'
64 __license__ = release.license
64 __license__ = release.license
65 __version__ = release.version
65 __version__ = release.version
66 __revision__ = release.revision
66 __revision__ = release.revision
@@ -1,262 +1,258 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 IPython's alias component
4 System command aliases.
5
5
6 Authors:
6 Authors:
7
7
8 * Fernando Perez
8 * Brian Granger
9 * Brian Granger
9 """
10 """
10
11
11 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
13 #
14 #
14 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
17
18
18 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
19 # Imports
20 # Imports
20 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
21
22
22 import __builtin__
23 import __builtin__
23 import keyword
24 import keyword
24 import os
25 import os
25 import re
26 import re
26 import sys
27 import sys
27
28
28 from IPython.core.component import Component
29 from IPython.config.configurable import Configurable
29 from IPython.core.splitinput import split_user_input
30 from IPython.core.splitinput import split_user_input
30
31
31 from IPython.utils.traitlets import List
32 from IPython.utils.traitlets import List, Instance
32 from IPython.utils.autoattr import auto_attr
33 from IPython.utils.autoattr import auto_attr
33 from IPython.utils.warn import warn, error
34 from IPython.utils.warn import warn, error
34
35
35 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
36 # Utilities
37 # Utilities
37 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
38
39
39 # This is used as the pattern for calls to split_user_input.
40 # This is used as the pattern for calls to split_user_input.
40 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
41 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
41
42
42 def default_aliases():
43 def default_aliases():
43 # Make some aliases automatically
44 # Make some aliases automatically
44 # Prepare list of shell aliases to auto-define
45 # Prepare list of shell aliases to auto-define
45 if os.name == 'posix':
46 if os.name == 'posix':
46 default_aliases = ('mkdir mkdir', 'rmdir rmdir',
47 default_aliases = ('mkdir mkdir', 'rmdir rmdir',
47 'mv mv -i','rm rm -i','cp cp -i',
48 'mv mv -i','rm rm -i','cp cp -i',
48 'cat cat','less less','clear clear',
49 'cat cat','less less','clear clear',
49 # a better ls
50 # a better ls
50 'ls ls -F',
51 'ls ls -F',
51 # long ls
52 # long ls
52 'll ls -lF')
53 'll ls -lF')
53 # Extra ls aliases with color, which need special treatment on BSD
54 # Extra ls aliases with color, which need special treatment on BSD
54 # variants
55 # variants
55 ls_extra = ( # color ls
56 ls_extra = ( # color ls
56 'lc ls -F -o --color',
57 'lc ls -F -o --color',
57 # ls normal files only
58 # ls normal files only
58 'lf ls -F -o --color %l | grep ^-',
59 'lf ls -F -o --color %l | grep ^-',
59 # ls symbolic links
60 # ls symbolic links
60 'lk ls -F -o --color %l | grep ^l',
61 'lk ls -F -o --color %l | grep ^l',
61 # directories or links to directories,
62 # directories or links to directories,
62 'ldir ls -F -o --color %l | grep /$',
63 'ldir ls -F -o --color %l | grep /$',
63 # things which are executable
64 # things which are executable
64 'lx ls -F -o --color %l | grep ^-..x',
65 'lx ls -F -o --color %l | grep ^-..x',
65 )
66 )
66 # The BSDs don't ship GNU ls, so they don't understand the
67 # The BSDs don't ship GNU ls, so they don't understand the
67 # --color switch out of the box
68 # --color switch out of the box
68 if 'bsd' in sys.platform:
69 if 'bsd' in sys.platform:
69 ls_extra = ( # ls normal files only
70 ls_extra = ( # ls normal files only
70 'lf ls -lF | grep ^-',
71 'lf ls -lF | grep ^-',
71 # ls symbolic links
72 # ls symbolic links
72 'lk ls -lF | grep ^l',
73 'lk ls -lF | grep ^l',
73 # directories or links to directories,
74 # directories or links to directories,
74 'ldir ls -lF | grep /$',
75 'ldir ls -lF | grep /$',
75 # things which are executable
76 # things which are executable
76 'lx ls -lF | grep ^-..x',
77 'lx ls -lF | grep ^-..x',
77 )
78 )
78 default_aliases = default_aliases + ls_extra
79 default_aliases = default_aliases + ls_extra
79 elif os.name in ['nt','dos']:
80 elif os.name in ['nt','dos']:
80 default_aliases = ('ls dir /on',
81 default_aliases = ('ls dir /on',
81 'ddir dir /ad /on', 'ldir dir /ad /on',
82 'ddir dir /ad /on', 'ldir dir /ad /on',
82 'mkdir mkdir','rmdir rmdir','echo echo',
83 'mkdir mkdir','rmdir rmdir','echo echo',
83 'ren ren','cls cls','copy copy')
84 'ren ren','cls cls','copy copy')
84 else:
85 else:
85 default_aliases = ()
86 default_aliases = ()
86 return [s.split(None,1) for s in default_aliases]
87 return [s.split(None,1) for s in default_aliases]
87
88
88
89
89 class AliasError(Exception):
90 class AliasError(Exception):
90 pass
91 pass
91
92
92
93
93 class InvalidAliasError(AliasError):
94 class InvalidAliasError(AliasError):
94 pass
95 pass
95
96
96
97
97 #-----------------------------------------------------------------------------
98 #-----------------------------------------------------------------------------
98 # Main AliasManager class
99 # Main AliasManager class
99 #-----------------------------------------------------------------------------
100 #-----------------------------------------------------------------------------
100
101
101
102
102 class AliasManager(Component):
103 class AliasManager(Configurable):
103
104
104 default_aliases = List(default_aliases(), config=True)
105 default_aliases = List(default_aliases(), config=True)
105 user_aliases = List(default_value=[], config=True)
106 user_aliases = List(default_value=[], config=True)
107 shell = Instance('IPython.core.iplib.InteractiveShellABC')
106
108
107 def __init__(self, parent, config=None):
109 def __init__(self, shell=None, config=None):
108 super(AliasManager, self).__init__(parent, config=config)
110 super(AliasManager, self).__init__(shell=shell, config=config)
109 self.alias_table = {}
111 self.alias_table = {}
110 self.exclude_aliases()
112 self.exclude_aliases()
111 self.init_aliases()
113 self.init_aliases()
112
114
113 @auto_attr
114 def shell(self):
115 return Component.get_instances(
116 root=self.root,
117 klass='IPython.core.iplib.InteractiveShell')[0]
118
119 def __contains__(self, name):
115 def __contains__(self, name):
120 if name in self.alias_table:
116 if name in self.alias_table:
121 return True
117 return True
122 else:
118 else:
123 return False
119 return False
124
120
125 @property
121 @property
126 def aliases(self):
122 def aliases(self):
127 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
123 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
128
124
129 def exclude_aliases(self):
125 def exclude_aliases(self):
130 # set of things NOT to alias (keywords, builtins and some magics)
126 # set of things NOT to alias (keywords, builtins and some magics)
131 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
127 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
132 no_alias.update(set(keyword.kwlist))
128 no_alias.update(set(keyword.kwlist))
133 no_alias.update(set(__builtin__.__dict__.keys()))
129 no_alias.update(set(__builtin__.__dict__.keys()))
134 self.no_alias = no_alias
130 self.no_alias = no_alias
135
131
136 def init_aliases(self):
132 def init_aliases(self):
137 # Load default aliases
133 # Load default aliases
138 for name, cmd in self.default_aliases:
134 for name, cmd in self.default_aliases:
139 self.soft_define_alias(name, cmd)
135 self.soft_define_alias(name, cmd)
140
136
141 # Load user aliases
137 # Load user aliases
142 for name, cmd in self.user_aliases:
138 for name, cmd in self.user_aliases:
143 self.soft_define_alias(name, cmd)
139 self.soft_define_alias(name, cmd)
144
140
145 def clear_aliases(self):
141 def clear_aliases(self):
146 self.alias_table.clear()
142 self.alias_table.clear()
147
143
148 def soft_define_alias(self, name, cmd):
144 def soft_define_alias(self, name, cmd):
149 """Define an alias, but don't raise on an AliasError."""
145 """Define an alias, but don't raise on an AliasError."""
150 try:
146 try:
151 self.define_alias(name, cmd)
147 self.define_alias(name, cmd)
152 except AliasError, e:
148 except AliasError, e:
153 error("Invalid alias: %s" % e)
149 error("Invalid alias: %s" % e)
154
150
155 def define_alias(self, name, cmd):
151 def define_alias(self, name, cmd):
156 """Define a new alias after validating it.
152 """Define a new alias after validating it.
157
153
158 This will raise an :exc:`AliasError` if there are validation
154 This will raise an :exc:`AliasError` if there are validation
159 problems.
155 problems.
160 """
156 """
161 nargs = self.validate_alias(name, cmd)
157 nargs = self.validate_alias(name, cmd)
162 self.alias_table[name] = (nargs, cmd)
158 self.alias_table[name] = (nargs, cmd)
163
159
164 def undefine_alias(self, name):
160 def undefine_alias(self, name):
165 if self.alias_table.has_key(name):
161 if self.alias_table.has_key(name):
166 del self.alias_table[name]
162 del self.alias_table[name]
167
163
168 def validate_alias(self, name, cmd):
164 def validate_alias(self, name, cmd):
169 """Validate an alias and return the its number of arguments."""
165 """Validate an alias and return the its number of arguments."""
170 if name in self.no_alias:
166 if name in self.no_alias:
171 raise InvalidAliasError("The name %s can't be aliased "
167 raise InvalidAliasError("The name %s can't be aliased "
172 "because it is a keyword or builtin." % name)
168 "because it is a keyword or builtin." % name)
173 if not (isinstance(cmd, basestring)):
169 if not (isinstance(cmd, basestring)):
174 raise InvalidAliasError("An alias command must be a string, "
170 raise InvalidAliasError("An alias command must be a string, "
175 "got: %r" % name)
171 "got: %r" % name)
176 nargs = cmd.count('%s')
172 nargs = cmd.count('%s')
177 if nargs>0 and cmd.find('%l')>=0:
173 if nargs>0 and cmd.find('%l')>=0:
178 raise InvalidAliasError('The %s and %l specifiers are mutually '
174 raise InvalidAliasError('The %s and %l specifiers are mutually '
179 'exclusive in alias definitions.')
175 'exclusive in alias definitions.')
180 return nargs
176 return nargs
181
177
182 def call_alias(self, alias, rest=''):
178 def call_alias(self, alias, rest=''):
183 """Call an alias given its name and the rest of the line."""
179 """Call an alias given its name and the rest of the line."""
184 cmd = self.transform_alias(alias, rest)
180 cmd = self.transform_alias(alias, rest)
185 try:
181 try:
186 self.shell.system(cmd)
182 self.shell.system(cmd)
187 except:
183 except:
188 self.shell.showtraceback()
184 self.shell.showtraceback()
189
185
190 def transform_alias(self, alias,rest=''):
186 def transform_alias(self, alias,rest=''):
191 """Transform alias to system command string."""
187 """Transform alias to system command string."""
192 nargs, cmd = self.alias_table[alias]
188 nargs, cmd = self.alias_table[alias]
193
189
194 if ' ' in cmd and os.path.isfile(cmd):
190 if ' ' in cmd and os.path.isfile(cmd):
195 cmd = '"%s"' % cmd
191 cmd = '"%s"' % cmd
196
192
197 # Expand the %l special to be the user's input line
193 # Expand the %l special to be the user's input line
198 if cmd.find('%l') >= 0:
194 if cmd.find('%l') >= 0:
199 cmd = cmd.replace('%l', rest)
195 cmd = cmd.replace('%l', rest)
200 rest = ''
196 rest = ''
201 if nargs==0:
197 if nargs==0:
202 # Simple, argument-less aliases
198 # Simple, argument-less aliases
203 cmd = '%s %s' % (cmd, rest)
199 cmd = '%s %s' % (cmd, rest)
204 else:
200 else:
205 # Handle aliases with positional arguments
201 # Handle aliases with positional arguments
206 args = rest.split(None, nargs)
202 args = rest.split(None, nargs)
207 if len(args) < nargs:
203 if len(args) < nargs:
208 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
204 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
209 (alias, nargs, len(args)))
205 (alias, nargs, len(args)))
210 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
206 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
211 return cmd
207 return cmd
212
208
213 def expand_alias(self, line):
209 def expand_alias(self, line):
214 """ Expand an alias in the command line
210 """ Expand an alias in the command line
215
211
216 Returns the provided command line, possibly with the first word
212 Returns the provided command line, possibly with the first word
217 (command) translated according to alias expansion rules.
213 (command) translated according to alias expansion rules.
218
214
219 [ipython]|16> _ip.expand_aliases("np myfile.txt")
215 [ipython]|16> _ip.expand_aliases("np myfile.txt")
220 <16> 'q:/opt/np/notepad++.exe myfile.txt'
216 <16> 'q:/opt/np/notepad++.exe myfile.txt'
221 """
217 """
222
218
223 pre,fn,rest = split_user_input(line)
219 pre,fn,rest = split_user_input(line)
224 res = pre + self.expand_aliases(fn, rest)
220 res = pre + self.expand_aliases(fn, rest)
225 return res
221 return res
226
222
227 def expand_aliases(self, fn, rest):
223 def expand_aliases(self, fn, rest):
228 """Expand multiple levels of aliases:
224 """Expand multiple levels of aliases:
229
225
230 if:
226 if:
231
227
232 alias foo bar /tmp
228 alias foo bar /tmp
233 alias baz foo
229 alias baz foo
234
230
235 then:
231 then:
236
232
237 baz huhhahhei -> bar /tmp huhhahhei
233 baz huhhahhei -> bar /tmp huhhahhei
238
234
239 """
235 """
240 line = fn + " " + rest
236 line = fn + " " + rest
241
237
242 done = set()
238 done = set()
243 while 1:
239 while 1:
244 pre,fn,rest = split_user_input(line, shell_line_split)
240 pre,fn,rest = split_user_input(line, shell_line_split)
245 if fn in self.alias_table:
241 if fn in self.alias_table:
246 if fn in done:
242 if fn in done:
247 warn("Cyclic alias definition, repeated '%s'" % fn)
243 warn("Cyclic alias definition, repeated '%s'" % fn)
248 return ""
244 return ""
249 done.add(fn)
245 done.add(fn)
250
246
251 l2 = self.transform_alias(fn, rest)
247 l2 = self.transform_alias(fn, rest)
252 if l2 == line:
248 if l2 == line:
253 break
249 break
254 # ls -> ls -F should not recurse forever
250 # ls -> ls -F should not recurse forever
255 if l2.split(None,1)[0] == line.split(None,1)[0]:
251 if l2.split(None,1)[0] == line.split(None,1)[0]:
256 line = l2
252 line = l2
257 break
253 break
258 line=l2
254 line=l2
259 else:
255 else:
260 break
256 break
261
257
262 return line
258 return line
@@ -1,453 +1,453 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 An application for IPython.
3 An application for IPython.
4
4
5 All top-level applications should use the classes in this module for
5 All top-level applications should use the classes in this module for
6 handling configuration and creating componenets.
6 handling configuration and creating componenets.
7
7
8 The job of an :class:`Application` is to create the master configuration
8 The job of an :class:`Application` is to create the master configuration
9 object and then create the components, passing the config to them.
9 object and then create the configurable objects, passing the config to them.
10
10
11 Authors:
11 Authors:
12
12
13 * Brian Granger
13 * Brian Granger
14 * Fernando Perez
14 * Fernando Perez
15
15
16 Notes
16 Notes
17 -----
17 -----
18 """
18 """
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Copyright (C) 2008-2009 The IPython Development Team
21 # Copyright (C) 2008-2009 The IPython Development Team
22 #
22 #
23 # Distributed under the terms of the BSD License. The full license is in
23 # Distributed under the terms of the BSD License. The full license is in
24 # the file COPYING, distributed as part of this software.
24 # the file COPYING, distributed as part of this software.
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Imports
28 # Imports
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 import logging
31 import logging
32 import os
32 import os
33 import sys
33 import sys
34
34
35 from IPython.core import release, crashhandler
35 from IPython.core import release, crashhandler
36 from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
36 from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
37 from IPython.config.loader import (
37 from IPython.config.loader import (
38 PyFileConfigLoader,
38 PyFileConfigLoader,
39 ArgParseConfigLoader,
39 ArgParseConfigLoader,
40 Config,
40 Config,
41 )
41 )
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Classes and functions
44 # Classes and functions
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 class ApplicationError(Exception):
47 class ApplicationError(Exception):
48 pass
48 pass
49
49
50
50
51 class BaseAppConfigLoader(ArgParseConfigLoader):
51 class BaseAppConfigLoader(ArgParseConfigLoader):
52 """Default command line options for IPython based applications."""
52 """Default command line options for IPython based applications."""
53
53
54 def _add_ipython_dir(self, parser):
54 def _add_ipython_dir(self, parser):
55 """Add the --ipython-dir option to the parser."""
55 """Add the --ipython-dir option to the parser."""
56 paa = parser.add_argument
56 paa = parser.add_argument
57 paa('--ipython-dir',
57 paa('--ipython-dir',
58 dest='Global.ipython_dir',type=unicode,
58 dest='Global.ipython_dir',type=unicode,
59 help=
59 help=
60 """Set to override default location of the IPython directory
60 """Set to override default location of the IPython directory
61 IPYTHON_DIR, stored as Global.ipython_dir. This can also be
61 IPYTHON_DIR, stored as Global.ipython_dir. This can also be
62 specified through the environment variable IPYTHON_DIR.""",
62 specified through the environment variable IPYTHON_DIR.""",
63 metavar='Global.ipython_dir')
63 metavar='Global.ipython_dir')
64
64
65 def _add_log_level(self, parser):
65 def _add_log_level(self, parser):
66 """Add the --log-level option to the parser."""
66 """Add the --log-level option to the parser."""
67 paa = parser.add_argument
67 paa = parser.add_argument
68 paa('--log-level',
68 paa('--log-level',
69 dest="Global.log_level",type=int,
69 dest="Global.log_level",type=int,
70 help='Set the log level (0,10,20,30,40,50). Default is 30.',
70 help='Set the log level (0,10,20,30,40,50). Default is 30.',
71 metavar='Global.log_level')
71 metavar='Global.log_level')
72
72
73 def _add_arguments(self):
73 def _add_arguments(self):
74 self._add_ipython_dir(self.parser)
74 self._add_ipython_dir(self.parser)
75 self._add_log_level(self.parser)
75 self._add_log_level(self.parser)
76
76
77
77
78 class Application(object):
78 class Application(object):
79 """Load a config, construct components and set them running.
79 """Load a config, construct configurables and set them running.
80
80
81 The configuration of an application can be done via three different Config
81 The configuration of an application can be done via three different Config
82 objects, which are loaded and ultimately merged into a single one used
82 objects, which are loaded and ultimately merged into a single one used
83 from that point on by the app. These are:
83 from that point on by the app. These are:
84
84
85 1. default_config: internal defaults, implemented in code.
85 1. default_config: internal defaults, implemented in code.
86 2. file_config: read from the filesystem.
86 2. file_config: read from the filesystem.
87 3. command_line_config: read from the system's command line flags.
87 3. command_line_config: read from the system's command line flags.
88
88
89 During initialization, 3 is actually read before 2, since at the
89 During initialization, 3 is actually read before 2, since at the
90 command-line one may override the location of the file to be read. But the
90 command-line one may override the location of the file to be read. But the
91 above is the order in which the merge is made.
91 above is the order in which the merge is made.
92 """
92 """
93
93
94 name = u'ipython'
94 name = u'ipython'
95 description = 'IPython: an enhanced interactive Python shell.'
95 description = 'IPython: an enhanced interactive Python shell.'
96 #: Usage message printed by argparse. If None, auto-generate
96 #: Usage message printed by argparse. If None, auto-generate
97 usage = None
97 usage = None
98 #: The command line config loader. Subclass of ArgParseConfigLoader.
98 #: The command line config loader. Subclass of ArgParseConfigLoader.
99 command_line_loader = BaseAppConfigLoader
99 command_line_loader = BaseAppConfigLoader
100 #: The name of the config file to load, determined at runtime
100 #: The name of the config file to load, determined at runtime
101 config_file_name = None
101 config_file_name = None
102 #: The name of the default config file. Track separately from the actual
102 #: The name of the default config file. Track separately from the actual
103 #: name because some logic happens only if we aren't using the default.
103 #: name because some logic happens only if we aren't using the default.
104 default_config_file_name = u'ipython_config.py'
104 default_config_file_name = u'ipython_config.py'
105 default_log_level = logging.WARN
105 default_log_level = logging.WARN
106 #: Set by --profile option
106 #: Set by --profile option
107 profile_name = None
107 profile_name = None
108 #: User's ipython directory, typically ~/.ipython/
108 #: User's ipython directory, typically ~/.ipython/
109 ipython_dir = None
109 ipython_dir = None
110 #: Internal defaults, implemented in code.
110 #: Internal defaults, implemented in code.
111 default_config = None
111 default_config = None
112 #: Read from the filesystem.
112 #: Read from the filesystem.
113 file_config = None
113 file_config = None
114 #: Read from the system's command line flags.
114 #: Read from the system's command line flags.
115 command_line_config = None
115 command_line_config = None
116 #: The final config that will be passed to the component.
116 #: The final config that will be passed to the main object.
117 master_config = None
117 master_config = None
118 #: A reference to the argv to be used (typically ends up being sys.argv[1:])
118 #: A reference to the argv to be used (typically ends up being sys.argv[1:])
119 argv = None
119 argv = None
120 #: extra arguments computed by the command-line loader
120 #: extra arguments computed by the command-line loader
121 extra_args = None
121 extra_args = None
122 #: The class to use as the crash handler.
122 #: The class to use as the crash handler.
123 crash_handler_class = crashhandler.CrashHandler
123 crash_handler_class = crashhandler.CrashHandler
124
124
125 # Private attributes
125 # Private attributes
126 _exiting = False
126 _exiting = False
127 _initialized = False
127 _initialized = False
128
128
129 def __init__(self, argv=None):
129 def __init__(self, argv=None):
130 self.argv = sys.argv[1:] if argv is None else argv
130 self.argv = sys.argv[1:] if argv is None else argv
131 self.init_logger()
131 self.init_logger()
132
132
133 def init_logger(self):
133 def init_logger(self):
134 self.log = logging.getLogger(self.__class__.__name__)
134 self.log = logging.getLogger(self.__class__.__name__)
135 # This is used as the default until the command line arguments are read.
135 # This is used as the default until the command line arguments are read.
136 self.log.setLevel(self.default_log_level)
136 self.log.setLevel(self.default_log_level)
137 self._log_handler = logging.StreamHandler()
137 self._log_handler = logging.StreamHandler()
138 self._log_formatter = logging.Formatter("[%(name)s] %(message)s")
138 self._log_formatter = logging.Formatter("[%(name)s] %(message)s")
139 self._log_handler.setFormatter(self._log_formatter)
139 self._log_handler.setFormatter(self._log_formatter)
140 self.log.addHandler(self._log_handler)
140 self.log.addHandler(self._log_handler)
141
141
142 def _set_log_level(self, level):
142 def _set_log_level(self, level):
143 self.log.setLevel(level)
143 self.log.setLevel(level)
144
144
145 def _get_log_level(self):
145 def _get_log_level(self):
146 return self.log.level
146 return self.log.level
147
147
148 log_level = property(_get_log_level, _set_log_level)
148 log_level = property(_get_log_level, _set_log_level)
149
149
150 def initialize(self):
150 def initialize(self):
151 """Initialize the application.
151 """Initialize the application.
152
152
153 Loads all configuration information and sets all application state, but
153 Loads all configuration information and sets all application state, but
154 does not start any relevant processing (typically some kind of event
154 does not start any relevant processing (typically some kind of event
155 loop).
155 loop).
156
156
157 Once this method has been called, the application is flagged as
157 Once this method has been called, the application is flagged as
158 initialized and the method becomes a no-op."""
158 initialized and the method becomes a no-op."""
159
159
160 if self._initialized:
160 if self._initialized:
161 return
161 return
162
162
163 # The first part is protected with an 'attempt' wrapper, that will log
163 # The first part is protected with an 'attempt' wrapper, that will log
164 # failures with the basic system traceback machinery. Once our crash
164 # failures with the basic system traceback machinery. Once our crash
165 # handler is in place, we can let any subsequent exception propagate,
165 # handler is in place, we can let any subsequent exception propagate,
166 # as our handler will log it with much better detail than the default.
166 # as our handler will log it with much better detail than the default.
167 self.attempt(self.create_crash_handler)
167 self.attempt(self.create_crash_handler)
168
168
169 # Configuration phase
169 # Configuration phase
170 # Default config (internally hardwired in application code)
170 # Default config (internally hardwired in application code)
171 self.create_default_config()
171 self.create_default_config()
172 self.log_default_config()
172 self.log_default_config()
173 self.set_default_config_log_level()
173 self.set_default_config_log_level()
174
174
175 # Command-line config
175 # Command-line config
176 self.pre_load_command_line_config()
176 self.pre_load_command_line_config()
177 self.load_command_line_config()
177 self.load_command_line_config()
178 self.set_command_line_config_log_level()
178 self.set_command_line_config_log_level()
179 self.post_load_command_line_config()
179 self.post_load_command_line_config()
180 self.log_command_line_config()
180 self.log_command_line_config()
181
181
182 # Find resources needed for filesystem access, using information from
182 # Find resources needed for filesystem access, using information from
183 # the above two
183 # the above two
184 self.find_ipython_dir()
184 self.find_ipython_dir()
185 self.find_resources()
185 self.find_resources()
186 self.find_config_file_name()
186 self.find_config_file_name()
187 self.find_config_file_paths()
187 self.find_config_file_paths()
188
188
189 # File-based config
189 # File-based config
190 self.pre_load_file_config()
190 self.pre_load_file_config()
191 self.load_file_config()
191 self.load_file_config()
192 self.set_file_config_log_level()
192 self.set_file_config_log_level()
193 self.post_load_file_config()
193 self.post_load_file_config()
194 self.log_file_config()
194 self.log_file_config()
195
195
196 # Merge all config objects into a single one the app can then use
196 # Merge all config objects into a single one the app can then use
197 self.merge_configs()
197 self.merge_configs()
198 self.log_master_config()
198 self.log_master_config()
199
199
200 # Construction phase
200 # Construction phase
201 self.pre_construct()
201 self.pre_construct()
202 self.construct()
202 self.construct()
203 self.post_construct()
203 self.post_construct()
204
204
205 # Done, flag as such and
205 # Done, flag as such and
206 self._initialized = True
206 self._initialized = True
207
207
208 def start(self):
208 def start(self):
209 """Start the application."""
209 """Start the application."""
210 self.initialize()
210 self.initialize()
211 self.start_app()
211 self.start_app()
212
212
213 #-------------------------------------------------------------------------
213 #-------------------------------------------------------------------------
214 # Various stages of Application creation
214 # Various stages of Application creation
215 #-------------------------------------------------------------------------
215 #-------------------------------------------------------------------------
216
216
217 def create_crash_handler(self):
217 def create_crash_handler(self):
218 """Create a crash handler, typically setting sys.excepthook to it."""
218 """Create a crash handler, typically setting sys.excepthook to it."""
219 self.crash_handler = self.crash_handler_class(self)
219 self.crash_handler = self.crash_handler_class(self)
220 sys.excepthook = self.crash_handler
220 sys.excepthook = self.crash_handler
221
221
222 def create_default_config(self):
222 def create_default_config(self):
223 """Create defaults that can't be set elsewhere.
223 """Create defaults that can't be set elsewhere.
224
224
225 For the most part, we try to set default in the class attributes
225 For the most part, we try to set default in the class attributes
226 of Components. But, defaults the top-level Application (which is
226 of Configurables. But, defaults the top-level Application (which is
227 not a HasTraits or Component) are not set in this way. Instead
227 not a HasTraits or Configurables) are not set in this way. Instead
228 we set them here. The Global section is for variables like this that
228 we set them here. The Global section is for variables like this that
229 don't belong to a particular component.
229 don't belong to a particular configurable.
230 """
230 """
231 c = Config()
231 c = Config()
232 c.Global.ipython_dir = get_ipython_dir()
232 c.Global.ipython_dir = get_ipython_dir()
233 c.Global.log_level = self.log_level
233 c.Global.log_level = self.log_level
234 self.default_config = c
234 self.default_config = c
235
235
236 def log_default_config(self):
236 def log_default_config(self):
237 self.log.debug('Default config loaded:')
237 self.log.debug('Default config loaded:')
238 self.log.debug(repr(self.default_config))
238 self.log.debug(repr(self.default_config))
239
239
240 def set_default_config_log_level(self):
240 def set_default_config_log_level(self):
241 try:
241 try:
242 self.log_level = self.default_config.Global.log_level
242 self.log_level = self.default_config.Global.log_level
243 except AttributeError:
243 except AttributeError:
244 # Fallback to the default_log_level class attribute
244 # Fallback to the default_log_level class attribute
245 pass
245 pass
246
246
247 def create_command_line_config(self):
247 def create_command_line_config(self):
248 """Create and return a command line config loader."""
248 """Create and return a command line config loader."""
249 return self.command_line_loader(
249 return self.command_line_loader(
250 self.argv,
250 self.argv,
251 description=self.description,
251 description=self.description,
252 version=release.version,
252 version=release.version,
253 usage=self.usage
253 usage=self.usage
254 )
254 )
255
255
256 def pre_load_command_line_config(self):
256 def pre_load_command_line_config(self):
257 """Do actions just before loading the command line config."""
257 """Do actions just before loading the command line config."""
258 pass
258 pass
259
259
260 def load_command_line_config(self):
260 def load_command_line_config(self):
261 """Load the command line config."""
261 """Load the command line config."""
262 loader = self.create_command_line_config()
262 loader = self.create_command_line_config()
263 self.command_line_config = loader.load_config()
263 self.command_line_config = loader.load_config()
264 self.extra_args = loader.get_extra_args()
264 self.extra_args = loader.get_extra_args()
265
265
266 def set_command_line_config_log_level(self):
266 def set_command_line_config_log_level(self):
267 try:
267 try:
268 self.log_level = self.command_line_config.Global.log_level
268 self.log_level = self.command_line_config.Global.log_level
269 except AttributeError:
269 except AttributeError:
270 pass
270 pass
271
271
272 def post_load_command_line_config(self):
272 def post_load_command_line_config(self):
273 """Do actions just after loading the command line config."""
273 """Do actions just after loading the command line config."""
274 pass
274 pass
275
275
276 def log_command_line_config(self):
276 def log_command_line_config(self):
277 self.log.debug("Command line config loaded:")
277 self.log.debug("Command line config loaded:")
278 self.log.debug(repr(self.command_line_config))
278 self.log.debug(repr(self.command_line_config))
279
279
280 def find_ipython_dir(self):
280 def find_ipython_dir(self):
281 """Set the IPython directory.
281 """Set the IPython directory.
282
282
283 This sets ``self.ipython_dir``, but the actual value that is passed to
283 This sets ``self.ipython_dir``, but the actual value that is passed to
284 the application is kept in either ``self.default_config`` or
284 the application is kept in either ``self.default_config`` or
285 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
285 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
286 ``sys.path`` so config files there can be referenced by other config
286 ``sys.path`` so config files there can be referenced by other config
287 files.
287 files.
288 """
288 """
289
289
290 try:
290 try:
291 self.ipython_dir = self.command_line_config.Global.ipython_dir
291 self.ipython_dir = self.command_line_config.Global.ipython_dir
292 except AttributeError:
292 except AttributeError:
293 self.ipython_dir = self.default_config.Global.ipython_dir
293 self.ipython_dir = self.default_config.Global.ipython_dir
294 sys.path.append(os.path.abspath(self.ipython_dir))
294 sys.path.append(os.path.abspath(self.ipython_dir))
295 if not os.path.isdir(self.ipython_dir):
295 if not os.path.isdir(self.ipython_dir):
296 os.makedirs(self.ipython_dir, mode=0777)
296 os.makedirs(self.ipython_dir, mode=0777)
297 self.log.debug("IPYTHON_DIR set to: %s" % self.ipython_dir)
297 self.log.debug("IPYTHON_DIR set to: %s" % self.ipython_dir)
298
298
299 def find_resources(self):
299 def find_resources(self):
300 """Find other resources that need to be in place.
300 """Find other resources that need to be in place.
301
301
302 Things like cluster directories need to be in place to find the
302 Things like cluster directories need to be in place to find the
303 config file. These happen right after the IPython directory has
303 config file. These happen right after the IPython directory has
304 been set.
304 been set.
305 """
305 """
306 pass
306 pass
307
307
308 def find_config_file_name(self):
308 def find_config_file_name(self):
309 """Find the config file name for this application.
309 """Find the config file name for this application.
310
310
311 This must set ``self.config_file_name`` to the filename of the
311 This must set ``self.config_file_name`` to the filename of the
312 config file to use (just the filename). The search paths for the
312 config file to use (just the filename). The search paths for the
313 config file are set in :meth:`find_config_file_paths` and then passed
313 config file are set in :meth:`find_config_file_paths` and then passed
314 to the config file loader where they are resolved to an absolute path.
314 to the config file loader where they are resolved to an absolute path.
315
315
316 If a profile has been set at the command line, this will resolve it.
316 If a profile has been set at the command line, this will resolve it.
317 """
317 """
318 try:
318 try:
319 self.config_file_name = self.command_line_config.Global.config_file
319 self.config_file_name = self.command_line_config.Global.config_file
320 except AttributeError:
320 except AttributeError:
321 pass
321 pass
322 else:
322 else:
323 return
323 return
324
324
325 try:
325 try:
326 self.profile_name = self.command_line_config.Global.profile
326 self.profile_name = self.command_line_config.Global.profile
327 except AttributeError:
327 except AttributeError:
328 # Just use the default as there is no profile
328 # Just use the default as there is no profile
329 self.config_file_name = self.default_config_file_name
329 self.config_file_name = self.default_config_file_name
330 else:
330 else:
331 # Use the default config file name and profile name if set
331 # Use the default config file name and profile name if set
332 # to determine the used config file name.
332 # to determine the used config file name.
333 name_parts = self.default_config_file_name.split('.')
333 name_parts = self.default_config_file_name.split('.')
334 name_parts.insert(1, u'_' + self.profile_name + u'.')
334 name_parts.insert(1, u'_' + self.profile_name + u'.')
335 self.config_file_name = ''.join(name_parts)
335 self.config_file_name = ''.join(name_parts)
336
336
337 def find_config_file_paths(self):
337 def find_config_file_paths(self):
338 """Set the search paths for resolving the config file.
338 """Set the search paths for resolving the config file.
339
339
340 This must set ``self.config_file_paths`` to a sequence of search
340 This must set ``self.config_file_paths`` to a sequence of search
341 paths to pass to the config file loader.
341 paths to pass to the config file loader.
342 """
342 """
343 # Include our own profiles directory last, so that users can still find
343 # Include our own profiles directory last, so that users can still find
344 # our shipped copies of builtin profiles even if they don't have them
344 # our shipped copies of builtin profiles even if they don't have them
345 # in their local ipython directory.
345 # in their local ipython directory.
346 prof_dir = os.path.join(get_ipython_package_dir(), 'config', 'profile')
346 prof_dir = os.path.join(get_ipython_package_dir(), 'config', 'profile')
347 self.config_file_paths = (os.getcwd(), self.ipython_dir, prof_dir)
347 self.config_file_paths = (os.getcwd(), self.ipython_dir, prof_dir)
348
348
349 def pre_load_file_config(self):
349 def pre_load_file_config(self):
350 """Do actions before the config file is loaded."""
350 """Do actions before the config file is loaded."""
351 pass
351 pass
352
352
353 def load_file_config(self):
353 def load_file_config(self):
354 """Load the config file.
354 """Load the config file.
355
355
356 This tries to load the config file from disk. If successful, the
356 This tries to load the config file from disk. If successful, the
357 ``CONFIG_FILE`` config variable is set to the resolved config file
357 ``CONFIG_FILE`` config variable is set to the resolved config file
358 location. If not successful, an empty config is used.
358 location. If not successful, an empty config is used.
359 """
359 """
360 self.log.debug("Attempting to load config file: %s" %
360 self.log.debug("Attempting to load config file: %s" %
361 self.config_file_name)
361 self.config_file_name)
362 loader = PyFileConfigLoader(self.config_file_name,
362 loader = PyFileConfigLoader(self.config_file_name,
363 path=self.config_file_paths)
363 path=self.config_file_paths)
364 try:
364 try:
365 self.file_config = loader.load_config()
365 self.file_config = loader.load_config()
366 self.file_config.Global.config_file = loader.full_filename
366 self.file_config.Global.config_file = loader.full_filename
367 except IOError:
367 except IOError:
368 # Only warn if the default config file was NOT being used.
368 # Only warn if the default config file was NOT being used.
369 if not self.config_file_name==self.default_config_file_name:
369 if not self.config_file_name==self.default_config_file_name:
370 self.log.warn("Config file not found, skipping: %s" %
370 self.log.warn("Config file not found, skipping: %s" %
371 self.config_file_name, exc_info=True)
371 self.config_file_name, exc_info=True)
372 self.file_config = Config()
372 self.file_config = Config()
373 except:
373 except:
374 self.log.warn("Error loading config file: %s" %
374 self.log.warn("Error loading config file: %s" %
375 self.config_file_name, exc_info=True)
375 self.config_file_name, exc_info=True)
376 self.file_config = Config()
376 self.file_config = Config()
377
377
378 def set_file_config_log_level(self):
378 def set_file_config_log_level(self):
379 # We need to keeep self.log_level updated. But we only use the value
379 # We need to keeep self.log_level updated. But we only use the value
380 # of the file_config if a value was not specified at the command
380 # of the file_config if a value was not specified at the command
381 # line, because the command line overrides everything.
381 # line, because the command line overrides everything.
382 if not hasattr(self.command_line_config.Global, 'log_level'):
382 if not hasattr(self.command_line_config.Global, 'log_level'):
383 try:
383 try:
384 self.log_level = self.file_config.Global.log_level
384 self.log_level = self.file_config.Global.log_level
385 except AttributeError:
385 except AttributeError:
386 pass # Use existing value
386 pass # Use existing value
387
387
388 def post_load_file_config(self):
388 def post_load_file_config(self):
389 """Do actions after the config file is loaded."""
389 """Do actions after the config file is loaded."""
390 pass
390 pass
391
391
392 def log_file_config(self):
392 def log_file_config(self):
393 if hasattr(self.file_config.Global, 'config_file'):
393 if hasattr(self.file_config.Global, 'config_file'):
394 self.log.debug("Config file loaded: %s" %
394 self.log.debug("Config file loaded: %s" %
395 self.file_config.Global.config_file)
395 self.file_config.Global.config_file)
396 self.log.debug(repr(self.file_config))
396 self.log.debug(repr(self.file_config))
397
397
398 def merge_configs(self):
398 def merge_configs(self):
399 """Merge the default, command line and file config objects."""
399 """Merge the default, command line and file config objects."""
400 config = Config()
400 config = Config()
401 config._merge(self.default_config)
401 config._merge(self.default_config)
402 config._merge(self.file_config)
402 config._merge(self.file_config)
403 config._merge(self.command_line_config)
403 config._merge(self.command_line_config)
404
404
405 # XXX fperez - propose to Brian we rename master_config to simply
405 # XXX fperez - propose to Brian we rename master_config to simply
406 # config, I think this is going to be heavily used in examples and
406 # config, I think this is going to be heavily used in examples and
407 # application code and the name is shorter/easier to find/remember.
407 # application code and the name is shorter/easier to find/remember.
408 # For now, just alias it...
408 # For now, just alias it...
409 self.master_config = config
409 self.master_config = config
410 self.config = config
410 self.config = config
411
411
412 def log_master_config(self):
412 def log_master_config(self):
413 self.log.debug("Master config created:")
413 self.log.debug("Master config created:")
414 self.log.debug(repr(self.master_config))
414 self.log.debug(repr(self.master_config))
415
415
416 def pre_construct(self):
416 def pre_construct(self):
417 """Do actions after the config has been built, but before construct."""
417 """Do actions after the config has been built, but before construct."""
418 pass
418 pass
419
419
420 def construct(self):
420 def construct(self):
421 """Construct the main components that make up this app."""
421 """Construct the main objects that make up this app."""
422 self.log.debug("Constructing components for application")
422 self.log.debug("Constructing main objects for application")
423
423
424 def post_construct(self):
424 def post_construct(self):
425 """Do actions after construct, but before starting the app."""
425 """Do actions after construct, but before starting the app."""
426 pass
426 pass
427
427
428 def start_app(self):
428 def start_app(self):
429 """Actually start the app."""
429 """Actually start the app."""
430 self.log.debug("Starting application")
430 self.log.debug("Starting application")
431
431
432 #-------------------------------------------------------------------------
432 #-------------------------------------------------------------------------
433 # Utility methods
433 # Utility methods
434 #-------------------------------------------------------------------------
434 #-------------------------------------------------------------------------
435
435
436 def exit(self, exit_status=0):
436 def exit(self, exit_status=0):
437 if self._exiting:
437 if self._exiting:
438 pass
438 pass
439 else:
439 else:
440 self.log.debug("Exiting application: %s" % self.name)
440 self.log.debug("Exiting application: %s" % self.name)
441 self._exiting = True
441 self._exiting = True
442 sys.exit(exit_status)
442 sys.exit(exit_status)
443
443
444 def attempt(self, func):
444 def attempt(self, func):
445 try:
445 try:
446 func()
446 func()
447 except SystemExit:
447 except SystemExit:
448 raise
448 raise
449 except:
449 except:
450 self.log.critical("Aborting application: %s" % self.name,
450 self.log.critical("Aborting application: %s" % self.name,
451 exc_info=True)
451 exc_info=True)
452 self.exit(0)
452 self.exit(0)
453
453
@@ -1,118 +1,115 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 A context manager for managing things injected into :mod:`__builtin__`.
4 A context manager for managing things injected into :mod:`__builtin__`.
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
12 # Copyright (C) 2008-2009 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import __builtin__
22 import __builtin__
23
23
24 from IPython.core.component import Component
24 from IPython.config.configurable import Configurable
25 from IPython.core.quitter import Quitter
25 from IPython.core.quitter import Quitter
26
26
27 from IPython.utils.autoattr import auto_attr
27 from IPython.utils.traitlets import Instance
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Classes and functions
30 # Classes and functions
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33
33
34 class __BuiltinUndefined(object): pass
34 class __BuiltinUndefined(object): pass
35 BuiltinUndefined = __BuiltinUndefined()
35 BuiltinUndefined = __BuiltinUndefined()
36
36
37
37
38 class BuiltinTrap(Component):
38 class BuiltinTrap(Configurable):
39
39
40 def __init__(self, parent):
40 shell = Instance('IPython.core.iplib.InteractiveShellABC')
41 super(BuiltinTrap, self).__init__(parent, None, None)
41
42 def __init__(self, shell=None):
43 super(BuiltinTrap, self).__init__(shell=shell, config=None)
42 self._orig_builtins = {}
44 self._orig_builtins = {}
43 # We define this to track if a single BuiltinTrap is nested.
45 # We define this to track if a single BuiltinTrap is nested.
44 # Only turn off the trap when the outermost call to __exit__ is made.
46 # Only turn off the trap when the outermost call to __exit__ is made.
45 self._nested_level = 0
47 self._nested_level = 0
46
48 self.shell = shell
47 @auto_attr
48 def shell(self):
49 return Component.get_instances(
50 root=self.root,
51 klass='IPython.core.iplib.InteractiveShell')[0]
52
49
53 def __enter__(self):
50 def __enter__(self):
54 if self._nested_level == 0:
51 if self._nested_level == 0:
55 self.set()
52 self.set()
56 self._nested_level += 1
53 self._nested_level += 1
57 # I return self, so callers can use add_builtin in a with clause.
54 # I return self, so callers can use add_builtin in a with clause.
58 return self
55 return self
59
56
60 def __exit__(self, type, value, traceback):
57 def __exit__(self, type, value, traceback):
61 if self._nested_level == 1:
58 if self._nested_level == 1:
62 self.unset()
59 self.unset()
63 self._nested_level -= 1
60 self._nested_level -= 1
64 # Returning False will cause exceptions to propagate
61 # Returning False will cause exceptions to propagate
65 return False
62 return False
66
63
67 def add_builtin(self, key, value):
64 def add_builtin(self, key, value):
68 """Add a builtin and save the original."""
65 """Add a builtin and save the original."""
69 orig = __builtin__.__dict__.get(key, BuiltinUndefined)
66 orig = __builtin__.__dict__.get(key, BuiltinUndefined)
70 self._orig_builtins[key] = orig
67 self._orig_builtins[key] = orig
71 __builtin__.__dict__[key] = value
68 __builtin__.__dict__[key] = value
72
69
73 def remove_builtin(self, key):
70 def remove_builtin(self, key):
74 """Remove an added builtin and re-set the original."""
71 """Remove an added builtin and re-set the original."""
75 try:
72 try:
76 orig = self._orig_builtins.pop(key)
73 orig = self._orig_builtins.pop(key)
77 except KeyError:
74 except KeyError:
78 pass
75 pass
79 else:
76 else:
80 if orig is BuiltinUndefined:
77 if orig is BuiltinUndefined:
81 del __builtin__.__dict__[key]
78 del __builtin__.__dict__[key]
82 else:
79 else:
83 __builtin__.__dict__[key] = orig
80 __builtin__.__dict__[key] = orig
84
81
85 def set(self):
82 def set(self):
86 """Store ipython references in the __builtin__ namespace."""
83 """Store ipython references in the __builtin__ namespace."""
87 self.add_builtin('exit', Quitter(self.shell, 'exit'))
84 self.add_builtin('exit', Quitter(self.shell, 'exit'))
88 self.add_builtin('quit', Quitter(self.shell, 'quit'))
85 self.add_builtin('quit', Quitter(self.shell, 'quit'))
89 self.add_builtin('get_ipython', self.shell.get_ipython)
86 self.add_builtin('get_ipython', self.shell.get_ipython)
90
87
91 # Recursive reload function
88 # Recursive reload function
92 try:
89 try:
93 from IPython.lib import deepreload
90 from IPython.lib import deepreload
94 if self.shell.deep_reload:
91 if self.shell.deep_reload:
95 self.add_builtin('reload', deepreload.reload)
92 self.add_builtin('reload', deepreload.reload)
96 else:
93 else:
97 self.add_builtin('dreload', deepreload.reload)
94 self.add_builtin('dreload', deepreload.reload)
98 del deepreload
95 del deepreload
99 except ImportError:
96 except ImportError:
100 pass
97 pass
101
98
102 # Keep in the builtins a flag for when IPython is active. We set it
99 # Keep in the builtins a flag for when IPython is active. We set it
103 # with setdefault so that multiple nested IPythons don't clobber one
100 # with setdefault so that multiple nested IPythons don't clobber one
104 # another. Each will increase its value by one upon being activated,
101 # another. Each will increase its value by one upon being activated,
105 # which also gives us a way to determine the nesting level.
102 # which also gives us a way to determine the nesting level.
106 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
103 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
107
104
108 def unset(self):
105 def unset(self):
109 """Remove any builtins which might have been added by add_builtins, or
106 """Remove any builtins which might have been added by add_builtins, or
110 restore overwritten ones to their previous values."""
107 restore overwritten ones to their previous values."""
111 for key in self._orig_builtins.keys():
108 for key in self._orig_builtins.keys():
112 self.remove_builtin(key)
109 self.remove_builtin(key)
113 self._orig_builtins.clear()
110 self._orig_builtins.clear()
114 self._builtins_added = False
111 self._builtins_added = False
115 try:
112 try:
116 del __builtin__.__dict__['__IPYTHON__active']
113 del __builtin__.__dict__['__IPYTHON__active']
117 except KeyError:
114 except KeyError:
118 pass
115 pass
@@ -1,75 +1,71 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 A context manager for handling sys.displayhook.
4 A context manager for handling sys.displayhook.
5
5
6 Authors:
6 Authors:
7
7
8 * Robert Kern
8 * Robert Kern
9 * Brian Granger
9 * Brian Granger
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import sys
23 import sys
24
24
25 from IPython.core.component import Component
25 from IPython.config.configurable import Configurable
26 from IPython.utils.traitlets import Any
26
27
27 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
28 # Classes and functions
29 # Classes and functions
29 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
30
31
31
32
32 class DisplayTrap(Component):
33 class DisplayTrap(Configurable):
33 """Object to manage sys.displayhook.
34 """Object to manage sys.displayhook.
34
35
35 This came from IPython.core.kernel.display_hook, but is simplified
36 This came from IPython.core.kernel.display_hook, but is simplified
36 (no callbacks or formatters) until more of the core is refactored.
37 (no callbacks or formatters) until more of the core is refactored.
37 """
38 """
38
39
39 def __init__(self, parent, hook):
40 hook = Any
40 super(DisplayTrap, self).__init__(parent, None, None)
41
41 self.hook = hook
42 def __init__(self, hook=None):
43 super(DisplayTrap, self).__init__(hook=hook, config=None)
42 self.old_hook = None
44 self.old_hook = None
43 # We define this to track if a single BuiltinTrap is nested.
45 # We define this to track if a single BuiltinTrap is nested.
44 # Only turn off the trap when the outermost call to __exit__ is made.
46 # Only turn off the trap when the outermost call to __exit__ is made.
45 self._nested_level = 0
47 self._nested_level = 0
46
48
47 # @auto_attr
48 # def shell(self):
49 # return Component.get_instances(
50 # root=self.root,
51 # klass='IPython.core.iplib.InteractiveShell')[0]
52
53 def __enter__(self):
49 def __enter__(self):
54 if self._nested_level == 0:
50 if self._nested_level == 0:
55 self.set()
51 self.set()
56 self._nested_level += 1
52 self._nested_level += 1
57 return self
53 return self
58
54
59 def __exit__(self, type, value, traceback):
55 def __exit__(self, type, value, traceback):
60 if self._nested_level == 1:
56 if self._nested_level == 1:
61 self.unset()
57 self.unset()
62 self._nested_level -= 1
58 self._nested_level -= 1
63 # Returning False will cause exceptions to propagate
59 # Returning False will cause exceptions to propagate
64 return False
60 return False
65
61
66 def set(self):
62 def set(self):
67 """Set the hook."""
63 """Set the hook."""
68 if sys.displayhook is not self.hook:
64 if sys.displayhook is not self.hook:
69 self.old_hook = sys.displayhook
65 self.old_hook = sys.displayhook
70 sys.displayhook = self.hook
66 sys.displayhook = self.hook
71
67
72 def unset(self):
68 def unset(self):
73 """Unset the hook."""
69 """Unset the hook."""
74 sys.displayhook = self.old_hook
70 sys.displayhook = self.old_hook
75
71
@@ -1,36 +1,30 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 This module is *completely* deprecated and should no longer be used for
4 This module is *completely* deprecated and should no longer be used for
5 any purpose. Currently, we have a few parts of the core that have
5 any purpose. Currently, we have a few parts of the core that have
6 not been componentized and thus, still rely on this module. When everything
6 not been componentized and thus, still rely on this module. When everything
7 has been made into a component, this module will be sent to deathrow.
7 has been made into a component, this module will be sent to deathrow.
8 """
8 """
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2009 The IPython Development Team
11 # Copyright (C) 2008-2009 The IPython Development Team
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Imports
18 # Imports
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Classes and functions
22 # Classes and functions
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25
25
26 def get():
26 def get():
27 """Get the most recently created InteractiveShell instance."""
27 """Get the global InteractiveShell instance."""
28 from IPython.core.iplib import InteractiveShell
28 from IPython.core.iplib import InteractiveShell
29 insts = InteractiveShell.get_instances()
29 return InteractiveShell.instance()
30 if len(insts)==0:
30
31 return None
32 most_recent = insts[0]
33 for inst in insts[1:]:
34 if inst.created > most_recent.created:
35 most_recent = inst
36 return most_recent
@@ -1,665 +1,665 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 The :class:`~IPython.core.application.Application` object for the command
4 The :class:`~IPython.core.application.Application` object for the command
5 line :command:`ipython` program.
5 line :command:`ipython` program.
6
6
7 Authors
7 Authors
8 -------
8 -------
9
9
10 * Brian Granger
10 * Brian Granger
11 * Fernando Perez
11 * Fernando Perez
12 """
12 """
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Copyright (C) 2008-2010 The IPython Development Team
15 # Copyright (C) 2008-2010 The IPython Development Team
16 #
16 #
17 # Distributed under the terms of the BSD License. The full license is in
17 # Distributed under the terms of the BSD License. The full license is in
18 # the file COPYING, distributed as part of this software.
18 # the file COPYING, distributed as part of this software.
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Imports
22 # Imports
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 from __future__ import absolute_import
25 from __future__ import absolute_import
26
26
27 import logging
27 import logging
28 import os
28 import os
29 import sys
29 import sys
30
30
31 from IPython.core import release
31 from IPython.core import release
32 from IPython.core.crashhandler import CrashHandler
32 from IPython.core.crashhandler import CrashHandler
33 from IPython.core.application import Application, BaseAppConfigLoader
33 from IPython.core.application import Application, BaseAppConfigLoader
34 from IPython.core.iplib import InteractiveShell
34 from IPython.core.iplib import InteractiveShell
35 from IPython.config.loader import (
35 from IPython.config.loader import (
36 Config,
36 Config,
37 PyFileConfigLoader
37 PyFileConfigLoader
38 )
38 )
39 from IPython.lib import inputhook
39 from IPython.lib import inputhook
40 from IPython.utils.path import filefind, get_ipython_dir
40 from IPython.utils.path import filefind, get_ipython_dir
41 from . import usage
41 from . import usage
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Globals, utilities and helpers
44 # Globals, utilities and helpers
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 #: The default config file name for this application.
47 #: The default config file name for this application.
48 default_config_file_name = u'ipython_config.py'
48 default_config_file_name = u'ipython_config.py'
49
49
50
50
51 class IPAppConfigLoader(BaseAppConfigLoader):
51 class IPAppConfigLoader(BaseAppConfigLoader):
52
52
53 def _add_arguments(self):
53 def _add_arguments(self):
54 super(IPAppConfigLoader, self)._add_arguments()
54 super(IPAppConfigLoader, self)._add_arguments()
55 paa = self.parser.add_argument
55 paa = self.parser.add_argument
56 paa('-p',
56 paa('-p',
57 '--profile', dest='Global.profile', type=unicode,
57 '--profile', dest='Global.profile', type=unicode,
58 help=
58 help=
59 """The string name of the ipython profile to be used. Assume that your
59 """The string name of the ipython profile to be used. Assume that your
60 config file is ipython_config-<name>.py (looks in current dir first,
60 config file is ipython_config-<name>.py (looks in current dir first,
61 then in IPYTHON_DIR). This is a quick way to keep and load multiple
61 then in IPYTHON_DIR). This is a quick way to keep and load multiple
62 config files for different tasks, especially if include your basic one
62 config files for different tasks, especially if include your basic one
63 in your more specialized ones. You can keep a basic
63 in your more specialized ones. You can keep a basic
64 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
64 IPYTHON_DIR/ipython_config.py file and then have other 'profiles' which
65 include this one and load extra things for particular tasks.""",
65 include this one and load extra things for particular tasks.""",
66 metavar='Global.profile')
66 metavar='Global.profile')
67 paa('--config-file',
67 paa('--config-file',
68 dest='Global.config_file', type=unicode,
68 dest='Global.config_file', type=unicode,
69 help=
69 help=
70 """Set the config file name to override default. Normally IPython
70 """Set the config file name to override default. Normally IPython
71 loads ipython_config.py (from current directory) or
71 loads ipython_config.py (from current directory) or
72 IPYTHON_DIR/ipython_config.py. If the loading of your config file
72 IPYTHON_DIR/ipython_config.py. If the loading of your config file
73 fails, IPython starts with a bare bones configuration (no modules
73 fails, IPython starts with a bare bones configuration (no modules
74 loaded at all).""",
74 loaded at all).""",
75 metavar='Global.config_file')
75 metavar='Global.config_file')
76 paa('--autocall',
76 paa('--autocall',
77 dest='InteractiveShell.autocall', type=int,
77 dest='InteractiveShell.autocall', type=int,
78 help=
78 help=
79 """Make IPython automatically call any callable object even if you
79 """Make IPython automatically call any callable object even if you
80 didn't type explicit parentheses. For example, 'str 43' becomes
80 didn't type explicit parentheses. For example, 'str 43' becomes
81 'str(43)' automatically. The value can be '0' to disable the feature,
81 'str(43)' automatically. The value can be '0' to disable the feature,
82 '1' for 'smart' autocall, where it is not applied if there are no more
82 '1' for 'smart' autocall, where it is not applied if there are no more
83 arguments on the line, and '2' for 'full' autocall, where all callable
83 arguments on the line, and '2' for 'full' autocall, where all callable
84 objects are automatically called (even if no arguments are present).
84 objects are automatically called (even if no arguments are present).
85 The default is '1'.""",
85 The default is '1'.""",
86 metavar='InteractiveShell.autocall')
86 metavar='InteractiveShell.autocall')
87 paa('--autoindent',
87 paa('--autoindent',
88 action='store_true', dest='InteractiveShell.autoindent',
88 action='store_true', dest='InteractiveShell.autoindent',
89 help='Turn on autoindenting.')
89 help='Turn on autoindenting.')
90 paa('--no-autoindent',
90 paa('--no-autoindent',
91 action='store_false', dest='InteractiveShell.autoindent',
91 action='store_false', dest='InteractiveShell.autoindent',
92 help='Turn off autoindenting.')
92 help='Turn off autoindenting.')
93 paa('--automagic',
93 paa('--automagic',
94 action='store_true', dest='InteractiveShell.automagic',
94 action='store_true', dest='InteractiveShell.automagic',
95 help=
95 help=
96 """Turn on the auto calling of magic commands. Type %%magic at the
96 """Turn on the auto calling of magic commands. Type %%magic at the
97 IPython prompt for more information.""")
97 IPython prompt for more information.""")
98 paa('--no-automagic',
98 paa('--no-automagic',
99 action='store_false', dest='InteractiveShell.automagic',
99 action='store_false', dest='InteractiveShell.automagic',
100 help='Turn off the auto calling of magic commands.')
100 help='Turn off the auto calling of magic commands.')
101 paa('--autoedit-syntax',
101 paa('--autoedit-syntax',
102 action='store_true', dest='InteractiveShell.autoedit_syntax',
102 action='store_true', dest='InteractiveShell.autoedit_syntax',
103 help='Turn on auto editing of files with syntax errors.')
103 help='Turn on auto editing of files with syntax errors.')
104 paa('--no-autoedit-syntax',
104 paa('--no-autoedit-syntax',
105 action='store_false', dest='InteractiveShell.autoedit_syntax',
105 action='store_false', dest='InteractiveShell.autoedit_syntax',
106 help='Turn off auto editing of files with syntax errors.')
106 help='Turn off auto editing of files with syntax errors.')
107 paa('--banner',
107 paa('--banner',
108 action='store_true', dest='Global.display_banner',
108 action='store_true', dest='Global.display_banner',
109 help='Display a banner upon starting IPython.')
109 help='Display a banner upon starting IPython.')
110 paa('--no-banner',
110 paa('--no-banner',
111 action='store_false', dest='Global.display_banner',
111 action='store_false', dest='Global.display_banner',
112 help="Don't display a banner upon starting IPython.")
112 help="Don't display a banner upon starting IPython.")
113 paa('--cache-size',
113 paa('--cache-size',
114 type=int, dest='InteractiveShell.cache_size',
114 type=int, dest='InteractiveShell.cache_size',
115 help=
115 help=
116 """Set the size of the output cache. The default is 1000, you can
116 """Set the size of the output cache. The default is 1000, you can
117 change it permanently in your config file. Setting it to 0 completely
117 change it permanently in your config file. Setting it to 0 completely
118 disables the caching system, and the minimum value accepted is 20 (if
118 disables the caching system, and the minimum value accepted is 20 (if
119 you provide a value less than 20, it is reset to 0 and a warning is
119 you provide a value less than 20, it is reset to 0 and a warning is
120 issued). This limit is defined because otherwise you'll spend more
120 issued). This limit is defined because otherwise you'll spend more
121 time re-flushing a too small cache than working""",
121 time re-flushing a too small cache than working""",
122 metavar='InteractiveShell.cache_size')
122 metavar='InteractiveShell.cache_size')
123 paa('--classic',
123 paa('--classic',
124 action='store_true', dest='Global.classic',
124 action='store_true', dest='Global.classic',
125 help="Gives IPython a similar feel to the classic Python prompt.")
125 help="Gives IPython a similar feel to the classic Python prompt.")
126 paa('--colors',
126 paa('--colors',
127 type=str, dest='InteractiveShell.colors',
127 type=str, dest='InteractiveShell.colors',
128 help="Set the color scheme (NoColor, Linux, and LightBG).",
128 help="Set the color scheme (NoColor, Linux, and LightBG).",
129 metavar='InteractiveShell.colors')
129 metavar='InteractiveShell.colors')
130 paa('--color-info',
130 paa('--color-info',
131 action='store_true', dest='InteractiveShell.color_info',
131 action='store_true', dest='InteractiveShell.color_info',
132 help=
132 help=
133 """IPython can display information about objects via a set of func-
133 """IPython can display information about objects via a set of func-
134 tions, and optionally can use colors for this, syntax highlighting
134 tions, and optionally can use colors for this, syntax highlighting
135 source code and various other elements. However, because this
135 source code and various other elements. However, because this
136 information is passed through a pager (like 'less') and many pagers get
136 information is passed through a pager (like 'less') and many pagers get
137 confused with color codes, this option is off by default. You can test
137 confused with color codes, this option is off by default. You can test
138 it and turn it on permanently in your ipython_config.py file if it
138 it and turn it on permanently in your ipython_config.py file if it
139 works for you. Test it and turn it on permanently if it works with
139 works for you. Test it and turn it on permanently if it works with
140 your system. The magic function %%color_info allows you to toggle this
140 your system. The magic function %%color_info allows you to toggle this
141 inter- actively for testing.""")
141 inter- actively for testing.""")
142 paa('--no-color-info',
142 paa('--no-color-info',
143 action='store_false', dest='InteractiveShell.color_info',
143 action='store_false', dest='InteractiveShell.color_info',
144 help="Disable using colors for info related things.")
144 help="Disable using colors for info related things.")
145 paa('--confirm-exit',
145 paa('--confirm-exit',
146 action='store_true', dest='InteractiveShell.confirm_exit',
146 action='store_true', dest='InteractiveShell.confirm_exit',
147 help=
147 help=
148 """Set to confirm when you try to exit IPython with an EOF (Control-D
148 """Set to confirm when you try to exit IPython with an EOF (Control-D
149 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
149 in Unix, Control-Z/Enter in Windows). By typing 'exit', 'quit' or
150 '%%Exit', you can force a direct exit without any confirmation.""")
150 '%%Exit', you can force a direct exit without any confirmation.""")
151 paa('--no-confirm-exit',
151 paa('--no-confirm-exit',
152 action='store_false', dest='InteractiveShell.confirm_exit',
152 action='store_false', dest='InteractiveShell.confirm_exit',
153 help="Don't prompt the user when exiting.")
153 help="Don't prompt the user when exiting.")
154 paa('--deep-reload',
154 paa('--deep-reload',
155 action='store_true', dest='InteractiveShell.deep_reload',
155 action='store_true', dest='InteractiveShell.deep_reload',
156 help=
156 help=
157 """Enable deep (recursive) reloading by default. IPython can use the
157 """Enable deep (recursive) reloading by default. IPython can use the
158 deep_reload module which reloads changes in modules recursively (it
158 deep_reload module which reloads changes in modules recursively (it
159 replaces the reload() function, so you don't need to change anything to
159 replaces the reload() function, so you don't need to change anything to
160 use it). deep_reload() forces a full reload of modules whose code may
160 use it). deep_reload() forces a full reload of modules whose code may
161 have changed, which the default reload() function does not. When
161 have changed, which the default reload() function does not. When
162 deep_reload is off, IPython will use the normal reload(), but
162 deep_reload is off, IPython will use the normal reload(), but
163 deep_reload will still be available as dreload(). This fea- ture is off
163 deep_reload will still be available as dreload(). This fea- ture is off
164 by default [which means that you have both normal reload() and
164 by default [which means that you have both normal reload() and
165 dreload()].""")
165 dreload()].""")
166 paa('--no-deep-reload',
166 paa('--no-deep-reload',
167 action='store_false', dest='InteractiveShell.deep_reload',
167 action='store_false', dest='InteractiveShell.deep_reload',
168 help="Disable deep (recursive) reloading by default.")
168 help="Disable deep (recursive) reloading by default.")
169 paa('--editor',
169 paa('--editor',
170 type=str, dest='InteractiveShell.editor',
170 type=str, dest='InteractiveShell.editor',
171 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
171 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
172 metavar='InteractiveShell.editor')
172 metavar='InteractiveShell.editor')
173 paa('--log','-l',
173 paa('--log','-l',
174 action='store_true', dest='InteractiveShell.logstart',
174 action='store_true', dest='InteractiveShell.logstart',
175 help="Start logging to the default log file (./ipython_log.py).")
175 help="Start logging to the default log file (./ipython_log.py).")
176 paa('--logfile','-lf',
176 paa('--logfile','-lf',
177 type=unicode, dest='InteractiveShell.logfile',
177 type=unicode, dest='InteractiveShell.logfile',
178 help="Start logging to logfile with this name.",
178 help="Start logging to logfile with this name.",
179 metavar='InteractiveShell.logfile')
179 metavar='InteractiveShell.logfile')
180 paa('--log-append','-la',
180 paa('--log-append','-la',
181 type=unicode, dest='InteractiveShell.logappend',
181 type=unicode, dest='InteractiveShell.logappend',
182 help="Start logging to the given file in append mode.",
182 help="Start logging to the given file in append mode.",
183 metavar='InteractiveShell.logfile')
183 metavar='InteractiveShell.logfile')
184 paa('--pdb',
184 paa('--pdb',
185 action='store_true', dest='InteractiveShell.pdb',
185 action='store_true', dest='InteractiveShell.pdb',
186 help="Enable auto calling the pdb debugger after every exception.")
186 help="Enable auto calling the pdb debugger after every exception.")
187 paa('--no-pdb',
187 paa('--no-pdb',
188 action='store_false', dest='InteractiveShell.pdb',
188 action='store_false', dest='InteractiveShell.pdb',
189 help="Disable auto calling the pdb debugger after every exception.")
189 help="Disable auto calling the pdb debugger after every exception.")
190 paa('--pprint',
190 paa('--pprint',
191 action='store_true', dest='InteractiveShell.pprint',
191 action='store_true', dest='InteractiveShell.pprint',
192 help="Enable auto pretty printing of results.")
192 help="Enable auto pretty printing of results.")
193 paa('--no-pprint',
193 paa('--no-pprint',
194 action='store_false', dest='InteractiveShell.pprint',
194 action='store_false', dest='InteractiveShell.pprint',
195 help="Disable auto auto pretty printing of results.")
195 help="Disable auto auto pretty printing of results.")
196 paa('--prompt-in1','-pi1',
196 paa('--prompt-in1','-pi1',
197 type=str, dest='InteractiveShell.prompt_in1',
197 type=str, dest='InteractiveShell.prompt_in1',
198 help=
198 help=
199 """Set the main input prompt ('In [\#]: '). Note that if you are using
199 """Set the main input prompt ('In [\#]: '). Note that if you are using
200 numbered prompts, the number is represented with a '\#' in the string.
200 numbered prompts, the number is represented with a '\#' in the string.
201 Don't forget to quote strings with spaces embedded in them. Most
201 Don't forget to quote strings with spaces embedded in them. Most
202 bash-like escapes can be used to customize IPython's prompts, as well
202 bash-like escapes can be used to customize IPython's prompts, as well
203 as a few additional ones which are IPython-spe- cific. All valid
203 as a few additional ones which are IPython-spe- cific. All valid
204 prompt escapes are described in detail in the Customization section of
204 prompt escapes are described in detail in the Customization section of
205 the IPython manual.""",
205 the IPython manual.""",
206 metavar='InteractiveShell.prompt_in1')
206 metavar='InteractiveShell.prompt_in1')
207 paa('--prompt-in2','-pi2',
207 paa('--prompt-in2','-pi2',
208 type=str, dest='InteractiveShell.prompt_in2',
208 type=str, dest='InteractiveShell.prompt_in2',
209 help=
209 help=
210 """Set the secondary input prompt (' .\D.: '). Similar to the previous
210 """Set the secondary input prompt (' .\D.: '). Similar to the previous
211 option, but used for the continuation prompts. The special sequence
211 option, but used for the continuation prompts. The special sequence
212 '\D' is similar to '\#', but with all digits replaced by dots (so you
212 '\D' is similar to '\#', but with all digits replaced by dots (so you
213 can have your continuation prompt aligned with your input prompt).
213 can have your continuation prompt aligned with your input prompt).
214 Default: ' .\D.: ' (note three spaces at the start for alignment with
214 Default: ' .\D.: ' (note three spaces at the start for alignment with
215 'In [\#]')""",
215 'In [\#]')""",
216 metavar='InteractiveShell.prompt_in2')
216 metavar='InteractiveShell.prompt_in2')
217 paa('--prompt-out','-po',
217 paa('--prompt-out','-po',
218 type=str, dest='InteractiveShell.prompt_out',
218 type=str, dest='InteractiveShell.prompt_out',
219 help="Set the output prompt ('Out[\#]:')",
219 help="Set the output prompt ('Out[\#]:')",
220 metavar='InteractiveShell.prompt_out')
220 metavar='InteractiveShell.prompt_out')
221 paa('--quick',
221 paa('--quick',
222 action='store_true', dest='Global.quick',
222 action='store_true', dest='Global.quick',
223 help="Enable quick startup with no config files.")
223 help="Enable quick startup with no config files.")
224 paa('--readline',
224 paa('--readline',
225 action='store_true', dest='InteractiveShell.readline_use',
225 action='store_true', dest='InteractiveShell.readline_use',
226 help="Enable readline for command line usage.")
226 help="Enable readline for command line usage.")
227 paa('--no-readline',
227 paa('--no-readline',
228 action='store_false', dest='InteractiveShell.readline_use',
228 action='store_false', dest='InteractiveShell.readline_use',
229 help="Disable readline for command line usage.")
229 help="Disable readline for command line usage.")
230 paa('--screen-length','-sl',
230 paa('--screen-length','-sl',
231 type=int, dest='InteractiveShell.screen_length',
231 type=int, dest='InteractiveShell.screen_length',
232 help=
232 help=
233 """Number of lines of your screen, used to control printing of very
233 """Number of lines of your screen, used to control printing of very
234 long strings. Strings longer than this number of lines will be sent
234 long strings. Strings longer than this number of lines will be sent
235 through a pager instead of directly printed. The default value for
235 through a pager instead of directly printed. The default value for
236 this is 0, which means IPython will auto-detect your screen size every
236 this is 0, which means IPython will auto-detect your screen size every
237 time it needs to print certain potentially long strings (this doesn't
237 time it needs to print certain potentially long strings (this doesn't
238 change the behavior of the 'print' keyword, it's only triggered
238 change the behavior of the 'print' keyword, it's only triggered
239 internally). If for some reason this isn't working well (it needs
239 internally). If for some reason this isn't working well (it needs
240 curses support), specify it yourself. Otherwise don't change the
240 curses support), specify it yourself. Otherwise don't change the
241 default.""",
241 default.""",
242 metavar='InteractiveShell.screen_length')
242 metavar='InteractiveShell.screen_length')
243 paa('--separate-in','-si',
243 paa('--separate-in','-si',
244 type=str, dest='InteractiveShell.separate_in',
244 type=str, dest='InteractiveShell.separate_in',
245 help="Separator before input prompts. Default '\\n'.",
245 help="Separator before input prompts. Default '\\n'.",
246 metavar='InteractiveShell.separate_in')
246 metavar='InteractiveShell.separate_in')
247 paa('--separate-out','-so',
247 paa('--separate-out','-so',
248 type=str, dest='InteractiveShell.separate_out',
248 type=str, dest='InteractiveShell.separate_out',
249 help="Separator before output prompts. Default 0 (nothing).",
249 help="Separator before output prompts. Default 0 (nothing).",
250 metavar='InteractiveShell.separate_out')
250 metavar='InteractiveShell.separate_out')
251 paa('--separate-out2','-so2',
251 paa('--separate-out2','-so2',
252 type=str, dest='InteractiveShell.separate_out2',
252 type=str, dest='InteractiveShell.separate_out2',
253 help="Separator after output prompts. Default 0 (nonight).",
253 help="Separator after output prompts. Default 0 (nonight).",
254 metavar='InteractiveShell.separate_out2')
254 metavar='InteractiveShell.separate_out2')
255 paa('--no-sep',
255 paa('--no-sep',
256 action='store_true', dest='Global.nosep',
256 action='store_true', dest='Global.nosep',
257 help="Eliminate all spacing between prompts.")
257 help="Eliminate all spacing between prompts.")
258 paa('--term-title',
258 paa('--term-title',
259 action='store_true', dest='InteractiveShell.term_title',
259 action='store_true', dest='InteractiveShell.term_title',
260 help="Enable auto setting the terminal title.")
260 help="Enable auto setting the terminal title.")
261 paa('--no-term-title',
261 paa('--no-term-title',
262 action='store_false', dest='InteractiveShell.term_title',
262 action='store_false', dest='InteractiveShell.term_title',
263 help="Disable auto setting the terminal title.")
263 help="Disable auto setting the terminal title.")
264 paa('--xmode',
264 paa('--xmode',
265 type=str, dest='InteractiveShell.xmode',
265 type=str, dest='InteractiveShell.xmode',
266 help=
266 help=
267 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
267 """Exception reporting mode ('Plain','Context','Verbose'). Plain:
268 similar to python's normal traceback printing. Context: prints 5 lines
268 similar to python's normal traceback printing. Context: prints 5 lines
269 of context source code around each line in the traceback. Verbose:
269 of context source code around each line in the traceback. Verbose:
270 similar to Context, but additionally prints the variables currently
270 similar to Context, but additionally prints the variables currently
271 visible where the exception happened (shortening their strings if too
271 visible where the exception happened (shortening their strings if too
272 long). This can potentially be very slow, if you happen to have a huge
272 long). This can potentially be very slow, if you happen to have a huge
273 data structure whose string representation is complex to compute.
273 data structure whose string representation is complex to compute.
274 Your computer may appear to freeze for a while with cpu usage at 100%%.
274 Your computer may appear to freeze for a while with cpu usage at 100%%.
275 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
275 If this occurs, you can cancel the traceback with Ctrl-C (maybe hitting
276 it more than once).
276 it more than once).
277 """,
277 """,
278 metavar='InteractiveShell.xmode')
278 metavar='InteractiveShell.xmode')
279 paa('--ext',
279 paa('--ext',
280 type=str, dest='Global.extra_extension',
280 type=str, dest='Global.extra_extension',
281 help="The dotted module name of an IPython extension to load.",
281 help="The dotted module name of an IPython extension to load.",
282 metavar='Global.extra_extension')
282 metavar='Global.extra_extension')
283 paa('-c',
283 paa('-c',
284 type=str, dest='Global.code_to_run',
284 type=str, dest='Global.code_to_run',
285 help="Execute the given command string.",
285 help="Execute the given command string.",
286 metavar='Global.code_to_run')
286 metavar='Global.code_to_run')
287 paa('-i',
287 paa('-i',
288 action='store_true', dest='Global.force_interact',
288 action='store_true', dest='Global.force_interact',
289 help=
289 help=
290 "If running code from the command line, become interactive afterwards.")
290 "If running code from the command line, become interactive afterwards.")
291
291
292 # Options to start with GUI control enabled from the beginning
292 # Options to start with GUI control enabled from the beginning
293 paa('--gui',
293 paa('--gui',
294 type=str, dest='Global.gui',
294 type=str, dest='Global.gui',
295 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
295 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
296 metavar='gui-mode')
296 metavar='gui-mode')
297 paa('--pylab','-pylab',
297 paa('--pylab','-pylab',
298 type=str, dest='Global.pylab',
298 type=str, dest='Global.pylab',
299 nargs='?', const='auto', metavar='gui-mode',
299 nargs='?', const='auto', metavar='gui-mode',
300 help="Pre-load matplotlib and numpy for interactive use. "+
300 help="Pre-load matplotlib and numpy for interactive use. "+
301 "If no value is given, the gui backend is matplotlib's, else use "+
301 "If no value is given, the gui backend is matplotlib's, else use "+
302 "one of: ['tk', 'qt', 'wx', 'gtk'].")
302 "one of: ['tk', 'qt', 'wx', 'gtk'].")
303
303
304 # Legacy GUI options. Leave them in for backwards compatibility, but the
304 # Legacy GUI options. Leave them in for backwards compatibility, but the
305 # 'thread' names are really a misnomer now.
305 # 'thread' names are really a misnomer now.
306 paa('--wthread', '-wthread',
306 paa('--wthread', '-wthread',
307 action='store_true', dest='Global.wthread',
307 action='store_true', dest='Global.wthread',
308 help=
308 help=
309 """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""")
309 """Enable wxPython event loop integration. (DEPRECATED, use --gui wx)""")
310 paa('--q4thread', '--qthread', '-q4thread', '-qthread',
310 paa('--q4thread', '--qthread', '-q4thread', '-qthread',
311 action='store_true', dest='Global.q4thread',
311 action='store_true', dest='Global.q4thread',
312 help=
312 help=
313 """Enable Qt4 event loop integration. Qt3 is no longer supported.
313 """Enable Qt4 event loop integration. Qt3 is no longer supported.
314 (DEPRECATED, use --gui qt)""")
314 (DEPRECATED, use --gui qt)""")
315 paa('--gthread', '-gthread',
315 paa('--gthread', '-gthread',
316 action='store_true', dest='Global.gthread',
316 action='store_true', dest='Global.gthread',
317 help=
317 help=
318 """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""")
318 """Enable GTK event loop integration. (DEPRECATED, use --gui gtk)""")
319
319
320
320
321 #-----------------------------------------------------------------------------
321 #-----------------------------------------------------------------------------
322 # Crash handler for this application
322 # Crash handler for this application
323 #-----------------------------------------------------------------------------
323 #-----------------------------------------------------------------------------
324
324
325
325
326 _message_template = """\
326 _message_template = """\
327 Oops, $self.app_name crashed. We do our best to make it stable, but...
327 Oops, $self.app_name crashed. We do our best to make it stable, but...
328
328
329 A crash report was automatically generated with the following information:
329 A crash report was automatically generated with the following information:
330 - A verbatim copy of the crash traceback.
330 - A verbatim copy of the crash traceback.
331 - A copy of your input history during this session.
331 - A copy of your input history during this session.
332 - Data on your current $self.app_name configuration.
332 - Data on your current $self.app_name configuration.
333
333
334 It was left in the file named:
334 It was left in the file named:
335 \t'$self.crash_report_fname'
335 \t'$self.crash_report_fname'
336 If you can email this file to the developers, the information in it will help
336 If you can email this file to the developers, the information in it will help
337 them in understanding and correcting the problem.
337 them in understanding and correcting the problem.
338
338
339 You can mail it to: $self.contact_name at $self.contact_email
339 You can mail it to: $self.contact_name at $self.contact_email
340 with the subject '$self.app_name Crash Report'.
340 with the subject '$self.app_name Crash Report'.
341
341
342 If you want to do it now, the following command will work (under Unix):
342 If you want to do it now, the following command will work (under Unix):
343 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
343 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
344
344
345 To ensure accurate tracking of this issue, please file a report about it at:
345 To ensure accurate tracking of this issue, please file a report about it at:
346 $self.bug_tracker
346 $self.bug_tracker
347 """
347 """
348
348
349 class IPAppCrashHandler(CrashHandler):
349 class IPAppCrashHandler(CrashHandler):
350 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
350 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
351
351
352 message_template = _message_template
352 message_template = _message_template
353
353
354 def __init__(self, app):
354 def __init__(self, app):
355 contact_name = release.authors['Fernando'][0]
355 contact_name = release.authors['Fernando'][0]
356 contact_email = release.authors['Fernando'][1]
356 contact_email = release.authors['Fernando'][1]
357 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
357 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
358 super(IPAppCrashHandler,self).__init__(
358 super(IPAppCrashHandler,self).__init__(
359 app, contact_name, contact_email, bug_tracker
359 app, contact_name, contact_email, bug_tracker
360 )
360 )
361
361
362 def make_report(self,traceback):
362 def make_report(self,traceback):
363 """Return a string containing a crash report."""
363 """Return a string containing a crash report."""
364
364
365 sec_sep = self.section_sep
365 sec_sep = self.section_sep
366 # Start with parent report
366 # Start with parent report
367 report = [super(IPAppCrashHandler, self).make_report(traceback)]
367 report = [super(IPAppCrashHandler, self).make_report(traceback)]
368 # Add interactive-specific info we may have
368 # Add interactive-specific info we may have
369 rpt_add = report.append
369 rpt_add = report.append
370 try:
370 try:
371 rpt_add(sec_sep+"History of session input:")
371 rpt_add(sec_sep+"History of session input:")
372 for line in self.app.shell.user_ns['_ih']:
372 for line in self.app.shell.user_ns['_ih']:
373 rpt_add(line)
373 rpt_add(line)
374 rpt_add('\n*** Last line of input (may not be in above history):\n')
374 rpt_add('\n*** Last line of input (may not be in above history):\n')
375 rpt_add(self.app.shell._last_input_line+'\n')
375 rpt_add(self.app.shell._last_input_line+'\n')
376 except:
376 except:
377 pass
377 pass
378
378
379 return ''.join(report)
379 return ''.join(report)
380
380
381
381
382 #-----------------------------------------------------------------------------
382 #-----------------------------------------------------------------------------
383 # Main classes and functions
383 # Main classes and functions
384 #-----------------------------------------------------------------------------
384 #-----------------------------------------------------------------------------
385
385
386 class IPythonApp(Application):
386 class IPythonApp(Application):
387 name = u'ipython'
387 name = u'ipython'
388 #: argparse formats better the 'usage' than the 'description' field
388 #: argparse formats better the 'usage' than the 'description' field
389 description = None
389 description = None
390 usage = usage.cl_usage
390 usage = usage.cl_usage
391 command_line_loader = IPAppConfigLoader
391 command_line_loader = IPAppConfigLoader
392 default_config_file_name = default_config_file_name
392 default_config_file_name = default_config_file_name
393 crash_handler_class = IPAppCrashHandler
393 crash_handler_class = IPAppCrashHandler
394
394
395 def create_default_config(self):
395 def create_default_config(self):
396 super(IPythonApp, self).create_default_config()
396 super(IPythonApp, self).create_default_config()
397 # Eliminate multiple lookups
397 # Eliminate multiple lookups
398 Global = self.default_config.Global
398 Global = self.default_config.Global
399
399
400 # Set all default values
400 # Set all default values
401 Global.display_banner = True
401 Global.display_banner = True
402
402
403 # If the -c flag is given or a file is given to run at the cmd line
403 # If the -c flag is given or a file is given to run at the cmd line
404 # like "ipython foo.py", normally we exit without starting the main
404 # like "ipython foo.py", normally we exit without starting the main
405 # loop. The force_interact config variable allows a user to override
405 # loop. The force_interact config variable allows a user to override
406 # this and interact. It is also set by the -i cmd line flag, just
406 # this and interact. It is also set by the -i cmd line flag, just
407 # like Python.
407 # like Python.
408 Global.force_interact = False
408 Global.force_interact = False
409
409
410 # By default always interact by starting the IPython mainloop.
410 # By default always interact by starting the IPython mainloop.
411 Global.interact = True
411 Global.interact = True
412
412
413 # No GUI integration by default
413 # No GUI integration by default
414 Global.gui = False
414 Global.gui = False
415 # Pylab off by default
415 # Pylab off by default
416 Global.pylab = False
416 Global.pylab = False
417
417
418 # Deprecated versions of gui support that used threading, we support
418 # Deprecated versions of gui support that used threading, we support
419 # them just for bacwards compatibility as an alternate spelling for
419 # them just for bacwards compatibility as an alternate spelling for
420 # '--gui X'
420 # '--gui X'
421 Global.qthread = False
421 Global.qthread = False
422 Global.q4thread = False
422 Global.q4thread = False
423 Global.wthread = False
423 Global.wthread = False
424 Global.gthread = False
424 Global.gthread = False
425
425
426 def load_file_config(self):
426 def load_file_config(self):
427 if hasattr(self.command_line_config.Global, 'quick'):
427 if hasattr(self.command_line_config.Global, 'quick'):
428 if self.command_line_config.Global.quick:
428 if self.command_line_config.Global.quick:
429 self.file_config = Config()
429 self.file_config = Config()
430 return
430 return
431 super(IPythonApp, self).load_file_config()
431 super(IPythonApp, self).load_file_config()
432
432
433 def post_load_file_config(self):
433 def post_load_file_config(self):
434 if hasattr(self.command_line_config.Global, 'extra_extension'):
434 if hasattr(self.command_line_config.Global, 'extra_extension'):
435 if not hasattr(self.file_config.Global, 'extensions'):
435 if not hasattr(self.file_config.Global, 'extensions'):
436 self.file_config.Global.extensions = []
436 self.file_config.Global.extensions = []
437 self.file_config.Global.extensions.append(
437 self.file_config.Global.extensions.append(
438 self.command_line_config.Global.extra_extension)
438 self.command_line_config.Global.extra_extension)
439 del self.command_line_config.Global.extra_extension
439 del self.command_line_config.Global.extra_extension
440
440
441 def pre_construct(self):
441 def pre_construct(self):
442 config = self.master_config
442 config = self.master_config
443
443
444 if hasattr(config.Global, 'classic'):
444 if hasattr(config.Global, 'classic'):
445 if config.Global.classic:
445 if config.Global.classic:
446 config.InteractiveShell.cache_size = 0
446 config.InteractiveShell.cache_size = 0
447 config.InteractiveShell.pprint = 0
447 config.InteractiveShell.pprint = 0
448 config.InteractiveShell.prompt_in1 = '>>> '
448 config.InteractiveShell.prompt_in1 = '>>> '
449 config.InteractiveShell.prompt_in2 = '... '
449 config.InteractiveShell.prompt_in2 = '... '
450 config.InteractiveShell.prompt_out = ''
450 config.InteractiveShell.prompt_out = ''
451 config.InteractiveShell.separate_in = \
451 config.InteractiveShell.separate_in = \
452 config.InteractiveShell.separate_out = \
452 config.InteractiveShell.separate_out = \
453 config.InteractiveShell.separate_out2 = ''
453 config.InteractiveShell.separate_out2 = ''
454 config.InteractiveShell.colors = 'NoColor'
454 config.InteractiveShell.colors = 'NoColor'
455 config.InteractiveShell.xmode = 'Plain'
455 config.InteractiveShell.xmode = 'Plain'
456
456
457 if hasattr(config.Global, 'nosep'):
457 if hasattr(config.Global, 'nosep'):
458 if config.Global.nosep:
458 if config.Global.nosep:
459 config.InteractiveShell.separate_in = \
459 config.InteractiveShell.separate_in = \
460 config.InteractiveShell.separate_out = \
460 config.InteractiveShell.separate_out = \
461 config.InteractiveShell.separate_out2 = ''
461 config.InteractiveShell.separate_out2 = ''
462
462
463 # if there is code of files to run from the cmd line, don't interact
463 # if there is code of files to run from the cmd line, don't interact
464 # unless the -i flag (Global.force_interact) is true.
464 # unless the -i flag (Global.force_interact) is true.
465 code_to_run = config.Global.get('code_to_run','')
465 code_to_run = config.Global.get('code_to_run','')
466 file_to_run = False
466 file_to_run = False
467 if self.extra_args and self.extra_args[0]:
467 if self.extra_args and self.extra_args[0]:
468 file_to_run = True
468 file_to_run = True
469 if file_to_run or code_to_run:
469 if file_to_run or code_to_run:
470 if not config.Global.force_interact:
470 if not config.Global.force_interact:
471 config.Global.interact = False
471 config.Global.interact = False
472
472
473 def construct(self):
473 def construct(self):
474 # I am a little hesitant to put these into InteractiveShell itself.
474 # I am a little hesitant to put these into InteractiveShell itself.
475 # But that might be the place for them
475 # But that might be the place for them
476 sys.path.insert(0, '')
476 sys.path.insert(0, '')
477
477
478 # Create an InteractiveShell instance
478 # Create an InteractiveShell instance.
479 self.shell = InteractiveShell(None, self.master_config)
479 self.shell = InteractiveShell.instance(config=self.master_config)
480
480
481 def post_construct(self):
481 def post_construct(self):
482 """Do actions after construct, but before starting the app."""
482 """Do actions after construct, but before starting the app."""
483 config = self.master_config
483 config = self.master_config
484
484
485 # shell.display_banner should always be False for the terminal
485 # shell.display_banner should always be False for the terminal
486 # based app, because we call shell.show_banner() by hand below
486 # based app, because we call shell.show_banner() by hand below
487 # so the banner shows *before* all extension loading stuff.
487 # so the banner shows *before* all extension loading stuff.
488 self.shell.display_banner = False
488 self.shell.display_banner = False
489 if config.Global.display_banner and \
489 if config.Global.display_banner and \
490 config.Global.interact:
490 config.Global.interact:
491 self.shell.show_banner()
491 self.shell.show_banner()
492
492
493 # Make sure there is a space below the banner.
493 # Make sure there is a space below the banner.
494 if self.log_level <= logging.INFO: print
494 if self.log_level <= logging.INFO: print
495
495
496 # Now a variety of things that happen after the banner is printed.
496 # Now a variety of things that happen after the banner is printed.
497 self._enable_gui_pylab()
497 self._enable_gui_pylab()
498 self._load_extensions()
498 self._load_extensions()
499 self._run_exec_lines()
499 self._run_exec_lines()
500 self._run_exec_files()
500 self._run_exec_files()
501 self._run_cmd_line_code()
501 self._run_cmd_line_code()
502
502
503 def _enable_gui_pylab(self):
503 def _enable_gui_pylab(self):
504 """Enable GUI event loop integration, taking pylab into account."""
504 """Enable GUI event loop integration, taking pylab into account."""
505 Global = self.master_config.Global
505 Global = self.master_config.Global
506
506
507 # Select which gui to use
507 # Select which gui to use
508 if Global.gui:
508 if Global.gui:
509 gui = Global.gui
509 gui = Global.gui
510 # The following are deprecated, but there's likely to be a lot of use
510 # The following are deprecated, but there's likely to be a lot of use
511 # of this form out there, so we might as well support it for now. But
511 # of this form out there, so we might as well support it for now. But
512 # the --gui option above takes precedence.
512 # the --gui option above takes precedence.
513 elif Global.wthread:
513 elif Global.wthread:
514 gui = inputhook.GUI_WX
514 gui = inputhook.GUI_WX
515 elif Global.qthread:
515 elif Global.qthread:
516 gui = inputhook.GUI_QT
516 gui = inputhook.GUI_QT
517 elif Global.gthread:
517 elif Global.gthread:
518 gui = inputhook.GUI_GTK
518 gui = inputhook.GUI_GTK
519 else:
519 else:
520 gui = None
520 gui = None
521
521
522 # Using --pylab will also require gui activation, though which toolkit
522 # Using --pylab will also require gui activation, though which toolkit
523 # to use may be chosen automatically based on mpl configuration.
523 # to use may be chosen automatically based on mpl configuration.
524 if Global.pylab:
524 if Global.pylab:
525 activate = self.shell.enable_pylab
525 activate = self.shell.enable_pylab
526 if Global.pylab == 'auto':
526 if Global.pylab == 'auto':
527 gui = None
527 gui = None
528 else:
528 else:
529 gui = Global.pylab
529 gui = Global.pylab
530 else:
530 else:
531 # Enable only GUI integration, no pylab
531 # Enable only GUI integration, no pylab
532 activate = inputhook.enable_gui
532 activate = inputhook.enable_gui
533
533
534 if gui or Global.pylab:
534 if gui or Global.pylab:
535 try:
535 try:
536 self.log.info("Enabling GUI event loop integration, "
536 self.log.info("Enabling GUI event loop integration, "
537 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
537 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
538 activate(gui)
538 activate(gui)
539 except:
539 except:
540 self.log.warn("Error in enabling GUI event loop integration:")
540 self.log.warn("Error in enabling GUI event loop integration:")
541 self.shell.showtraceback()
541 self.shell.showtraceback()
542
542
543 def _load_extensions(self):
543 def _load_extensions(self):
544 """Load all IPython extensions in Global.extensions.
544 """Load all IPython extensions in Global.extensions.
545
545
546 This uses the :meth:`InteractiveShell.load_extensions` to load all
546 This uses the :meth:`ExtensionManager.load_extensions` to load all
547 the extensions listed in ``self.master_config.Global.extensions``.
547 the extensions listed in ``self.master_config.Global.extensions``.
548 """
548 """
549 try:
549 try:
550 if hasattr(self.master_config.Global, 'extensions'):
550 if hasattr(self.master_config.Global, 'extensions'):
551 self.log.debug("Loading IPython extensions...")
551 self.log.debug("Loading IPython extensions...")
552 extensions = self.master_config.Global.extensions
552 extensions = self.master_config.Global.extensions
553 for ext in extensions:
553 for ext in extensions:
554 try:
554 try:
555 self.log.info("Loading IPython extension: %s" % ext)
555 self.log.info("Loading IPython extension: %s" % ext)
556 self.shell.load_extension(ext)
556 self.shell.extension_manager.load_extension(ext)
557 except:
557 except:
558 self.log.warn("Error in loading extension: %s" % ext)
558 self.log.warn("Error in loading extension: %s" % ext)
559 self.shell.showtraceback()
559 self.shell.showtraceback()
560 except:
560 except:
561 self.log.warn("Unknown error in loading extensions:")
561 self.log.warn("Unknown error in loading extensions:")
562 self.shell.showtraceback()
562 self.shell.showtraceback()
563
563
564 def _run_exec_lines(self):
564 def _run_exec_lines(self):
565 """Run lines of code in Global.exec_lines in the user's namespace."""
565 """Run lines of code in Global.exec_lines in the user's namespace."""
566 try:
566 try:
567 if hasattr(self.master_config.Global, 'exec_lines'):
567 if hasattr(self.master_config.Global, 'exec_lines'):
568 self.log.debug("Running code from Global.exec_lines...")
568 self.log.debug("Running code from Global.exec_lines...")
569 exec_lines = self.master_config.Global.exec_lines
569 exec_lines = self.master_config.Global.exec_lines
570 for line in exec_lines:
570 for line in exec_lines:
571 try:
571 try:
572 self.log.info("Running code in user namespace: %s" %
572 self.log.info("Running code in user namespace: %s" %
573 line)
573 line)
574 self.shell.runlines(line)
574 self.shell.runlines(line)
575 except:
575 except:
576 self.log.warn("Error in executing line in user "
576 self.log.warn("Error in executing line in user "
577 "namespace: %s" % line)
577 "namespace: %s" % line)
578 self.shell.showtraceback()
578 self.shell.showtraceback()
579 except:
579 except:
580 self.log.warn("Unknown error in handling Global.exec_lines:")
580 self.log.warn("Unknown error in handling Global.exec_lines:")
581 self.shell.showtraceback()
581 self.shell.showtraceback()
582
582
583 def _exec_file(self, fname):
583 def _exec_file(self, fname):
584 full_filename = filefind(fname, [u'.', self.ipython_dir])
584 full_filename = filefind(fname, [u'.', self.ipython_dir])
585 if os.path.isfile(full_filename):
585 if os.path.isfile(full_filename):
586 if full_filename.endswith(u'.py'):
586 if full_filename.endswith(u'.py'):
587 self.log.info("Running file in user namespace: %s" %
587 self.log.info("Running file in user namespace: %s" %
588 full_filename)
588 full_filename)
589 # Ensure that __file__ is always defined to match Python behavior
589 # Ensure that __file__ is always defined to match Python behavior
590 self.shell.user_ns['__file__'] = fname
590 self.shell.user_ns['__file__'] = fname
591 try:
591 try:
592 self.shell.safe_execfile(full_filename, self.shell.user_ns)
592 self.shell.safe_execfile(full_filename, self.shell.user_ns)
593 finally:
593 finally:
594 del self.shell.user_ns['__file__']
594 del self.shell.user_ns['__file__']
595 elif full_filename.endswith('.ipy'):
595 elif full_filename.endswith('.ipy'):
596 self.log.info("Running file in user namespace: %s" %
596 self.log.info("Running file in user namespace: %s" %
597 full_filename)
597 full_filename)
598 self.shell.safe_execfile_ipy(full_filename)
598 self.shell.safe_execfile_ipy(full_filename)
599 else:
599 else:
600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
601 % full_filename)
601 % full_filename)
602 def _run_exec_files(self):
602 def _run_exec_files(self):
603 try:
603 try:
604 if hasattr(self.master_config.Global, 'exec_files'):
604 if hasattr(self.master_config.Global, 'exec_files'):
605 self.log.debug("Running files in Global.exec_files...")
605 self.log.debug("Running files in Global.exec_files...")
606 exec_files = self.master_config.Global.exec_files
606 exec_files = self.master_config.Global.exec_files
607 for fname in exec_files:
607 for fname in exec_files:
608 self._exec_file(fname)
608 self._exec_file(fname)
609 except:
609 except:
610 self.log.warn("Unknown error in handling Global.exec_files:")
610 self.log.warn("Unknown error in handling Global.exec_files:")
611 self.shell.showtraceback()
611 self.shell.showtraceback()
612
612
613 def _run_cmd_line_code(self):
613 def _run_cmd_line_code(self):
614 if hasattr(self.master_config.Global, 'code_to_run'):
614 if hasattr(self.master_config.Global, 'code_to_run'):
615 line = self.master_config.Global.code_to_run
615 line = self.master_config.Global.code_to_run
616 try:
616 try:
617 self.log.info("Running code given at command line (-c): %s" %
617 self.log.info("Running code given at command line (-c): %s" %
618 line)
618 line)
619 self.shell.runlines(line)
619 self.shell.runlines(line)
620 except:
620 except:
621 self.log.warn("Error in executing line in user namespace: %s" %
621 self.log.warn("Error in executing line in user namespace: %s" %
622 line)
622 line)
623 self.shell.showtraceback()
623 self.shell.showtraceback()
624 return
624 return
625 # Like Python itself, ignore the second if the first of these is present
625 # Like Python itself, ignore the second if the first of these is present
626 try:
626 try:
627 fname = self.extra_args[0]
627 fname = self.extra_args[0]
628 except:
628 except:
629 pass
629 pass
630 else:
630 else:
631 try:
631 try:
632 self._exec_file(fname)
632 self._exec_file(fname)
633 except:
633 except:
634 self.log.warn("Error in executing file in user namespace: %s" %
634 self.log.warn("Error in executing file in user namespace: %s" %
635 fname)
635 fname)
636 self.shell.showtraceback()
636 self.shell.showtraceback()
637
637
638 def start_app(self):
638 def start_app(self):
639 if self.master_config.Global.interact:
639 if self.master_config.Global.interact:
640 self.log.debug("Starting IPython's mainloop...")
640 self.log.debug("Starting IPython's mainloop...")
641 self.shell.mainloop()
641 self.shell.mainloop()
642 else:
642 else:
643 self.log.debug("IPython not interactive, start_app is no-op...")
643 self.log.debug("IPython not interactive, start_app is no-op...")
644
644
645
645
646 def load_default_config(ipython_dir=None):
646 def load_default_config(ipython_dir=None):
647 """Load the default config file from the default ipython_dir.
647 """Load the default config file from the default ipython_dir.
648
648
649 This is useful for embedded shells.
649 This is useful for embedded shells.
650 """
650 """
651 if ipython_dir is None:
651 if ipython_dir is None:
652 ipython_dir = get_ipython_dir()
652 ipython_dir = get_ipython_dir()
653 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
653 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
654 config = cl.load_config()
654 config = cl.load_config()
655 return config
655 return config
656
656
657
657
658 def launch_new_instance():
658 def launch_new_instance():
659 """Create and run a full blown IPython instance"""
659 """Create and run a full blown IPython instance"""
660 app = IPythonApp()
660 app = IPythonApp()
661 app.start()
661 app.start()
662
662
663
663
664 if __name__ == '__main__':
664 if __name__ == '__main__':
665 launch_new_instance()
665 launch_new_instance()
@@ -1,2582 +1,2523 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """Main IPython class."""
3 Main IPython Component
4 """
5
3
6 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 # Copyright (C) 2008-2009 The IPython Development Team
7 # Copyright (C) 2008-2010 The IPython Development Team
10 #
8 #
11 # 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
12 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
14
12
15 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
16 # Imports
14 # Imports
17 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
18
16
19 from __future__ import with_statement
17 from __future__ import with_statement
20 from __future__ import absolute_import
18 from __future__ import absolute_import
21
19
22 import __builtin__
20 import __builtin__
21 import abc
23 import bdb
22 import bdb
24 import codeop
23 import codeop
25 import exceptions
24 import exceptions
26 import new
25 import new
27 import os
26 import os
28 import re
27 import re
29 import string
28 import string
30 import sys
29 import sys
31 import tempfile
30 import tempfile
32 from contextlib import nested
31 from contextlib import nested
33
32
34 from IPython.core import debugger, oinspect
33 from IPython.core import debugger, oinspect
35 from IPython.core import history as ipcorehist
34 from IPython.core import history as ipcorehist
36 from IPython.core import prefilter
35 from IPython.core import prefilter
37 from IPython.core import shadowns
36 from IPython.core import shadowns
38 from IPython.core import ultratb
37 from IPython.core import ultratb
39 from IPython.core.alias import AliasManager
38 from IPython.core.alias import AliasManager
40 from IPython.core.builtin_trap import BuiltinTrap
39 from IPython.core.builtin_trap import BuiltinTrap
41 from IPython.core.component import Component
40 from IPython.config.configurable import Configurable
42 from IPython.core.display_trap import DisplayTrap
41 from IPython.core.display_trap import DisplayTrap
43 from IPython.core.error import TryNext, UsageError
42 from IPython.core.error import TryNext, UsageError
43 from IPython.core.extensions import ExtensionManager
44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
45 from IPython.core.logger import Logger
45 from IPython.core.logger import Logger
46 from IPython.core.magic import Magic
46 from IPython.core.magic import Magic
47 from IPython.core.plugin import PluginManager
47 from IPython.core.prefilter import PrefilterManager
48 from IPython.core.prefilter import PrefilterManager
48 from IPython.core.prompts import CachedOutput
49 from IPython.core.prompts import CachedOutput
49 from IPython.core.usage import interactive_usage, default_banner
50 from IPython.core.usage import interactive_usage, default_banner
50 import IPython.core.hooks
51 import IPython.core.hooks
51 from IPython.external.Itpl import ItplNS
52 from IPython.external.Itpl import ItplNS
52 from IPython.lib.inputhook import enable_gui
53 from IPython.lib.inputhook import enable_gui
53 from IPython.lib.backgroundjobs import BackgroundJobManager
54 from IPython.lib.backgroundjobs import BackgroundJobManager
54 from IPython.lib.pylabtools import pylab_activate
55 from IPython.lib.pylabtools import pylab_activate
55 from IPython.utils import PyColorize
56 from IPython.utils import PyColorize
56 from IPython.utils import pickleshare
57 from IPython.utils import pickleshare
57 from IPython.utils.doctestreload import doctest_reload
58 from IPython.utils.doctestreload import doctest_reload
58 from IPython.utils.ipstruct import Struct
59 from IPython.utils.ipstruct import Struct
59 from IPython.utils.io import Term, ask_yes_no
60 from IPython.utils.io import Term, ask_yes_no
60 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
61 from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
61 from IPython.utils.process import (
62 from IPython.utils.process import (
62 abbrev_cwd,
63 abbrev_cwd,
63 getoutput,
64 getoutput,
64 getoutputerror
65 getoutputerror
65 )
66 )
66 # import IPython.utils.rlineimpl as readline
67 # import IPython.utils.rlineimpl as readline
67 from IPython.utils.strdispatch import StrDispatch
68 from IPython.utils.strdispatch import StrDispatch
68 from IPython.utils.syspathcontext import prepended_to_syspath
69 from IPython.utils.syspathcontext import prepended_to_syspath
69 from IPython.utils.terminal import toggle_set_term_title, set_term_title
70 from IPython.utils.terminal import toggle_set_term_title, set_term_title
70 from IPython.utils.warn import warn, error, fatal
71 from IPython.utils.warn import warn, error, fatal
71 from IPython.utils.traitlets import (
72 from IPython.utils.traitlets import (
72 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
73 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode, Instance
73 )
74 )
74
75
75 # from IPython.utils import growl
76 # from IPython.utils import growl
76 # growl.start("IPython")
77 # growl.start("IPython")
77
78
78 #-----------------------------------------------------------------------------
79 #-----------------------------------------------------------------------------
79 # Globals
80 # Globals
80 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
81
82
82 # store the builtin raw_input globally, and use this always, in case user code
83 # store the builtin raw_input globally, and use this always, in case user code
83 # overwrites it (like wx.py.PyShell does)
84 # overwrites it (like wx.py.PyShell does)
84 raw_input_original = raw_input
85 raw_input_original = raw_input
85
86
86 # compiled regexps for autoindent management
87 # compiled regexps for autoindent management
87 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88
89
89 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
90 # Utilities
91 # Utilities
91 #-----------------------------------------------------------------------------
92 #-----------------------------------------------------------------------------
92
93
93 ini_spaces_re = re.compile(r'^(\s+)')
94 ini_spaces_re = re.compile(r'^(\s+)')
94
95
95
96
96 def num_ini_spaces(strng):
97 def num_ini_spaces(strng):
97 """Return the number of initial spaces in a string"""
98 """Return the number of initial spaces in a string"""
98
99
99 ini_spaces = ini_spaces_re.match(strng)
100 ini_spaces = ini_spaces_re.match(strng)
100 if ini_spaces:
101 if ini_spaces:
101 return ini_spaces.end()
102 return ini_spaces.end()
102 else:
103 else:
103 return 0
104 return 0
104
105
105
106
106 def softspace(file, newvalue):
107 def softspace(file, newvalue):
107 """Copied from code.py, to remove the dependency"""
108 """Copied from code.py, to remove the dependency"""
108
109
109 oldvalue = 0
110 oldvalue = 0
110 try:
111 try:
111 oldvalue = file.softspace
112 oldvalue = file.softspace
112 except AttributeError:
113 except AttributeError:
113 pass
114 pass
114 try:
115 try:
115 file.softspace = newvalue
116 file.softspace = newvalue
116 except (AttributeError, TypeError):
117 except (AttributeError, TypeError):
117 # "attribute-less object" or "read-only attributes"
118 # "attribute-less object" or "read-only attributes"
118 pass
119 pass
119 return oldvalue
120 return oldvalue
120
121
121
122
122 def no_op(*a, **kw): pass
123 def no_op(*a, **kw): pass
123
124
124 class SpaceInInput(exceptions.Exception): pass
125 class SpaceInInput(exceptions.Exception): pass
125
126
126 class Bunch: pass
127 class Bunch: pass
127
128
128 class InputList(list):
129 class InputList(list):
129 """Class to store user input.
130 """Class to store user input.
130
131
131 It's basically a list, but slices return a string instead of a list, thus
132 It's basically a list, but slices return a string instead of a list, thus
132 allowing things like (assuming 'In' is an instance):
133 allowing things like (assuming 'In' is an instance):
133
134
134 exec In[4:7]
135 exec In[4:7]
135
136
136 or
137 or
137
138
138 exec In[5:9] + In[14] + In[21:25]"""
139 exec In[5:9] + In[14] + In[21:25]"""
139
140
140 def __getslice__(self,i,j):
141 def __getslice__(self,i,j):
141 return ''.join(list.__getslice__(self,i,j))
142 return ''.join(list.__getslice__(self,i,j))
142
143
143
144
144 class SyntaxTB(ultratb.ListTB):
145 class SyntaxTB(ultratb.ListTB):
145 """Extension which holds some state: the last exception value"""
146 """Extension which holds some state: the last exception value"""
146
147
147 def __init__(self,color_scheme = 'NoColor'):
148 def __init__(self,color_scheme = 'NoColor'):
148 ultratb.ListTB.__init__(self,color_scheme)
149 ultratb.ListTB.__init__(self,color_scheme)
149 self.last_syntax_error = None
150 self.last_syntax_error = None
150
151
151 def __call__(self, etype, value, elist):
152 def __call__(self, etype, value, elist):
152 self.last_syntax_error = value
153 self.last_syntax_error = value
153 ultratb.ListTB.__call__(self,etype,value,elist)
154 ultratb.ListTB.__call__(self,etype,value,elist)
154
155
155 def clear_err_state(self):
156 def clear_err_state(self):
156 """Return the current error state and clear it"""
157 """Return the current error state and clear it"""
157 e = self.last_syntax_error
158 e = self.last_syntax_error
158 self.last_syntax_error = None
159 self.last_syntax_error = None
159 return e
160 return e
160
161
161
162
162 def get_default_editor():
163 def get_default_editor():
163 try:
164 try:
164 ed = os.environ['EDITOR']
165 ed = os.environ['EDITOR']
165 except KeyError:
166 except KeyError:
166 if os.name == 'posix':
167 if os.name == 'posix':
167 ed = 'vi' # the only one guaranteed to be there!
168 ed = 'vi' # the only one guaranteed to be there!
168 else:
169 else:
169 ed = 'notepad' # same in Windows!
170 ed = 'notepad' # same in Windows!
170 return ed
171 return ed
171
172
172
173
173 def get_default_colors():
174 def get_default_colors():
174 if sys.platform=='darwin':
175 if sys.platform=='darwin':
175 return "LightBG"
176 return "LightBG"
176 elif os.name=='nt':
177 elif os.name=='nt':
177 return 'Linux'
178 return 'Linux'
178 else:
179 else:
179 return 'Linux'
180 return 'Linux'
180
181
181
182
182 class SeparateStr(Str):
183 class SeparateStr(Str):
183 """A Str subclass to validate separate_in, separate_out, etc.
184 """A Str subclass to validate separate_in, separate_out, etc.
184
185
185 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
186 This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
186 """
187 """
187
188
188 def validate(self, obj, value):
189 def validate(self, obj, value):
189 if value == '0': value = ''
190 if value == '0': value = ''
190 value = value.replace('\\n','\n')
191 value = value.replace('\\n','\n')
191 return super(SeparateStr, self).validate(obj, value)
192 return super(SeparateStr, self).validate(obj, value)
192
193
193
194
194 #-----------------------------------------------------------------------------
195 #-----------------------------------------------------------------------------
195 # Main IPython class
196 # Main IPython class
196 #-----------------------------------------------------------------------------
197 #-----------------------------------------------------------------------------
197
198
198
199
199 class InteractiveShell(Component, Magic):
200 class InteractiveShell(Configurable, Magic):
200 """An enhanced, interactive shell for Python."""
201 """An enhanced, interactive shell for Python."""
201
202
202 autocall = Enum((0,1,2), default_value=1, config=True)
203 autocall = Enum((0,1,2), default_value=1, config=True)
203 autoedit_syntax = CBool(False, config=True)
204 autoedit_syntax = CBool(False, config=True)
204 autoindent = CBool(True, config=True)
205 autoindent = CBool(True, config=True)
205 automagic = CBool(True, config=True)
206 automagic = CBool(True, config=True)
206 banner = Str('')
207 banner = Str('')
207 banner1 = Str(default_banner, config=True)
208 banner1 = Str(default_banner, config=True)
208 banner2 = Str('', config=True)
209 banner2 = Str('', config=True)
209 cache_size = Int(1000, config=True)
210 cache_size = Int(1000, config=True)
210 color_info = CBool(True, config=True)
211 color_info = CBool(True, config=True)
211 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
212 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
212 default_value=get_default_colors(), config=True)
213 default_value=get_default_colors(), config=True)
213 confirm_exit = CBool(True, config=True)
214 confirm_exit = CBool(True, config=True)
214 debug = CBool(False, config=True)
215 debug = CBool(False, config=True)
215 deep_reload = CBool(False, config=True)
216 deep_reload = CBool(False, config=True)
216 # This display_banner only controls whether or not self.show_banner()
217 # This display_banner only controls whether or not self.show_banner()
217 # is called when mainloop/interact are called. The default is False
218 # is called when mainloop/interact are called. The default is False
218 # because for the terminal based application, the banner behavior
219 # because for the terminal based application, the banner behavior
219 # is controlled by Global.display_banner, which IPythonApp looks at
220 # is controlled by Global.display_banner, which IPythonApp looks at
220 # to determine if *it* should call show_banner() by hand or not.
221 # to determine if *it* should call show_banner() by hand or not.
221 display_banner = CBool(False) # This isn't configurable!
222 display_banner = CBool(False) # This isn't configurable!
222 embedded = CBool(False)
223 embedded = CBool(False)
223 embedded_active = CBool(False)
224 embedded_active = CBool(False)
224 editor = Str(get_default_editor(), config=True)
225 editor = Str(get_default_editor(), config=True)
225 filename = Str("<ipython console>")
226 filename = Str("<ipython console>")
226 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
227 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
227 logstart = CBool(False, config=True)
228 logstart = CBool(False, config=True)
228 logfile = Str('', config=True)
229 logfile = Str('', config=True)
229 logappend = Str('', config=True)
230 logappend = Str('', config=True)
230 object_info_string_level = Enum((0,1,2), default_value=0,
231 object_info_string_level = Enum((0,1,2), default_value=0,
231 config=True)
232 config=True)
232 pager = Str('less', config=True)
233 pager = Str('less', config=True)
233 pdb = CBool(False, config=True)
234 pdb = CBool(False, config=True)
234 pprint = CBool(True, config=True)
235 pprint = CBool(True, config=True)
235 profile = Str('', config=True)
236 profile = Str('', config=True)
236 prompt_in1 = Str('In [\\#]: ', config=True)
237 prompt_in1 = Str('In [\\#]: ', config=True)
237 prompt_in2 = Str(' .\\D.: ', config=True)
238 prompt_in2 = Str(' .\\D.: ', config=True)
238 prompt_out = Str('Out[\\#]: ', config=True)
239 prompt_out = Str('Out[\\#]: ', config=True)
239 prompts_pad_left = CBool(True, config=True)
240 prompts_pad_left = CBool(True, config=True)
240 quiet = CBool(False, config=True)
241 quiet = CBool(False, config=True)
241
242
242 readline_use = CBool(True, config=True)
243 readline_use = CBool(True, config=True)
243 readline_merge_completions = CBool(True, config=True)
244 readline_merge_completions = CBool(True, config=True)
244 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
245 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
245 readline_remove_delims = Str('-/~', config=True)
246 readline_remove_delims = Str('-/~', config=True)
246 readline_parse_and_bind = List([
247 readline_parse_and_bind = List([
247 'tab: complete',
248 'tab: complete',
248 '"\C-l": clear-screen',
249 '"\C-l": clear-screen',
249 'set show-all-if-ambiguous on',
250 'set show-all-if-ambiguous on',
250 '"\C-o": tab-insert',
251 '"\C-o": tab-insert',
251 '"\M-i": " "',
252 '"\M-i": " "',
252 '"\M-o": "\d\d\d\d"',
253 '"\M-o": "\d\d\d\d"',
253 '"\M-I": "\d\d\d\d"',
254 '"\M-I": "\d\d\d\d"',
254 '"\C-r": reverse-search-history',
255 '"\C-r": reverse-search-history',
255 '"\C-s": forward-search-history',
256 '"\C-s": forward-search-history',
256 '"\C-p": history-search-backward',
257 '"\C-p": history-search-backward',
257 '"\C-n": history-search-forward',
258 '"\C-n": history-search-forward',
258 '"\e[A": history-search-backward',
259 '"\e[A": history-search-backward',
259 '"\e[B": history-search-forward',
260 '"\e[B": history-search-forward',
260 '"\C-k": kill-line',
261 '"\C-k": kill-line',
261 '"\C-u": unix-line-discard',
262 '"\C-u": unix-line-discard',
262 ], allow_none=False, config=True)
263 ], allow_none=False, config=True)
263
264
264 screen_length = Int(0, config=True)
265 screen_length = Int(0, config=True)
265
266
266 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
267 # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
267 separate_in = SeparateStr('\n', config=True)
268 separate_in = SeparateStr('\n', config=True)
268 separate_out = SeparateStr('', config=True)
269 separate_out = SeparateStr('', config=True)
269 separate_out2 = SeparateStr('', config=True)
270 separate_out2 = SeparateStr('', config=True)
270
271
271 system_header = Str('IPython system call: ', config=True)
272 system_header = Str('IPython system call: ', config=True)
272 system_verbose = CBool(False, config=True)
273 system_verbose = CBool(False, config=True)
273 term_title = CBool(False, config=True)
274 term_title = CBool(False, config=True)
274 wildcards_case_sensitive = CBool(True, config=True)
275 wildcards_case_sensitive = CBool(True, config=True)
275 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
276 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
276 default_value='Context', config=True)
277 default_value='Context', config=True)
277
278
278 autoexec = List(allow_none=False)
279 autoexec = List(allow_none=False)
279
280
280 # class attribute to indicate whether the class supports threads or not.
281 # class attribute to indicate whether the class supports threads or not.
281 # Subclasses with thread support should override this as needed.
282 # Subclasses with thread support should override this as needed.
282 isthreaded = False
283 isthreaded = False
283
284
284 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
285 # Subcomponents of InteractiveShell
286 alias_manager = Instance('IPython.core.alias.AliasManager')
287 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
288 builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
289 display_trap = Instance('IPython.core.display_trap.DisplayTrap')
290 extension_manager = Instance('IPython.core.extensions.ExtensionManager')
291 plugin_manager = Instance('IPython.core.plugin.PluginManager')
292
293 def __init__(self, config=None, ipython_dir=None, usage=None,
285 user_ns=None, user_global_ns=None,
294 user_ns=None, user_global_ns=None,
286 banner1=None, banner2=None, display_banner=None,
295 banner1=None, banner2=None, display_banner=None,
287 custom_exceptions=((),None)):
296 custom_exceptions=((),None)):
288
297
289 # This is where traits with a config_key argument are updated
298 # This is where traits with a config_key argument are updated
290 # from the values on config.
299 # from the values on config.
291 super(InteractiveShell, self).__init__(parent, config=config)
300 super(InteractiveShell, self).__init__(config=config)
292
301
293 # These are relatively independent and stateless
302 # These are relatively independent and stateless
294 self.init_ipython_dir(ipython_dir)
303 self.init_ipython_dir(ipython_dir)
295 self.init_instance_attrs()
304 self.init_instance_attrs()
296 self.init_term_title()
305 self.init_term_title()
297 self.init_usage(usage)
306 self.init_usage(usage)
298 self.init_banner(banner1, banner2, display_banner)
307 self.init_banner(banner1, banner2, display_banner)
299
308
300 # Create namespaces (user_ns, user_global_ns, etc.)
309 # Create namespaces (user_ns, user_global_ns, etc.)
301 self.init_create_namespaces(user_ns, user_global_ns)
310 self.init_create_namespaces(user_ns, user_global_ns)
302 # This has to be done after init_create_namespaces because it uses
311 # This has to be done after init_create_namespaces because it uses
303 # something in self.user_ns, but before init_sys_modules, which
312 # something in self.user_ns, but before init_sys_modules, which
304 # is the first thing to modify sys.
313 # is the first thing to modify sys.
305 self.save_sys_module_state()
314 self.save_sys_module_state()
306 self.init_sys_modules()
315 self.init_sys_modules()
307
316
308 self.init_history()
317 self.init_history()
309 self.init_encoding()
318 self.init_encoding()
310 self.init_prefilter()
319 self.init_prefilter()
311
320
312 Magic.__init__(self, self)
321 Magic.__init__(self, self)
313
322
314 self.init_syntax_highlighting()
323 self.init_syntax_highlighting()
315 self.init_hooks()
324 self.init_hooks()
316 self.init_pushd_popd_magic()
325 self.init_pushd_popd_magic()
317 self.init_traceback_handlers(custom_exceptions)
326 self.init_traceback_handlers(custom_exceptions)
318 self.init_user_ns()
327 self.init_user_ns()
319 self.init_logger()
328 self.init_logger()
320 self.init_alias()
329 self.init_alias()
321 self.init_builtins()
330 self.init_builtins()
322
331
323 # pre_config_initialization
332 # pre_config_initialization
324 self.init_shadow_hist()
333 self.init_shadow_hist()
325
334
326 # The next section should contain averything that was in ipmaker.
335 # The next section should contain averything that was in ipmaker.
327 self.init_logstart()
336 self.init_logstart()
328
337
329 # The following was in post_config_initialization
338 # The following was in post_config_initialization
330 self.init_inspector()
339 self.init_inspector()
331 self.init_readline()
340 self.init_readline()
332 self.init_prompts()
341 self.init_prompts()
333 self.init_displayhook()
342 self.init_displayhook()
334 self.init_reload_doctest()
343 self.init_reload_doctest()
335 self.init_magics()
344 self.init_magics()
336 self.init_pdb()
345 self.init_pdb()
346 self.init_extension_manager()
347 self.init_plugin_manager()
337 self.hooks.late_startup_hook()
348 self.hooks.late_startup_hook()
338
349
350 @classmethod
351 def instance(cls, *args, **kwargs):
352 """Returns a global InteractiveShell instance."""
353 if not hasattr(cls, "_instance"):
354 cls._instance = cls(*args, **kwargs)
355 return cls._instance
356
357 @classmethod
358 def initialized(cls):
359 return hasattr(cls, "_instance")
360
339 def get_ipython(self):
361 def get_ipython(self):
340 """Return the currently running IPython instance."""
362 """Return the currently running IPython instance."""
341 return self
363 return self
342
364
343 #-------------------------------------------------------------------------
365 #-------------------------------------------------------------------------
344 # Trait changed handlers
366 # Trait changed handlers
345 #-------------------------------------------------------------------------
367 #-------------------------------------------------------------------------
346
368
347 def _banner1_changed(self):
369 def _banner1_changed(self):
348 self.compute_banner()
370 self.compute_banner()
349
371
350 def _banner2_changed(self):
372 def _banner2_changed(self):
351 self.compute_banner()
373 self.compute_banner()
352
374
353 def _ipython_dir_changed(self, name, new):
375 def _ipython_dir_changed(self, name, new):
354 if not os.path.isdir(new):
376 if not os.path.isdir(new):
355 os.makedirs(new, mode = 0777)
377 os.makedirs(new, mode = 0777)
356 if not os.path.isdir(self.ipython_extension_dir):
357 os.makedirs(self.ipython_extension_dir, mode = 0777)
358
359 @property
360 def ipython_extension_dir(self):
361 return os.path.join(self.ipython_dir, 'extensions')
362
378
363 @property
379 @property
364 def usable_screen_length(self):
380 def usable_screen_length(self):
365 if self.screen_length == 0:
381 if self.screen_length == 0:
366 return 0
382 return 0
367 else:
383 else:
368 num_lines_bot = self.separate_in.count('\n')+1
384 num_lines_bot = self.separate_in.count('\n')+1
369 return self.screen_length - num_lines_bot
385 return self.screen_length - num_lines_bot
370
386
371 def _term_title_changed(self, name, new_value):
387 def _term_title_changed(self, name, new_value):
372 self.init_term_title()
388 self.init_term_title()
373
389
374 def set_autoindent(self,value=None):
390 def set_autoindent(self,value=None):
375 """Set the autoindent flag, checking for readline support.
391 """Set the autoindent flag, checking for readline support.
376
392
377 If called with no arguments, it acts as a toggle."""
393 If called with no arguments, it acts as a toggle."""
378
394
379 if not self.has_readline:
395 if not self.has_readline:
380 if os.name == 'posix':
396 if os.name == 'posix':
381 warn("The auto-indent feature requires the readline library")
397 warn("The auto-indent feature requires the readline library")
382 self.autoindent = 0
398 self.autoindent = 0
383 return
399 return
384 if value is None:
400 if value is None:
385 self.autoindent = not self.autoindent
401 self.autoindent = not self.autoindent
386 else:
402 else:
387 self.autoindent = value
403 self.autoindent = value
388
404
389 #-------------------------------------------------------------------------
405 #-------------------------------------------------------------------------
390 # init_* methods called by __init__
406 # init_* methods called by __init__
391 #-------------------------------------------------------------------------
407 #-------------------------------------------------------------------------
392
408
393 def init_ipython_dir(self, ipython_dir):
409 def init_ipython_dir(self, ipython_dir):
394 if ipython_dir is not None:
410 if ipython_dir is not None:
395 self.ipython_dir = ipython_dir
411 self.ipython_dir = ipython_dir
396 self.config.Global.ipython_dir = self.ipython_dir
412 self.config.Global.ipython_dir = self.ipython_dir
397 return
413 return
398
414
399 if hasattr(self.config.Global, 'ipython_dir'):
415 if hasattr(self.config.Global, 'ipython_dir'):
400 self.ipython_dir = self.config.Global.ipython_dir
416 self.ipython_dir = self.config.Global.ipython_dir
401 else:
417 else:
402 self.ipython_dir = get_ipython_dir()
418 self.ipython_dir = get_ipython_dir()
403
419
404 # All children can just read this
420 # All children can just read this
405 self.config.Global.ipython_dir = self.ipython_dir
421 self.config.Global.ipython_dir = self.ipython_dir
406
422
407 def init_instance_attrs(self):
423 def init_instance_attrs(self):
408 self.jobs = BackgroundJobManager()
424 self.jobs = BackgroundJobManager()
409 self.more = False
425 self.more = False
410
426
411 # command compiler
427 # command compiler
412 self.compile = codeop.CommandCompiler()
428 self.compile = codeop.CommandCompiler()
413
429
414 # User input buffer
430 # User input buffer
415 self.buffer = []
431 self.buffer = []
416
432
417 # Make an empty namespace, which extension writers can rely on both
433 # Make an empty namespace, which extension writers can rely on both
418 # existing and NEVER being used by ipython itself. This gives them a
434 # existing and NEVER being used by ipython itself. This gives them a
419 # convenient location for storing additional information and state
435 # convenient location for storing additional information and state
420 # their extensions may require, without fear of collisions with other
436 # their extensions may require, without fear of collisions with other
421 # ipython names that may develop later.
437 # ipython names that may develop later.
422 self.meta = Struct()
438 self.meta = Struct()
423
439
424 # Object variable to store code object waiting execution. This is
440 # Object variable to store code object waiting execution. This is
425 # used mainly by the multithreaded shells, but it can come in handy in
441 # used mainly by the multithreaded shells, but it can come in handy in
426 # other situations. No need to use a Queue here, since it's a single
442 # other situations. No need to use a Queue here, since it's a single
427 # item which gets cleared once run.
443 # item which gets cleared once run.
428 self.code_to_run = None
444 self.code_to_run = None
429
445
430 # Flag to mark unconditional exit
446 # Flag to mark unconditional exit
431 self.exit_now = False
447 self.exit_now = False
432
448
433 # Temporary files used for various purposes. Deleted at exit.
449 # Temporary files used for various purposes. Deleted at exit.
434 self.tempfiles = []
450 self.tempfiles = []
435
451
436 # Keep track of readline usage (later set by init_readline)
452 # Keep track of readline usage (later set by init_readline)
437 self.has_readline = False
453 self.has_readline = False
438
454
439 # keep track of where we started running (mainly for crash post-mortem)
455 # keep track of where we started running (mainly for crash post-mortem)
440 # This is not being used anywhere currently.
456 # This is not being used anywhere currently.
441 self.starting_dir = os.getcwd()
457 self.starting_dir = os.getcwd()
442
458
443 # Indentation management
459 # Indentation management
444 self.indent_current_nsp = 0
460 self.indent_current_nsp = 0
445
461
446 def init_term_title(self):
462 def init_term_title(self):
447 # Enable or disable the terminal title.
463 # Enable or disable the terminal title.
448 if self.term_title:
464 if self.term_title:
449 toggle_set_term_title(True)
465 toggle_set_term_title(True)
450 set_term_title('IPython: ' + abbrev_cwd())
466 set_term_title('IPython: ' + abbrev_cwd())
451 else:
467 else:
452 toggle_set_term_title(False)
468 toggle_set_term_title(False)
453
469
454 def init_usage(self, usage=None):
470 def init_usage(self, usage=None):
455 if usage is None:
471 if usage is None:
456 self.usage = interactive_usage
472 self.usage = interactive_usage
457 else:
473 else:
458 self.usage = usage
474 self.usage = usage
459
475
460 def init_encoding(self):
476 def init_encoding(self):
461 # Get system encoding at startup time. Certain terminals (like Emacs
477 # Get system encoding at startup time. Certain terminals (like Emacs
462 # under Win32 have it set to None, and we need to have a known valid
478 # under Win32 have it set to None, and we need to have a known valid
463 # encoding to use in the raw_input() method
479 # encoding to use in the raw_input() method
464 try:
480 try:
465 self.stdin_encoding = sys.stdin.encoding or 'ascii'
481 self.stdin_encoding = sys.stdin.encoding or 'ascii'
466 except AttributeError:
482 except AttributeError:
467 self.stdin_encoding = 'ascii'
483 self.stdin_encoding = 'ascii'
468
484
469 def init_syntax_highlighting(self):
485 def init_syntax_highlighting(self):
470 # Python source parser/formatter for syntax highlighting
486 # Python source parser/formatter for syntax highlighting
471 pyformat = PyColorize.Parser().format
487 pyformat = PyColorize.Parser().format
472 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
488 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
473
489
474 def init_pushd_popd_magic(self):
490 def init_pushd_popd_magic(self):
475 # for pushd/popd management
491 # for pushd/popd management
476 try:
492 try:
477 self.home_dir = get_home_dir()
493 self.home_dir = get_home_dir()
478 except HomeDirError, msg:
494 except HomeDirError, msg:
479 fatal(msg)
495 fatal(msg)
480
496
481 self.dir_stack = []
497 self.dir_stack = []
482
498
483 def init_logger(self):
499 def init_logger(self):
484 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
500 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
485 # local shortcut, this is used a LOT
501 # local shortcut, this is used a LOT
486 self.log = self.logger.log
502 self.log = self.logger.log
487
503
488 def init_logstart(self):
504 def init_logstart(self):
489 if self.logappend:
505 if self.logappend:
490 self.magic_logstart(self.logappend + ' append')
506 self.magic_logstart(self.logappend + ' append')
491 elif self.logfile:
507 elif self.logfile:
492 self.magic_logstart(self.logfile)
508 self.magic_logstart(self.logfile)
493 elif self.logstart:
509 elif self.logstart:
494 self.magic_logstart()
510 self.magic_logstart()
495
511
496 def init_builtins(self):
512 def init_builtins(self):
497 self.builtin_trap = BuiltinTrap(self)
513 self.builtin_trap = BuiltinTrap(shell=self)
498
514
499 def init_inspector(self):
515 def init_inspector(self):
500 # Object inspector
516 # Object inspector
501 self.inspector = oinspect.Inspector(oinspect.InspectColors,
517 self.inspector = oinspect.Inspector(oinspect.InspectColors,
502 PyColorize.ANSICodeColors,
518 PyColorize.ANSICodeColors,
503 'NoColor',
519 'NoColor',
504 self.object_info_string_level)
520 self.object_info_string_level)
505
521
506 def init_prompts(self):
522 def init_prompts(self):
507 # Initialize cache, set in/out prompts and printing system
523 # Initialize cache, set in/out prompts and printing system
508 self.outputcache = CachedOutput(self,
524 self.outputcache = CachedOutput(self,
509 self.cache_size,
525 self.cache_size,
510 self.pprint,
526 self.pprint,
511 input_sep = self.separate_in,
527 input_sep = self.separate_in,
512 output_sep = self.separate_out,
528 output_sep = self.separate_out,
513 output_sep2 = self.separate_out2,
529 output_sep2 = self.separate_out2,
514 ps1 = self.prompt_in1,
530 ps1 = self.prompt_in1,
515 ps2 = self.prompt_in2,
531 ps2 = self.prompt_in2,
516 ps_out = self.prompt_out,
532 ps_out = self.prompt_out,
517 pad_left = self.prompts_pad_left)
533 pad_left = self.prompts_pad_left)
518
534
519 # user may have over-ridden the default print hook:
535 # user may have over-ridden the default print hook:
520 try:
536 try:
521 self.outputcache.__class__.display = self.hooks.display
537 self.outputcache.__class__.display = self.hooks.display
522 except AttributeError:
538 except AttributeError:
523 pass
539 pass
524
540
525 def init_displayhook(self):
541 def init_displayhook(self):
526 self.display_trap = DisplayTrap(self, self.outputcache)
542 self.display_trap = DisplayTrap(hook=self.outputcache)
527
543
528 def init_reload_doctest(self):
544 def init_reload_doctest(self):
529 # Do a proper resetting of doctest, including the necessary displayhook
545 # Do a proper resetting of doctest, including the necessary displayhook
530 # monkeypatching
546 # monkeypatching
531 try:
547 try:
532 doctest_reload()
548 doctest_reload()
533 except ImportError:
549 except ImportError:
534 warn("doctest module does not exist.")
550 warn("doctest module does not exist.")
535
551
536 #-------------------------------------------------------------------------
552 #-------------------------------------------------------------------------
537 # Things related to the banner
553 # Things related to the banner
538 #-------------------------------------------------------------------------
554 #-------------------------------------------------------------------------
539
555
540 def init_banner(self, banner1, banner2, display_banner):
556 def init_banner(self, banner1, banner2, display_banner):
541 if banner1 is not None:
557 if banner1 is not None:
542 self.banner1 = banner1
558 self.banner1 = banner1
543 if banner2 is not None:
559 if banner2 is not None:
544 self.banner2 = banner2
560 self.banner2 = banner2
545 if display_banner is not None:
561 if display_banner is not None:
546 self.display_banner = display_banner
562 self.display_banner = display_banner
547 self.compute_banner()
563 self.compute_banner()
548
564
549 def show_banner(self, banner=None):
565 def show_banner(self, banner=None):
550 if banner is None:
566 if banner is None:
551 banner = self.banner
567 banner = self.banner
552 self.write(banner)
568 self.write(banner)
553
569
554 def compute_banner(self):
570 def compute_banner(self):
555 self.banner = self.banner1 + '\n'
571 self.banner = self.banner1 + '\n'
556 if self.profile:
572 if self.profile:
557 self.banner += '\nIPython profile: %s\n' % self.profile
573 self.banner += '\nIPython profile: %s\n' % self.profile
558 if self.banner2:
574 if self.banner2:
559 self.banner += '\n' + self.banner2 + '\n'
575 self.banner += '\n' + self.banner2 + '\n'
560
576
561 #-------------------------------------------------------------------------
577 #-------------------------------------------------------------------------
562 # Things related to injections into the sys module
578 # Things related to injections into the sys module
563 #-------------------------------------------------------------------------
579 #-------------------------------------------------------------------------
564
580
565 def save_sys_module_state(self):
581 def save_sys_module_state(self):
566 """Save the state of hooks in the sys module.
582 """Save the state of hooks in the sys module.
567
583
568 This has to be called after self.user_ns is created.
584 This has to be called after self.user_ns is created.
569 """
585 """
570 self._orig_sys_module_state = {}
586 self._orig_sys_module_state = {}
571 self._orig_sys_module_state['stdin'] = sys.stdin
587 self._orig_sys_module_state['stdin'] = sys.stdin
572 self._orig_sys_module_state['stdout'] = sys.stdout
588 self._orig_sys_module_state['stdout'] = sys.stdout
573 self._orig_sys_module_state['stderr'] = sys.stderr
589 self._orig_sys_module_state['stderr'] = sys.stderr
574 self._orig_sys_module_state['excepthook'] = sys.excepthook
590 self._orig_sys_module_state['excepthook'] = sys.excepthook
575 try:
591 try:
576 self._orig_sys_modules_main_name = self.user_ns['__name__']
592 self._orig_sys_modules_main_name = self.user_ns['__name__']
577 except KeyError:
593 except KeyError:
578 pass
594 pass
579
595
580 def restore_sys_module_state(self):
596 def restore_sys_module_state(self):
581 """Restore the state of the sys module."""
597 """Restore the state of the sys module."""
582 try:
598 try:
583 for k, v in self._orig_sys_module_state.items():
599 for k, v in self._orig_sys_module_state.items():
584 setattr(sys, k, v)
600 setattr(sys, k, v)
585 except AttributeError:
601 except AttributeError:
586 pass
602 pass
587 try:
603 try:
588 delattr(sys, 'ipcompleter')
604 delattr(sys, 'ipcompleter')
589 except AttributeError:
605 except AttributeError:
590 pass
606 pass
591 # Reset what what done in self.init_sys_modules
607 # Reset what what done in self.init_sys_modules
592 try:
608 try:
593 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
609 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
594 except (AttributeError, KeyError):
610 except (AttributeError, KeyError):
595 pass
611 pass
596
612
597 #-------------------------------------------------------------------------
613 #-------------------------------------------------------------------------
598 # Things related to hooks
614 # Things related to hooks
599 #-------------------------------------------------------------------------
615 #-------------------------------------------------------------------------
600
616
601 def init_hooks(self):
617 def init_hooks(self):
602 # hooks holds pointers used for user-side customizations
618 # hooks holds pointers used for user-side customizations
603 self.hooks = Struct()
619 self.hooks = Struct()
604
620
605 self.strdispatchers = {}
621 self.strdispatchers = {}
606
622
607 # Set all default hooks, defined in the IPython.hooks module.
623 # Set all default hooks, defined in the IPython.hooks module.
608 hooks = IPython.core.hooks
624 hooks = IPython.core.hooks
609 for hook_name in hooks.__all__:
625 for hook_name in hooks.__all__:
610 # default hooks have priority 100, i.e. low; user hooks should have
626 # default hooks have priority 100, i.e. low; user hooks should have
611 # 0-100 priority
627 # 0-100 priority
612 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
628 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
613
629
614 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
630 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
615 """set_hook(name,hook) -> sets an internal IPython hook.
631 """set_hook(name,hook) -> sets an internal IPython hook.
616
632
617 IPython exposes some of its internal API as user-modifiable hooks. By
633 IPython exposes some of its internal API as user-modifiable hooks. By
618 adding your function to one of these hooks, you can modify IPython's
634 adding your function to one of these hooks, you can modify IPython's
619 behavior to call at runtime your own routines."""
635 behavior to call at runtime your own routines."""
620
636
621 # At some point in the future, this should validate the hook before it
637 # At some point in the future, this should validate the hook before it
622 # accepts it. Probably at least check that the hook takes the number
638 # accepts it. Probably at least check that the hook takes the number
623 # of args it's supposed to.
639 # of args it's supposed to.
624
640
625 f = new.instancemethod(hook,self,self.__class__)
641 f = new.instancemethod(hook,self,self.__class__)
626
642
627 # check if the hook is for strdispatcher first
643 # check if the hook is for strdispatcher first
628 if str_key is not None:
644 if str_key is not None:
629 sdp = self.strdispatchers.get(name, StrDispatch())
645 sdp = self.strdispatchers.get(name, StrDispatch())
630 sdp.add_s(str_key, f, priority )
646 sdp.add_s(str_key, f, priority )
631 self.strdispatchers[name] = sdp
647 self.strdispatchers[name] = sdp
632 return
648 return
633 if re_key is not None:
649 if re_key is not None:
634 sdp = self.strdispatchers.get(name, StrDispatch())
650 sdp = self.strdispatchers.get(name, StrDispatch())
635 sdp.add_re(re.compile(re_key), f, priority )
651 sdp.add_re(re.compile(re_key), f, priority )
636 self.strdispatchers[name] = sdp
652 self.strdispatchers[name] = sdp
637 return
653 return
638
654
639 dp = getattr(self.hooks, name, None)
655 dp = getattr(self.hooks, name, None)
640 if name not in IPython.core.hooks.__all__:
656 if name not in IPython.core.hooks.__all__:
641 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
657 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
642 if not dp:
658 if not dp:
643 dp = IPython.core.hooks.CommandChainDispatcher()
659 dp = IPython.core.hooks.CommandChainDispatcher()
644
660
645 try:
661 try:
646 dp.add(f,priority)
662 dp.add(f,priority)
647 except AttributeError:
663 except AttributeError:
648 # it was not commandchain, plain old func - replace
664 # it was not commandchain, plain old func - replace
649 dp = f
665 dp = f
650
666
651 setattr(self.hooks,name, dp)
667 setattr(self.hooks,name, dp)
652
668
653 #-------------------------------------------------------------------------
669 #-------------------------------------------------------------------------
654 # Things related to the "main" module
670 # Things related to the "main" module
655 #-------------------------------------------------------------------------
671 #-------------------------------------------------------------------------
656
672
657 def new_main_mod(self,ns=None):
673 def new_main_mod(self,ns=None):
658 """Return a new 'main' module object for user code execution.
674 """Return a new 'main' module object for user code execution.
659 """
675 """
660 main_mod = self._user_main_module
676 main_mod = self._user_main_module
661 init_fakemod_dict(main_mod,ns)
677 init_fakemod_dict(main_mod,ns)
662 return main_mod
678 return main_mod
663
679
664 def cache_main_mod(self,ns,fname):
680 def cache_main_mod(self,ns,fname):
665 """Cache a main module's namespace.
681 """Cache a main module's namespace.
666
682
667 When scripts are executed via %run, we must keep a reference to the
683 When scripts are executed via %run, we must keep a reference to the
668 namespace of their __main__ module (a FakeModule instance) around so
684 namespace of their __main__ module (a FakeModule instance) around so
669 that Python doesn't clear it, rendering objects defined therein
685 that Python doesn't clear it, rendering objects defined therein
670 useless.
686 useless.
671
687
672 This method keeps said reference in a private dict, keyed by the
688 This method keeps said reference in a private dict, keyed by the
673 absolute path of the module object (which corresponds to the script
689 absolute path of the module object (which corresponds to the script
674 path). This way, for multiple executions of the same script we only
690 path). This way, for multiple executions of the same script we only
675 keep one copy of the namespace (the last one), thus preventing memory
691 keep one copy of the namespace (the last one), thus preventing memory
676 leaks from old references while allowing the objects from the last
692 leaks from old references while allowing the objects from the last
677 execution to be accessible.
693 execution to be accessible.
678
694
679 Note: we can not allow the actual FakeModule instances to be deleted,
695 Note: we can not allow the actual FakeModule instances to be deleted,
680 because of how Python tears down modules (it hard-sets all their
696 because of how Python tears down modules (it hard-sets all their
681 references to None without regard for reference counts). This method
697 references to None without regard for reference counts). This method
682 must therefore make a *copy* of the given namespace, to allow the
698 must therefore make a *copy* of the given namespace, to allow the
683 original module's __dict__ to be cleared and reused.
699 original module's __dict__ to be cleared and reused.
684
700
685
701
686 Parameters
702 Parameters
687 ----------
703 ----------
688 ns : a namespace (a dict, typically)
704 ns : a namespace (a dict, typically)
689
705
690 fname : str
706 fname : str
691 Filename associated with the namespace.
707 Filename associated with the namespace.
692
708
693 Examples
709 Examples
694 --------
710 --------
695
711
696 In [10]: import IPython
712 In [10]: import IPython
697
713
698 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
714 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
699
715
700 In [12]: IPython.__file__ in _ip._main_ns_cache
716 In [12]: IPython.__file__ in _ip._main_ns_cache
701 Out[12]: True
717 Out[12]: True
702 """
718 """
703 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
719 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
704
720
705 def clear_main_mod_cache(self):
721 def clear_main_mod_cache(self):
706 """Clear the cache of main modules.
722 """Clear the cache of main modules.
707
723
708 Mainly for use by utilities like %reset.
724 Mainly for use by utilities like %reset.
709
725
710 Examples
726 Examples
711 --------
727 --------
712
728
713 In [15]: import IPython
729 In [15]: import IPython
714
730
715 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
731 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
716
732
717 In [17]: len(_ip._main_ns_cache) > 0
733 In [17]: len(_ip._main_ns_cache) > 0
718 Out[17]: True
734 Out[17]: True
719
735
720 In [18]: _ip.clear_main_mod_cache()
736 In [18]: _ip.clear_main_mod_cache()
721
737
722 In [19]: len(_ip._main_ns_cache) == 0
738 In [19]: len(_ip._main_ns_cache) == 0
723 Out[19]: True
739 Out[19]: True
724 """
740 """
725 self._main_ns_cache.clear()
741 self._main_ns_cache.clear()
726
742
727 #-------------------------------------------------------------------------
743 #-------------------------------------------------------------------------
728 # Things related to debugging
744 # Things related to debugging
729 #-------------------------------------------------------------------------
745 #-------------------------------------------------------------------------
730
746
731 def init_pdb(self):
747 def init_pdb(self):
732 # Set calling of pdb on exceptions
748 # Set calling of pdb on exceptions
733 # self.call_pdb is a property
749 # self.call_pdb is a property
734 self.call_pdb = self.pdb
750 self.call_pdb = self.pdb
735
751
736 def _get_call_pdb(self):
752 def _get_call_pdb(self):
737 return self._call_pdb
753 return self._call_pdb
738
754
739 def _set_call_pdb(self,val):
755 def _set_call_pdb(self,val):
740
756
741 if val not in (0,1,False,True):
757 if val not in (0,1,False,True):
742 raise ValueError,'new call_pdb value must be boolean'
758 raise ValueError,'new call_pdb value must be boolean'
743
759
744 # store value in instance
760 # store value in instance
745 self._call_pdb = val
761 self._call_pdb = val
746
762
747 # notify the actual exception handlers
763 # notify the actual exception handlers
748 self.InteractiveTB.call_pdb = val
764 self.InteractiveTB.call_pdb = val
749 if self.isthreaded:
765 if self.isthreaded:
750 try:
766 try:
751 self.sys_excepthook.call_pdb = val
767 self.sys_excepthook.call_pdb = val
752 except:
768 except:
753 warn('Failed to activate pdb for threaded exception handler')
769 warn('Failed to activate pdb for threaded exception handler')
754
770
755 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
771 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
756 'Control auto-activation of pdb at exceptions')
772 'Control auto-activation of pdb at exceptions')
757
773
758 def debugger(self,force=False):
774 def debugger(self,force=False):
759 """Call the pydb/pdb debugger.
775 """Call the pydb/pdb debugger.
760
776
761 Keywords:
777 Keywords:
762
778
763 - force(False): by default, this routine checks the instance call_pdb
779 - force(False): by default, this routine checks the instance call_pdb
764 flag and does not actually invoke the debugger if the flag is false.
780 flag and does not actually invoke the debugger if the flag is false.
765 The 'force' option forces the debugger to activate even if the flag
781 The 'force' option forces the debugger to activate even if the flag
766 is false.
782 is false.
767 """
783 """
768
784
769 if not (force or self.call_pdb):
785 if not (force or self.call_pdb):
770 return
786 return
771
787
772 if not hasattr(sys,'last_traceback'):
788 if not hasattr(sys,'last_traceback'):
773 error('No traceback has been produced, nothing to debug.')
789 error('No traceback has been produced, nothing to debug.')
774 return
790 return
775
791
776 # use pydb if available
792 # use pydb if available
777 if debugger.has_pydb:
793 if debugger.has_pydb:
778 from pydb import pm
794 from pydb import pm
779 else:
795 else:
780 # fallback to our internal debugger
796 # fallback to our internal debugger
781 pm = lambda : self.InteractiveTB.debugger(force=True)
797 pm = lambda : self.InteractiveTB.debugger(force=True)
782 self.history_saving_wrapper(pm)()
798 self.history_saving_wrapper(pm)()
783
799
784 #-------------------------------------------------------------------------
800 #-------------------------------------------------------------------------
785 # Things related to IPython's various namespaces
801 # Things related to IPython's various namespaces
786 #-------------------------------------------------------------------------
802 #-------------------------------------------------------------------------
787
803
788 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
804 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
789 # Create the namespace where the user will operate. user_ns is
805 # Create the namespace where the user will operate. user_ns is
790 # normally the only one used, and it is passed to the exec calls as
806 # normally the only one used, and it is passed to the exec calls as
791 # the locals argument. But we do carry a user_global_ns namespace
807 # the locals argument. But we do carry a user_global_ns namespace
792 # given as the exec 'globals' argument, This is useful in embedding
808 # given as the exec 'globals' argument, This is useful in embedding
793 # situations where the ipython shell opens in a context where the
809 # situations where the ipython shell opens in a context where the
794 # distinction between locals and globals is meaningful. For
810 # distinction between locals and globals is meaningful. For
795 # non-embedded contexts, it is just the same object as the user_ns dict.
811 # non-embedded contexts, it is just the same object as the user_ns dict.
796
812
797 # FIXME. For some strange reason, __builtins__ is showing up at user
813 # FIXME. For some strange reason, __builtins__ is showing up at user
798 # level as a dict instead of a module. This is a manual fix, but I
814 # level as a dict instead of a module. This is a manual fix, but I
799 # should really track down where the problem is coming from. Alex
815 # should really track down where the problem is coming from. Alex
800 # Schmolck reported this problem first.
816 # Schmolck reported this problem first.
801
817
802 # A useful post by Alex Martelli on this topic:
818 # A useful post by Alex Martelli on this topic:
803 # Re: inconsistent value from __builtins__
819 # Re: inconsistent value from __builtins__
804 # Von: Alex Martelli <aleaxit@yahoo.com>
820 # Von: Alex Martelli <aleaxit@yahoo.com>
805 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
821 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
806 # Gruppen: comp.lang.python
822 # Gruppen: comp.lang.python
807
823
808 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
824 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
809 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
825 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
810 # > <type 'dict'>
826 # > <type 'dict'>
811 # > >>> print type(__builtins__)
827 # > >>> print type(__builtins__)
812 # > <type 'module'>
828 # > <type 'module'>
813 # > Is this difference in return value intentional?
829 # > Is this difference in return value intentional?
814
830
815 # Well, it's documented that '__builtins__' can be either a dictionary
831 # Well, it's documented that '__builtins__' can be either a dictionary
816 # or a module, and it's been that way for a long time. Whether it's
832 # or a module, and it's been that way for a long time. Whether it's
817 # intentional (or sensible), I don't know. In any case, the idea is
833 # intentional (or sensible), I don't know. In any case, the idea is
818 # that if you need to access the built-in namespace directly, you
834 # that if you need to access the built-in namespace directly, you
819 # should start with "import __builtin__" (note, no 's') which will
835 # should start with "import __builtin__" (note, no 's') which will
820 # definitely give you a module. Yeah, it's somewhat confusing:-(.
836 # definitely give you a module. Yeah, it's somewhat confusing:-(.
821
837
822 # These routines return properly built dicts as needed by the rest of
838 # These routines return properly built dicts as needed by the rest of
823 # the code, and can also be used by extension writers to generate
839 # the code, and can also be used by extension writers to generate
824 # properly initialized namespaces.
840 # properly initialized namespaces.
825 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
841 user_ns, user_global_ns = self.make_user_namespaces(user_ns, user_global_ns)
826
842
827 # Assign namespaces
843 # Assign namespaces
828 # This is the namespace where all normal user variables live
844 # This is the namespace where all normal user variables live
829 self.user_ns = user_ns
845 self.user_ns = user_ns
830 self.user_global_ns = user_global_ns
846 self.user_global_ns = user_global_ns
831
847
832 # An auxiliary namespace that checks what parts of the user_ns were
848 # An auxiliary namespace that checks what parts of the user_ns were
833 # loaded at startup, so we can list later only variables defined in
849 # loaded at startup, so we can list later only variables defined in
834 # actual interactive use. Since it is always a subset of user_ns, it
850 # actual interactive use. Since it is always a subset of user_ns, it
835 # doesn't need to be separately tracked in the ns_table.
851 # doesn't need to be separately tracked in the ns_table.
836 self.user_ns_hidden = {}
852 self.user_ns_hidden = {}
837
853
838 # A namespace to keep track of internal data structures to prevent
854 # A namespace to keep track of internal data structures to prevent
839 # them from cluttering user-visible stuff. Will be updated later
855 # them from cluttering user-visible stuff. Will be updated later
840 self.internal_ns = {}
856 self.internal_ns = {}
841
857
842 # Now that FakeModule produces a real module, we've run into a nasty
858 # Now that FakeModule produces a real module, we've run into a nasty
843 # problem: after script execution (via %run), the module where the user
859 # problem: after script execution (via %run), the module where the user
844 # code ran is deleted. Now that this object is a true module (needed
860 # code ran is deleted. Now that this object is a true module (needed
845 # so docetst and other tools work correctly), the Python module
861 # so docetst and other tools work correctly), the Python module
846 # teardown mechanism runs over it, and sets to None every variable
862 # teardown mechanism runs over it, and sets to None every variable
847 # present in that module. Top-level references to objects from the
863 # present in that module. Top-level references to objects from the
848 # script survive, because the user_ns is updated with them. However,
864 # script survive, because the user_ns is updated with them. However,
849 # calling functions defined in the script that use other things from
865 # calling functions defined in the script that use other things from
850 # the script will fail, because the function's closure had references
866 # the script will fail, because the function's closure had references
851 # to the original objects, which are now all None. So we must protect
867 # to the original objects, which are now all None. So we must protect
852 # these modules from deletion by keeping a cache.
868 # these modules from deletion by keeping a cache.
853 #
869 #
854 # To avoid keeping stale modules around (we only need the one from the
870 # To avoid keeping stale modules around (we only need the one from the
855 # last run), we use a dict keyed with the full path to the script, so
871 # last run), we use a dict keyed with the full path to the script, so
856 # only the last version of the module is held in the cache. Note,
872 # only the last version of the module is held in the cache. Note,
857 # however, that we must cache the module *namespace contents* (their
873 # however, that we must cache the module *namespace contents* (their
858 # __dict__). Because if we try to cache the actual modules, old ones
874 # __dict__). Because if we try to cache the actual modules, old ones
859 # (uncached) could be destroyed while still holding references (such as
875 # (uncached) could be destroyed while still holding references (such as
860 # those held by GUI objects that tend to be long-lived)>
876 # those held by GUI objects that tend to be long-lived)>
861 #
877 #
862 # The %reset command will flush this cache. See the cache_main_mod()
878 # The %reset command will flush this cache. See the cache_main_mod()
863 # and clear_main_mod_cache() methods for details on use.
879 # and clear_main_mod_cache() methods for details on use.
864
880
865 # This is the cache used for 'main' namespaces
881 # This is the cache used for 'main' namespaces
866 self._main_ns_cache = {}
882 self._main_ns_cache = {}
867 # And this is the single instance of FakeModule whose __dict__ we keep
883 # And this is the single instance of FakeModule whose __dict__ we keep
868 # copying and clearing for reuse on each %run
884 # copying and clearing for reuse on each %run
869 self._user_main_module = FakeModule()
885 self._user_main_module = FakeModule()
870
886
871 # A table holding all the namespaces IPython deals with, so that
887 # A table holding all the namespaces IPython deals with, so that
872 # introspection facilities can search easily.
888 # introspection facilities can search easily.
873 self.ns_table = {'user':user_ns,
889 self.ns_table = {'user':user_ns,
874 'user_global':user_global_ns,
890 'user_global':user_global_ns,
875 'internal':self.internal_ns,
891 'internal':self.internal_ns,
876 'builtin':__builtin__.__dict__
892 'builtin':__builtin__.__dict__
877 }
893 }
878
894
879 # Similarly, track all namespaces where references can be held and that
895 # Similarly, track all namespaces where references can be held and that
880 # we can safely clear (so it can NOT include builtin). This one can be
896 # we can safely clear (so it can NOT include builtin). This one can be
881 # a simple list.
897 # a simple list.
882 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
898 self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
883 self.internal_ns, self._main_ns_cache ]
899 self.internal_ns, self._main_ns_cache ]
884
900
885 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
901 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
886 """Return a valid local and global user interactive namespaces.
902 """Return a valid local and global user interactive namespaces.
887
903
888 This builds a dict with the minimal information needed to operate as a
904 This builds a dict with the minimal information needed to operate as a
889 valid IPython user namespace, which you can pass to the various
905 valid IPython user namespace, which you can pass to the various
890 embedding classes in ipython. The default implementation returns the
906 embedding classes in ipython. The default implementation returns the
891 same dict for both the locals and the globals to allow functions to
907 same dict for both the locals and the globals to allow functions to
892 refer to variables in the namespace. Customized implementations can
908 refer to variables in the namespace. Customized implementations can
893 return different dicts. The locals dictionary can actually be anything
909 return different dicts. The locals dictionary can actually be anything
894 following the basic mapping protocol of a dict, but the globals dict
910 following the basic mapping protocol of a dict, but the globals dict
895 must be a true dict, not even a subclass. It is recommended that any
911 must be a true dict, not even a subclass. It is recommended that any
896 custom object for the locals namespace synchronize with the globals
912 custom object for the locals namespace synchronize with the globals
897 dict somehow.
913 dict somehow.
898
914
899 Raises TypeError if the provided globals namespace is not a true dict.
915 Raises TypeError if the provided globals namespace is not a true dict.
900
916
901 Parameters
917 Parameters
902 ----------
918 ----------
903 user_ns : dict-like, optional
919 user_ns : dict-like, optional
904 The current user namespace. The items in this namespace should
920 The current user namespace. The items in this namespace should
905 be included in the output. If None, an appropriate blank
921 be included in the output. If None, an appropriate blank
906 namespace should be created.
922 namespace should be created.
907 user_global_ns : dict, optional
923 user_global_ns : dict, optional
908 The current user global namespace. The items in this namespace
924 The current user global namespace. The items in this namespace
909 should be included in the output. If None, an appropriate
925 should be included in the output. If None, an appropriate
910 blank namespace should be created.
926 blank namespace should be created.
911
927
912 Returns
928 Returns
913 -------
929 -------
914 A pair of dictionary-like object to be used as the local namespace
930 A pair of dictionary-like object to be used as the local namespace
915 of the interpreter and a dict to be used as the global namespace.
931 of the interpreter and a dict to be used as the global namespace.
916 """
932 """
917
933
918
934
919 # We must ensure that __builtin__ (without the final 's') is always
935 # We must ensure that __builtin__ (without the final 's') is always
920 # available and pointing to the __builtin__ *module*. For more details:
936 # available and pointing to the __builtin__ *module*. For more details:
921 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
937 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
922
938
923 if user_ns is None:
939 if user_ns is None:
924 # Set __name__ to __main__ to better match the behavior of the
940 # Set __name__ to __main__ to better match the behavior of the
925 # normal interpreter.
941 # normal interpreter.
926 user_ns = {'__name__' :'__main__',
942 user_ns = {'__name__' :'__main__',
927 '__builtin__' : __builtin__,
943 '__builtin__' : __builtin__,
928 '__builtins__' : __builtin__,
944 '__builtins__' : __builtin__,
929 }
945 }
930 else:
946 else:
931 user_ns.setdefault('__name__','__main__')
947 user_ns.setdefault('__name__','__main__')
932 user_ns.setdefault('__builtin__',__builtin__)
948 user_ns.setdefault('__builtin__',__builtin__)
933 user_ns.setdefault('__builtins__',__builtin__)
949 user_ns.setdefault('__builtins__',__builtin__)
934
950
935 if user_global_ns is None:
951 if user_global_ns is None:
936 user_global_ns = user_ns
952 user_global_ns = user_ns
937 if type(user_global_ns) is not dict:
953 if type(user_global_ns) is not dict:
938 raise TypeError("user_global_ns must be a true dict; got %r"
954 raise TypeError("user_global_ns must be a true dict; got %r"
939 % type(user_global_ns))
955 % type(user_global_ns))
940
956
941 return user_ns, user_global_ns
957 return user_ns, user_global_ns
942
958
943 def init_sys_modules(self):
959 def init_sys_modules(self):
944 # We need to insert into sys.modules something that looks like a
960 # We need to insert into sys.modules something that looks like a
945 # module but which accesses the IPython namespace, for shelve and
961 # module but which accesses the IPython namespace, for shelve and
946 # pickle to work interactively. Normally they rely on getting
962 # pickle to work interactively. Normally they rely on getting
947 # everything out of __main__, but for embedding purposes each IPython
963 # everything out of __main__, but for embedding purposes each IPython
948 # instance has its own private namespace, so we can't go shoving
964 # instance has its own private namespace, so we can't go shoving
949 # everything into __main__.
965 # everything into __main__.
950
966
951 # note, however, that we should only do this for non-embedded
967 # note, however, that we should only do this for non-embedded
952 # ipythons, which really mimic the __main__.__dict__ with their own
968 # ipythons, which really mimic the __main__.__dict__ with their own
953 # namespace. Embedded instances, on the other hand, should not do
969 # namespace. Embedded instances, on the other hand, should not do
954 # this because they need to manage the user local/global namespaces
970 # this because they need to manage the user local/global namespaces
955 # only, but they live within a 'normal' __main__ (meaning, they
971 # only, but they live within a 'normal' __main__ (meaning, they
956 # shouldn't overtake the execution environment of the script they're
972 # shouldn't overtake the execution environment of the script they're
957 # embedded in).
973 # embedded in).
958
974
959 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
975 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
960
976
961 try:
977 try:
962 main_name = self.user_ns['__name__']
978 main_name = self.user_ns['__name__']
963 except KeyError:
979 except KeyError:
964 raise KeyError('user_ns dictionary MUST have a "__name__" key')
980 raise KeyError('user_ns dictionary MUST have a "__name__" key')
965 else:
981 else:
966 sys.modules[main_name] = FakeModule(self.user_ns)
982 sys.modules[main_name] = FakeModule(self.user_ns)
967
983
968 def init_user_ns(self):
984 def init_user_ns(self):
969 """Initialize all user-visible namespaces to their minimum defaults.
985 """Initialize all user-visible namespaces to their minimum defaults.
970
986
971 Certain history lists are also initialized here, as they effectively
987 Certain history lists are also initialized here, as they effectively
972 act as user namespaces.
988 act as user namespaces.
973
989
974 Notes
990 Notes
975 -----
991 -----
976 All data structures here are only filled in, they are NOT reset by this
992 All data structures here are only filled in, they are NOT reset by this
977 method. If they were not empty before, data will simply be added to
993 method. If they were not empty before, data will simply be added to
978 therm.
994 therm.
979 """
995 """
980 # This function works in two parts: first we put a few things in
996 # This function works in two parts: first we put a few things in
981 # user_ns, and we sync that contents into user_ns_hidden so that these
997 # user_ns, and we sync that contents into user_ns_hidden so that these
982 # initial variables aren't shown by %who. After the sync, we add the
998 # initial variables aren't shown by %who. After the sync, we add the
983 # rest of what we *do* want the user to see with %who even on a new
999 # rest of what we *do* want the user to see with %who even on a new
984 # session (probably nothing, so theye really only see their own stuff)
1000 # session (probably nothing, so theye really only see their own stuff)
985
1001
986 # The user dict must *always* have a __builtin__ reference to the
1002 # The user dict must *always* have a __builtin__ reference to the
987 # Python standard __builtin__ namespace, which must be imported.
1003 # Python standard __builtin__ namespace, which must be imported.
988 # This is so that certain operations in prompt evaluation can be
1004 # This is so that certain operations in prompt evaluation can be
989 # reliably executed with builtins. Note that we can NOT use
1005 # reliably executed with builtins. Note that we can NOT use
990 # __builtins__ (note the 's'), because that can either be a dict or a
1006 # __builtins__ (note the 's'), because that can either be a dict or a
991 # module, and can even mutate at runtime, depending on the context
1007 # module, and can even mutate at runtime, depending on the context
992 # (Python makes no guarantees on it). In contrast, __builtin__ is
1008 # (Python makes no guarantees on it). In contrast, __builtin__ is
993 # always a module object, though it must be explicitly imported.
1009 # always a module object, though it must be explicitly imported.
994
1010
995 # For more details:
1011 # For more details:
996 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1012 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
997 ns = dict(__builtin__ = __builtin__)
1013 ns = dict(__builtin__ = __builtin__)
998
1014
999 # Put 'help' in the user namespace
1015 # Put 'help' in the user namespace
1000 try:
1016 try:
1001 from site import _Helper
1017 from site import _Helper
1002 ns['help'] = _Helper()
1018 ns['help'] = _Helper()
1003 except ImportError:
1019 except ImportError:
1004 warn('help() not available - check site.py')
1020 warn('help() not available - check site.py')
1005
1021
1006 # make global variables for user access to the histories
1022 # make global variables for user access to the histories
1007 ns['_ih'] = self.input_hist
1023 ns['_ih'] = self.input_hist
1008 ns['_oh'] = self.output_hist
1024 ns['_oh'] = self.output_hist
1009 ns['_dh'] = self.dir_hist
1025 ns['_dh'] = self.dir_hist
1010
1026
1011 ns['_sh'] = shadowns
1027 ns['_sh'] = shadowns
1012
1028
1013 # user aliases to input and output histories. These shouldn't show up
1029 # user aliases to input and output histories. These shouldn't show up
1014 # in %who, as they can have very large reprs.
1030 # in %who, as they can have very large reprs.
1015 ns['In'] = self.input_hist
1031 ns['In'] = self.input_hist
1016 ns['Out'] = self.output_hist
1032 ns['Out'] = self.output_hist
1017
1033
1018 # Store myself as the public api!!!
1034 # Store myself as the public api!!!
1019 ns['get_ipython'] = self.get_ipython
1035 ns['get_ipython'] = self.get_ipython
1020
1036
1021 # Sync what we've added so far to user_ns_hidden so these aren't seen
1037 # Sync what we've added so far to user_ns_hidden so these aren't seen
1022 # by %who
1038 # by %who
1023 self.user_ns_hidden.update(ns)
1039 self.user_ns_hidden.update(ns)
1024
1040
1025 # Anything put into ns now would show up in %who. Think twice before
1041 # Anything put into ns now would show up in %who. Think twice before
1026 # putting anything here, as we really want %who to show the user their
1042 # putting anything here, as we really want %who to show the user their
1027 # stuff, not our variables.
1043 # stuff, not our variables.
1028
1044
1029 # Finally, update the real user's namespace
1045 # Finally, update the real user's namespace
1030 self.user_ns.update(ns)
1046 self.user_ns.update(ns)
1031
1047
1032
1048
1033 def reset(self):
1049 def reset(self):
1034 """Clear all internal namespaces.
1050 """Clear all internal namespaces.
1035
1051
1036 Note that this is much more aggressive than %reset, since it clears
1052 Note that this is much more aggressive than %reset, since it clears
1037 fully all namespaces, as well as all input/output lists.
1053 fully all namespaces, as well as all input/output lists.
1038 """
1054 """
1039 for ns in self.ns_refs_table:
1055 for ns in self.ns_refs_table:
1040 ns.clear()
1056 ns.clear()
1041
1057
1042 self.alias_manager.clear_aliases()
1058 self.alias_manager.clear_aliases()
1043
1059
1044 # Clear input and output histories
1060 # Clear input and output histories
1045 self.input_hist[:] = []
1061 self.input_hist[:] = []
1046 self.input_hist_raw[:] = []
1062 self.input_hist_raw[:] = []
1047 self.output_hist.clear()
1063 self.output_hist.clear()
1048
1064
1049 # Restore the user namespaces to minimal usability
1065 # Restore the user namespaces to minimal usability
1050 self.init_user_ns()
1066 self.init_user_ns()
1051
1067
1052 # Restore the default and user aliases
1068 # Restore the default and user aliases
1053 self.alias_manager.init_aliases()
1069 self.alias_manager.init_aliases()
1054
1070
1055 def reset_selective(self, regex=None):
1071 def reset_selective(self, regex=None):
1056 """Clear selective variables from internal namespaces based on a specified regular expression.
1072 """Clear selective variables from internal namespaces based on a specified regular expression.
1057
1073
1058 Parameters
1074 Parameters
1059 ----------
1075 ----------
1060 regex : string or compiled pattern, optional
1076 regex : string or compiled pattern, optional
1061 A regular expression pattern that will be used in searching variable names in the users
1077 A regular expression pattern that will be used in searching variable names in the users
1062 namespaces.
1078 namespaces.
1063 """
1079 """
1064 if regex is not None:
1080 if regex is not None:
1065 try:
1081 try:
1066 m = re.compile(regex)
1082 m = re.compile(regex)
1067 except TypeError:
1083 except TypeError:
1068 raise TypeError('regex must be a string or compiled pattern')
1084 raise TypeError('regex must be a string or compiled pattern')
1069 # Search for keys in each namespace that match the given regex
1085 # Search for keys in each namespace that match the given regex
1070 # If a match is found, delete the key/value pair.
1086 # If a match is found, delete the key/value pair.
1071 for ns in self.ns_refs_table:
1087 for ns in self.ns_refs_table:
1072 for var in ns:
1088 for var in ns:
1073 if m.search(var):
1089 if m.search(var):
1074 del ns[var]
1090 del ns[var]
1075
1091
1076 def push(self, variables, interactive=True):
1092 def push(self, variables, interactive=True):
1077 """Inject a group of variables into the IPython user namespace.
1093 """Inject a group of variables into the IPython user namespace.
1078
1094
1079 Parameters
1095 Parameters
1080 ----------
1096 ----------
1081 variables : dict, str or list/tuple of str
1097 variables : dict, str or list/tuple of str
1082 The variables to inject into the user's namespace. If a dict,
1098 The variables to inject into the user's namespace. If a dict,
1083 a simple update is done. If a str, the string is assumed to
1099 a simple update is done. If a str, the string is assumed to
1084 have variable names separated by spaces. A list/tuple of str
1100 have variable names separated by spaces. A list/tuple of str
1085 can also be used to give the variable names. If just the variable
1101 can also be used to give the variable names. If just the variable
1086 names are give (list/tuple/str) then the variable values looked
1102 names are give (list/tuple/str) then the variable values looked
1087 up in the callers frame.
1103 up in the callers frame.
1088 interactive : bool
1104 interactive : bool
1089 If True (default), the variables will be listed with the ``who``
1105 If True (default), the variables will be listed with the ``who``
1090 magic.
1106 magic.
1091 """
1107 """
1092 vdict = None
1108 vdict = None
1093
1109
1094 # We need a dict of name/value pairs to do namespace updates.
1110 # We need a dict of name/value pairs to do namespace updates.
1095 if isinstance(variables, dict):
1111 if isinstance(variables, dict):
1096 vdict = variables
1112 vdict = variables
1097 elif isinstance(variables, (basestring, list, tuple)):
1113 elif isinstance(variables, (basestring, list, tuple)):
1098 if isinstance(variables, basestring):
1114 if isinstance(variables, basestring):
1099 vlist = variables.split()
1115 vlist = variables.split()
1100 else:
1116 else:
1101 vlist = variables
1117 vlist = variables
1102 vdict = {}
1118 vdict = {}
1103 cf = sys._getframe(1)
1119 cf = sys._getframe(1)
1104 for name in vlist:
1120 for name in vlist:
1105 try:
1121 try:
1106 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1122 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1107 except:
1123 except:
1108 print ('Could not get variable %s from %s' %
1124 print ('Could not get variable %s from %s' %
1109 (name,cf.f_code.co_name))
1125 (name,cf.f_code.co_name))
1110 else:
1126 else:
1111 raise ValueError('variables must be a dict/str/list/tuple')
1127 raise ValueError('variables must be a dict/str/list/tuple')
1112
1128
1113 # Propagate variables to user namespace
1129 # Propagate variables to user namespace
1114 self.user_ns.update(vdict)
1130 self.user_ns.update(vdict)
1115
1131
1116 # And configure interactive visibility
1132 # And configure interactive visibility
1117 config_ns = self.user_ns_hidden
1133 config_ns = self.user_ns_hidden
1118 if interactive:
1134 if interactive:
1119 for name, val in vdict.iteritems():
1135 for name, val in vdict.iteritems():
1120 config_ns.pop(name, None)
1136 config_ns.pop(name, None)
1121 else:
1137 else:
1122 for name,val in vdict.iteritems():
1138 for name,val in vdict.iteritems():
1123 config_ns[name] = val
1139 config_ns[name] = val
1124
1140
1125 #-------------------------------------------------------------------------
1141 #-------------------------------------------------------------------------
1126 # Things related to history management
1142 # Things related to history management
1127 #-------------------------------------------------------------------------
1143 #-------------------------------------------------------------------------
1128
1144
1129 def init_history(self):
1145 def init_history(self):
1130 # List of input with multi-line handling.
1146 # List of input with multi-line handling.
1131 self.input_hist = InputList()
1147 self.input_hist = InputList()
1132 # This one will hold the 'raw' input history, without any
1148 # This one will hold the 'raw' input history, without any
1133 # pre-processing. This will allow users to retrieve the input just as
1149 # pre-processing. This will allow users to retrieve the input just as
1134 # it was exactly typed in by the user, with %hist -r.
1150 # it was exactly typed in by the user, with %hist -r.
1135 self.input_hist_raw = InputList()
1151 self.input_hist_raw = InputList()
1136
1152
1137 # list of visited directories
1153 # list of visited directories
1138 try:
1154 try:
1139 self.dir_hist = [os.getcwd()]
1155 self.dir_hist = [os.getcwd()]
1140 except OSError:
1156 except OSError:
1141 self.dir_hist = []
1157 self.dir_hist = []
1142
1158
1143 # dict of output history
1159 # dict of output history
1144 self.output_hist = {}
1160 self.output_hist = {}
1145
1161
1146 # Now the history file
1162 # Now the history file
1147 if self.profile:
1163 if self.profile:
1148 histfname = 'history-%s' % self.profile
1164 histfname = 'history-%s' % self.profile
1149 else:
1165 else:
1150 histfname = 'history'
1166 histfname = 'history'
1151 self.histfile = os.path.join(self.ipython_dir, histfname)
1167 self.histfile = os.path.join(self.ipython_dir, histfname)
1152
1168
1153 # Fill the history zero entry, user counter starts at 1
1169 # Fill the history zero entry, user counter starts at 1
1154 self.input_hist.append('\n')
1170 self.input_hist.append('\n')
1155 self.input_hist_raw.append('\n')
1171 self.input_hist_raw.append('\n')
1156
1172
1157 def init_shadow_hist(self):
1173 def init_shadow_hist(self):
1158 try:
1174 try:
1159 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1175 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1160 except exceptions.UnicodeDecodeError:
1176 except exceptions.UnicodeDecodeError:
1161 print "Your ipython_dir can't be decoded to unicode!"
1177 print "Your ipython_dir can't be decoded to unicode!"
1162 print "Please set HOME environment variable to something that"
1178 print "Please set HOME environment variable to something that"
1163 print r"only has ASCII characters, e.g. c:\home"
1179 print r"only has ASCII characters, e.g. c:\home"
1164 print "Now it is", self.ipython_dir
1180 print "Now it is", self.ipython_dir
1165 sys.exit()
1181 sys.exit()
1166 self.shadowhist = ipcorehist.ShadowHist(self.db)
1182 self.shadowhist = ipcorehist.ShadowHist(self.db)
1167
1183
1168 def savehist(self):
1184 def savehist(self):
1169 """Save input history to a file (via readline library)."""
1185 """Save input history to a file (via readline library)."""
1170
1186
1171 try:
1187 try:
1172 self.readline.write_history_file(self.histfile)
1188 self.readline.write_history_file(self.histfile)
1173 except:
1189 except:
1174 print 'Unable to save IPython command history to file: ' + \
1190 print 'Unable to save IPython command history to file: ' + \
1175 `self.histfile`
1191 `self.histfile`
1176
1192
1177 def reloadhist(self):
1193 def reloadhist(self):
1178 """Reload the input history from disk file."""
1194 """Reload the input history from disk file."""
1179
1195
1180 try:
1196 try:
1181 self.readline.clear_history()
1197 self.readline.clear_history()
1182 self.readline.read_history_file(self.shell.histfile)
1198 self.readline.read_history_file(self.shell.histfile)
1183 except AttributeError:
1199 except AttributeError:
1184 pass
1200 pass
1185
1201
1186 def history_saving_wrapper(self, func):
1202 def history_saving_wrapper(self, func):
1187 """ Wrap func for readline history saving
1203 """ Wrap func for readline history saving
1188
1204
1189 Convert func into callable that saves & restores
1205 Convert func into callable that saves & restores
1190 history around the call """
1206 history around the call """
1191
1207
1192 if self.has_readline:
1208 if self.has_readline:
1193 from IPython.utils import rlineimpl as readline
1209 from IPython.utils import rlineimpl as readline
1194 else:
1210 else:
1195 return func
1211 return func
1196
1212
1197 def wrapper():
1213 def wrapper():
1198 self.savehist()
1214 self.savehist()
1199 try:
1215 try:
1200 func()
1216 func()
1201 finally:
1217 finally:
1202 readline.read_history_file(self.histfile)
1218 readline.read_history_file(self.histfile)
1203 return wrapper
1219 return wrapper
1204
1220
1205 #-------------------------------------------------------------------------
1221 #-------------------------------------------------------------------------
1206 # Things related to exception handling and tracebacks (not debugging)
1222 # Things related to exception handling and tracebacks (not debugging)
1207 #-------------------------------------------------------------------------
1223 #-------------------------------------------------------------------------
1208
1224
1209 def init_traceback_handlers(self, custom_exceptions):
1225 def init_traceback_handlers(self, custom_exceptions):
1210 # Syntax error handler.
1226 # Syntax error handler.
1211 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1227 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1212
1228
1213 # The interactive one is initialized with an offset, meaning we always
1229 # The interactive one is initialized with an offset, meaning we always
1214 # want to remove the topmost item in the traceback, which is our own
1230 # want to remove the topmost item in the traceback, which is our own
1215 # internal code. Valid modes: ['Plain','Context','Verbose']
1231 # internal code. Valid modes: ['Plain','Context','Verbose']
1216 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1232 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1217 color_scheme='NoColor',
1233 color_scheme='NoColor',
1218 tb_offset = 1)
1234 tb_offset = 1)
1219
1235
1220 # The instance will store a pointer to the system-wide exception hook,
1236 # The instance will store a pointer to the system-wide exception hook,
1221 # so that runtime code (such as magics) can access it. This is because
1237 # so that runtime code (such as magics) can access it. This is because
1222 # during the read-eval loop, it may get temporarily overwritten.
1238 # during the read-eval loop, it may get temporarily overwritten.
1223 self.sys_excepthook = sys.excepthook
1239 self.sys_excepthook = sys.excepthook
1224
1240
1225 # and add any custom exception handlers the user may have specified
1241 # and add any custom exception handlers the user may have specified
1226 self.set_custom_exc(*custom_exceptions)
1242 self.set_custom_exc(*custom_exceptions)
1227
1243
1228 # Set the exception mode
1244 # Set the exception mode
1229 self.InteractiveTB.set_mode(mode=self.xmode)
1245 self.InteractiveTB.set_mode(mode=self.xmode)
1230
1246
1231 def set_custom_exc(self,exc_tuple,handler):
1247 def set_custom_exc(self,exc_tuple,handler):
1232 """set_custom_exc(exc_tuple,handler)
1248 """set_custom_exc(exc_tuple,handler)
1233
1249
1234 Set a custom exception handler, which will be called if any of the
1250 Set a custom exception handler, which will be called if any of the
1235 exceptions in exc_tuple occur in the mainloop (specifically, in the
1251 exceptions in exc_tuple occur in the mainloop (specifically, in the
1236 runcode() method.
1252 runcode() method.
1237
1253
1238 Inputs:
1254 Inputs:
1239
1255
1240 - exc_tuple: a *tuple* of valid exceptions to call the defined
1256 - exc_tuple: a *tuple* of valid exceptions to call the defined
1241 handler for. It is very important that you use a tuple, and NOT A
1257 handler for. It is very important that you use a tuple, and NOT A
1242 LIST here, because of the way Python's except statement works. If
1258 LIST here, because of the way Python's except statement works. If
1243 you only want to trap a single exception, use a singleton tuple:
1259 you only want to trap a single exception, use a singleton tuple:
1244
1260
1245 exc_tuple == (MyCustomException,)
1261 exc_tuple == (MyCustomException,)
1246
1262
1247 - handler: this must be defined as a function with the following
1263 - handler: this must be defined as a function with the following
1248 basic interface: def my_handler(self,etype,value,tb).
1264 basic interface: def my_handler(self,etype,value,tb).
1249
1265
1250 This will be made into an instance method (via new.instancemethod)
1266 This will be made into an instance method (via new.instancemethod)
1251 of IPython itself, and it will be called if any of the exceptions
1267 of IPython itself, and it will be called if any of the exceptions
1252 listed in the exc_tuple are caught. If the handler is None, an
1268 listed in the exc_tuple are caught. If the handler is None, an
1253 internal basic one is used, which just prints basic info.
1269 internal basic one is used, which just prints basic info.
1254
1270
1255 WARNING: by putting in your own exception handler into IPython's main
1271 WARNING: by putting in your own exception handler into IPython's main
1256 execution loop, you run a very good chance of nasty crashes. This
1272 execution loop, you run a very good chance of nasty crashes. This
1257 facility should only be used if you really know what you are doing."""
1273 facility should only be used if you really know what you are doing."""
1258
1274
1259 assert type(exc_tuple)==type(()) , \
1275 assert type(exc_tuple)==type(()) , \
1260 "The custom exceptions must be given AS A TUPLE."
1276 "The custom exceptions must be given AS A TUPLE."
1261
1277
1262 def dummy_handler(self,etype,value,tb):
1278 def dummy_handler(self,etype,value,tb):
1263 print '*** Simple custom exception handler ***'
1279 print '*** Simple custom exception handler ***'
1264 print 'Exception type :',etype
1280 print 'Exception type :',etype
1265 print 'Exception value:',value
1281 print 'Exception value:',value
1266 print 'Traceback :',tb
1282 print 'Traceback :',tb
1267 print 'Source code :','\n'.join(self.buffer)
1283 print 'Source code :','\n'.join(self.buffer)
1268
1284
1269 if handler is None: handler = dummy_handler
1285 if handler is None: handler = dummy_handler
1270
1286
1271 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1287 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1272 self.custom_exceptions = exc_tuple
1288 self.custom_exceptions = exc_tuple
1273
1289
1274 def excepthook(self, etype, value, tb):
1290 def excepthook(self, etype, value, tb):
1275 """One more defense for GUI apps that call sys.excepthook.
1291 """One more defense for GUI apps that call sys.excepthook.
1276
1292
1277 GUI frameworks like wxPython trap exceptions and call
1293 GUI frameworks like wxPython trap exceptions and call
1278 sys.excepthook themselves. I guess this is a feature that
1294 sys.excepthook themselves. I guess this is a feature that
1279 enables them to keep running after exceptions that would
1295 enables them to keep running after exceptions that would
1280 otherwise kill their mainloop. This is a bother for IPython
1296 otherwise kill their mainloop. This is a bother for IPython
1281 which excepts to catch all of the program exceptions with a try:
1297 which excepts to catch all of the program exceptions with a try:
1282 except: statement.
1298 except: statement.
1283
1299
1284 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1300 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1285 any app directly invokes sys.excepthook, it will look to the user like
1301 any app directly invokes sys.excepthook, it will look to the user like
1286 IPython crashed. In order to work around this, we can disable the
1302 IPython crashed. In order to work around this, we can disable the
1287 CrashHandler and replace it with this excepthook instead, which prints a
1303 CrashHandler and replace it with this excepthook instead, which prints a
1288 regular traceback using our InteractiveTB. In this fashion, apps which
1304 regular traceback using our InteractiveTB. In this fashion, apps which
1289 call sys.excepthook will generate a regular-looking exception from
1305 call sys.excepthook will generate a regular-looking exception from
1290 IPython, and the CrashHandler will only be triggered by real IPython
1306 IPython, and the CrashHandler will only be triggered by real IPython
1291 crashes.
1307 crashes.
1292
1308
1293 This hook should be used sparingly, only in places which are not likely
1309 This hook should be used sparingly, only in places which are not likely
1294 to be true IPython errors.
1310 to be true IPython errors.
1295 """
1311 """
1296 self.showtraceback((etype,value,tb),tb_offset=0)
1312 self.showtraceback((etype,value,tb),tb_offset=0)
1297
1313
1298 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1314 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1299 exception_only=False):
1315 exception_only=False):
1300 """Display the exception that just occurred.
1316 """Display the exception that just occurred.
1301
1317
1302 If nothing is known about the exception, this is the method which
1318 If nothing is known about the exception, this is the method which
1303 should be used throughout the code for presenting user tracebacks,
1319 should be used throughout the code for presenting user tracebacks,
1304 rather than directly invoking the InteractiveTB object.
1320 rather than directly invoking the InteractiveTB object.
1305
1321
1306 A specific showsyntaxerror() also exists, but this method can take
1322 A specific showsyntaxerror() also exists, but this method can take
1307 care of calling it if needed, so unless you are explicitly catching a
1323 care of calling it if needed, so unless you are explicitly catching a
1308 SyntaxError exception, don't try to analyze the stack manually and
1324 SyntaxError exception, don't try to analyze the stack manually and
1309 simply call this method."""
1325 simply call this method."""
1310
1326
1311 try:
1327 try:
1312 if exc_tuple is None:
1328 if exc_tuple is None:
1313 etype, value, tb = sys.exc_info()
1329 etype, value, tb = sys.exc_info()
1314 else:
1330 else:
1315 etype, value, tb = exc_tuple
1331 etype, value, tb = exc_tuple
1316
1332
1317 if etype is None:
1333 if etype is None:
1318 if hasattr(sys, 'last_type'):
1334 if hasattr(sys, 'last_type'):
1319 etype, value, tb = sys.last_type, sys.last_value, \
1335 etype, value, tb = sys.last_type, sys.last_value, \
1320 sys.last_traceback
1336 sys.last_traceback
1321 else:
1337 else:
1322 self.write('No traceback available to show.\n')
1338 self.write('No traceback available to show.\n')
1323 return
1339 return
1324
1340
1325 if etype is SyntaxError:
1341 if etype is SyntaxError:
1326 # Though this won't be called by syntax errors in the input
1342 # Though this won't be called by syntax errors in the input
1327 # line, there may be SyntaxError cases whith imported code.
1343 # line, there may be SyntaxError cases whith imported code.
1328 self.showsyntaxerror(filename)
1344 self.showsyntaxerror(filename)
1329 elif etype is UsageError:
1345 elif etype is UsageError:
1330 print "UsageError:", value
1346 print "UsageError:", value
1331 else:
1347 else:
1332 # WARNING: these variables are somewhat deprecated and not
1348 # WARNING: these variables are somewhat deprecated and not
1333 # necessarily safe to use in a threaded environment, but tools
1349 # necessarily safe to use in a threaded environment, but tools
1334 # like pdb depend on their existence, so let's set them. If we
1350 # like pdb depend on their existence, so let's set them. If we
1335 # find problems in the field, we'll need to revisit their use.
1351 # find problems in the field, we'll need to revisit their use.
1336 sys.last_type = etype
1352 sys.last_type = etype
1337 sys.last_value = value
1353 sys.last_value = value
1338 sys.last_traceback = tb
1354 sys.last_traceback = tb
1339
1355
1340 if etype in self.custom_exceptions:
1356 if etype in self.custom_exceptions:
1341 self.CustomTB(etype,value,tb)
1357 self.CustomTB(etype,value,tb)
1342 else:
1358 else:
1343 if exception_only:
1359 if exception_only:
1344 m = ('An exception has occurred, use %tb to see the '
1360 m = ('An exception has occurred, use %tb to see the '
1345 'full traceback.')
1361 'full traceback.')
1346 print m
1362 print m
1347 self.InteractiveTB.show_exception_only(etype, value)
1363 self.InteractiveTB.show_exception_only(etype, value)
1348 else:
1364 else:
1349 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1365 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1350 if self.InteractiveTB.call_pdb:
1366 if self.InteractiveTB.call_pdb:
1351 # pdb mucks up readline, fix it back
1367 # pdb mucks up readline, fix it back
1352 self.set_completer()
1368 self.set_completer()
1353
1369
1354 except KeyboardInterrupt:
1370 except KeyboardInterrupt:
1355 self.write("\nKeyboardInterrupt\n")
1371 self.write("\nKeyboardInterrupt\n")
1356
1372
1357
1373
1358 def showsyntaxerror(self, filename=None):
1374 def showsyntaxerror(self, filename=None):
1359 """Display the syntax error that just occurred.
1375 """Display the syntax error that just occurred.
1360
1376
1361 This doesn't display a stack trace because there isn't one.
1377 This doesn't display a stack trace because there isn't one.
1362
1378
1363 If a filename is given, it is stuffed in the exception instead
1379 If a filename is given, it is stuffed in the exception instead
1364 of what was there before (because Python's parser always uses
1380 of what was there before (because Python's parser always uses
1365 "<string>" when reading from a string).
1381 "<string>" when reading from a string).
1366 """
1382 """
1367 etype, value, last_traceback = sys.exc_info()
1383 etype, value, last_traceback = sys.exc_info()
1368
1384
1369 # See note about these variables in showtraceback() above
1385 # See note about these variables in showtraceback() above
1370 sys.last_type = etype
1386 sys.last_type = etype
1371 sys.last_value = value
1387 sys.last_value = value
1372 sys.last_traceback = last_traceback
1388 sys.last_traceback = last_traceback
1373
1389
1374 if filename and etype is SyntaxError:
1390 if filename and etype is SyntaxError:
1375 # Work hard to stuff the correct filename in the exception
1391 # Work hard to stuff the correct filename in the exception
1376 try:
1392 try:
1377 msg, (dummy_filename, lineno, offset, line) = value
1393 msg, (dummy_filename, lineno, offset, line) = value
1378 except:
1394 except:
1379 # Not the format we expect; leave it alone
1395 # Not the format we expect; leave it alone
1380 pass
1396 pass
1381 else:
1397 else:
1382 # Stuff in the right filename
1398 # Stuff in the right filename
1383 try:
1399 try:
1384 # Assume SyntaxError is a class exception
1400 # Assume SyntaxError is a class exception
1385 value = SyntaxError(msg, (filename, lineno, offset, line))
1401 value = SyntaxError(msg, (filename, lineno, offset, line))
1386 except:
1402 except:
1387 # If that failed, assume SyntaxError is a string
1403 # If that failed, assume SyntaxError is a string
1388 value = msg, (filename, lineno, offset, line)
1404 value = msg, (filename, lineno, offset, line)
1389 self.SyntaxTB(etype,value,[])
1405 self.SyntaxTB(etype,value,[])
1390
1406
1391 def edit_syntax_error(self):
1407 def edit_syntax_error(self):
1392 """The bottom half of the syntax error handler called in the main loop.
1408 """The bottom half of the syntax error handler called in the main loop.
1393
1409
1394 Loop until syntax error is fixed or user cancels.
1410 Loop until syntax error is fixed or user cancels.
1395 """
1411 """
1396
1412
1397 while self.SyntaxTB.last_syntax_error:
1413 while self.SyntaxTB.last_syntax_error:
1398 # copy and clear last_syntax_error
1414 # copy and clear last_syntax_error
1399 err = self.SyntaxTB.clear_err_state()
1415 err = self.SyntaxTB.clear_err_state()
1400 if not self._should_recompile(err):
1416 if not self._should_recompile(err):
1401 return
1417 return
1402 try:
1418 try:
1403 # may set last_syntax_error again if a SyntaxError is raised
1419 # may set last_syntax_error again if a SyntaxError is raised
1404 self.safe_execfile(err.filename,self.user_ns)
1420 self.safe_execfile(err.filename,self.user_ns)
1405 except:
1421 except:
1406 self.showtraceback()
1422 self.showtraceback()
1407 else:
1423 else:
1408 try:
1424 try:
1409 f = file(err.filename)
1425 f = file(err.filename)
1410 try:
1426 try:
1411 # This should be inside a display_trap block and I
1427 # This should be inside a display_trap block and I
1412 # think it is.
1428 # think it is.
1413 sys.displayhook(f.read())
1429 sys.displayhook(f.read())
1414 finally:
1430 finally:
1415 f.close()
1431 f.close()
1416 except:
1432 except:
1417 self.showtraceback()
1433 self.showtraceback()
1418
1434
1419 def _should_recompile(self,e):
1435 def _should_recompile(self,e):
1420 """Utility routine for edit_syntax_error"""
1436 """Utility routine for edit_syntax_error"""
1421
1437
1422 if e.filename in ('<ipython console>','<input>','<string>',
1438 if e.filename in ('<ipython console>','<input>','<string>',
1423 '<console>','<BackgroundJob compilation>',
1439 '<console>','<BackgroundJob compilation>',
1424 None):
1440 None):
1425
1441
1426 return False
1442 return False
1427 try:
1443 try:
1428 if (self.autoedit_syntax and
1444 if (self.autoedit_syntax and
1429 not self.ask_yes_no('Return to editor to correct syntax error? '
1445 not self.ask_yes_no('Return to editor to correct syntax error? '
1430 '[Y/n] ','y')):
1446 '[Y/n] ','y')):
1431 return False
1447 return False
1432 except EOFError:
1448 except EOFError:
1433 return False
1449 return False
1434
1450
1435 def int0(x):
1451 def int0(x):
1436 try:
1452 try:
1437 return int(x)
1453 return int(x)
1438 except TypeError:
1454 except TypeError:
1439 return 0
1455 return 0
1440 # always pass integer line and offset values to editor hook
1456 # always pass integer line and offset values to editor hook
1441 try:
1457 try:
1442 self.hooks.fix_error_editor(e.filename,
1458 self.hooks.fix_error_editor(e.filename,
1443 int0(e.lineno),int0(e.offset),e.msg)
1459 int0(e.lineno),int0(e.offset),e.msg)
1444 except TryNext:
1460 except TryNext:
1445 warn('Could not open editor')
1461 warn('Could not open editor')
1446 return False
1462 return False
1447 return True
1463 return True
1448
1464
1449 #-------------------------------------------------------------------------
1465 #-------------------------------------------------------------------------
1450 # Things related to tab completion
1466 # Things related to tab completion
1451 #-------------------------------------------------------------------------
1467 #-------------------------------------------------------------------------
1452
1468
1453 def complete(self, text):
1469 def complete(self, text):
1454 """Return a sorted list of all possible completions on text.
1470 """Return a sorted list of all possible completions on text.
1455
1471
1456 Inputs:
1472 Inputs:
1457
1473
1458 - text: a string of text to be completed on.
1474 - text: a string of text to be completed on.
1459
1475
1460 This is a wrapper around the completion mechanism, similar to what
1476 This is a wrapper around the completion mechanism, similar to what
1461 readline does at the command line when the TAB key is hit. By
1477 readline does at the command line when the TAB key is hit. By
1462 exposing it as a method, it can be used by other non-readline
1478 exposing it as a method, it can be used by other non-readline
1463 environments (such as GUIs) for text completion.
1479 environments (such as GUIs) for text completion.
1464
1480
1465 Simple usage example:
1481 Simple usage example:
1466
1482
1467 In [7]: x = 'hello'
1483 In [7]: x = 'hello'
1468
1484
1469 In [8]: x
1485 In [8]: x
1470 Out[8]: 'hello'
1486 Out[8]: 'hello'
1471
1487
1472 In [9]: print x
1488 In [9]: print x
1473 hello
1489 hello
1474
1490
1475 In [10]: _ip.complete('x.l')
1491 In [10]: _ip.complete('x.l')
1476 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1492 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1477 """
1493 """
1478
1494
1479 # Inject names into __builtin__ so we can complete on the added names.
1495 # Inject names into __builtin__ so we can complete on the added names.
1480 with self.builtin_trap:
1496 with self.builtin_trap:
1481 complete = self.Completer.complete
1497 complete = self.Completer.complete
1482 state = 0
1498 state = 0
1483 # use a dict so we get unique keys, since ipyhton's multiple
1499 # use a dict so we get unique keys, since ipyhton's multiple
1484 # completers can return duplicates. When we make 2.4 a requirement,
1500 # completers can return duplicates. When we make 2.4 a requirement,
1485 # start using sets instead, which are faster.
1501 # start using sets instead, which are faster.
1486 comps = {}
1502 comps = {}
1487 while True:
1503 while True:
1488 newcomp = complete(text,state,line_buffer=text)
1504 newcomp = complete(text,state,line_buffer=text)
1489 if newcomp is None:
1505 if newcomp is None:
1490 break
1506 break
1491 comps[newcomp] = 1
1507 comps[newcomp] = 1
1492 state += 1
1508 state += 1
1493 outcomps = comps.keys()
1509 outcomps = comps.keys()
1494 outcomps.sort()
1510 outcomps.sort()
1495 #print "T:",text,"OC:",outcomps # dbg
1511 #print "T:",text,"OC:",outcomps # dbg
1496 #print "vars:",self.user_ns.keys()
1512 #print "vars:",self.user_ns.keys()
1497 return outcomps
1513 return outcomps
1498
1514
1499 def set_custom_completer(self,completer,pos=0):
1515 def set_custom_completer(self,completer,pos=0):
1500 """Adds a new custom completer function.
1516 """Adds a new custom completer function.
1501
1517
1502 The position argument (defaults to 0) is the index in the completers
1518 The position argument (defaults to 0) is the index in the completers
1503 list where you want the completer to be inserted."""
1519 list where you want the completer to be inserted."""
1504
1520
1505 newcomp = new.instancemethod(completer,self.Completer,
1521 newcomp = new.instancemethod(completer,self.Completer,
1506 self.Completer.__class__)
1522 self.Completer.__class__)
1507 self.Completer.matchers.insert(pos,newcomp)
1523 self.Completer.matchers.insert(pos,newcomp)
1508
1524
1509 def set_completer(self):
1525 def set_completer(self):
1510 """Reset readline's completer to be our own."""
1526 """Reset readline's completer to be our own."""
1511 self.readline.set_completer(self.Completer.complete)
1527 self.readline.set_completer(self.Completer.complete)
1512
1528
1513 def set_completer_frame(self, frame=None):
1529 def set_completer_frame(self, frame=None):
1514 """Set the frame of the completer."""
1530 """Set the frame of the completer."""
1515 if frame:
1531 if frame:
1516 self.Completer.namespace = frame.f_locals
1532 self.Completer.namespace = frame.f_locals
1517 self.Completer.global_namespace = frame.f_globals
1533 self.Completer.global_namespace = frame.f_globals
1518 else:
1534 else:
1519 self.Completer.namespace = self.user_ns
1535 self.Completer.namespace = self.user_ns
1520 self.Completer.global_namespace = self.user_global_ns
1536 self.Completer.global_namespace = self.user_global_ns
1521
1537
1522 #-------------------------------------------------------------------------
1538 #-------------------------------------------------------------------------
1523 # Things related to readline
1539 # Things related to readline
1524 #-------------------------------------------------------------------------
1540 #-------------------------------------------------------------------------
1525
1541
1526 def init_readline(self):
1542 def init_readline(self):
1527 """Command history completion/saving/reloading."""
1543 """Command history completion/saving/reloading."""
1528
1544
1529 if self.readline_use:
1545 if self.readline_use:
1530 import IPython.utils.rlineimpl as readline
1546 import IPython.utils.rlineimpl as readline
1531
1547
1532 self.rl_next_input = None
1548 self.rl_next_input = None
1533 self.rl_do_indent = False
1549 self.rl_do_indent = False
1534
1550
1535 if not self.readline_use or not readline.have_readline:
1551 if not self.readline_use or not readline.have_readline:
1536 self.has_readline = False
1552 self.has_readline = False
1537 self.readline = None
1553 self.readline = None
1538 # Set a number of methods that depend on readline to be no-op
1554 # Set a number of methods that depend on readline to be no-op
1539 self.savehist = no_op
1555 self.savehist = no_op
1540 self.reloadhist = no_op
1556 self.reloadhist = no_op
1541 self.set_completer = no_op
1557 self.set_completer = no_op
1542 self.set_custom_completer = no_op
1558 self.set_custom_completer = no_op
1543 self.set_completer_frame = no_op
1559 self.set_completer_frame = no_op
1544 warn('Readline services not available or not loaded.')
1560 warn('Readline services not available or not loaded.')
1545 else:
1561 else:
1546 self.has_readline = True
1562 self.has_readline = True
1547 self.readline = readline
1563 self.readline = readline
1548 sys.modules['readline'] = readline
1564 sys.modules['readline'] = readline
1549 import atexit
1565 import atexit
1550 from IPython.core.completer import IPCompleter
1566 from IPython.core.completer import IPCompleter
1551 self.Completer = IPCompleter(self,
1567 self.Completer = IPCompleter(self,
1552 self.user_ns,
1568 self.user_ns,
1553 self.user_global_ns,
1569 self.user_global_ns,
1554 self.readline_omit__names,
1570 self.readline_omit__names,
1555 self.alias_manager.alias_table)
1571 self.alias_manager.alias_table)
1556 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1572 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1557 self.strdispatchers['complete_command'] = sdisp
1573 self.strdispatchers['complete_command'] = sdisp
1558 self.Completer.custom_completers = sdisp
1574 self.Completer.custom_completers = sdisp
1559 # Platform-specific configuration
1575 # Platform-specific configuration
1560 if os.name == 'nt':
1576 if os.name == 'nt':
1561 self.readline_startup_hook = readline.set_pre_input_hook
1577 self.readline_startup_hook = readline.set_pre_input_hook
1562 else:
1578 else:
1563 self.readline_startup_hook = readline.set_startup_hook
1579 self.readline_startup_hook = readline.set_startup_hook
1564
1580
1565 # Load user's initrc file (readline config)
1581 # Load user's initrc file (readline config)
1566 # Or if libedit is used, load editrc.
1582 # Or if libedit is used, load editrc.
1567 inputrc_name = os.environ.get('INPUTRC')
1583 inputrc_name = os.environ.get('INPUTRC')
1568 if inputrc_name is None:
1584 if inputrc_name is None:
1569 home_dir = get_home_dir()
1585 home_dir = get_home_dir()
1570 if home_dir is not None:
1586 if home_dir is not None:
1571 inputrc_name = '.inputrc'
1587 inputrc_name = '.inputrc'
1572 if readline.uses_libedit:
1588 if readline.uses_libedit:
1573 inputrc_name = '.editrc'
1589 inputrc_name = '.editrc'
1574 inputrc_name = os.path.join(home_dir, inputrc_name)
1590 inputrc_name = os.path.join(home_dir, inputrc_name)
1575 if os.path.isfile(inputrc_name):
1591 if os.path.isfile(inputrc_name):
1576 try:
1592 try:
1577 readline.read_init_file(inputrc_name)
1593 readline.read_init_file(inputrc_name)
1578 except:
1594 except:
1579 warn('Problems reading readline initialization file <%s>'
1595 warn('Problems reading readline initialization file <%s>'
1580 % inputrc_name)
1596 % inputrc_name)
1581
1597
1582 # save this in sys so embedded copies can restore it properly
1598 # save this in sys so embedded copies can restore it properly
1583 sys.ipcompleter = self.Completer.complete
1599 sys.ipcompleter = self.Completer.complete
1584 self.set_completer()
1600 self.set_completer()
1585
1601
1586 # Configure readline according to user's prefs
1602 # Configure readline according to user's prefs
1587 # This is only done if GNU readline is being used. If libedit
1603 # This is only done if GNU readline is being used. If libedit
1588 # is being used (as on Leopard) the readline config is
1604 # is being used (as on Leopard) the readline config is
1589 # not run as the syntax for libedit is different.
1605 # not run as the syntax for libedit is different.
1590 if not readline.uses_libedit:
1606 if not readline.uses_libedit:
1591 for rlcommand in self.readline_parse_and_bind:
1607 for rlcommand in self.readline_parse_and_bind:
1592 #print "loading rl:",rlcommand # dbg
1608 #print "loading rl:",rlcommand # dbg
1593 readline.parse_and_bind(rlcommand)
1609 readline.parse_and_bind(rlcommand)
1594
1610
1595 # Remove some chars from the delimiters list. If we encounter
1611 # Remove some chars from the delimiters list. If we encounter
1596 # unicode chars, discard them.
1612 # unicode chars, discard them.
1597 delims = readline.get_completer_delims().encode("ascii", "ignore")
1613 delims = readline.get_completer_delims().encode("ascii", "ignore")
1598 delims = delims.translate(string._idmap,
1614 delims = delims.translate(string._idmap,
1599 self.readline_remove_delims)
1615 self.readline_remove_delims)
1600 readline.set_completer_delims(delims)
1616 readline.set_completer_delims(delims)
1601 # otherwise we end up with a monster history after a while:
1617 # otherwise we end up with a monster history after a while:
1602 readline.set_history_length(1000)
1618 readline.set_history_length(1000)
1603 try:
1619 try:
1604 #print '*** Reading readline history' # dbg
1620 #print '*** Reading readline history' # dbg
1605 readline.read_history_file(self.histfile)
1621 readline.read_history_file(self.histfile)
1606 except IOError:
1622 except IOError:
1607 pass # It doesn't exist yet.
1623 pass # It doesn't exist yet.
1608
1624
1609 atexit.register(self.atexit_operations)
1625 atexit.register(self.atexit_operations)
1610 del atexit
1626 del atexit
1611
1627
1612 # Configure auto-indent for all platforms
1628 # Configure auto-indent for all platforms
1613 self.set_autoindent(self.autoindent)
1629 self.set_autoindent(self.autoindent)
1614
1630
1615 def set_next_input(self, s):
1631 def set_next_input(self, s):
1616 """ Sets the 'default' input string for the next command line.
1632 """ Sets the 'default' input string for the next command line.
1617
1633
1618 Requires readline.
1634 Requires readline.
1619
1635
1620 Example:
1636 Example:
1621
1637
1622 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1638 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1623 [D:\ipython]|2> Hello Word_ # cursor is here
1639 [D:\ipython]|2> Hello Word_ # cursor is here
1624 """
1640 """
1625
1641
1626 self.rl_next_input = s
1642 self.rl_next_input = s
1627
1643
1628 def pre_readline(self):
1644 def pre_readline(self):
1629 """readline hook to be used at the start of each line.
1645 """readline hook to be used at the start of each line.
1630
1646
1631 Currently it handles auto-indent only."""
1647 Currently it handles auto-indent only."""
1632
1648
1633 #debugx('self.indent_current_nsp','pre_readline:')
1649 #debugx('self.indent_current_nsp','pre_readline:')
1634
1650
1635 if self.rl_do_indent:
1651 if self.rl_do_indent:
1636 self.readline.insert_text(self._indent_current_str())
1652 self.readline.insert_text(self._indent_current_str())
1637 if self.rl_next_input is not None:
1653 if self.rl_next_input is not None:
1638 self.readline.insert_text(self.rl_next_input)
1654 self.readline.insert_text(self.rl_next_input)
1639 self.rl_next_input = None
1655 self.rl_next_input = None
1640
1656
1641 def _indent_current_str(self):
1657 def _indent_current_str(self):
1642 """return the current level of indentation as a string"""
1658 """return the current level of indentation as a string"""
1643 return self.indent_current_nsp * ' '
1659 return self.indent_current_nsp * ' '
1644
1660
1645 #-------------------------------------------------------------------------
1661 #-------------------------------------------------------------------------
1646 # Things related to magics
1662 # Things related to magics
1647 #-------------------------------------------------------------------------
1663 #-------------------------------------------------------------------------
1648
1664
1649 def init_magics(self):
1665 def init_magics(self):
1650 # Set user colors (don't do it in the constructor above so that it
1666 # Set user colors (don't do it in the constructor above so that it
1651 # doesn't crash if colors option is invalid)
1667 # doesn't crash if colors option is invalid)
1652 self.magic_colors(self.colors)
1668 self.magic_colors(self.colors)
1653 # History was moved to a separate module
1669 # History was moved to a separate module
1654 from . import history
1670 from . import history
1655 history.init_ipython(self)
1671 history.init_ipython(self)
1656
1672
1657 def magic(self,arg_s):
1673 def magic(self,arg_s):
1658 """Call a magic function by name.
1674 """Call a magic function by name.
1659
1675
1660 Input: a string containing the name of the magic function to call and any
1676 Input: a string containing the name of the magic function to call and any
1661 additional arguments to be passed to the magic.
1677 additional arguments to be passed to the magic.
1662
1678
1663 magic('name -opt foo bar') is equivalent to typing at the ipython
1679 magic('name -opt foo bar') is equivalent to typing at the ipython
1664 prompt:
1680 prompt:
1665
1681
1666 In[1]: %name -opt foo bar
1682 In[1]: %name -opt foo bar
1667
1683
1668 To call a magic without arguments, simply use magic('name').
1684 To call a magic without arguments, simply use magic('name').
1669
1685
1670 This provides a proper Python function to call IPython's magics in any
1686 This provides a proper Python function to call IPython's magics in any
1671 valid Python code you can type at the interpreter, including loops and
1687 valid Python code you can type at the interpreter, including loops and
1672 compound statements.
1688 compound statements.
1673 """
1689 """
1674 args = arg_s.split(' ',1)
1690 args = arg_s.split(' ',1)
1675 magic_name = args[0]
1691 magic_name = args[0]
1676 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1692 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1677
1693
1678 try:
1694 try:
1679 magic_args = args[1]
1695 magic_args = args[1]
1680 except IndexError:
1696 except IndexError:
1681 magic_args = ''
1697 magic_args = ''
1682 fn = getattr(self,'magic_'+magic_name,None)
1698 fn = getattr(self,'magic_'+magic_name,None)
1683 if fn is None:
1699 if fn is None:
1684 error("Magic function `%s` not found." % magic_name)
1700 error("Magic function `%s` not found." % magic_name)
1685 else:
1701 else:
1686 magic_args = self.var_expand(magic_args,1)
1702 magic_args = self.var_expand(magic_args,1)
1687 with nested(self.builtin_trap,):
1703 with nested(self.builtin_trap,):
1688 result = fn(magic_args)
1704 result = fn(magic_args)
1689 return result
1705 return result
1690
1706
1691 def define_magic(self, magicname, func):
1707 def define_magic(self, magicname, func):
1692 """Expose own function as magic function for ipython
1708 """Expose own function as magic function for ipython
1693
1709
1694 def foo_impl(self,parameter_s=''):
1710 def foo_impl(self,parameter_s=''):
1695 'My very own magic!. (Use docstrings, IPython reads them).'
1711 'My very own magic!. (Use docstrings, IPython reads them).'
1696 print 'Magic function. Passed parameter is between < >:'
1712 print 'Magic function. Passed parameter is between < >:'
1697 print '<%s>' % parameter_s
1713 print '<%s>' % parameter_s
1698 print 'The self object is:',self
1714 print 'The self object is:',self
1699
1715
1700 self.define_magic('foo',foo_impl)
1716 self.define_magic('foo',foo_impl)
1701 """
1717 """
1702
1718
1703 import new
1719 import new
1704 im = new.instancemethod(func,self, self.__class__)
1720 im = new.instancemethod(func,self, self.__class__)
1705 old = getattr(self, "magic_" + magicname, None)
1721 old = getattr(self, "magic_" + magicname, None)
1706 setattr(self, "magic_" + magicname, im)
1722 setattr(self, "magic_" + magicname, im)
1707 return old
1723 return old
1708
1724
1709 #-------------------------------------------------------------------------
1725 #-------------------------------------------------------------------------
1710 # Things related to macros
1726 # Things related to macros
1711 #-------------------------------------------------------------------------
1727 #-------------------------------------------------------------------------
1712
1728
1713 def define_macro(self, name, themacro):
1729 def define_macro(self, name, themacro):
1714 """Define a new macro
1730 """Define a new macro
1715
1731
1716 Parameters
1732 Parameters
1717 ----------
1733 ----------
1718 name : str
1734 name : str
1719 The name of the macro.
1735 The name of the macro.
1720 themacro : str or Macro
1736 themacro : str or Macro
1721 The action to do upon invoking the macro. If a string, a new
1737 The action to do upon invoking the macro. If a string, a new
1722 Macro object is created by passing the string to it.
1738 Macro object is created by passing the string to it.
1723 """
1739 """
1724
1740
1725 from IPython.core import macro
1741 from IPython.core import macro
1726
1742
1727 if isinstance(themacro, basestring):
1743 if isinstance(themacro, basestring):
1728 themacro = macro.Macro(themacro)
1744 themacro = macro.Macro(themacro)
1729 if not isinstance(themacro, macro.Macro):
1745 if not isinstance(themacro, macro.Macro):
1730 raise ValueError('A macro must be a string or a Macro instance.')
1746 raise ValueError('A macro must be a string or a Macro instance.')
1731 self.user_ns[name] = themacro
1747 self.user_ns[name] = themacro
1732
1748
1733 #-------------------------------------------------------------------------
1749 #-------------------------------------------------------------------------
1734 # Things related to the running of system commands
1750 # Things related to the running of system commands
1735 #-------------------------------------------------------------------------
1751 #-------------------------------------------------------------------------
1736
1752
1737 def system(self, cmd):
1753 def system(self, cmd):
1738 """Make a system call, using IPython."""
1754 """Make a system call, using IPython."""
1739 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1755 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1740
1756
1741 #-------------------------------------------------------------------------
1757 #-------------------------------------------------------------------------
1742 # Things related to aliases
1758 # Things related to aliases
1743 #-------------------------------------------------------------------------
1759 #-------------------------------------------------------------------------
1744
1760
1745 def init_alias(self):
1761 def init_alias(self):
1746 self.alias_manager = AliasManager(self, config=self.config)
1762 self.alias_manager = AliasManager(shell=self, config=self.config)
1747 self.ns_table['alias'] = self.alias_manager.alias_table,
1763 self.ns_table['alias'] = self.alias_manager.alias_table,
1748
1764
1749 #-------------------------------------------------------------------------
1765 #-------------------------------------------------------------------------
1766 # Things related to extensions and plugins
1767 #-------------------------------------------------------------------------
1768
1769 def init_extension_manager(self):
1770 self.extension_manager = ExtensionManager(shell=self, config=self.config)
1771
1772 def init_plugin_manager(self):
1773 self.plugin_manager = PluginManager(config=self.config)
1774
1775 #-------------------------------------------------------------------------
1750 # Things related to the running of code
1776 # Things related to the running of code
1751 #-------------------------------------------------------------------------
1777 #-------------------------------------------------------------------------
1752
1778
1753 def ex(self, cmd):
1779 def ex(self, cmd):
1754 """Execute a normal python statement in user namespace."""
1780 """Execute a normal python statement in user namespace."""
1755 with nested(self.builtin_trap,):
1781 with nested(self.builtin_trap,):
1756 exec cmd in self.user_global_ns, self.user_ns
1782 exec cmd in self.user_global_ns, self.user_ns
1757
1783
1758 def ev(self, expr):
1784 def ev(self, expr):
1759 """Evaluate python expression expr in user namespace.
1785 """Evaluate python expression expr in user namespace.
1760
1786
1761 Returns the result of evaluation
1787 Returns the result of evaluation
1762 """
1788 """
1763 with nested(self.builtin_trap,):
1789 with nested(self.builtin_trap,):
1764 return eval(expr, self.user_global_ns, self.user_ns)
1790 return eval(expr, self.user_global_ns, self.user_ns)
1765
1791
1766 def mainloop(self, display_banner=None):
1792 def mainloop(self, display_banner=None):
1767 """Start the mainloop.
1793 """Start the mainloop.
1768
1794
1769 If an optional banner argument is given, it will override the
1795 If an optional banner argument is given, it will override the
1770 internally created default banner.
1796 internally created default banner.
1771 """
1797 """
1772
1798
1773 with nested(self.builtin_trap, self.display_trap):
1799 with nested(self.builtin_trap, self.display_trap):
1774
1800
1775 # if you run stuff with -c <cmd>, raw hist is not updated
1801 # if you run stuff with -c <cmd>, raw hist is not updated
1776 # ensure that it's in sync
1802 # ensure that it's in sync
1777 if len(self.input_hist) != len (self.input_hist_raw):
1803 if len(self.input_hist) != len (self.input_hist_raw):
1778 self.input_hist_raw = InputList(self.input_hist)
1804 self.input_hist_raw = InputList(self.input_hist)
1779
1805
1780 while 1:
1806 while 1:
1781 try:
1807 try:
1782 self.interact(display_banner=display_banner)
1808 self.interact(display_banner=display_banner)
1783 #self.interact_with_readline()
1809 #self.interact_with_readline()
1784 # XXX for testing of a readline-decoupled repl loop, call
1810 # XXX for testing of a readline-decoupled repl loop, call
1785 # interact_with_readline above
1811 # interact_with_readline above
1786 break
1812 break
1787 except KeyboardInterrupt:
1813 except KeyboardInterrupt:
1788 # this should not be necessary, but KeyboardInterrupt
1814 # this should not be necessary, but KeyboardInterrupt
1789 # handling seems rather unpredictable...
1815 # handling seems rather unpredictable...
1790 self.write("\nKeyboardInterrupt in interact()\n")
1816 self.write("\nKeyboardInterrupt in interact()\n")
1791
1817
1792 def interact_prompt(self):
1818 def interact_prompt(self):
1793 """ Print the prompt (in read-eval-print loop)
1819 """ Print the prompt (in read-eval-print loop)
1794
1820
1795 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1821 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1796 used in standard IPython flow.
1822 used in standard IPython flow.
1797 """
1823 """
1798 if self.more:
1824 if self.more:
1799 try:
1825 try:
1800 prompt = self.hooks.generate_prompt(True)
1826 prompt = self.hooks.generate_prompt(True)
1801 except:
1827 except:
1802 self.showtraceback()
1828 self.showtraceback()
1803 if self.autoindent:
1829 if self.autoindent:
1804 self.rl_do_indent = True
1830 self.rl_do_indent = True
1805
1831
1806 else:
1832 else:
1807 try:
1833 try:
1808 prompt = self.hooks.generate_prompt(False)
1834 prompt = self.hooks.generate_prompt(False)
1809 except:
1835 except:
1810 self.showtraceback()
1836 self.showtraceback()
1811 self.write(prompt)
1837 self.write(prompt)
1812
1838
1813 def interact_handle_input(self,line):
1839 def interact_handle_input(self,line):
1814 """ Handle the input line (in read-eval-print loop)
1840 """ Handle the input line (in read-eval-print loop)
1815
1841
1816 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1842 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1817 used in standard IPython flow.
1843 used in standard IPython flow.
1818 """
1844 """
1819 if line.lstrip() == line:
1845 if line.lstrip() == line:
1820 self.shadowhist.add(line.strip())
1846 self.shadowhist.add(line.strip())
1821 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1847 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1822
1848
1823 if line.strip():
1849 if line.strip():
1824 if self.more:
1850 if self.more:
1825 self.input_hist_raw[-1] += '%s\n' % line
1851 self.input_hist_raw[-1] += '%s\n' % line
1826 else:
1852 else:
1827 self.input_hist_raw.append('%s\n' % line)
1853 self.input_hist_raw.append('%s\n' % line)
1828
1854
1829
1855
1830 self.more = self.push_line(lineout)
1856 self.more = self.push_line(lineout)
1831 if (self.SyntaxTB.last_syntax_error and
1857 if (self.SyntaxTB.last_syntax_error and
1832 self.autoedit_syntax):
1858 self.autoedit_syntax):
1833 self.edit_syntax_error()
1859 self.edit_syntax_error()
1834
1860
1835 def interact_with_readline(self):
1861 def interact_with_readline(self):
1836 """ Demo of using interact_handle_input, interact_prompt
1862 """ Demo of using interact_handle_input, interact_prompt
1837
1863
1838 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1864 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1839 it should work like this.
1865 it should work like this.
1840 """
1866 """
1841 self.readline_startup_hook(self.pre_readline)
1867 self.readline_startup_hook(self.pre_readline)
1842 while not self.exit_now:
1868 while not self.exit_now:
1843 self.interact_prompt()
1869 self.interact_prompt()
1844 if self.more:
1870 if self.more:
1845 self.rl_do_indent = True
1871 self.rl_do_indent = True
1846 else:
1872 else:
1847 self.rl_do_indent = False
1873 self.rl_do_indent = False
1848 line = raw_input_original().decode(self.stdin_encoding)
1874 line = raw_input_original().decode(self.stdin_encoding)
1849 self.interact_handle_input(line)
1875 self.interact_handle_input(line)
1850
1876
1851 def interact(self, display_banner=None):
1877 def interact(self, display_banner=None):
1852 """Closely emulate the interactive Python console."""
1878 """Closely emulate the interactive Python console."""
1853
1879
1854 # batch run -> do not interact
1880 # batch run -> do not interact
1855 if self.exit_now:
1881 if self.exit_now:
1856 return
1882 return
1857
1883
1858 if display_banner is None:
1884 if display_banner is None:
1859 display_banner = self.display_banner
1885 display_banner = self.display_banner
1860 if display_banner:
1886 if display_banner:
1861 self.show_banner()
1887 self.show_banner()
1862
1888
1863 more = 0
1889 more = 0
1864
1890
1865 # Mark activity in the builtins
1891 # Mark activity in the builtins
1866 __builtin__.__dict__['__IPYTHON__active'] += 1
1892 __builtin__.__dict__['__IPYTHON__active'] += 1
1867
1893
1868 if self.has_readline:
1894 if self.has_readline:
1869 self.readline_startup_hook(self.pre_readline)
1895 self.readline_startup_hook(self.pre_readline)
1870 # exit_now is set by a call to %Exit or %Quit, through the
1896 # exit_now is set by a call to %Exit or %Quit, through the
1871 # ask_exit callback.
1897 # ask_exit callback.
1872
1898
1873 while not self.exit_now:
1899 while not self.exit_now:
1874 self.hooks.pre_prompt_hook()
1900 self.hooks.pre_prompt_hook()
1875 if more:
1901 if more:
1876 try:
1902 try:
1877 prompt = self.hooks.generate_prompt(True)
1903 prompt = self.hooks.generate_prompt(True)
1878 except:
1904 except:
1879 self.showtraceback()
1905 self.showtraceback()
1880 if self.autoindent:
1906 if self.autoindent:
1881 self.rl_do_indent = True
1907 self.rl_do_indent = True
1882
1908
1883 else:
1909 else:
1884 try:
1910 try:
1885 prompt = self.hooks.generate_prompt(False)
1911 prompt = self.hooks.generate_prompt(False)
1886 except:
1912 except:
1887 self.showtraceback()
1913 self.showtraceback()
1888 try:
1914 try:
1889 line = self.raw_input(prompt, more)
1915 line = self.raw_input(prompt, more)
1890 if self.exit_now:
1916 if self.exit_now:
1891 # quick exit on sys.std[in|out] close
1917 # quick exit on sys.std[in|out] close
1892 break
1918 break
1893 if self.autoindent:
1919 if self.autoindent:
1894 self.rl_do_indent = False
1920 self.rl_do_indent = False
1895
1921
1896 except KeyboardInterrupt:
1922 except KeyboardInterrupt:
1897 #double-guard against keyboardinterrupts during kbdint handling
1923 #double-guard against keyboardinterrupts during kbdint handling
1898 try:
1924 try:
1899 self.write('\nKeyboardInterrupt\n')
1925 self.write('\nKeyboardInterrupt\n')
1900 self.resetbuffer()
1926 self.resetbuffer()
1901 # keep cache in sync with the prompt counter:
1927 # keep cache in sync with the prompt counter:
1902 self.outputcache.prompt_count -= 1
1928 self.outputcache.prompt_count -= 1
1903
1929
1904 if self.autoindent:
1930 if self.autoindent:
1905 self.indent_current_nsp = 0
1931 self.indent_current_nsp = 0
1906 more = 0
1932 more = 0
1907 except KeyboardInterrupt:
1933 except KeyboardInterrupt:
1908 pass
1934 pass
1909 except EOFError:
1935 except EOFError:
1910 if self.autoindent:
1936 if self.autoindent:
1911 self.rl_do_indent = False
1937 self.rl_do_indent = False
1912 if self.has_readline:
1938 if self.has_readline:
1913 self.readline_startup_hook(None)
1939 self.readline_startup_hook(None)
1914 self.write('\n')
1940 self.write('\n')
1915 self.exit()
1941 self.exit()
1916 except bdb.BdbQuit:
1942 except bdb.BdbQuit:
1917 warn('The Python debugger has exited with a BdbQuit exception.\n'
1943 warn('The Python debugger has exited with a BdbQuit exception.\n'
1918 'Because of how pdb handles the stack, it is impossible\n'
1944 'Because of how pdb handles the stack, it is impossible\n'
1919 'for IPython to properly format this particular exception.\n'
1945 'for IPython to properly format this particular exception.\n'
1920 'IPython will resume normal operation.')
1946 'IPython will resume normal operation.')
1921 except:
1947 except:
1922 # exceptions here are VERY RARE, but they can be triggered
1948 # exceptions here are VERY RARE, but they can be triggered
1923 # asynchronously by signal handlers, for example.
1949 # asynchronously by signal handlers, for example.
1924 self.showtraceback()
1950 self.showtraceback()
1925 else:
1951 else:
1926 more = self.push_line(line)
1952 more = self.push_line(line)
1927 if (self.SyntaxTB.last_syntax_error and
1953 if (self.SyntaxTB.last_syntax_error and
1928 self.autoedit_syntax):
1954 self.autoedit_syntax):
1929 self.edit_syntax_error()
1955 self.edit_syntax_error()
1930
1956
1931 # We are off again...
1957 # We are off again...
1932 __builtin__.__dict__['__IPYTHON__active'] -= 1
1958 __builtin__.__dict__['__IPYTHON__active'] -= 1
1933
1959
1934 # Turn off the exit flag, so the mainloop can be restarted if desired
1960 # Turn off the exit flag, so the mainloop can be restarted if desired
1935 self.exit_now = False
1961 self.exit_now = False
1936
1962
1937 def safe_execfile(self, fname, *where, **kw):
1963 def safe_execfile(self, fname, *where, **kw):
1938 """A safe version of the builtin execfile().
1964 """A safe version of the builtin execfile().
1939
1965
1940 This version will never throw an exception, but instead print
1966 This version will never throw an exception, but instead print
1941 helpful error messages to the screen. This only works on pure
1967 helpful error messages to the screen. This only works on pure
1942 Python files with the .py extension.
1968 Python files with the .py extension.
1943
1969
1944 Parameters
1970 Parameters
1945 ----------
1971 ----------
1946 fname : string
1972 fname : string
1947 The name of the file to be executed.
1973 The name of the file to be executed.
1948 where : tuple
1974 where : tuple
1949 One or two namespaces, passed to execfile() as (globals,locals).
1975 One or two namespaces, passed to execfile() as (globals,locals).
1950 If only one is given, it is passed as both.
1976 If only one is given, it is passed as both.
1951 exit_ignore : bool (False)
1977 exit_ignore : bool (False)
1952 If True, then silence SystemExit for non-zero status (it is always
1978 If True, then silence SystemExit for non-zero status (it is always
1953 silenced for zero status, as it is so common).
1979 silenced for zero status, as it is so common).
1954 """
1980 """
1955 kw.setdefault('exit_ignore', False)
1981 kw.setdefault('exit_ignore', False)
1956
1982
1957 fname = os.path.abspath(os.path.expanduser(fname))
1983 fname = os.path.abspath(os.path.expanduser(fname))
1958
1984
1959 # Make sure we have a .py file
1985 # Make sure we have a .py file
1960 if not fname.endswith('.py'):
1986 if not fname.endswith('.py'):
1961 warn('File must end with .py to be run using execfile: <%s>' % fname)
1987 warn('File must end with .py to be run using execfile: <%s>' % fname)
1962
1988
1963 # Make sure we can open the file
1989 # Make sure we can open the file
1964 try:
1990 try:
1965 with open(fname) as thefile:
1991 with open(fname) as thefile:
1966 pass
1992 pass
1967 except:
1993 except:
1968 warn('Could not open file <%s> for safe execution.' % fname)
1994 warn('Could not open file <%s> for safe execution.' % fname)
1969 return
1995 return
1970
1996
1971 # Find things also in current directory. This is needed to mimic the
1997 # Find things also in current directory. This is needed to mimic the
1972 # behavior of running a script from the system command line, where
1998 # behavior of running a script from the system command line, where
1973 # Python inserts the script's directory into sys.path
1999 # Python inserts the script's directory into sys.path
1974 dname = os.path.dirname(fname)
2000 dname = os.path.dirname(fname)
1975
2001
1976 with prepended_to_syspath(dname):
2002 with prepended_to_syspath(dname):
1977 try:
2003 try:
1978 execfile(fname,*where)
2004 execfile(fname,*where)
1979 except SystemExit, status:
2005 except SystemExit, status:
1980 # If the call was made with 0 or None exit status (sys.exit(0)
2006 # If the call was made with 0 or None exit status (sys.exit(0)
1981 # or sys.exit() ), don't bother showing a traceback, as both of
2007 # or sys.exit() ), don't bother showing a traceback, as both of
1982 # these are considered normal by the OS:
2008 # these are considered normal by the OS:
1983 # > python -c'import sys;sys.exit(0)'; echo $?
2009 # > python -c'import sys;sys.exit(0)'; echo $?
1984 # 0
2010 # 0
1985 # > python -c'import sys;sys.exit()'; echo $?
2011 # > python -c'import sys;sys.exit()'; echo $?
1986 # 0
2012 # 0
1987 # For other exit status, we show the exception unless
2013 # For other exit status, we show the exception unless
1988 # explicitly silenced, but only in short form.
2014 # explicitly silenced, but only in short form.
1989 if status.code not in (0, None) and not kw['exit_ignore']:
2015 if status.code not in (0, None) and not kw['exit_ignore']:
1990 self.showtraceback(exception_only=True)
2016 self.showtraceback(exception_only=True)
1991 except:
2017 except:
1992 self.showtraceback()
2018 self.showtraceback()
1993
2019
1994 def safe_execfile_ipy(self, fname):
2020 def safe_execfile_ipy(self, fname):
1995 """Like safe_execfile, but for .ipy files with IPython syntax.
2021 """Like safe_execfile, but for .ipy files with IPython syntax.
1996
2022
1997 Parameters
2023 Parameters
1998 ----------
2024 ----------
1999 fname : str
2025 fname : str
2000 The name of the file to execute. The filename must have a
2026 The name of the file to execute. The filename must have a
2001 .ipy extension.
2027 .ipy extension.
2002 """
2028 """
2003 fname = os.path.abspath(os.path.expanduser(fname))
2029 fname = os.path.abspath(os.path.expanduser(fname))
2004
2030
2005 # Make sure we have a .py file
2031 # Make sure we have a .py file
2006 if not fname.endswith('.ipy'):
2032 if not fname.endswith('.ipy'):
2007 warn('File must end with .py to be run using execfile: <%s>' % fname)
2033 warn('File must end with .py to be run using execfile: <%s>' % fname)
2008
2034
2009 # Make sure we can open the file
2035 # Make sure we can open the file
2010 try:
2036 try:
2011 with open(fname) as thefile:
2037 with open(fname) as thefile:
2012 pass
2038 pass
2013 except:
2039 except:
2014 warn('Could not open file <%s> for safe execution.' % fname)
2040 warn('Could not open file <%s> for safe execution.' % fname)
2015 return
2041 return
2016
2042
2017 # Find things also in current directory. This is needed to mimic the
2043 # Find things also in current directory. This is needed to mimic the
2018 # behavior of running a script from the system command line, where
2044 # behavior of running a script from the system command line, where
2019 # Python inserts the script's directory into sys.path
2045 # Python inserts the script's directory into sys.path
2020 dname = os.path.dirname(fname)
2046 dname = os.path.dirname(fname)
2021
2047
2022 with prepended_to_syspath(dname):
2048 with prepended_to_syspath(dname):
2023 try:
2049 try:
2024 with open(fname) as thefile:
2050 with open(fname) as thefile:
2025 script = thefile.read()
2051 script = thefile.read()
2026 # self.runlines currently captures all exceptions
2052 # self.runlines currently captures all exceptions
2027 # raise in user code. It would be nice if there were
2053 # raise in user code. It would be nice if there were
2028 # versions of runlines, execfile that did raise, so
2054 # versions of runlines, execfile that did raise, so
2029 # we could catch the errors.
2055 # we could catch the errors.
2030 self.runlines(script, clean=True)
2056 self.runlines(script, clean=True)
2031 except:
2057 except:
2032 self.showtraceback()
2058 self.showtraceback()
2033 warn('Unknown failure executing file: <%s>' % fname)
2059 warn('Unknown failure executing file: <%s>' % fname)
2034
2060
2035 def _is_secondary_block_start(self, s):
2061 def _is_secondary_block_start(self, s):
2036 if not s.endswith(':'):
2062 if not s.endswith(':'):
2037 return False
2063 return False
2038 if (s.startswith('elif') or
2064 if (s.startswith('elif') or
2039 s.startswith('else') or
2065 s.startswith('else') or
2040 s.startswith('except') or
2066 s.startswith('except') or
2041 s.startswith('finally')):
2067 s.startswith('finally')):
2042 return True
2068 return True
2043
2069
2044 def cleanup_ipy_script(self, script):
2070 def cleanup_ipy_script(self, script):
2045 """Make a script safe for self.runlines()
2071 """Make a script safe for self.runlines()
2046
2072
2047 Currently, IPython is lines based, with blocks being detected by
2073 Currently, IPython is lines based, with blocks being detected by
2048 empty lines. This is a problem for block based scripts that may
2074 empty lines. This is a problem for block based scripts that may
2049 not have empty lines after blocks. This script adds those empty
2075 not have empty lines after blocks. This script adds those empty
2050 lines to make scripts safe for running in the current line based
2076 lines to make scripts safe for running in the current line based
2051 IPython.
2077 IPython.
2052 """
2078 """
2053 res = []
2079 res = []
2054 lines = script.splitlines()
2080 lines = script.splitlines()
2055 level = 0
2081 level = 0
2056
2082
2057 for l in lines:
2083 for l in lines:
2058 lstripped = l.lstrip()
2084 lstripped = l.lstrip()
2059 stripped = l.strip()
2085 stripped = l.strip()
2060 if not stripped:
2086 if not stripped:
2061 continue
2087 continue
2062 newlevel = len(l) - len(lstripped)
2088 newlevel = len(l) - len(lstripped)
2063 if level > 0 and newlevel == 0 and \
2089 if level > 0 and newlevel == 0 and \
2064 not self._is_secondary_block_start(stripped):
2090 not self._is_secondary_block_start(stripped):
2065 # add empty line
2091 # add empty line
2066 res.append('')
2092 res.append('')
2067 res.append(l)
2093 res.append(l)
2068 level = newlevel
2094 level = newlevel
2069
2095
2070 return '\n'.join(res) + '\n'
2096 return '\n'.join(res) + '\n'
2071
2097
2072 def runlines(self, lines, clean=False):
2098 def runlines(self, lines, clean=False):
2073 """Run a string of one or more lines of source.
2099 """Run a string of one or more lines of source.
2074
2100
2075 This method is capable of running a string containing multiple source
2101 This method is capable of running a string containing multiple source
2076 lines, as if they had been entered at the IPython prompt. Since it
2102 lines, as if they had been entered at the IPython prompt. Since it
2077 exposes IPython's processing machinery, the given strings can contain
2103 exposes IPython's processing machinery, the given strings can contain
2078 magic calls (%magic), special shell access (!cmd), etc.
2104 magic calls (%magic), special shell access (!cmd), etc.
2079 """
2105 """
2080
2106
2081 if isinstance(lines, (list, tuple)):
2107 if isinstance(lines, (list, tuple)):
2082 lines = '\n'.join(lines)
2108 lines = '\n'.join(lines)
2083
2109
2084 if clean:
2110 if clean:
2085 lines = self.cleanup_ipy_script(lines)
2111 lines = self.cleanup_ipy_script(lines)
2086
2112
2087 # We must start with a clean buffer, in case this is run from an
2113 # We must start with a clean buffer, in case this is run from an
2088 # interactive IPython session (via a magic, for example).
2114 # interactive IPython session (via a magic, for example).
2089 self.resetbuffer()
2115 self.resetbuffer()
2090 lines = lines.splitlines()
2116 lines = lines.splitlines()
2091 more = 0
2117 more = 0
2092
2118
2093 with nested(self.builtin_trap, self.display_trap):
2119 with nested(self.builtin_trap, self.display_trap):
2094 for line in lines:
2120 for line in lines:
2095 # skip blank lines so we don't mess up the prompt counter, but do
2121 # skip blank lines so we don't mess up the prompt counter, but do
2096 # NOT skip even a blank line if we are in a code block (more is
2122 # NOT skip even a blank line if we are in a code block (more is
2097 # true)
2123 # true)
2098
2124
2099 if line or more:
2125 if line or more:
2100 # push to raw history, so hist line numbers stay in sync
2126 # push to raw history, so hist line numbers stay in sync
2101 self.input_hist_raw.append("# " + line + "\n")
2127 self.input_hist_raw.append("# " + line + "\n")
2102 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2128 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2103 more = self.push_line(prefiltered)
2129 more = self.push_line(prefiltered)
2104 # IPython's runsource returns None if there was an error
2130 # IPython's runsource returns None if there was an error
2105 # compiling the code. This allows us to stop processing right
2131 # compiling the code. This allows us to stop processing right
2106 # away, so the user gets the error message at the right place.
2132 # away, so the user gets the error message at the right place.
2107 if more is None:
2133 if more is None:
2108 break
2134 break
2109 else:
2135 else:
2110 self.input_hist_raw.append("\n")
2136 self.input_hist_raw.append("\n")
2111 # final newline in case the input didn't have it, so that the code
2137 # final newline in case the input didn't have it, so that the code
2112 # actually does get executed
2138 # actually does get executed
2113 if more:
2139 if more:
2114 self.push_line('\n')
2140 self.push_line('\n')
2115
2141
2116 def runsource(self, source, filename='<input>', symbol='single'):
2142 def runsource(self, source, filename='<input>', symbol='single'):
2117 """Compile and run some source in the interpreter.
2143 """Compile and run some source in the interpreter.
2118
2144
2119 Arguments are as for compile_command().
2145 Arguments are as for compile_command().
2120
2146
2121 One several things can happen:
2147 One several things can happen:
2122
2148
2123 1) The input is incorrect; compile_command() raised an
2149 1) The input is incorrect; compile_command() raised an
2124 exception (SyntaxError or OverflowError). A syntax traceback
2150 exception (SyntaxError or OverflowError). A syntax traceback
2125 will be printed by calling the showsyntaxerror() method.
2151 will be printed by calling the showsyntaxerror() method.
2126
2152
2127 2) The input is incomplete, and more input is required;
2153 2) The input is incomplete, and more input is required;
2128 compile_command() returned None. Nothing happens.
2154 compile_command() returned None. Nothing happens.
2129
2155
2130 3) The input is complete; compile_command() returned a code
2156 3) The input is complete; compile_command() returned a code
2131 object. The code is executed by calling self.runcode() (which
2157 object. The code is executed by calling self.runcode() (which
2132 also handles run-time exceptions, except for SystemExit).
2158 also handles run-time exceptions, except for SystemExit).
2133
2159
2134 The return value is:
2160 The return value is:
2135
2161
2136 - True in case 2
2162 - True in case 2
2137
2163
2138 - False in the other cases, unless an exception is raised, where
2164 - False in the other cases, unless an exception is raised, where
2139 None is returned instead. This can be used by external callers to
2165 None is returned instead. This can be used by external callers to
2140 know whether to continue feeding input or not.
2166 know whether to continue feeding input or not.
2141
2167
2142 The return value can be used to decide whether to use sys.ps1 or
2168 The return value can be used to decide whether to use sys.ps1 or
2143 sys.ps2 to prompt the next line."""
2169 sys.ps2 to prompt the next line."""
2144
2170
2145 # if the source code has leading blanks, add 'if 1:\n' to it
2171 # if the source code has leading blanks, add 'if 1:\n' to it
2146 # this allows execution of indented pasted code. It is tempting
2172 # this allows execution of indented pasted code. It is tempting
2147 # to add '\n' at the end of source to run commands like ' a=1'
2173 # to add '\n' at the end of source to run commands like ' a=1'
2148 # directly, but this fails for more complicated scenarios
2174 # directly, but this fails for more complicated scenarios
2149 source=source.encode(self.stdin_encoding)
2175 source=source.encode(self.stdin_encoding)
2150 if source[:1] in [' ', '\t']:
2176 if source[:1] in [' ', '\t']:
2151 source = 'if 1:\n%s' % source
2177 source = 'if 1:\n%s' % source
2152
2178
2153 try:
2179 try:
2154 code = self.compile(source,filename,symbol)
2180 code = self.compile(source,filename,symbol)
2155 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2181 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2156 # Case 1
2182 # Case 1
2157 self.showsyntaxerror(filename)
2183 self.showsyntaxerror(filename)
2158 return None
2184 return None
2159
2185
2160 if code is None:
2186 if code is None:
2161 # Case 2
2187 # Case 2
2162 return True
2188 return True
2163
2189
2164 # Case 3
2190 # Case 3
2165 # We store the code object so that threaded shells and
2191 # We store the code object so that threaded shells and
2166 # custom exception handlers can access all this info if needed.
2192 # custom exception handlers can access all this info if needed.
2167 # The source corresponding to this can be obtained from the
2193 # The source corresponding to this can be obtained from the
2168 # buffer attribute as '\n'.join(self.buffer).
2194 # buffer attribute as '\n'.join(self.buffer).
2169 self.code_to_run = code
2195 self.code_to_run = code
2170 # now actually execute the code object
2196 # now actually execute the code object
2171 if self.runcode(code) == 0:
2197 if self.runcode(code) == 0:
2172 return False
2198 return False
2173 else:
2199 else:
2174 return None
2200 return None
2175
2201
2176 def runcode(self,code_obj):
2202 def runcode(self,code_obj):
2177 """Execute a code object.
2203 """Execute a code object.
2178
2204
2179 When an exception occurs, self.showtraceback() is called to display a
2205 When an exception occurs, self.showtraceback() is called to display a
2180 traceback.
2206 traceback.
2181
2207
2182 Return value: a flag indicating whether the code to be run completed
2208 Return value: a flag indicating whether the code to be run completed
2183 successfully:
2209 successfully:
2184
2210
2185 - 0: successful execution.
2211 - 0: successful execution.
2186 - 1: an error occurred.
2212 - 1: an error occurred.
2187 """
2213 """
2188
2214
2189 # Set our own excepthook in case the user code tries to call it
2215 # Set our own excepthook in case the user code tries to call it
2190 # directly, so that the IPython crash handler doesn't get triggered
2216 # directly, so that the IPython crash handler doesn't get triggered
2191 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2217 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2192
2218
2193 # we save the original sys.excepthook in the instance, in case config
2219 # we save the original sys.excepthook in the instance, in case config
2194 # code (such as magics) needs access to it.
2220 # code (such as magics) needs access to it.
2195 self.sys_excepthook = old_excepthook
2221 self.sys_excepthook = old_excepthook
2196 outflag = 1 # happens in more places, so it's easier as default
2222 outflag = 1 # happens in more places, so it's easier as default
2197 try:
2223 try:
2198 try:
2224 try:
2199 self.hooks.pre_runcode_hook()
2225 self.hooks.pre_runcode_hook()
2200 exec code_obj in self.user_global_ns, self.user_ns
2226 exec code_obj in self.user_global_ns, self.user_ns
2201 finally:
2227 finally:
2202 # Reset our crash handler in place
2228 # Reset our crash handler in place
2203 sys.excepthook = old_excepthook
2229 sys.excepthook = old_excepthook
2204 except SystemExit:
2230 except SystemExit:
2205 self.resetbuffer()
2231 self.resetbuffer()
2206 self.showtraceback(exception_only=True)
2232 self.showtraceback(exception_only=True)
2207 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2233 warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
2208 except self.custom_exceptions:
2234 except self.custom_exceptions:
2209 etype,value,tb = sys.exc_info()
2235 etype,value,tb = sys.exc_info()
2210 self.CustomTB(etype,value,tb)
2236 self.CustomTB(etype,value,tb)
2211 except:
2237 except:
2212 self.showtraceback()
2238 self.showtraceback()
2213 else:
2239 else:
2214 outflag = 0
2240 outflag = 0
2215 if softspace(sys.stdout, 0):
2241 if softspace(sys.stdout, 0):
2216 print
2242 print
2217 # Flush out code object which has been run (and source)
2243 # Flush out code object which has been run (and source)
2218 self.code_to_run = None
2244 self.code_to_run = None
2219 return outflag
2245 return outflag
2220
2246
2221 def push_line(self, line):
2247 def push_line(self, line):
2222 """Push a line to the interpreter.
2248 """Push a line to the interpreter.
2223
2249
2224 The line should not have a trailing newline; it may have
2250 The line should not have a trailing newline; it may have
2225 internal newlines. The line is appended to a buffer and the
2251 internal newlines. The line is appended to a buffer and the
2226 interpreter's runsource() method is called with the
2252 interpreter's runsource() method is called with the
2227 concatenated contents of the buffer as source. If this
2253 concatenated contents of the buffer as source. If this
2228 indicates that the command was executed or invalid, the buffer
2254 indicates that the command was executed or invalid, the buffer
2229 is reset; otherwise, the command is incomplete, and the buffer
2255 is reset; otherwise, the command is incomplete, and the buffer
2230 is left as it was after the line was appended. The return
2256 is left as it was after the line was appended. The return
2231 value is 1 if more input is required, 0 if the line was dealt
2257 value is 1 if more input is required, 0 if the line was dealt
2232 with in some way (this is the same as runsource()).
2258 with in some way (this is the same as runsource()).
2233 """
2259 """
2234
2260
2235 # autoindent management should be done here, and not in the
2261 # autoindent management should be done here, and not in the
2236 # interactive loop, since that one is only seen by keyboard input. We
2262 # interactive loop, since that one is only seen by keyboard input. We
2237 # need this done correctly even for code run via runlines (which uses
2263 # need this done correctly even for code run via runlines (which uses
2238 # push).
2264 # push).
2239
2265
2240 #print 'push line: <%s>' % line # dbg
2266 #print 'push line: <%s>' % line # dbg
2241 for subline in line.splitlines():
2267 for subline in line.splitlines():
2242 self._autoindent_update(subline)
2268 self._autoindent_update(subline)
2243 self.buffer.append(line)
2269 self.buffer.append(line)
2244 more = self.runsource('\n'.join(self.buffer), self.filename)
2270 more = self.runsource('\n'.join(self.buffer), self.filename)
2245 if not more:
2271 if not more:
2246 self.resetbuffer()
2272 self.resetbuffer()
2247 return more
2273 return more
2248
2274
2249 def _autoindent_update(self,line):
2275 def _autoindent_update(self,line):
2250 """Keep track of the indent level."""
2276 """Keep track of the indent level."""
2251
2277
2252 #debugx('line')
2278 #debugx('line')
2253 #debugx('self.indent_current_nsp')
2279 #debugx('self.indent_current_nsp')
2254 if self.autoindent:
2280 if self.autoindent:
2255 if line:
2281 if line:
2256 inisp = num_ini_spaces(line)
2282 inisp = num_ini_spaces(line)
2257 if inisp < self.indent_current_nsp:
2283 if inisp < self.indent_current_nsp:
2258 self.indent_current_nsp = inisp
2284 self.indent_current_nsp = inisp
2259
2285
2260 if line[-1] == ':':
2286 if line[-1] == ':':
2261 self.indent_current_nsp += 4
2287 self.indent_current_nsp += 4
2262 elif dedent_re.match(line):
2288 elif dedent_re.match(line):
2263 self.indent_current_nsp -= 4
2289 self.indent_current_nsp -= 4
2264 else:
2290 else:
2265 self.indent_current_nsp = 0
2291 self.indent_current_nsp = 0
2266
2292
2267 def resetbuffer(self):
2293 def resetbuffer(self):
2268 """Reset the input buffer."""
2294 """Reset the input buffer."""
2269 self.buffer[:] = []
2295 self.buffer[:] = []
2270
2296
2271 def raw_input(self,prompt='',continue_prompt=False):
2297 def raw_input(self,prompt='',continue_prompt=False):
2272 """Write a prompt and read a line.
2298 """Write a prompt and read a line.
2273
2299
2274 The returned line does not include the trailing newline.
2300 The returned line does not include the trailing newline.
2275 When the user enters the EOF key sequence, EOFError is raised.
2301 When the user enters the EOF key sequence, EOFError is raised.
2276
2302
2277 Optional inputs:
2303 Optional inputs:
2278
2304
2279 - prompt(''): a string to be printed to prompt the user.
2305 - prompt(''): a string to be printed to prompt the user.
2280
2306
2281 - continue_prompt(False): whether this line is the first one or a
2307 - continue_prompt(False): whether this line is the first one or a
2282 continuation in a sequence of inputs.
2308 continuation in a sequence of inputs.
2283 """
2309 """
2284 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2310 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2285
2311
2286 # Code run by the user may have modified the readline completer state.
2312 # Code run by the user may have modified the readline completer state.
2287 # We must ensure that our completer is back in place.
2313 # We must ensure that our completer is back in place.
2288
2314
2289 if self.has_readline:
2315 if self.has_readline:
2290 self.set_completer()
2316 self.set_completer()
2291
2317
2292 try:
2318 try:
2293 line = raw_input_original(prompt).decode(self.stdin_encoding)
2319 line = raw_input_original(prompt).decode(self.stdin_encoding)
2294 except ValueError:
2320 except ValueError:
2295 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2321 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2296 " or sys.stdout.close()!\nExiting IPython!")
2322 " or sys.stdout.close()!\nExiting IPython!")
2297 self.ask_exit()
2323 self.ask_exit()
2298 return ""
2324 return ""
2299
2325
2300 # Try to be reasonably smart about not re-indenting pasted input more
2326 # Try to be reasonably smart about not re-indenting pasted input more
2301 # than necessary. We do this by trimming out the auto-indent initial
2327 # than necessary. We do this by trimming out the auto-indent initial
2302 # spaces, if the user's actual input started itself with whitespace.
2328 # spaces, if the user's actual input started itself with whitespace.
2303 #debugx('self.buffer[-1]')
2329 #debugx('self.buffer[-1]')
2304
2330
2305 if self.autoindent:
2331 if self.autoindent:
2306 if num_ini_spaces(line) > self.indent_current_nsp:
2332 if num_ini_spaces(line) > self.indent_current_nsp:
2307 line = line[self.indent_current_nsp:]
2333 line = line[self.indent_current_nsp:]
2308 self.indent_current_nsp = 0
2334 self.indent_current_nsp = 0
2309
2335
2310 # store the unfiltered input before the user has any chance to modify
2336 # store the unfiltered input before the user has any chance to modify
2311 # it.
2337 # it.
2312 if line.strip():
2338 if line.strip():
2313 if continue_prompt:
2339 if continue_prompt:
2314 self.input_hist_raw[-1] += '%s\n' % line
2340 self.input_hist_raw[-1] += '%s\n' % line
2315 if self.has_readline and self.readline_use:
2341 if self.has_readline and self.readline_use:
2316 try:
2342 try:
2317 histlen = self.readline.get_current_history_length()
2343 histlen = self.readline.get_current_history_length()
2318 if histlen > 1:
2344 if histlen > 1:
2319 newhist = self.input_hist_raw[-1].rstrip()
2345 newhist = self.input_hist_raw[-1].rstrip()
2320 self.readline.remove_history_item(histlen-1)
2346 self.readline.remove_history_item(histlen-1)
2321 self.readline.replace_history_item(histlen-2,
2347 self.readline.replace_history_item(histlen-2,
2322 newhist.encode(self.stdin_encoding))
2348 newhist.encode(self.stdin_encoding))
2323 except AttributeError:
2349 except AttributeError:
2324 pass # re{move,place}_history_item are new in 2.4.
2350 pass # re{move,place}_history_item are new in 2.4.
2325 else:
2351 else:
2326 self.input_hist_raw.append('%s\n' % line)
2352 self.input_hist_raw.append('%s\n' % line)
2327 # only entries starting at first column go to shadow history
2353 # only entries starting at first column go to shadow history
2328 if line.lstrip() == line:
2354 if line.lstrip() == line:
2329 self.shadowhist.add(line.strip())
2355 self.shadowhist.add(line.strip())
2330 elif not continue_prompt:
2356 elif not continue_prompt:
2331 self.input_hist_raw.append('\n')
2357 self.input_hist_raw.append('\n')
2332 try:
2358 try:
2333 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2359 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2334 except:
2360 except:
2335 # blanket except, in case a user-defined prefilter crashes, so it
2361 # blanket except, in case a user-defined prefilter crashes, so it
2336 # can't take all of ipython with it.
2362 # can't take all of ipython with it.
2337 self.showtraceback()
2363 self.showtraceback()
2338 return ''
2364 return ''
2339 else:
2365 else:
2340 return lineout
2366 return lineout
2341
2367
2342 #-------------------------------------------------------------------------
2368 #-------------------------------------------------------------------------
2343 # Working with components
2344 #-------------------------------------------------------------------------
2345
2346 def get_component(self, name=None, klass=None):
2347 """Fetch a component by name and klass in my tree."""
2348 c = Component.get_instances(root=self, name=name, klass=klass)
2349 if len(c) == 0:
2350 return None
2351 if len(c) == 1:
2352 return c[0]
2353 else:
2354 return c
2355
2356 #-------------------------------------------------------------------------
2357 # IPython extensions
2358 #-------------------------------------------------------------------------
2359
2360 def load_extension(self, module_str):
2361 """Load an IPython extension by its module name.
2362
2363 An IPython extension is an importable Python module that has
2364 a function with the signature::
2365
2366 def load_ipython_extension(ipython):
2367 # Do things with ipython
2368
2369 This function is called after your extension is imported and the
2370 currently active :class:`InteractiveShell` instance is passed as
2371 the only argument. You can do anything you want with IPython at
2372 that point, including defining new magic and aliases, adding new
2373 components, etc.
2374
2375 The :func:`load_ipython_extension` will be called again is you
2376 load or reload the extension again. It is up to the extension
2377 author to add code to manage that.
2378
2379 You can put your extension modules anywhere you want, as long as
2380 they can be imported by Python's standard import mechanism. However,
2381 to make it easy to write extensions, you can also put your extensions
2382 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
2383 is added to ``sys.path`` automatically.
2384
2385 If :func:`load_ipython_extension` returns anything, this function
2386 will return that object.
2387 """
2388 from IPython.utils.syspathcontext import prepended_to_syspath
2389
2390 if module_str not in sys.modules:
2391 with prepended_to_syspath(self.ipython_extension_dir):
2392 __import__(module_str)
2393 mod = sys.modules[module_str]
2394 return self._call_load_ipython_extension(mod)
2395
2396 def unload_extension(self, module_str):
2397 """Unload an IPython extension by its module name.
2398
2399 This function looks up the extension's name in ``sys.modules`` and
2400 simply calls ``mod.unload_ipython_extension(self)``.
2401 """
2402 if module_str in sys.modules:
2403 mod = sys.modules[module_str]
2404 self._call_unload_ipython_extension(mod)
2405
2406 def reload_extension(self, module_str):
2407 """Reload an IPython extension by calling reload.
2408
2409 If the module has not been loaded before,
2410 :meth:`InteractiveShell.load_extension` is called. Otherwise
2411 :func:`reload` is called and then the :func:`load_ipython_extension`
2412 function of the module, if it exists is called.
2413 """
2414 from IPython.utils.syspathcontext import prepended_to_syspath
2415
2416 with prepended_to_syspath(self.ipython_extension_dir):
2417 if module_str in sys.modules:
2418 mod = sys.modules[module_str]
2419 reload(mod)
2420 self._call_load_ipython_extension(mod)
2421 else:
2422 self.load_extension(module_str)
2423
2424 def _call_load_ipython_extension(self, mod):
2425 if hasattr(mod, 'load_ipython_extension'):
2426 return mod.load_ipython_extension(self)
2427
2428 def _call_unload_ipython_extension(self, mod):
2429 if hasattr(mod, 'unload_ipython_extension'):
2430 return mod.unload_ipython_extension(self)
2431
2432 #-------------------------------------------------------------------------
2433 # Things related to the prefilter
2369 # Things related to the prefilter
2434 #-------------------------------------------------------------------------
2370 #-------------------------------------------------------------------------
2435
2371
2436 def init_prefilter(self):
2372 def init_prefilter(self):
2437 self.prefilter_manager = PrefilterManager(self, config=self.config)
2373 self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
2438 # Ultimately this will be refactored in the new interpreter code, but
2374 # Ultimately this will be refactored in the new interpreter code, but
2439 # for now, we should expose the main prefilter method (there's legacy
2375 # for now, we should expose the main prefilter method (there's legacy
2440 # code out there that may rely on this).
2376 # code out there that may rely on this).
2441 self.prefilter = self.prefilter_manager.prefilter_lines
2377 self.prefilter = self.prefilter_manager.prefilter_lines
2442
2378
2443 #-------------------------------------------------------------------------
2379 #-------------------------------------------------------------------------
2444 # Utilities
2380 # Utilities
2445 #-------------------------------------------------------------------------
2381 #-------------------------------------------------------------------------
2446
2382
2447 def getoutput(self, cmd):
2383 def getoutput(self, cmd):
2448 return getoutput(self.var_expand(cmd,depth=2),
2384 return getoutput(self.var_expand(cmd,depth=2),
2449 header=self.system_header,
2385 header=self.system_header,
2450 verbose=self.system_verbose)
2386 verbose=self.system_verbose)
2451
2387
2452 def getoutputerror(self, cmd):
2388 def getoutputerror(self, cmd):
2453 return getoutputerror(self.var_expand(cmd,depth=2),
2389 return getoutputerror(self.var_expand(cmd,depth=2),
2454 header=self.system_header,
2390 header=self.system_header,
2455 verbose=self.system_verbose)
2391 verbose=self.system_verbose)
2456
2392
2457 def var_expand(self,cmd,depth=0):
2393 def var_expand(self,cmd,depth=0):
2458 """Expand python variables in a string.
2394 """Expand python variables in a string.
2459
2395
2460 The depth argument indicates how many frames above the caller should
2396 The depth argument indicates how many frames above the caller should
2461 be walked to look for the local namespace where to expand variables.
2397 be walked to look for the local namespace where to expand variables.
2462
2398
2463 The global namespace for expansion is always the user's interactive
2399 The global namespace for expansion is always the user's interactive
2464 namespace.
2400 namespace.
2465 """
2401 """
2466
2402
2467 return str(ItplNS(cmd,
2403 return str(ItplNS(cmd,
2468 self.user_ns, # globals
2404 self.user_ns, # globals
2469 # Skip our own frame in searching for locals:
2405 # Skip our own frame in searching for locals:
2470 sys._getframe(depth+1).f_locals # locals
2406 sys._getframe(depth+1).f_locals # locals
2471 ))
2407 ))
2472
2408
2473 def mktempfile(self,data=None):
2409 def mktempfile(self,data=None):
2474 """Make a new tempfile and return its filename.
2410 """Make a new tempfile and return its filename.
2475
2411
2476 This makes a call to tempfile.mktemp, but it registers the created
2412 This makes a call to tempfile.mktemp, but it registers the created
2477 filename internally so ipython cleans it up at exit time.
2413 filename internally so ipython cleans it up at exit time.
2478
2414
2479 Optional inputs:
2415 Optional inputs:
2480
2416
2481 - data(None): if data is given, it gets written out to the temp file
2417 - data(None): if data is given, it gets written out to the temp file
2482 immediately, and the file is closed again."""
2418 immediately, and the file is closed again."""
2483
2419
2484 filename = tempfile.mktemp('.py','ipython_edit_')
2420 filename = tempfile.mktemp('.py','ipython_edit_')
2485 self.tempfiles.append(filename)
2421 self.tempfiles.append(filename)
2486
2422
2487 if data:
2423 if data:
2488 tmp_file = open(filename,'w')
2424 tmp_file = open(filename,'w')
2489 tmp_file.write(data)
2425 tmp_file.write(data)
2490 tmp_file.close()
2426 tmp_file.close()
2491 return filename
2427 return filename
2492
2428
2493 def write(self,data):
2429 def write(self,data):
2494 """Write a string to the default output"""
2430 """Write a string to the default output"""
2495 Term.cout.write(data)
2431 Term.cout.write(data)
2496
2432
2497 def write_err(self,data):
2433 def write_err(self,data):
2498 """Write a string to the default error output"""
2434 """Write a string to the default error output"""
2499 Term.cerr.write(data)
2435 Term.cerr.write(data)
2500
2436
2501 def ask_yes_no(self,prompt,default=True):
2437 def ask_yes_no(self,prompt,default=True):
2502 if self.quiet:
2438 if self.quiet:
2503 return True
2439 return True
2504 return ask_yes_no(prompt,default)
2440 return ask_yes_no(prompt,default)
2505
2441
2506 #-------------------------------------------------------------------------
2442 #-------------------------------------------------------------------------
2507 # Things related to GUI support and pylab
2443 # Things related to GUI support and pylab
2508 #-------------------------------------------------------------------------
2444 #-------------------------------------------------------------------------
2509
2445
2510 def enable_pylab(self, gui=None):
2446 def enable_pylab(self, gui=None):
2511 """Activate pylab support at runtime.
2447 """Activate pylab support at runtime.
2512
2448
2513 This turns on support for matplotlib, preloads into the interactive
2449 This turns on support for matplotlib, preloads into the interactive
2514 namespace all of numpy and pylab, and configures IPython to correcdtly
2450 namespace all of numpy and pylab, and configures IPython to correcdtly
2515 interact with the GUI event loop. The GUI backend to be used can be
2451 interact with the GUI event loop. The GUI backend to be used can be
2516 optionally selected with the optional :param:`gui` argument.
2452 optionally selected with the optional :param:`gui` argument.
2517
2453
2518 Parameters
2454 Parameters
2519 ----------
2455 ----------
2520 gui : optional, string
2456 gui : optional, string
2521
2457
2522 If given, dictates the choice of matplotlib GUI backend to use
2458 If given, dictates the choice of matplotlib GUI backend to use
2523 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2459 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2524 'gtk'), otherwise we use the default chosen by matplotlib (as
2460 'gtk'), otherwise we use the default chosen by matplotlib (as
2525 dictated by the matplotlib build-time options plus the user's
2461 dictated by the matplotlib build-time options plus the user's
2526 matplotlibrc configuration file).
2462 matplotlibrc configuration file).
2527 """
2463 """
2528 # We want to prevent the loading of pylab to pollute the user's
2464 # We want to prevent the loading of pylab to pollute the user's
2529 # namespace as shown by the %who* magics, so we execute the activation
2465 # namespace as shown by the %who* magics, so we execute the activation
2530 # code in an empty namespace, and we update *both* user_ns and
2466 # code in an empty namespace, and we update *both* user_ns and
2531 # user_ns_hidden with this information.
2467 # user_ns_hidden with this information.
2532 ns = {}
2468 ns = {}
2533 gui = pylab_activate(ns, gui)
2469 gui = pylab_activate(ns, gui)
2534 self.user_ns.update(ns)
2470 self.user_ns.update(ns)
2535 self.user_ns_hidden.update(ns)
2471 self.user_ns_hidden.update(ns)
2536 # Now we must activate the gui pylab wants to use, and fix %run to take
2472 # Now we must activate the gui pylab wants to use, and fix %run to take
2537 # plot updates into account
2473 # plot updates into account
2538 enable_gui(gui)
2474 enable_gui(gui)
2539 self.magic_run = self._pylab_magic_run
2475 self.magic_run = self._pylab_magic_run
2540
2476
2541 #-------------------------------------------------------------------------
2477 #-------------------------------------------------------------------------
2542 # Things related to IPython exiting
2478 # Things related to IPython exiting
2543 #-------------------------------------------------------------------------
2479 #-------------------------------------------------------------------------
2544
2480
2545 def ask_exit(self):
2481 def ask_exit(self):
2546 """ Ask the shell to exit. Can be overiden and used as a callback. """
2482 """ Ask the shell to exit. Can be overiden and used as a callback. """
2547 self.exit_now = True
2483 self.exit_now = True
2548
2484
2549 def exit(self):
2485 def exit(self):
2550 """Handle interactive exit.
2486 """Handle interactive exit.
2551
2487
2552 This method calls the ask_exit callback."""
2488 This method calls the ask_exit callback."""
2553 if self.confirm_exit:
2489 if self.confirm_exit:
2554 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2490 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2555 self.ask_exit()
2491 self.ask_exit()
2556 else:
2492 else:
2557 self.ask_exit()
2493 self.ask_exit()
2558
2494
2559 def atexit_operations(self):
2495 def atexit_operations(self):
2560 """This will be executed at the time of exit.
2496 """This will be executed at the time of exit.
2561
2497
2562 Saving of persistent data should be performed here.
2498 Saving of persistent data should be performed here.
2563 """
2499 """
2564 self.savehist()
2500 self.savehist()
2565
2501
2566 # Cleanup all tempfiles left around
2502 # Cleanup all tempfiles left around
2567 for tfile in self.tempfiles:
2503 for tfile in self.tempfiles:
2568 try:
2504 try:
2569 os.unlink(tfile)
2505 os.unlink(tfile)
2570 except OSError:
2506 except OSError:
2571 pass
2507 pass
2572
2508
2573 # Clear all user namespaces to release all references cleanly.
2509 # Clear all user namespaces to release all references cleanly.
2574 self.reset()
2510 self.reset()
2575
2511
2576 # Run user hooks
2512 # Run user hooks
2577 self.hooks.shutdown_hook()
2513 self.hooks.shutdown_hook()
2578
2514
2579 def cleanup(self):
2515 def cleanup(self):
2580 self.restore_sys_module_state()
2516 self.restore_sys_module_state()
2581
2517
2582
2518
2519 class InteractiveShellABC(object):
2520 """An abstract base class for InteractiveShell."""
2521 __metaclass__ = abc.ABCMeta
2522
2523 InteractiveShellABC.register(InteractiveShell)
@@ -1,3708 +1,3708 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-2007 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2008-2009 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
17
18 import __builtin__
18 import __builtin__
19 import __future__
19 import __future__
20 import bdb
20 import bdb
21 import inspect
21 import inspect
22 import os
22 import os
23 import sys
23 import sys
24 import shutil
24 import shutil
25 import re
25 import re
26 import time
26 import time
27 import textwrap
27 import textwrap
28 import types
28 import types
29 from cStringIO import StringIO
29 from cStringIO import StringIO
30 from getopt import getopt,GetoptError
30 from getopt import getopt,GetoptError
31 from pprint import pformat
31 from pprint import pformat
32
32
33 # cProfile was added in Python2.5
33 # cProfile was added in Python2.5
34 try:
34 try:
35 import cProfile as profile
35 import cProfile as profile
36 import pstats
36 import pstats
37 except ImportError:
37 except ImportError:
38 # profile isn't bundled by default in Debian for license reasons
38 # profile isn't bundled by default in Debian for license reasons
39 try:
39 try:
40 import profile,pstats
40 import profile,pstats
41 except ImportError:
41 except ImportError:
42 profile = pstats = None
42 profile = pstats = None
43
43
44 # print_function was added to __future__ in Python2.6, remove this when we drop
44 # print_function was added to __future__ in Python2.6, remove this when we drop
45 # 2.5 compatibility
45 # 2.5 compatibility
46 if not hasattr(__future__,'CO_FUTURE_PRINT_FUNCTION'):
46 if not hasattr(__future__,'CO_FUTURE_PRINT_FUNCTION'):
47 __future__.CO_FUTURE_PRINT_FUNCTION = 65536
47 __future__.CO_FUTURE_PRINT_FUNCTION = 65536
48
48
49 import IPython
49 import IPython
50 from IPython.core import debugger, oinspect
50 from IPython.core import debugger, oinspect
51 from IPython.core.error import TryNext
51 from IPython.core.error import TryNext
52 from IPython.core.error import UsageError
52 from IPython.core.error import UsageError
53 from IPython.core.fakemodule import FakeModule
53 from IPython.core.fakemodule import FakeModule
54 from IPython.core.macro import Macro
54 from IPython.core.macro import Macro
55 from IPython.core.page import page
55 from IPython.core.page import page
56 from IPython.core.prefilter import ESC_MAGIC
56 from IPython.core.prefilter import ESC_MAGIC
57 from IPython.lib.pylabtools import mpl_runner
57 from IPython.lib.pylabtools import mpl_runner
58 from IPython.lib.inputhook import enable_gui
58 from IPython.lib.inputhook import enable_gui
59 from IPython.external.Itpl import itpl, printpl
59 from IPython.external.Itpl import itpl, printpl
60 from IPython.testing import decorators as testdec
60 from IPython.testing import decorators as testdec
61 from IPython.utils.io import Term, file_read, nlprint
61 from IPython.utils.io import Term, file_read, nlprint
62 from IPython.utils.path import get_py_filename
62 from IPython.utils.path import get_py_filename
63 from IPython.utils.process import arg_split, abbrev_cwd
63 from IPython.utils.process import arg_split, abbrev_cwd
64 from IPython.utils.terminal import set_term_title
64 from IPython.utils.terminal import set_term_title
65 from IPython.utils.text import LSString, SList, StringTypes
65 from IPython.utils.text import LSString, SList, StringTypes
66 from IPython.utils.timing import clock, clock2
66 from IPython.utils.timing import clock, clock2
67 from IPython.utils.warn import warn, error
67 from IPython.utils.warn import warn, error
68 from IPython.utils.ipstruct import Struct
68 from IPython.utils.ipstruct import Struct
69 import IPython.utils.generics
69 import IPython.utils.generics
70
70
71 #-----------------------------------------------------------------------------
71 #-----------------------------------------------------------------------------
72 # Utility functions
72 # Utility functions
73 #-----------------------------------------------------------------------------
73 #-----------------------------------------------------------------------------
74
74
75 def on_off(tag):
75 def on_off(tag):
76 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
76 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
77 return ['OFF','ON'][tag]
77 return ['OFF','ON'][tag]
78
78
79 class Bunch: pass
79 class Bunch: pass
80
80
81 def compress_dhist(dh):
81 def compress_dhist(dh):
82 head, tail = dh[:-10], dh[-10:]
82 head, tail = dh[:-10], dh[-10:]
83
83
84 newhead = []
84 newhead = []
85 done = set()
85 done = set()
86 for h in head:
86 for h in head:
87 if h in done:
87 if h in done:
88 continue
88 continue
89 newhead.append(h)
89 newhead.append(h)
90 done.add(h)
90 done.add(h)
91
91
92 return newhead + tail
92 return newhead + tail
93
93
94
94
95 #***************************************************************************
95 #***************************************************************************
96 # Main class implementing Magic functionality
96 # Main class implementing Magic functionality
97
97
98 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
98 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
99 # on construction of the main InteractiveShell object. Something odd is going
99 # on construction of the main InteractiveShell object. Something odd is going
100 # on with super() calls, Component and the MRO... For now leave it as-is, but
100 # on with super() calls, Configurable and the MRO... For now leave it as-is, but
101 # eventually this needs to be clarified.
101 # eventually this needs to be clarified.
102 # BG: This is because InteractiveShell inherits from this, but is itself a
102 # BG: This is because InteractiveShell inherits from this, but is itself a
103 # Component. This messes up the MRO in some way. The fix is that we need to
103 # Configurable. This messes up the MRO in some way. The fix is that we need to
104 # make Magic a component that InteractiveShell does not subclass.
104 # make Magic a configurable that InteractiveShell does not subclass.
105
105
106 class Magic:
106 class Magic:
107 """Magic functions for InteractiveShell.
107 """Magic functions for InteractiveShell.
108
108
109 Shell functions which can be reached as %function_name. All magic
109 Shell functions which can be reached as %function_name. All magic
110 functions should accept a string, which they can parse for their own
110 functions should accept a string, which they can parse for their own
111 needs. This can make some functions easier to type, eg `%cd ../`
111 needs. This can make some functions easier to type, eg `%cd ../`
112 vs. `%cd("../")`
112 vs. `%cd("../")`
113
113
114 ALL definitions MUST begin with the prefix magic_. The user won't need it
114 ALL definitions MUST begin with the prefix magic_. The user won't need it
115 at the command line, but it is is needed in the definition. """
115 at the command line, but it is is needed in the definition. """
116
116
117 # class globals
117 # class globals
118 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
118 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
119 'Automagic is ON, % prefix NOT needed for magic functions.']
119 'Automagic is ON, % prefix NOT needed for magic functions.']
120
120
121 #......................................................................
121 #......................................................................
122 # some utility functions
122 # some utility functions
123
123
124 def __init__(self,shell):
124 def __init__(self,shell):
125
125
126 self.options_table = {}
126 self.options_table = {}
127 if profile is None:
127 if profile is None:
128 self.magic_prun = self.profile_missing_notice
128 self.magic_prun = self.profile_missing_notice
129 self.shell = shell
129 self.shell = shell
130
130
131 # namespace for holding state we may need
131 # namespace for holding state we may need
132 self._magic_state = Bunch()
132 self._magic_state = Bunch()
133
133
134 def profile_missing_notice(self, *args, **kwargs):
134 def profile_missing_notice(self, *args, **kwargs):
135 error("""\
135 error("""\
136 The profile module could not be found. It has been removed from the standard
136 The profile module could not be found. It has been removed from the standard
137 python packages because of its non-free license. To use profiling, install the
137 python packages because of its non-free license. To use profiling, install the
138 python-profiler package from non-free.""")
138 python-profiler package from non-free.""")
139
139
140 def default_option(self,fn,optstr):
140 def default_option(self,fn,optstr):
141 """Make an entry in the options_table for fn, with value optstr"""
141 """Make an entry in the options_table for fn, with value optstr"""
142
142
143 if fn not in self.lsmagic():
143 if fn not in self.lsmagic():
144 error("%s is not a magic function" % fn)
144 error("%s is not a magic function" % fn)
145 self.options_table[fn] = optstr
145 self.options_table[fn] = optstr
146
146
147 def lsmagic(self):
147 def lsmagic(self):
148 """Return a list of currently available magic functions.
148 """Return a list of currently available magic functions.
149
149
150 Gives a list of the bare names after mangling (['ls','cd', ...], not
150 Gives a list of the bare names after mangling (['ls','cd', ...], not
151 ['magic_ls','magic_cd',...]"""
151 ['magic_ls','magic_cd',...]"""
152
152
153 # FIXME. This needs a cleanup, in the way the magics list is built.
153 # FIXME. This needs a cleanup, in the way the magics list is built.
154
154
155 # magics in class definition
155 # magics in class definition
156 class_magic = lambda fn: fn.startswith('magic_') and \
156 class_magic = lambda fn: fn.startswith('magic_') and \
157 callable(Magic.__dict__[fn])
157 callable(Magic.__dict__[fn])
158 # in instance namespace (run-time user additions)
158 # in instance namespace (run-time user additions)
159 inst_magic = lambda fn: fn.startswith('magic_') and \
159 inst_magic = lambda fn: fn.startswith('magic_') and \
160 callable(self.__dict__[fn])
160 callable(self.__dict__[fn])
161 # and bound magics by user (so they can access self):
161 # and bound magics by user (so they can access self):
162 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
162 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
163 callable(self.__class__.__dict__[fn])
163 callable(self.__class__.__dict__[fn])
164 magics = filter(class_magic,Magic.__dict__.keys()) + \
164 magics = filter(class_magic,Magic.__dict__.keys()) + \
165 filter(inst_magic,self.__dict__.keys()) + \
165 filter(inst_magic,self.__dict__.keys()) + \
166 filter(inst_bound_magic,self.__class__.__dict__.keys())
166 filter(inst_bound_magic,self.__class__.__dict__.keys())
167 out = []
167 out = []
168 for fn in set(magics):
168 for fn in set(magics):
169 out.append(fn.replace('magic_','',1))
169 out.append(fn.replace('magic_','',1))
170 out.sort()
170 out.sort()
171 return out
171 return out
172
172
173 def extract_input_slices(self,slices,raw=False):
173 def extract_input_slices(self,slices,raw=False):
174 """Return as a string a set of input history slices.
174 """Return as a string a set of input history slices.
175
175
176 Inputs:
176 Inputs:
177
177
178 - slices: the set of slices is given as a list of strings (like
178 - slices: the set of slices is given as a list of strings (like
179 ['1','4:8','9'], since this function is for use by magic functions
179 ['1','4:8','9'], since this function is for use by magic functions
180 which get their arguments as strings.
180 which get their arguments as strings.
181
181
182 Optional inputs:
182 Optional inputs:
183
183
184 - raw(False): by default, the processed input is used. If this is
184 - raw(False): by default, the processed input is used. If this is
185 true, the raw input history is used instead.
185 true, the raw input history is used instead.
186
186
187 Note that slices can be called with two notations:
187 Note that slices can be called with two notations:
188
188
189 N:M -> standard python form, means including items N...(M-1).
189 N:M -> standard python form, means including items N...(M-1).
190
190
191 N-M -> include items N..M (closed endpoint)."""
191 N-M -> include items N..M (closed endpoint)."""
192
192
193 if raw:
193 if raw:
194 hist = self.shell.input_hist_raw
194 hist = self.shell.input_hist_raw
195 else:
195 else:
196 hist = self.shell.input_hist
196 hist = self.shell.input_hist
197
197
198 cmds = []
198 cmds = []
199 for chunk in slices:
199 for chunk in slices:
200 if ':' in chunk:
200 if ':' in chunk:
201 ini,fin = map(int,chunk.split(':'))
201 ini,fin = map(int,chunk.split(':'))
202 elif '-' in chunk:
202 elif '-' in chunk:
203 ini,fin = map(int,chunk.split('-'))
203 ini,fin = map(int,chunk.split('-'))
204 fin += 1
204 fin += 1
205 else:
205 else:
206 ini = int(chunk)
206 ini = int(chunk)
207 fin = ini+1
207 fin = ini+1
208 cmds.append(hist[ini:fin])
208 cmds.append(hist[ini:fin])
209 return cmds
209 return cmds
210
210
211 def _ofind(self, oname, namespaces=None):
211 def _ofind(self, oname, namespaces=None):
212 """Find an object in the available namespaces.
212 """Find an object in the available namespaces.
213
213
214 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
214 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
215
215
216 Has special code to detect magic functions.
216 Has special code to detect magic functions.
217 """
217 """
218 oname = oname.strip()
218 oname = oname.strip()
219 alias_ns = None
219 alias_ns = None
220 if namespaces is None:
220 if namespaces is None:
221 # Namespaces to search in:
221 # Namespaces to search in:
222 # Put them in a list. The order is important so that we
222 # Put them in a list. The order is important so that we
223 # find things in the same order that Python finds them.
223 # find things in the same order that Python finds them.
224 namespaces = [ ('Interactive', self.shell.user_ns),
224 namespaces = [ ('Interactive', self.shell.user_ns),
225 ('IPython internal', self.shell.internal_ns),
225 ('IPython internal', self.shell.internal_ns),
226 ('Python builtin', __builtin__.__dict__),
226 ('Python builtin', __builtin__.__dict__),
227 ('Alias', self.shell.alias_manager.alias_table),
227 ('Alias', self.shell.alias_manager.alias_table),
228 ]
228 ]
229 alias_ns = self.shell.alias_manager.alias_table
229 alias_ns = self.shell.alias_manager.alias_table
230
230
231 # initialize results to 'null'
231 # initialize results to 'null'
232 found = False; obj = None; ospace = None; ds = None;
232 found = False; obj = None; ospace = None; ds = None;
233 ismagic = False; isalias = False; parent = None
233 ismagic = False; isalias = False; parent = None
234
234
235 # We need to special-case 'print', which as of python2.6 registers as a
235 # We need to special-case 'print', which as of python2.6 registers as a
236 # function but should only be treated as one if print_function was
236 # function but should only be treated as one if print_function was
237 # loaded with a future import. In this case, just bail.
237 # loaded with a future import. In this case, just bail.
238 if (oname == 'print' and not (self.shell.compile.compiler.flags &
238 if (oname == 'print' and not (self.shell.compile.compiler.flags &
239 __future__.CO_FUTURE_PRINT_FUNCTION)):
239 __future__.CO_FUTURE_PRINT_FUNCTION)):
240 return {'found':found, 'obj':obj, 'namespace':ospace,
240 return {'found':found, 'obj':obj, 'namespace':ospace,
241 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
241 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
242
242
243 # Look for the given name by splitting it in parts. If the head is
243 # Look for the given name by splitting it in parts. If the head is
244 # found, then we look for all the remaining parts as members, and only
244 # found, then we look for all the remaining parts as members, and only
245 # declare success if we can find them all.
245 # declare success if we can find them all.
246 oname_parts = oname.split('.')
246 oname_parts = oname.split('.')
247 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
247 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
248 for nsname,ns in namespaces:
248 for nsname,ns in namespaces:
249 try:
249 try:
250 obj = ns[oname_head]
250 obj = ns[oname_head]
251 except KeyError:
251 except KeyError:
252 continue
252 continue
253 else:
253 else:
254 #print 'oname_rest:', oname_rest # dbg
254 #print 'oname_rest:', oname_rest # dbg
255 for part in oname_rest:
255 for part in oname_rest:
256 try:
256 try:
257 parent = obj
257 parent = obj
258 obj = getattr(obj,part)
258 obj = getattr(obj,part)
259 except:
259 except:
260 # Blanket except b/c some badly implemented objects
260 # Blanket except b/c some badly implemented objects
261 # allow __getattr__ to raise exceptions other than
261 # allow __getattr__ to raise exceptions other than
262 # AttributeError, which then crashes IPython.
262 # AttributeError, which then crashes IPython.
263 break
263 break
264 else:
264 else:
265 # If we finish the for loop (no break), we got all members
265 # If we finish the for loop (no break), we got all members
266 found = True
266 found = True
267 ospace = nsname
267 ospace = nsname
268 if ns == alias_ns:
268 if ns == alias_ns:
269 isalias = True
269 isalias = True
270 break # namespace loop
270 break # namespace loop
271
271
272 # Try to see if it's magic
272 # Try to see if it's magic
273 if not found:
273 if not found:
274 if oname.startswith(ESC_MAGIC):
274 if oname.startswith(ESC_MAGIC):
275 oname = oname[1:]
275 oname = oname[1:]
276 obj = getattr(self,'magic_'+oname,None)
276 obj = getattr(self,'magic_'+oname,None)
277 if obj is not None:
277 if obj is not None:
278 found = True
278 found = True
279 ospace = 'IPython internal'
279 ospace = 'IPython internal'
280 ismagic = True
280 ismagic = True
281
281
282 # Last try: special-case some literals like '', [], {}, etc:
282 # Last try: special-case some literals like '', [], {}, etc:
283 if not found and oname_head in ["''",'""','[]','{}','()']:
283 if not found and oname_head in ["''",'""','[]','{}','()']:
284 obj = eval(oname_head)
284 obj = eval(oname_head)
285 found = True
285 found = True
286 ospace = 'Interactive'
286 ospace = 'Interactive'
287
287
288 return {'found':found, 'obj':obj, 'namespace':ospace,
288 return {'found':found, 'obj':obj, 'namespace':ospace,
289 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
289 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
290
290
291 def arg_err(self,func):
291 def arg_err(self,func):
292 """Print docstring if incorrect arguments were passed"""
292 """Print docstring if incorrect arguments were passed"""
293 print 'Error in arguments:'
293 print 'Error in arguments:'
294 print oinspect.getdoc(func)
294 print oinspect.getdoc(func)
295
295
296 def format_latex(self,strng):
296 def format_latex(self,strng):
297 """Format a string for latex inclusion."""
297 """Format a string for latex inclusion."""
298
298
299 # Characters that need to be escaped for latex:
299 # Characters that need to be escaped for latex:
300 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
300 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
301 # Magic command names as headers:
301 # Magic command names as headers:
302 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
302 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
303 re.MULTILINE)
303 re.MULTILINE)
304 # Magic commands
304 # Magic commands
305 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
305 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
306 re.MULTILINE)
306 re.MULTILINE)
307 # Paragraph continue
307 # Paragraph continue
308 par_re = re.compile(r'\\$',re.MULTILINE)
308 par_re = re.compile(r'\\$',re.MULTILINE)
309
309
310 # The "\n" symbol
310 # The "\n" symbol
311 newline_re = re.compile(r'\\n')
311 newline_re = re.compile(r'\\n')
312
312
313 # Now build the string for output:
313 # Now build the string for output:
314 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
314 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
315 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
315 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
316 strng)
316 strng)
317 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
317 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
318 strng = par_re.sub(r'\\\\',strng)
318 strng = par_re.sub(r'\\\\',strng)
319 strng = escape_re.sub(r'\\\1',strng)
319 strng = escape_re.sub(r'\\\1',strng)
320 strng = newline_re.sub(r'\\textbackslash{}n',strng)
320 strng = newline_re.sub(r'\\textbackslash{}n',strng)
321 return strng
321 return strng
322
322
323 def format_screen(self,strng):
323 def format_screen(self,strng):
324 """Format a string for screen printing.
324 """Format a string for screen printing.
325
325
326 This removes some latex-type format codes."""
326 This removes some latex-type format codes."""
327 # Paragraph continue
327 # Paragraph continue
328 par_re = re.compile(r'\\$',re.MULTILINE)
328 par_re = re.compile(r'\\$',re.MULTILINE)
329 strng = par_re.sub('',strng)
329 strng = par_re.sub('',strng)
330 return strng
330 return strng
331
331
332 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
332 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
333 """Parse options passed to an argument string.
333 """Parse options passed to an argument string.
334
334
335 The interface is similar to that of getopt(), but it returns back a
335 The interface is similar to that of getopt(), but it returns back a
336 Struct with the options as keys and the stripped argument string still
336 Struct with the options as keys and the stripped argument string still
337 as a string.
337 as a string.
338
338
339 arg_str is quoted as a true sys.argv vector by using shlex.split.
339 arg_str is quoted as a true sys.argv vector by using shlex.split.
340 This allows us to easily expand variables, glob files, quote
340 This allows us to easily expand variables, glob files, quote
341 arguments, etc.
341 arguments, etc.
342
342
343 Options:
343 Options:
344 -mode: default 'string'. If given as 'list', the argument string is
344 -mode: default 'string'. If given as 'list', the argument string is
345 returned as a list (split on whitespace) instead of a string.
345 returned as a list (split on whitespace) instead of a string.
346
346
347 -list_all: put all option values in lists. Normally only options
347 -list_all: put all option values in lists. Normally only options
348 appearing more than once are put in a list.
348 appearing more than once are put in a list.
349
349
350 -posix (True): whether to split the input line in POSIX mode or not,
350 -posix (True): whether to split the input line in POSIX mode or not,
351 as per the conventions outlined in the shlex module from the
351 as per the conventions outlined in the shlex module from the
352 standard library."""
352 standard library."""
353
353
354 # inject default options at the beginning of the input line
354 # inject default options at the beginning of the input line
355 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
355 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
356 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
356 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
357
357
358 mode = kw.get('mode','string')
358 mode = kw.get('mode','string')
359 if mode not in ['string','list']:
359 if mode not in ['string','list']:
360 raise ValueError,'incorrect mode given: %s' % mode
360 raise ValueError,'incorrect mode given: %s' % mode
361 # Get options
361 # Get options
362 list_all = kw.get('list_all',0)
362 list_all = kw.get('list_all',0)
363 posix = kw.get('posix', os.name == 'posix')
363 posix = kw.get('posix', os.name == 'posix')
364
364
365 # Check if we have more than one argument to warrant extra processing:
365 # Check if we have more than one argument to warrant extra processing:
366 odict = {} # Dictionary with options
366 odict = {} # Dictionary with options
367 args = arg_str.split()
367 args = arg_str.split()
368 if len(args) >= 1:
368 if len(args) >= 1:
369 # If the list of inputs only has 0 or 1 thing in it, there's no
369 # If the list of inputs only has 0 or 1 thing in it, there's no
370 # need to look for options
370 # need to look for options
371 argv = arg_split(arg_str,posix)
371 argv = arg_split(arg_str,posix)
372 # Do regular option processing
372 # Do regular option processing
373 try:
373 try:
374 opts,args = getopt(argv,opt_str,*long_opts)
374 opts,args = getopt(argv,opt_str,*long_opts)
375 except GetoptError,e:
375 except GetoptError,e:
376 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
376 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
377 " ".join(long_opts)))
377 " ".join(long_opts)))
378 for o,a in opts:
378 for o,a in opts:
379 if o.startswith('--'):
379 if o.startswith('--'):
380 o = o[2:]
380 o = o[2:]
381 else:
381 else:
382 o = o[1:]
382 o = o[1:]
383 try:
383 try:
384 odict[o].append(a)
384 odict[o].append(a)
385 except AttributeError:
385 except AttributeError:
386 odict[o] = [odict[o],a]
386 odict[o] = [odict[o],a]
387 except KeyError:
387 except KeyError:
388 if list_all:
388 if list_all:
389 odict[o] = [a]
389 odict[o] = [a]
390 else:
390 else:
391 odict[o] = a
391 odict[o] = a
392
392
393 # Prepare opts,args for return
393 # Prepare opts,args for return
394 opts = Struct(odict)
394 opts = Struct(odict)
395 if mode == 'string':
395 if mode == 'string':
396 args = ' '.join(args)
396 args = ' '.join(args)
397
397
398 return opts,args
398 return opts,args
399
399
400 #......................................................................
400 #......................................................................
401 # And now the actual magic functions
401 # And now the actual magic functions
402
402
403 # Functions for IPython shell work (vars,funcs, config, etc)
403 # Functions for IPython shell work (vars,funcs, config, etc)
404 def magic_lsmagic(self, parameter_s = ''):
404 def magic_lsmagic(self, parameter_s = ''):
405 """List currently available magic functions."""
405 """List currently available magic functions."""
406 mesc = ESC_MAGIC
406 mesc = ESC_MAGIC
407 print 'Available magic functions:\n'+mesc+\
407 print 'Available magic functions:\n'+mesc+\
408 (' '+mesc).join(self.lsmagic())
408 (' '+mesc).join(self.lsmagic())
409 print '\n' + Magic.auto_status[self.shell.automagic]
409 print '\n' + Magic.auto_status[self.shell.automagic]
410 return None
410 return None
411
411
412 def magic_magic(self, parameter_s = ''):
412 def magic_magic(self, parameter_s = ''):
413 """Print information about the magic function system.
413 """Print information about the magic function system.
414
414
415 Supported formats: -latex, -brief, -rest
415 Supported formats: -latex, -brief, -rest
416 """
416 """
417
417
418 mode = ''
418 mode = ''
419 try:
419 try:
420 if parameter_s.split()[0] == '-latex':
420 if parameter_s.split()[0] == '-latex':
421 mode = 'latex'
421 mode = 'latex'
422 if parameter_s.split()[0] == '-brief':
422 if parameter_s.split()[0] == '-brief':
423 mode = 'brief'
423 mode = 'brief'
424 if parameter_s.split()[0] == '-rest':
424 if parameter_s.split()[0] == '-rest':
425 mode = 'rest'
425 mode = 'rest'
426 rest_docs = []
426 rest_docs = []
427 except:
427 except:
428 pass
428 pass
429
429
430 magic_docs = []
430 magic_docs = []
431 for fname in self.lsmagic():
431 for fname in self.lsmagic():
432 mname = 'magic_' + fname
432 mname = 'magic_' + fname
433 for space in (Magic,self,self.__class__):
433 for space in (Magic,self,self.__class__):
434 try:
434 try:
435 fn = space.__dict__[mname]
435 fn = space.__dict__[mname]
436 except KeyError:
436 except KeyError:
437 pass
437 pass
438 else:
438 else:
439 break
439 break
440 if mode == 'brief':
440 if mode == 'brief':
441 # only first line
441 # only first line
442 if fn.__doc__:
442 if fn.__doc__:
443 fndoc = fn.__doc__.split('\n',1)[0]
443 fndoc = fn.__doc__.split('\n',1)[0]
444 else:
444 else:
445 fndoc = 'No documentation'
445 fndoc = 'No documentation'
446 else:
446 else:
447 if fn.__doc__:
447 if fn.__doc__:
448 fndoc = fn.__doc__.rstrip()
448 fndoc = fn.__doc__.rstrip()
449 else:
449 else:
450 fndoc = 'No documentation'
450 fndoc = 'No documentation'
451
451
452
452
453 if mode == 'rest':
453 if mode == 'rest':
454 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
454 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
455 fname,fndoc))
455 fname,fndoc))
456
456
457 else:
457 else:
458 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
458 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
459 fname,fndoc))
459 fname,fndoc))
460
460
461 magic_docs = ''.join(magic_docs)
461 magic_docs = ''.join(magic_docs)
462
462
463 if mode == 'rest':
463 if mode == 'rest':
464 return "".join(rest_docs)
464 return "".join(rest_docs)
465
465
466 if mode == 'latex':
466 if mode == 'latex':
467 print self.format_latex(magic_docs)
467 print self.format_latex(magic_docs)
468 return
468 return
469 else:
469 else:
470 magic_docs = self.format_screen(magic_docs)
470 magic_docs = self.format_screen(magic_docs)
471 if mode == 'brief':
471 if mode == 'brief':
472 return magic_docs
472 return magic_docs
473
473
474 outmsg = """
474 outmsg = """
475 IPython's 'magic' functions
475 IPython's 'magic' functions
476 ===========================
476 ===========================
477
477
478 The magic function system provides a series of functions which allow you to
478 The magic function system provides a series of functions which allow you to
479 control the behavior of IPython itself, plus a lot of system-type
479 control the behavior of IPython itself, plus a lot of system-type
480 features. All these functions are prefixed with a % character, but parameters
480 features. All these functions are prefixed with a % character, but parameters
481 are given without parentheses or quotes.
481 are given without parentheses or quotes.
482
482
483 NOTE: If you have 'automagic' enabled (via the command line option or with the
483 NOTE: If you have 'automagic' enabled (via the command line option or with the
484 %automagic function), you don't need to type in the % explicitly. By default,
484 %automagic function), you don't need to type in the % explicitly. By default,
485 IPython ships with automagic on, so you should only rarely need the % escape.
485 IPython ships with automagic on, so you should only rarely need the % escape.
486
486
487 Example: typing '%cd mydir' (without the quotes) changes you working directory
487 Example: typing '%cd mydir' (without the quotes) changes you working directory
488 to 'mydir', if it exists.
488 to 'mydir', if it exists.
489
489
490 You can define your own magic functions to extend the system. See the supplied
490 You can define your own magic functions to extend the system. See the supplied
491 ipythonrc and example-magic.py files for details (in your ipython
491 ipythonrc and example-magic.py files for details (in your ipython
492 configuration directory, typically $HOME/.ipython/).
492 configuration directory, typically $HOME/.ipython/).
493
493
494 You can also define your own aliased names for magic functions. In your
494 You can also define your own aliased names for magic functions. In your
495 ipythonrc file, placing a line like:
495 ipythonrc file, placing a line like:
496
496
497 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
497 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
498
498
499 will define %pf as a new name for %profile.
499 will define %pf as a new name for %profile.
500
500
501 You can also call magics in code using the magic() function, which IPython
501 You can also call magics in code using the magic() function, which IPython
502 automatically adds to the builtin namespace. Type 'magic?' for details.
502 automatically adds to the builtin namespace. Type 'magic?' for details.
503
503
504 For a list of the available magic functions, use %lsmagic. For a description
504 For a list of the available magic functions, use %lsmagic. For a description
505 of any of them, type %magic_name?, e.g. '%cd?'.
505 of any of them, type %magic_name?, e.g. '%cd?'.
506
506
507 Currently the magic system has the following functions:\n"""
507 Currently the magic system has the following functions:\n"""
508
508
509 mesc = ESC_MAGIC
509 mesc = ESC_MAGIC
510 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
510 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
511 "\n\n%s%s\n\n%s" % (outmsg,
511 "\n\n%s%s\n\n%s" % (outmsg,
512 magic_docs,mesc,mesc,
512 magic_docs,mesc,mesc,
513 (' '+mesc).join(self.lsmagic()),
513 (' '+mesc).join(self.lsmagic()),
514 Magic.auto_status[self.shell.automagic] ) )
514 Magic.auto_status[self.shell.automagic] ) )
515
515
516 page(outmsg,screen_lines=self.shell.usable_screen_length)
516 page(outmsg,screen_lines=self.shell.usable_screen_length)
517
517
518
518
519 def magic_autoindent(self, parameter_s = ''):
519 def magic_autoindent(self, parameter_s = ''):
520 """Toggle autoindent on/off (if available)."""
520 """Toggle autoindent on/off (if available)."""
521
521
522 self.shell.set_autoindent()
522 self.shell.set_autoindent()
523 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
523 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
524
524
525
525
526 def magic_automagic(self, parameter_s = ''):
526 def magic_automagic(self, parameter_s = ''):
527 """Make magic functions callable without having to type the initial %.
527 """Make magic functions callable without having to type the initial %.
528
528
529 Without argumentsl toggles on/off (when off, you must call it as
529 Without argumentsl toggles on/off (when off, you must call it as
530 %automagic, of course). With arguments it sets the value, and you can
530 %automagic, of course). With arguments it sets the value, and you can
531 use any of (case insensitive):
531 use any of (case insensitive):
532
532
533 - on,1,True: to activate
533 - on,1,True: to activate
534
534
535 - off,0,False: to deactivate.
535 - off,0,False: to deactivate.
536
536
537 Note that magic functions have lowest priority, so if there's a
537 Note that magic functions have lowest priority, so if there's a
538 variable whose name collides with that of a magic fn, automagic won't
538 variable whose name collides with that of a magic fn, automagic won't
539 work for that function (you get the variable instead). However, if you
539 work for that function (you get the variable instead). However, if you
540 delete the variable (del var), the previously shadowed magic function
540 delete the variable (del var), the previously shadowed magic function
541 becomes visible to automagic again."""
541 becomes visible to automagic again."""
542
542
543 arg = parameter_s.lower()
543 arg = parameter_s.lower()
544 if parameter_s in ('on','1','true'):
544 if parameter_s in ('on','1','true'):
545 self.shell.automagic = True
545 self.shell.automagic = True
546 elif parameter_s in ('off','0','false'):
546 elif parameter_s in ('off','0','false'):
547 self.shell.automagic = False
547 self.shell.automagic = False
548 else:
548 else:
549 self.shell.automagic = not self.shell.automagic
549 self.shell.automagic = not self.shell.automagic
550 print '\n' + Magic.auto_status[self.shell.automagic]
550 print '\n' + Magic.auto_status[self.shell.automagic]
551
551
552 @testdec.skip_doctest
552 @testdec.skip_doctest
553 def magic_autocall(self, parameter_s = ''):
553 def magic_autocall(self, parameter_s = ''):
554 """Make functions callable without having to type parentheses.
554 """Make functions callable without having to type parentheses.
555
555
556 Usage:
556 Usage:
557
557
558 %autocall [mode]
558 %autocall [mode]
559
559
560 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
560 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
561 value is toggled on and off (remembering the previous state).
561 value is toggled on and off (remembering the previous state).
562
562
563 In more detail, these values mean:
563 In more detail, these values mean:
564
564
565 0 -> fully disabled
565 0 -> fully disabled
566
566
567 1 -> active, but do not apply if there are no arguments on the line.
567 1 -> active, but do not apply if there are no arguments on the line.
568
568
569 In this mode, you get:
569 In this mode, you get:
570
570
571 In [1]: callable
571 In [1]: callable
572 Out[1]: <built-in function callable>
572 Out[1]: <built-in function callable>
573
573
574 In [2]: callable 'hello'
574 In [2]: callable 'hello'
575 ------> callable('hello')
575 ------> callable('hello')
576 Out[2]: False
576 Out[2]: False
577
577
578 2 -> Active always. Even if no arguments are present, the callable
578 2 -> Active always. Even if no arguments are present, the callable
579 object is called:
579 object is called:
580
580
581 In [2]: float
581 In [2]: float
582 ------> float()
582 ------> float()
583 Out[2]: 0.0
583 Out[2]: 0.0
584
584
585 Note that even with autocall off, you can still use '/' at the start of
585 Note that even with autocall off, you can still use '/' at the start of
586 a line to treat the first argument on the command line as a function
586 a line to treat the first argument on the command line as a function
587 and add parentheses to it:
587 and add parentheses to it:
588
588
589 In [8]: /str 43
589 In [8]: /str 43
590 ------> str(43)
590 ------> str(43)
591 Out[8]: '43'
591 Out[8]: '43'
592
592
593 # all-random (note for auto-testing)
593 # all-random (note for auto-testing)
594 """
594 """
595
595
596 if parameter_s:
596 if parameter_s:
597 arg = int(parameter_s)
597 arg = int(parameter_s)
598 else:
598 else:
599 arg = 'toggle'
599 arg = 'toggle'
600
600
601 if not arg in (0,1,2,'toggle'):
601 if not arg in (0,1,2,'toggle'):
602 error('Valid modes: (0->Off, 1->Smart, 2->Full')
602 error('Valid modes: (0->Off, 1->Smart, 2->Full')
603 return
603 return
604
604
605 if arg in (0,1,2):
605 if arg in (0,1,2):
606 self.shell.autocall = arg
606 self.shell.autocall = arg
607 else: # toggle
607 else: # toggle
608 if self.shell.autocall:
608 if self.shell.autocall:
609 self._magic_state.autocall_save = self.shell.autocall
609 self._magic_state.autocall_save = self.shell.autocall
610 self.shell.autocall = 0
610 self.shell.autocall = 0
611 else:
611 else:
612 try:
612 try:
613 self.shell.autocall = self._magic_state.autocall_save
613 self.shell.autocall = self._magic_state.autocall_save
614 except AttributeError:
614 except AttributeError:
615 self.shell.autocall = self._magic_state.autocall_save = 1
615 self.shell.autocall = self._magic_state.autocall_save = 1
616
616
617 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
617 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
618
618
619 def magic_system_verbose(self, parameter_s = ''):
619 def magic_system_verbose(self, parameter_s = ''):
620 """Set verbose printing of system calls.
620 """Set verbose printing of system calls.
621
621
622 If called without an argument, act as a toggle"""
622 If called without an argument, act as a toggle"""
623
623
624 if parameter_s:
624 if parameter_s:
625 val = bool(eval(parameter_s))
625 val = bool(eval(parameter_s))
626 else:
626 else:
627 val = None
627 val = None
628
628
629 if self.shell.system_verbose:
629 if self.shell.system_verbose:
630 self.shell.system_verbose = False
630 self.shell.system_verbose = False
631 else:
631 else:
632 self.shell.system_verbose = True
632 self.shell.system_verbose = True
633 print "System verbose printing is:",\
633 print "System verbose printing is:",\
634 ['OFF','ON'][self.shell.system_verbose]
634 ['OFF','ON'][self.shell.system_verbose]
635
635
636
636
637 def magic_page(self, parameter_s=''):
637 def magic_page(self, parameter_s=''):
638 """Pretty print the object and display it through a pager.
638 """Pretty print the object and display it through a pager.
639
639
640 %page [options] OBJECT
640 %page [options] OBJECT
641
641
642 If no object is given, use _ (last output).
642 If no object is given, use _ (last output).
643
643
644 Options:
644 Options:
645
645
646 -r: page str(object), don't pretty-print it."""
646 -r: page str(object), don't pretty-print it."""
647
647
648 # After a function contributed by Olivier Aubert, slightly modified.
648 # After a function contributed by Olivier Aubert, slightly modified.
649
649
650 # Process options/args
650 # Process options/args
651 opts,args = self.parse_options(parameter_s,'r')
651 opts,args = self.parse_options(parameter_s,'r')
652 raw = 'r' in opts
652 raw = 'r' in opts
653
653
654 oname = args and args or '_'
654 oname = args and args or '_'
655 info = self._ofind(oname)
655 info = self._ofind(oname)
656 if info['found']:
656 if info['found']:
657 txt = (raw and str or pformat)( info['obj'] )
657 txt = (raw and str or pformat)( info['obj'] )
658 page(txt)
658 page(txt)
659 else:
659 else:
660 print 'Object `%s` not found' % oname
660 print 'Object `%s` not found' % oname
661
661
662 def magic_profile(self, parameter_s=''):
662 def magic_profile(self, parameter_s=''):
663 """Print your currently active IPython profile."""
663 """Print your currently active IPython profile."""
664 if self.shell.profile:
664 if self.shell.profile:
665 printpl('Current IPython profile: $self.shell.profile.')
665 printpl('Current IPython profile: $self.shell.profile.')
666 else:
666 else:
667 print 'No profile active.'
667 print 'No profile active.'
668
668
669 def magic_pinfo(self, parameter_s='', namespaces=None):
669 def magic_pinfo(self, parameter_s='', namespaces=None):
670 """Provide detailed information about an object.
670 """Provide detailed information about an object.
671
671
672 '%pinfo object' is just a synonym for object? or ?object."""
672 '%pinfo object' is just a synonym for object? or ?object."""
673
673
674 #print 'pinfo par: <%s>' % parameter_s # dbg
674 #print 'pinfo par: <%s>' % parameter_s # dbg
675
675
676
676
677 # detail_level: 0 -> obj? , 1 -> obj??
677 # detail_level: 0 -> obj? , 1 -> obj??
678 detail_level = 0
678 detail_level = 0
679 # We need to detect if we got called as 'pinfo pinfo foo', which can
679 # We need to detect if we got called as 'pinfo pinfo foo', which can
680 # happen if the user types 'pinfo foo?' at the cmd line.
680 # happen if the user types 'pinfo foo?' at the cmd line.
681 pinfo,qmark1,oname,qmark2 = \
681 pinfo,qmark1,oname,qmark2 = \
682 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
682 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
683 if pinfo or qmark1 or qmark2:
683 if pinfo or qmark1 or qmark2:
684 detail_level = 1
684 detail_level = 1
685 if "*" in oname:
685 if "*" in oname:
686 self.magic_psearch(oname)
686 self.magic_psearch(oname)
687 else:
687 else:
688 self._inspect('pinfo', oname, detail_level=detail_level,
688 self._inspect('pinfo', oname, detail_level=detail_level,
689 namespaces=namespaces)
689 namespaces=namespaces)
690
690
691 def magic_pdef(self, parameter_s='', namespaces=None):
691 def magic_pdef(self, parameter_s='', namespaces=None):
692 """Print the definition header for any callable object.
692 """Print the definition header for any callable object.
693
693
694 If the object is a class, print the constructor information."""
694 If the object is a class, print the constructor information."""
695 self._inspect('pdef',parameter_s, namespaces)
695 self._inspect('pdef',parameter_s, namespaces)
696
696
697 def magic_pdoc(self, parameter_s='', namespaces=None):
697 def magic_pdoc(self, parameter_s='', namespaces=None):
698 """Print the docstring for an object.
698 """Print the docstring for an object.
699
699
700 If the given object is a class, it will print both the class and the
700 If the given object is a class, it will print both the class and the
701 constructor docstrings."""
701 constructor docstrings."""
702 self._inspect('pdoc',parameter_s, namespaces)
702 self._inspect('pdoc',parameter_s, namespaces)
703
703
704 def magic_psource(self, parameter_s='', namespaces=None):
704 def magic_psource(self, parameter_s='', namespaces=None):
705 """Print (or run through pager) the source code for an object."""
705 """Print (or run through pager) the source code for an object."""
706 self._inspect('psource',parameter_s, namespaces)
706 self._inspect('psource',parameter_s, namespaces)
707
707
708 def magic_pfile(self, parameter_s=''):
708 def magic_pfile(self, parameter_s=''):
709 """Print (or run through pager) the file where an object is defined.
709 """Print (or run through pager) the file where an object is defined.
710
710
711 The file opens at the line where the object definition begins. IPython
711 The file opens at the line where the object definition begins. IPython
712 will honor the environment variable PAGER if set, and otherwise will
712 will honor the environment variable PAGER if set, and otherwise will
713 do its best to print the file in a convenient form.
713 do its best to print the file in a convenient form.
714
714
715 If the given argument is not an object currently defined, IPython will
715 If the given argument is not an object currently defined, IPython will
716 try to interpret it as a filename (automatically adding a .py extension
716 try to interpret it as a filename (automatically adding a .py extension
717 if needed). You can thus use %pfile as a syntax highlighting code
717 if needed). You can thus use %pfile as a syntax highlighting code
718 viewer."""
718 viewer."""
719
719
720 # first interpret argument as an object name
720 # first interpret argument as an object name
721 out = self._inspect('pfile',parameter_s)
721 out = self._inspect('pfile',parameter_s)
722 # if not, try the input as a filename
722 # if not, try the input as a filename
723 if out == 'not found':
723 if out == 'not found':
724 try:
724 try:
725 filename = get_py_filename(parameter_s)
725 filename = get_py_filename(parameter_s)
726 except IOError,msg:
726 except IOError,msg:
727 print msg
727 print msg
728 return
728 return
729 page(self.shell.inspector.format(file(filename).read()))
729 page(self.shell.inspector.format(file(filename).read()))
730
730
731 def _inspect(self,meth,oname,namespaces=None,**kw):
731 def _inspect(self,meth,oname,namespaces=None,**kw):
732 """Generic interface to the inspector system.
732 """Generic interface to the inspector system.
733
733
734 This function is meant to be called by pdef, pdoc & friends."""
734 This function is meant to be called by pdef, pdoc & friends."""
735
735
736 #oname = oname.strip()
736 #oname = oname.strip()
737 #print '1- oname: <%r>' % oname # dbg
737 #print '1- oname: <%r>' % oname # dbg
738 try:
738 try:
739 oname = oname.strip().encode('ascii')
739 oname = oname.strip().encode('ascii')
740 #print '2- oname: <%r>' % oname # dbg
740 #print '2- oname: <%r>' % oname # dbg
741 except UnicodeEncodeError:
741 except UnicodeEncodeError:
742 print 'Python identifiers can only contain ascii characters.'
742 print 'Python identifiers can only contain ascii characters.'
743 return 'not found'
743 return 'not found'
744
744
745 info = Struct(self._ofind(oname, namespaces))
745 info = Struct(self._ofind(oname, namespaces))
746
746
747 if info.found:
747 if info.found:
748 try:
748 try:
749 IPython.utils.generics.inspect_object(info.obj)
749 IPython.utils.generics.inspect_object(info.obj)
750 return
750 return
751 except TryNext:
751 except TryNext:
752 pass
752 pass
753 # Get the docstring of the class property if it exists.
753 # Get the docstring of the class property if it exists.
754 path = oname.split('.')
754 path = oname.split('.')
755 root = '.'.join(path[:-1])
755 root = '.'.join(path[:-1])
756 if info.parent is not None:
756 if info.parent is not None:
757 try:
757 try:
758 target = getattr(info.parent, '__class__')
758 target = getattr(info.parent, '__class__')
759 # The object belongs to a class instance.
759 # The object belongs to a class instance.
760 try:
760 try:
761 target = getattr(target, path[-1])
761 target = getattr(target, path[-1])
762 # The class defines the object.
762 # The class defines the object.
763 if isinstance(target, property):
763 if isinstance(target, property):
764 oname = root + '.__class__.' + path[-1]
764 oname = root + '.__class__.' + path[-1]
765 info = Struct(self._ofind(oname))
765 info = Struct(self._ofind(oname))
766 except AttributeError: pass
766 except AttributeError: pass
767 except AttributeError: pass
767 except AttributeError: pass
768
768
769 pmethod = getattr(self.shell.inspector,meth)
769 pmethod = getattr(self.shell.inspector,meth)
770 formatter = info.ismagic and self.format_screen or None
770 formatter = info.ismagic and self.format_screen or None
771 if meth == 'pdoc':
771 if meth == 'pdoc':
772 pmethod(info.obj,oname,formatter)
772 pmethod(info.obj,oname,formatter)
773 elif meth == 'pinfo':
773 elif meth == 'pinfo':
774 pmethod(info.obj,oname,formatter,info,**kw)
774 pmethod(info.obj,oname,formatter,info,**kw)
775 else:
775 else:
776 pmethod(info.obj,oname)
776 pmethod(info.obj,oname)
777 else:
777 else:
778 print 'Object `%s` not found.' % oname
778 print 'Object `%s` not found.' % oname
779 return 'not found' # so callers can take other action
779 return 'not found' # so callers can take other action
780
780
781 def magic_psearch(self, parameter_s=''):
781 def magic_psearch(self, parameter_s=''):
782 """Search for object in namespaces by wildcard.
782 """Search for object in namespaces by wildcard.
783
783
784 %psearch [options] PATTERN [OBJECT TYPE]
784 %psearch [options] PATTERN [OBJECT TYPE]
785
785
786 Note: ? can be used as a synonym for %psearch, at the beginning or at
786 Note: ? can be used as a synonym for %psearch, at the beginning or at
787 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
787 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
788 rest of the command line must be unchanged (options come first), so
788 rest of the command line must be unchanged (options come first), so
789 for example the following forms are equivalent
789 for example the following forms are equivalent
790
790
791 %psearch -i a* function
791 %psearch -i a* function
792 -i a* function?
792 -i a* function?
793 ?-i a* function
793 ?-i a* function
794
794
795 Arguments:
795 Arguments:
796
796
797 PATTERN
797 PATTERN
798
798
799 where PATTERN is a string containing * as a wildcard similar to its
799 where PATTERN is a string containing * as a wildcard similar to its
800 use in a shell. The pattern is matched in all namespaces on the
800 use in a shell. The pattern is matched in all namespaces on the
801 search path. By default objects starting with a single _ are not
801 search path. By default objects starting with a single _ are not
802 matched, many IPython generated objects have a single
802 matched, many IPython generated objects have a single
803 underscore. The default is case insensitive matching. Matching is
803 underscore. The default is case insensitive matching. Matching is
804 also done on the attributes of objects and not only on the objects
804 also done on the attributes of objects and not only on the objects
805 in a module.
805 in a module.
806
806
807 [OBJECT TYPE]
807 [OBJECT TYPE]
808
808
809 Is the name of a python type from the types module. The name is
809 Is the name of a python type from the types module. The name is
810 given in lowercase without the ending type, ex. StringType is
810 given in lowercase without the ending type, ex. StringType is
811 written string. By adding a type here only objects matching the
811 written string. By adding a type here only objects matching the
812 given type are matched. Using all here makes the pattern match all
812 given type are matched. Using all here makes the pattern match all
813 types (this is the default).
813 types (this is the default).
814
814
815 Options:
815 Options:
816
816
817 -a: makes the pattern match even objects whose names start with a
817 -a: makes the pattern match even objects whose names start with a
818 single underscore. These names are normally ommitted from the
818 single underscore. These names are normally ommitted from the
819 search.
819 search.
820
820
821 -i/-c: make the pattern case insensitive/sensitive. If neither of
821 -i/-c: make the pattern case insensitive/sensitive. If neither of
822 these options is given, the default is read from your ipythonrc
822 these options is given, the default is read from your ipythonrc
823 file. The option name which sets this value is
823 file. The option name which sets this value is
824 'wildcards_case_sensitive'. If this option is not specified in your
824 'wildcards_case_sensitive'. If this option is not specified in your
825 ipythonrc file, IPython's internal default is to do a case sensitive
825 ipythonrc file, IPython's internal default is to do a case sensitive
826 search.
826 search.
827
827
828 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
828 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
829 specifiy can be searched in any of the following namespaces:
829 specifiy can be searched in any of the following namespaces:
830 'builtin', 'user', 'user_global','internal', 'alias', where
830 'builtin', 'user', 'user_global','internal', 'alias', where
831 'builtin' and 'user' are the search defaults. Note that you should
831 'builtin' and 'user' are the search defaults. Note that you should
832 not use quotes when specifying namespaces.
832 not use quotes when specifying namespaces.
833
833
834 'Builtin' contains the python module builtin, 'user' contains all
834 'Builtin' contains the python module builtin, 'user' contains all
835 user data, 'alias' only contain the shell aliases and no python
835 user data, 'alias' only contain the shell aliases and no python
836 objects, 'internal' contains objects used by IPython. The
836 objects, 'internal' contains objects used by IPython. The
837 'user_global' namespace is only used by embedded IPython instances,
837 'user_global' namespace is only used by embedded IPython instances,
838 and it contains module-level globals. You can add namespaces to the
838 and it contains module-level globals. You can add namespaces to the
839 search with -s or exclude them with -e (these options can be given
839 search with -s or exclude them with -e (these options can be given
840 more than once).
840 more than once).
841
841
842 Examples:
842 Examples:
843
843
844 %psearch a* -> objects beginning with an a
844 %psearch a* -> objects beginning with an a
845 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
845 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
846 %psearch a* function -> all functions beginning with an a
846 %psearch a* function -> all functions beginning with an a
847 %psearch re.e* -> objects beginning with an e in module re
847 %psearch re.e* -> objects beginning with an e in module re
848 %psearch r*.e* -> objects that start with e in modules starting in r
848 %psearch r*.e* -> objects that start with e in modules starting in r
849 %psearch r*.* string -> all strings in modules beginning with r
849 %psearch r*.* string -> all strings in modules beginning with r
850
850
851 Case sensitve search:
851 Case sensitve search:
852
852
853 %psearch -c a* list all object beginning with lower case a
853 %psearch -c a* list all object beginning with lower case a
854
854
855 Show objects beginning with a single _:
855 Show objects beginning with a single _:
856
856
857 %psearch -a _* list objects beginning with a single underscore"""
857 %psearch -a _* list objects beginning with a single underscore"""
858 try:
858 try:
859 parameter_s = parameter_s.encode('ascii')
859 parameter_s = parameter_s.encode('ascii')
860 except UnicodeEncodeError:
860 except UnicodeEncodeError:
861 print 'Python identifiers can only contain ascii characters.'
861 print 'Python identifiers can only contain ascii characters.'
862 return
862 return
863
863
864 # default namespaces to be searched
864 # default namespaces to be searched
865 def_search = ['user','builtin']
865 def_search = ['user','builtin']
866
866
867 # Process options/args
867 # Process options/args
868 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
868 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
869 opt = opts.get
869 opt = opts.get
870 shell = self.shell
870 shell = self.shell
871 psearch = shell.inspector.psearch
871 psearch = shell.inspector.psearch
872
872
873 # select case options
873 # select case options
874 if opts.has_key('i'):
874 if opts.has_key('i'):
875 ignore_case = True
875 ignore_case = True
876 elif opts.has_key('c'):
876 elif opts.has_key('c'):
877 ignore_case = False
877 ignore_case = False
878 else:
878 else:
879 ignore_case = not shell.wildcards_case_sensitive
879 ignore_case = not shell.wildcards_case_sensitive
880
880
881 # Build list of namespaces to search from user options
881 # Build list of namespaces to search from user options
882 def_search.extend(opt('s',[]))
882 def_search.extend(opt('s',[]))
883 ns_exclude = ns_exclude=opt('e',[])
883 ns_exclude = ns_exclude=opt('e',[])
884 ns_search = [nm for nm in def_search if nm not in ns_exclude]
884 ns_search = [nm for nm in def_search if nm not in ns_exclude]
885
885
886 # Call the actual search
886 # Call the actual search
887 try:
887 try:
888 psearch(args,shell.ns_table,ns_search,
888 psearch(args,shell.ns_table,ns_search,
889 show_all=opt('a'),ignore_case=ignore_case)
889 show_all=opt('a'),ignore_case=ignore_case)
890 except:
890 except:
891 shell.showtraceback()
891 shell.showtraceback()
892
892
893 def magic_who_ls(self, parameter_s=''):
893 def magic_who_ls(self, parameter_s=''):
894 """Return a sorted list of all interactive variables.
894 """Return a sorted list of all interactive variables.
895
895
896 If arguments are given, only variables of types matching these
896 If arguments are given, only variables of types matching these
897 arguments are returned."""
897 arguments are returned."""
898
898
899 user_ns = self.shell.user_ns
899 user_ns = self.shell.user_ns
900 internal_ns = self.shell.internal_ns
900 internal_ns = self.shell.internal_ns
901 user_ns_hidden = self.shell.user_ns_hidden
901 user_ns_hidden = self.shell.user_ns_hidden
902 out = [ i for i in user_ns
902 out = [ i for i in user_ns
903 if not i.startswith('_') \
903 if not i.startswith('_') \
904 and not (i in internal_ns or i in user_ns_hidden) ]
904 and not (i in internal_ns or i in user_ns_hidden) ]
905
905
906 typelist = parameter_s.split()
906 typelist = parameter_s.split()
907 if typelist:
907 if typelist:
908 typeset = set(typelist)
908 typeset = set(typelist)
909 out = [i for i in out if type(i).__name__ in typeset]
909 out = [i for i in out if type(i).__name__ in typeset]
910
910
911 out.sort()
911 out.sort()
912 return out
912 return out
913
913
914 def magic_who(self, parameter_s=''):
914 def magic_who(self, parameter_s=''):
915 """Print all interactive variables, with some minimal formatting.
915 """Print all interactive variables, with some minimal formatting.
916
916
917 If any arguments are given, only variables whose type matches one of
917 If any arguments are given, only variables whose type matches one of
918 these are printed. For example:
918 these are printed. For example:
919
919
920 %who function str
920 %who function str
921
921
922 will only list functions and strings, excluding all other types of
922 will only list functions and strings, excluding all other types of
923 variables. To find the proper type names, simply use type(var) at a
923 variables. To find the proper type names, simply use type(var) at a
924 command line to see how python prints type names. For example:
924 command line to see how python prints type names. For example:
925
925
926 In [1]: type('hello')\\
926 In [1]: type('hello')\\
927 Out[1]: <type 'str'>
927 Out[1]: <type 'str'>
928
928
929 indicates that the type name for strings is 'str'.
929 indicates that the type name for strings is 'str'.
930
930
931 %who always excludes executed names loaded through your configuration
931 %who always excludes executed names loaded through your configuration
932 file and things which are internal to IPython.
932 file and things which are internal to IPython.
933
933
934 This is deliberate, as typically you may load many modules and the
934 This is deliberate, as typically you may load many modules and the
935 purpose of %who is to show you only what you've manually defined."""
935 purpose of %who is to show you only what you've manually defined."""
936
936
937 varlist = self.magic_who_ls(parameter_s)
937 varlist = self.magic_who_ls(parameter_s)
938 if not varlist:
938 if not varlist:
939 if parameter_s:
939 if parameter_s:
940 print 'No variables match your requested type.'
940 print 'No variables match your requested type.'
941 else:
941 else:
942 print 'Interactive namespace is empty.'
942 print 'Interactive namespace is empty.'
943 return
943 return
944
944
945 # if we have variables, move on...
945 # if we have variables, move on...
946 count = 0
946 count = 0
947 for i in varlist:
947 for i in varlist:
948 print i+'\t',
948 print i+'\t',
949 count += 1
949 count += 1
950 if count > 8:
950 if count > 8:
951 count = 0
951 count = 0
952 print
952 print
953 print
953 print
954
954
955 def magic_whos(self, parameter_s=''):
955 def magic_whos(self, parameter_s=''):
956 """Like %who, but gives some extra information about each variable.
956 """Like %who, but gives some extra information about each variable.
957
957
958 The same type filtering of %who can be applied here.
958 The same type filtering of %who can be applied here.
959
959
960 For all variables, the type is printed. Additionally it prints:
960 For all variables, the type is printed. Additionally it prints:
961
961
962 - For {},[],(): their length.
962 - For {},[],(): their length.
963
963
964 - For numpy and Numeric arrays, a summary with shape, number of
964 - For numpy and Numeric arrays, a summary with shape, number of
965 elements, typecode and size in memory.
965 elements, typecode and size in memory.
966
966
967 - Everything else: a string representation, snipping their middle if
967 - Everything else: a string representation, snipping their middle if
968 too long."""
968 too long."""
969
969
970 varnames = self.magic_who_ls(parameter_s)
970 varnames = self.magic_who_ls(parameter_s)
971 if not varnames:
971 if not varnames:
972 if parameter_s:
972 if parameter_s:
973 print 'No variables match your requested type.'
973 print 'No variables match your requested type.'
974 else:
974 else:
975 print 'Interactive namespace is empty.'
975 print 'Interactive namespace is empty.'
976 return
976 return
977
977
978 # if we have variables, move on...
978 # if we have variables, move on...
979
979
980 # for these types, show len() instead of data:
980 # for these types, show len() instead of data:
981 seq_types = [types.DictType,types.ListType,types.TupleType]
981 seq_types = [types.DictType,types.ListType,types.TupleType]
982
982
983 # for numpy/Numeric arrays, display summary info
983 # for numpy/Numeric arrays, display summary info
984 try:
984 try:
985 import numpy
985 import numpy
986 except ImportError:
986 except ImportError:
987 ndarray_type = None
987 ndarray_type = None
988 else:
988 else:
989 ndarray_type = numpy.ndarray.__name__
989 ndarray_type = numpy.ndarray.__name__
990 try:
990 try:
991 import Numeric
991 import Numeric
992 except ImportError:
992 except ImportError:
993 array_type = None
993 array_type = None
994 else:
994 else:
995 array_type = Numeric.ArrayType.__name__
995 array_type = Numeric.ArrayType.__name__
996
996
997 # Find all variable names and types so we can figure out column sizes
997 # Find all variable names and types so we can figure out column sizes
998 def get_vars(i):
998 def get_vars(i):
999 return self.shell.user_ns[i]
999 return self.shell.user_ns[i]
1000
1000
1001 # some types are well known and can be shorter
1001 # some types are well known and can be shorter
1002 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
1002 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
1003 def type_name(v):
1003 def type_name(v):
1004 tn = type(v).__name__
1004 tn = type(v).__name__
1005 return abbrevs.get(tn,tn)
1005 return abbrevs.get(tn,tn)
1006
1006
1007 varlist = map(get_vars,varnames)
1007 varlist = map(get_vars,varnames)
1008
1008
1009 typelist = []
1009 typelist = []
1010 for vv in varlist:
1010 for vv in varlist:
1011 tt = type_name(vv)
1011 tt = type_name(vv)
1012
1012
1013 if tt=='instance':
1013 if tt=='instance':
1014 typelist.append( abbrevs.get(str(vv.__class__),
1014 typelist.append( abbrevs.get(str(vv.__class__),
1015 str(vv.__class__)))
1015 str(vv.__class__)))
1016 else:
1016 else:
1017 typelist.append(tt)
1017 typelist.append(tt)
1018
1018
1019 # column labels and # of spaces as separator
1019 # column labels and # of spaces as separator
1020 varlabel = 'Variable'
1020 varlabel = 'Variable'
1021 typelabel = 'Type'
1021 typelabel = 'Type'
1022 datalabel = 'Data/Info'
1022 datalabel = 'Data/Info'
1023 colsep = 3
1023 colsep = 3
1024 # variable format strings
1024 # variable format strings
1025 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
1025 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
1026 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1026 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1027 aformat = "%s: %s elems, type `%s`, %s bytes"
1027 aformat = "%s: %s elems, type `%s`, %s bytes"
1028 # find the size of the columns to format the output nicely
1028 # find the size of the columns to format the output nicely
1029 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1029 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1030 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1030 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1031 # table header
1031 # table header
1032 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1032 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1033 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1033 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1034 # and the table itself
1034 # and the table itself
1035 kb = 1024
1035 kb = 1024
1036 Mb = 1048576 # kb**2
1036 Mb = 1048576 # kb**2
1037 for vname,var,vtype in zip(varnames,varlist,typelist):
1037 for vname,var,vtype in zip(varnames,varlist,typelist):
1038 print itpl(vformat),
1038 print itpl(vformat),
1039 if vtype in seq_types:
1039 if vtype in seq_types:
1040 print len(var)
1040 print len(var)
1041 elif vtype in [array_type,ndarray_type]:
1041 elif vtype in [array_type,ndarray_type]:
1042 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1042 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1043 if vtype==ndarray_type:
1043 if vtype==ndarray_type:
1044 # numpy
1044 # numpy
1045 vsize = var.size
1045 vsize = var.size
1046 vbytes = vsize*var.itemsize
1046 vbytes = vsize*var.itemsize
1047 vdtype = var.dtype
1047 vdtype = var.dtype
1048 else:
1048 else:
1049 # Numeric
1049 # Numeric
1050 vsize = Numeric.size(var)
1050 vsize = Numeric.size(var)
1051 vbytes = vsize*var.itemsize()
1051 vbytes = vsize*var.itemsize()
1052 vdtype = var.typecode()
1052 vdtype = var.typecode()
1053
1053
1054 if vbytes < 100000:
1054 if vbytes < 100000:
1055 print aformat % (vshape,vsize,vdtype,vbytes)
1055 print aformat % (vshape,vsize,vdtype,vbytes)
1056 else:
1056 else:
1057 print aformat % (vshape,vsize,vdtype,vbytes),
1057 print aformat % (vshape,vsize,vdtype,vbytes),
1058 if vbytes < Mb:
1058 if vbytes < Mb:
1059 print '(%s kb)' % (vbytes/kb,)
1059 print '(%s kb)' % (vbytes/kb,)
1060 else:
1060 else:
1061 print '(%s Mb)' % (vbytes/Mb,)
1061 print '(%s Mb)' % (vbytes/Mb,)
1062 else:
1062 else:
1063 try:
1063 try:
1064 vstr = str(var)
1064 vstr = str(var)
1065 except UnicodeEncodeError:
1065 except UnicodeEncodeError:
1066 vstr = unicode(var).encode(sys.getdefaultencoding(),
1066 vstr = unicode(var).encode(sys.getdefaultencoding(),
1067 'backslashreplace')
1067 'backslashreplace')
1068 vstr = vstr.replace('\n','\\n')
1068 vstr = vstr.replace('\n','\\n')
1069 if len(vstr) < 50:
1069 if len(vstr) < 50:
1070 print vstr
1070 print vstr
1071 else:
1071 else:
1072 printpl(vfmt_short)
1072 printpl(vfmt_short)
1073
1073
1074 def magic_reset(self, parameter_s=''):
1074 def magic_reset(self, parameter_s=''):
1075 """Resets the namespace by removing all names defined by the user.
1075 """Resets the namespace by removing all names defined by the user.
1076
1076
1077 Input/Output history are left around in case you need them.
1077 Input/Output history are left around in case you need them.
1078
1078
1079 Parameters
1079 Parameters
1080 ----------
1080 ----------
1081 -y : force reset without asking for confirmation.
1081 -y : force reset without asking for confirmation.
1082
1082
1083 Examples
1083 Examples
1084 --------
1084 --------
1085 In [6]: a = 1
1085 In [6]: a = 1
1086
1086
1087 In [7]: a
1087 In [7]: a
1088 Out[7]: 1
1088 Out[7]: 1
1089
1089
1090 In [8]: 'a' in _ip.user_ns
1090 In [8]: 'a' in _ip.user_ns
1091 Out[8]: True
1091 Out[8]: True
1092
1092
1093 In [9]: %reset -f
1093 In [9]: %reset -f
1094
1094
1095 In [10]: 'a' in _ip.user_ns
1095 In [10]: 'a' in _ip.user_ns
1096 Out[10]: False
1096 Out[10]: False
1097 """
1097 """
1098
1098
1099 if parameter_s == '-f':
1099 if parameter_s == '-f':
1100 ans = True
1100 ans = True
1101 else:
1101 else:
1102 ans = self.shell.ask_yes_no(
1102 ans = self.shell.ask_yes_no(
1103 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1103 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1104 if not ans:
1104 if not ans:
1105 print 'Nothing done.'
1105 print 'Nothing done.'
1106 return
1106 return
1107 user_ns = self.shell.user_ns
1107 user_ns = self.shell.user_ns
1108 for i in self.magic_who_ls():
1108 for i in self.magic_who_ls():
1109 del(user_ns[i])
1109 del(user_ns[i])
1110
1110
1111 # Also flush the private list of module references kept for script
1111 # Also flush the private list of module references kept for script
1112 # execution protection
1112 # execution protection
1113 self.shell.clear_main_mod_cache()
1113 self.shell.clear_main_mod_cache()
1114
1114
1115 def magic_reset_selective(self, parameter_s=''):
1115 def magic_reset_selective(self, parameter_s=''):
1116 """Resets the namespace by removing names defined by the user.
1116 """Resets the namespace by removing names defined by the user.
1117
1117
1118 Input/Output history are left around in case you need them.
1118 Input/Output history are left around in case you need them.
1119
1119
1120 %reset_selective [-f] regex
1120 %reset_selective [-f] regex
1121
1121
1122 No action is taken if regex is not included
1122 No action is taken if regex is not included
1123
1123
1124 Options
1124 Options
1125 -f : force reset without asking for confirmation.
1125 -f : force reset without asking for confirmation.
1126
1126
1127 Examples
1127 Examples
1128 --------
1128 --------
1129
1129
1130 We first fully reset the namespace so your output looks identical to
1130 We first fully reset the namespace so your output looks identical to
1131 this example for pedagogical reasons; in practice you do not need a
1131 this example for pedagogical reasons; in practice you do not need a
1132 full reset.
1132 full reset.
1133
1133
1134 In [1]: %reset -f
1134 In [1]: %reset -f
1135
1135
1136 Now, with a clean namespace we can make a few variables and use
1136 Now, with a clean namespace we can make a few variables and use
1137 %reset_selective to only delete names that match our regexp:
1137 %reset_selective to only delete names that match our regexp:
1138
1138
1139 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
1139 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
1140
1140
1141 In [3]: who_ls
1141 In [3]: who_ls
1142 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
1142 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
1143
1143
1144 In [4]: %reset_selective -f b[2-3]m
1144 In [4]: %reset_selective -f b[2-3]m
1145
1145
1146 In [5]: who_ls
1146 In [5]: who_ls
1147 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1147 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1148
1148
1149 In [6]: %reset_selective -f d
1149 In [6]: %reset_selective -f d
1150
1150
1151 In [7]: who_ls
1151 In [7]: who_ls
1152 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1152 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
1153
1153
1154 In [8]: %reset_selective -f c
1154 In [8]: %reset_selective -f c
1155
1155
1156 In [9]: who_ls
1156 In [9]: who_ls
1157 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
1157 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
1158
1158
1159 In [10]: %reset_selective -f b
1159 In [10]: %reset_selective -f b
1160
1160
1161 In [11]: who_ls
1161 In [11]: who_ls
1162 Out[11]: ['a']
1162 Out[11]: ['a']
1163 """
1163 """
1164
1164
1165 opts, regex = self.parse_options(parameter_s,'f')
1165 opts, regex = self.parse_options(parameter_s,'f')
1166
1166
1167 if opts.has_key('f'):
1167 if opts.has_key('f'):
1168 ans = True
1168 ans = True
1169 else:
1169 else:
1170 ans = self.shell.ask_yes_no(
1170 ans = self.shell.ask_yes_no(
1171 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1171 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1172 if not ans:
1172 if not ans:
1173 print 'Nothing done.'
1173 print 'Nothing done.'
1174 return
1174 return
1175 user_ns = self.shell.user_ns
1175 user_ns = self.shell.user_ns
1176 if not regex:
1176 if not regex:
1177 print 'No regex pattern specified. Nothing done.'
1177 print 'No regex pattern specified. Nothing done.'
1178 return
1178 return
1179 else:
1179 else:
1180 try:
1180 try:
1181 m = re.compile(regex)
1181 m = re.compile(regex)
1182 except TypeError:
1182 except TypeError:
1183 raise TypeError('regex must be a string or compiled pattern')
1183 raise TypeError('regex must be a string or compiled pattern')
1184 for i in self.magic_who_ls():
1184 for i in self.magic_who_ls():
1185 if m.search(i):
1185 if m.search(i):
1186 del(user_ns[i])
1186 del(user_ns[i])
1187
1187
1188 def magic_logstart(self,parameter_s=''):
1188 def magic_logstart(self,parameter_s=''):
1189 """Start logging anywhere in a session.
1189 """Start logging anywhere in a session.
1190
1190
1191 %logstart [-o|-r|-t] [log_name [log_mode]]
1191 %logstart [-o|-r|-t] [log_name [log_mode]]
1192
1192
1193 If no name is given, it defaults to a file named 'ipython_log.py' in your
1193 If no name is given, it defaults to a file named 'ipython_log.py' in your
1194 current directory, in 'rotate' mode (see below).
1194 current directory, in 'rotate' mode (see below).
1195
1195
1196 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1196 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1197 history up to that point and then continues logging.
1197 history up to that point and then continues logging.
1198
1198
1199 %logstart takes a second optional parameter: logging mode. This can be one
1199 %logstart takes a second optional parameter: logging mode. This can be one
1200 of (note that the modes are given unquoted):\\
1200 of (note that the modes are given unquoted):\\
1201 append: well, that says it.\\
1201 append: well, that says it.\\
1202 backup: rename (if exists) to name~ and start name.\\
1202 backup: rename (if exists) to name~ and start name.\\
1203 global: single logfile in your home dir, appended to.\\
1203 global: single logfile in your home dir, appended to.\\
1204 over : overwrite existing log.\\
1204 over : overwrite existing log.\\
1205 rotate: create rotating logs name.1~, name.2~, etc.
1205 rotate: create rotating logs name.1~, name.2~, etc.
1206
1206
1207 Options:
1207 Options:
1208
1208
1209 -o: log also IPython's output. In this mode, all commands which
1209 -o: log also IPython's output. In this mode, all commands which
1210 generate an Out[NN] prompt are recorded to the logfile, right after
1210 generate an Out[NN] prompt are recorded to the logfile, right after
1211 their corresponding input line. The output lines are always
1211 their corresponding input line. The output lines are always
1212 prepended with a '#[Out]# ' marker, so that the log remains valid
1212 prepended with a '#[Out]# ' marker, so that the log remains valid
1213 Python code.
1213 Python code.
1214
1214
1215 Since this marker is always the same, filtering only the output from
1215 Since this marker is always the same, filtering only the output from
1216 a log is very easy, using for example a simple awk call:
1216 a log is very easy, using for example a simple awk call:
1217
1217
1218 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1218 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1219
1219
1220 -r: log 'raw' input. Normally, IPython's logs contain the processed
1220 -r: log 'raw' input. Normally, IPython's logs contain the processed
1221 input, so that user lines are logged in their final form, converted
1221 input, so that user lines are logged in their final form, converted
1222 into valid Python. For example, %Exit is logged as
1222 into valid Python. For example, %Exit is logged as
1223 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1223 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1224 exactly as typed, with no transformations applied.
1224 exactly as typed, with no transformations applied.
1225
1225
1226 -t: put timestamps before each input line logged (these are put in
1226 -t: put timestamps before each input line logged (these are put in
1227 comments)."""
1227 comments)."""
1228
1228
1229 opts,par = self.parse_options(parameter_s,'ort')
1229 opts,par = self.parse_options(parameter_s,'ort')
1230 log_output = 'o' in opts
1230 log_output = 'o' in opts
1231 log_raw_input = 'r' in opts
1231 log_raw_input = 'r' in opts
1232 timestamp = 't' in opts
1232 timestamp = 't' in opts
1233
1233
1234 logger = self.shell.logger
1234 logger = self.shell.logger
1235
1235
1236 # if no args are given, the defaults set in the logger constructor by
1236 # if no args are given, the defaults set in the logger constructor by
1237 # ipytohn remain valid
1237 # ipytohn remain valid
1238 if par:
1238 if par:
1239 try:
1239 try:
1240 logfname,logmode = par.split()
1240 logfname,logmode = par.split()
1241 except:
1241 except:
1242 logfname = par
1242 logfname = par
1243 logmode = 'backup'
1243 logmode = 'backup'
1244 else:
1244 else:
1245 logfname = logger.logfname
1245 logfname = logger.logfname
1246 logmode = logger.logmode
1246 logmode = logger.logmode
1247 # put logfname into rc struct as if it had been called on the command
1247 # put logfname into rc struct as if it had been called on the command
1248 # line, so it ends up saved in the log header Save it in case we need
1248 # line, so it ends up saved in the log header Save it in case we need
1249 # to restore it...
1249 # to restore it...
1250 old_logfile = self.shell.logfile
1250 old_logfile = self.shell.logfile
1251 if logfname:
1251 if logfname:
1252 logfname = os.path.expanduser(logfname)
1252 logfname = os.path.expanduser(logfname)
1253 self.shell.logfile = logfname
1253 self.shell.logfile = logfname
1254
1254
1255 loghead = '# IPython log file\n\n'
1255 loghead = '# IPython log file\n\n'
1256 try:
1256 try:
1257 started = logger.logstart(logfname,loghead,logmode,
1257 started = logger.logstart(logfname,loghead,logmode,
1258 log_output,timestamp,log_raw_input)
1258 log_output,timestamp,log_raw_input)
1259 except:
1259 except:
1260 self.shell.logfile = old_logfile
1260 self.shell.logfile = old_logfile
1261 warn("Couldn't start log: %s" % sys.exc_info()[1])
1261 warn("Couldn't start log: %s" % sys.exc_info()[1])
1262 else:
1262 else:
1263 # log input history up to this point, optionally interleaving
1263 # log input history up to this point, optionally interleaving
1264 # output if requested
1264 # output if requested
1265
1265
1266 if timestamp:
1266 if timestamp:
1267 # disable timestamping for the previous history, since we've
1267 # disable timestamping for the previous history, since we've
1268 # lost those already (no time machine here).
1268 # lost those already (no time machine here).
1269 logger.timestamp = False
1269 logger.timestamp = False
1270
1270
1271 if log_raw_input:
1271 if log_raw_input:
1272 input_hist = self.shell.input_hist_raw
1272 input_hist = self.shell.input_hist_raw
1273 else:
1273 else:
1274 input_hist = self.shell.input_hist
1274 input_hist = self.shell.input_hist
1275
1275
1276 if log_output:
1276 if log_output:
1277 log_write = logger.log_write
1277 log_write = logger.log_write
1278 output_hist = self.shell.output_hist
1278 output_hist = self.shell.output_hist
1279 for n in range(1,len(input_hist)-1):
1279 for n in range(1,len(input_hist)-1):
1280 log_write(input_hist[n].rstrip())
1280 log_write(input_hist[n].rstrip())
1281 if n in output_hist:
1281 if n in output_hist:
1282 log_write(repr(output_hist[n]),'output')
1282 log_write(repr(output_hist[n]),'output')
1283 else:
1283 else:
1284 logger.log_write(input_hist[1:])
1284 logger.log_write(input_hist[1:])
1285 if timestamp:
1285 if timestamp:
1286 # re-enable timestamping
1286 # re-enable timestamping
1287 logger.timestamp = True
1287 logger.timestamp = True
1288
1288
1289 print ('Activating auto-logging. '
1289 print ('Activating auto-logging. '
1290 'Current session state plus future input saved.')
1290 'Current session state plus future input saved.')
1291 logger.logstate()
1291 logger.logstate()
1292
1292
1293 def magic_logstop(self,parameter_s=''):
1293 def magic_logstop(self,parameter_s=''):
1294 """Fully stop logging and close log file.
1294 """Fully stop logging and close log file.
1295
1295
1296 In order to start logging again, a new %logstart call needs to be made,
1296 In order to start logging again, a new %logstart call needs to be made,
1297 possibly (though not necessarily) with a new filename, mode and other
1297 possibly (though not necessarily) with a new filename, mode and other
1298 options."""
1298 options."""
1299 self.logger.logstop()
1299 self.logger.logstop()
1300
1300
1301 def magic_logoff(self,parameter_s=''):
1301 def magic_logoff(self,parameter_s=''):
1302 """Temporarily stop logging.
1302 """Temporarily stop logging.
1303
1303
1304 You must have previously started logging."""
1304 You must have previously started logging."""
1305 self.shell.logger.switch_log(0)
1305 self.shell.logger.switch_log(0)
1306
1306
1307 def magic_logon(self,parameter_s=''):
1307 def magic_logon(self,parameter_s=''):
1308 """Restart logging.
1308 """Restart logging.
1309
1309
1310 This function is for restarting logging which you've temporarily
1310 This function is for restarting logging which you've temporarily
1311 stopped with %logoff. For starting logging for the first time, you
1311 stopped with %logoff. For starting logging for the first time, you
1312 must use the %logstart function, which allows you to specify an
1312 must use the %logstart function, which allows you to specify an
1313 optional log filename."""
1313 optional log filename."""
1314
1314
1315 self.shell.logger.switch_log(1)
1315 self.shell.logger.switch_log(1)
1316
1316
1317 def magic_logstate(self,parameter_s=''):
1317 def magic_logstate(self,parameter_s=''):
1318 """Print the status of the logging system."""
1318 """Print the status of the logging system."""
1319
1319
1320 self.shell.logger.logstate()
1320 self.shell.logger.logstate()
1321
1321
1322 def magic_pdb(self, parameter_s=''):
1322 def magic_pdb(self, parameter_s=''):
1323 """Control the automatic calling of the pdb interactive debugger.
1323 """Control the automatic calling of the pdb interactive debugger.
1324
1324
1325 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1325 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1326 argument it works as a toggle.
1326 argument it works as a toggle.
1327
1327
1328 When an exception is triggered, IPython can optionally call the
1328 When an exception is triggered, IPython can optionally call the
1329 interactive pdb debugger after the traceback printout. %pdb toggles
1329 interactive pdb debugger after the traceback printout. %pdb toggles
1330 this feature on and off.
1330 this feature on and off.
1331
1331
1332 The initial state of this feature is set in your ipythonrc
1332 The initial state of this feature is set in your ipythonrc
1333 configuration file (the variable is called 'pdb').
1333 configuration file (the variable is called 'pdb').
1334
1334
1335 If you want to just activate the debugger AFTER an exception has fired,
1335 If you want to just activate the debugger AFTER an exception has fired,
1336 without having to type '%pdb on' and rerunning your code, you can use
1336 without having to type '%pdb on' and rerunning your code, you can use
1337 the %debug magic."""
1337 the %debug magic."""
1338
1338
1339 par = parameter_s.strip().lower()
1339 par = parameter_s.strip().lower()
1340
1340
1341 if par:
1341 if par:
1342 try:
1342 try:
1343 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1343 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1344 except KeyError:
1344 except KeyError:
1345 print ('Incorrect argument. Use on/1, off/0, '
1345 print ('Incorrect argument. Use on/1, off/0, '
1346 'or nothing for a toggle.')
1346 'or nothing for a toggle.')
1347 return
1347 return
1348 else:
1348 else:
1349 # toggle
1349 # toggle
1350 new_pdb = not self.shell.call_pdb
1350 new_pdb = not self.shell.call_pdb
1351
1351
1352 # set on the shell
1352 # set on the shell
1353 self.shell.call_pdb = new_pdb
1353 self.shell.call_pdb = new_pdb
1354 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1354 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1355
1355
1356 def magic_debug(self, parameter_s=''):
1356 def magic_debug(self, parameter_s=''):
1357 """Activate the interactive debugger in post-mortem mode.
1357 """Activate the interactive debugger in post-mortem mode.
1358
1358
1359 If an exception has just occurred, this lets you inspect its stack
1359 If an exception has just occurred, this lets you inspect its stack
1360 frames interactively. Note that this will always work only on the last
1360 frames interactively. Note that this will always work only on the last
1361 traceback that occurred, so you must call this quickly after an
1361 traceback that occurred, so you must call this quickly after an
1362 exception that you wish to inspect has fired, because if another one
1362 exception that you wish to inspect has fired, because if another one
1363 occurs, it clobbers the previous one.
1363 occurs, it clobbers the previous one.
1364
1364
1365 If you want IPython to automatically do this on every exception, see
1365 If you want IPython to automatically do this on every exception, see
1366 the %pdb magic for more details.
1366 the %pdb magic for more details.
1367 """
1367 """
1368 self.shell.debugger(force=True)
1368 self.shell.debugger(force=True)
1369
1369
1370 @testdec.skip_doctest
1370 @testdec.skip_doctest
1371 def magic_prun(self, parameter_s ='',user_mode=1,
1371 def magic_prun(self, parameter_s ='',user_mode=1,
1372 opts=None,arg_lst=None,prog_ns=None):
1372 opts=None,arg_lst=None,prog_ns=None):
1373
1373
1374 """Run a statement through the python code profiler.
1374 """Run a statement through the python code profiler.
1375
1375
1376 Usage:
1376 Usage:
1377 %prun [options] statement
1377 %prun [options] statement
1378
1378
1379 The given statement (which doesn't require quote marks) is run via the
1379 The given statement (which doesn't require quote marks) is run via the
1380 python profiler in a manner similar to the profile.run() function.
1380 python profiler in a manner similar to the profile.run() function.
1381 Namespaces are internally managed to work correctly; profile.run
1381 Namespaces are internally managed to work correctly; profile.run
1382 cannot be used in IPython because it makes certain assumptions about
1382 cannot be used in IPython because it makes certain assumptions about
1383 namespaces which do not hold under IPython.
1383 namespaces which do not hold under IPython.
1384
1384
1385 Options:
1385 Options:
1386
1386
1387 -l <limit>: you can place restrictions on what or how much of the
1387 -l <limit>: you can place restrictions on what or how much of the
1388 profile gets printed. The limit value can be:
1388 profile gets printed. The limit value can be:
1389
1389
1390 * A string: only information for function names containing this string
1390 * A string: only information for function names containing this string
1391 is printed.
1391 is printed.
1392
1392
1393 * An integer: only these many lines are printed.
1393 * An integer: only these many lines are printed.
1394
1394
1395 * A float (between 0 and 1): this fraction of the report is printed
1395 * A float (between 0 and 1): this fraction of the report is printed
1396 (for example, use a limit of 0.4 to see the topmost 40% only).
1396 (for example, use a limit of 0.4 to see the topmost 40% only).
1397
1397
1398 You can combine several limits with repeated use of the option. For
1398 You can combine several limits with repeated use of the option. For
1399 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1399 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1400 information about class constructors.
1400 information about class constructors.
1401
1401
1402 -r: return the pstats.Stats object generated by the profiling. This
1402 -r: return the pstats.Stats object generated by the profiling. This
1403 object has all the information about the profile in it, and you can
1403 object has all the information about the profile in it, and you can
1404 later use it for further analysis or in other functions.
1404 later use it for further analysis or in other functions.
1405
1405
1406 -s <key>: sort profile by given key. You can provide more than one key
1406 -s <key>: sort profile by given key. You can provide more than one key
1407 by using the option several times: '-s key1 -s key2 -s key3...'. The
1407 by using the option several times: '-s key1 -s key2 -s key3...'. The
1408 default sorting key is 'time'.
1408 default sorting key is 'time'.
1409
1409
1410 The following is copied verbatim from the profile documentation
1410 The following is copied verbatim from the profile documentation
1411 referenced below:
1411 referenced below:
1412
1412
1413 When more than one key is provided, additional keys are used as
1413 When more than one key is provided, additional keys are used as
1414 secondary criteria when the there is equality in all keys selected
1414 secondary criteria when the there is equality in all keys selected
1415 before them.
1415 before them.
1416
1416
1417 Abbreviations can be used for any key names, as long as the
1417 Abbreviations can be used for any key names, as long as the
1418 abbreviation is unambiguous. The following are the keys currently
1418 abbreviation is unambiguous. The following are the keys currently
1419 defined:
1419 defined:
1420
1420
1421 Valid Arg Meaning
1421 Valid Arg Meaning
1422 "calls" call count
1422 "calls" call count
1423 "cumulative" cumulative time
1423 "cumulative" cumulative time
1424 "file" file name
1424 "file" file name
1425 "module" file name
1425 "module" file name
1426 "pcalls" primitive call count
1426 "pcalls" primitive call count
1427 "line" line number
1427 "line" line number
1428 "name" function name
1428 "name" function name
1429 "nfl" name/file/line
1429 "nfl" name/file/line
1430 "stdname" standard name
1430 "stdname" standard name
1431 "time" internal time
1431 "time" internal time
1432
1432
1433 Note that all sorts on statistics are in descending order (placing
1433 Note that all sorts on statistics are in descending order (placing
1434 most time consuming items first), where as name, file, and line number
1434 most time consuming items first), where as name, file, and line number
1435 searches are in ascending order (i.e., alphabetical). The subtle
1435 searches are in ascending order (i.e., alphabetical). The subtle
1436 distinction between "nfl" and "stdname" is that the standard name is a
1436 distinction between "nfl" and "stdname" is that the standard name is a
1437 sort of the name as printed, which means that the embedded line
1437 sort of the name as printed, which means that the embedded line
1438 numbers get compared in an odd way. For example, lines 3, 20, and 40
1438 numbers get compared in an odd way. For example, lines 3, 20, and 40
1439 would (if the file names were the same) appear in the string order
1439 would (if the file names were the same) appear in the string order
1440 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1440 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1441 line numbers. In fact, sort_stats("nfl") is the same as
1441 line numbers. In fact, sort_stats("nfl") is the same as
1442 sort_stats("name", "file", "line").
1442 sort_stats("name", "file", "line").
1443
1443
1444 -T <filename>: save profile results as shown on screen to a text
1444 -T <filename>: save profile results as shown on screen to a text
1445 file. The profile is still shown on screen.
1445 file. The profile is still shown on screen.
1446
1446
1447 -D <filename>: save (via dump_stats) profile statistics to given
1447 -D <filename>: save (via dump_stats) profile statistics to given
1448 filename. This data is in a format understod by the pstats module, and
1448 filename. This data is in a format understod by the pstats module, and
1449 is generated by a call to the dump_stats() method of profile
1449 is generated by a call to the dump_stats() method of profile
1450 objects. The profile is still shown on screen.
1450 objects. The profile is still shown on screen.
1451
1451
1452 If you want to run complete programs under the profiler's control, use
1452 If you want to run complete programs under the profiler's control, use
1453 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1453 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1454 contains profiler specific options as described here.
1454 contains profiler specific options as described here.
1455
1455
1456 You can read the complete documentation for the profile module with::
1456 You can read the complete documentation for the profile module with::
1457
1457
1458 In [1]: import profile; profile.help()
1458 In [1]: import profile; profile.help()
1459 """
1459 """
1460
1460
1461 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1461 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1462 # protect user quote marks
1462 # protect user quote marks
1463 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1463 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1464
1464
1465 if user_mode: # regular user call
1465 if user_mode: # regular user call
1466 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1466 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1467 list_all=1)
1467 list_all=1)
1468 namespace = self.shell.user_ns
1468 namespace = self.shell.user_ns
1469 else: # called to run a program by %run -p
1469 else: # called to run a program by %run -p
1470 try:
1470 try:
1471 filename = get_py_filename(arg_lst[0])
1471 filename = get_py_filename(arg_lst[0])
1472 except IOError,msg:
1472 except IOError,msg:
1473 error(msg)
1473 error(msg)
1474 return
1474 return
1475
1475
1476 arg_str = 'execfile(filename,prog_ns)'
1476 arg_str = 'execfile(filename,prog_ns)'
1477 namespace = locals()
1477 namespace = locals()
1478
1478
1479 opts.merge(opts_def)
1479 opts.merge(opts_def)
1480
1480
1481 prof = profile.Profile()
1481 prof = profile.Profile()
1482 try:
1482 try:
1483 prof = prof.runctx(arg_str,namespace,namespace)
1483 prof = prof.runctx(arg_str,namespace,namespace)
1484 sys_exit = ''
1484 sys_exit = ''
1485 except SystemExit:
1485 except SystemExit:
1486 sys_exit = """*** SystemExit exception caught in code being profiled."""
1486 sys_exit = """*** SystemExit exception caught in code being profiled."""
1487
1487
1488 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1488 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1489
1489
1490 lims = opts.l
1490 lims = opts.l
1491 if lims:
1491 if lims:
1492 lims = [] # rebuild lims with ints/floats/strings
1492 lims = [] # rebuild lims with ints/floats/strings
1493 for lim in opts.l:
1493 for lim in opts.l:
1494 try:
1494 try:
1495 lims.append(int(lim))
1495 lims.append(int(lim))
1496 except ValueError:
1496 except ValueError:
1497 try:
1497 try:
1498 lims.append(float(lim))
1498 lims.append(float(lim))
1499 except ValueError:
1499 except ValueError:
1500 lims.append(lim)
1500 lims.append(lim)
1501
1501
1502 # Trap output.
1502 # Trap output.
1503 stdout_trap = StringIO()
1503 stdout_trap = StringIO()
1504
1504
1505 if hasattr(stats,'stream'):
1505 if hasattr(stats,'stream'):
1506 # In newer versions of python, the stats object has a 'stream'
1506 # In newer versions of python, the stats object has a 'stream'
1507 # attribute to write into.
1507 # attribute to write into.
1508 stats.stream = stdout_trap
1508 stats.stream = stdout_trap
1509 stats.print_stats(*lims)
1509 stats.print_stats(*lims)
1510 else:
1510 else:
1511 # For older versions, we manually redirect stdout during printing
1511 # For older versions, we manually redirect stdout during printing
1512 sys_stdout = sys.stdout
1512 sys_stdout = sys.stdout
1513 try:
1513 try:
1514 sys.stdout = stdout_trap
1514 sys.stdout = stdout_trap
1515 stats.print_stats(*lims)
1515 stats.print_stats(*lims)
1516 finally:
1516 finally:
1517 sys.stdout = sys_stdout
1517 sys.stdout = sys_stdout
1518
1518
1519 output = stdout_trap.getvalue()
1519 output = stdout_trap.getvalue()
1520 output = output.rstrip()
1520 output = output.rstrip()
1521
1521
1522 page(output,screen_lines=self.shell.usable_screen_length)
1522 page(output,screen_lines=self.shell.usable_screen_length)
1523 print sys_exit,
1523 print sys_exit,
1524
1524
1525 dump_file = opts.D[0]
1525 dump_file = opts.D[0]
1526 text_file = opts.T[0]
1526 text_file = opts.T[0]
1527 if dump_file:
1527 if dump_file:
1528 prof.dump_stats(dump_file)
1528 prof.dump_stats(dump_file)
1529 print '\n*** Profile stats marshalled to file',\
1529 print '\n*** Profile stats marshalled to file',\
1530 `dump_file`+'.',sys_exit
1530 `dump_file`+'.',sys_exit
1531 if text_file:
1531 if text_file:
1532 pfile = file(text_file,'w')
1532 pfile = file(text_file,'w')
1533 pfile.write(output)
1533 pfile.write(output)
1534 pfile.close()
1534 pfile.close()
1535 print '\n*** Profile printout saved to text file',\
1535 print '\n*** Profile printout saved to text file',\
1536 `text_file`+'.',sys_exit
1536 `text_file`+'.',sys_exit
1537
1537
1538 if opts.has_key('r'):
1538 if opts.has_key('r'):
1539 return stats
1539 return stats
1540 else:
1540 else:
1541 return None
1541 return None
1542
1542
1543 @testdec.skip_doctest
1543 @testdec.skip_doctest
1544 def magic_run(self, parameter_s ='',runner=None,
1544 def magic_run(self, parameter_s ='',runner=None,
1545 file_finder=get_py_filename):
1545 file_finder=get_py_filename):
1546 """Run the named file inside IPython as a program.
1546 """Run the named file inside IPython as a program.
1547
1547
1548 Usage:\\
1548 Usage:\\
1549 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1549 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1550
1550
1551 Parameters after the filename are passed as command-line arguments to
1551 Parameters after the filename are passed as command-line arguments to
1552 the program (put in sys.argv). Then, control returns to IPython's
1552 the program (put in sys.argv). Then, control returns to IPython's
1553 prompt.
1553 prompt.
1554
1554
1555 This is similar to running at a system prompt:\\
1555 This is similar to running at a system prompt:\\
1556 $ python file args\\
1556 $ python file args\\
1557 but with the advantage of giving you IPython's tracebacks, and of
1557 but with the advantage of giving you IPython's tracebacks, and of
1558 loading all variables into your interactive namespace for further use
1558 loading all variables into your interactive namespace for further use
1559 (unless -p is used, see below).
1559 (unless -p is used, see below).
1560
1560
1561 The file is executed in a namespace initially consisting only of
1561 The file is executed in a namespace initially consisting only of
1562 __name__=='__main__' and sys.argv constructed as indicated. It thus
1562 __name__=='__main__' and sys.argv constructed as indicated. It thus
1563 sees its environment as if it were being run as a stand-alone program
1563 sees its environment as if it were being run as a stand-alone program
1564 (except for sharing global objects such as previously imported
1564 (except for sharing global objects such as previously imported
1565 modules). But after execution, the IPython interactive namespace gets
1565 modules). But after execution, the IPython interactive namespace gets
1566 updated with all variables defined in the program (except for __name__
1566 updated with all variables defined in the program (except for __name__
1567 and sys.argv). This allows for very convenient loading of code for
1567 and sys.argv). This allows for very convenient loading of code for
1568 interactive work, while giving each program a 'clean sheet' to run in.
1568 interactive work, while giving each program a 'clean sheet' to run in.
1569
1569
1570 Options:
1570 Options:
1571
1571
1572 -n: __name__ is NOT set to '__main__', but to the running file's name
1572 -n: __name__ is NOT set to '__main__', but to the running file's name
1573 without extension (as python does under import). This allows running
1573 without extension (as python does under import). This allows running
1574 scripts and reloading the definitions in them without calling code
1574 scripts and reloading the definitions in them without calling code
1575 protected by an ' if __name__ == "__main__" ' clause.
1575 protected by an ' if __name__ == "__main__" ' clause.
1576
1576
1577 -i: run the file in IPython's namespace instead of an empty one. This
1577 -i: run the file in IPython's namespace instead of an empty one. This
1578 is useful if you are experimenting with code written in a text editor
1578 is useful if you are experimenting with code written in a text editor
1579 which depends on variables defined interactively.
1579 which depends on variables defined interactively.
1580
1580
1581 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1581 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1582 being run. This is particularly useful if IPython is being used to
1582 being run. This is particularly useful if IPython is being used to
1583 run unittests, which always exit with a sys.exit() call. In such
1583 run unittests, which always exit with a sys.exit() call. In such
1584 cases you are interested in the output of the test results, not in
1584 cases you are interested in the output of the test results, not in
1585 seeing a traceback of the unittest module.
1585 seeing a traceback of the unittest module.
1586
1586
1587 -t: print timing information at the end of the run. IPython will give
1587 -t: print timing information at the end of the run. IPython will give
1588 you an estimated CPU time consumption for your script, which under
1588 you an estimated CPU time consumption for your script, which under
1589 Unix uses the resource module to avoid the wraparound problems of
1589 Unix uses the resource module to avoid the wraparound problems of
1590 time.clock(). Under Unix, an estimate of time spent on system tasks
1590 time.clock(). Under Unix, an estimate of time spent on system tasks
1591 is also given (for Windows platforms this is reported as 0.0).
1591 is also given (for Windows platforms this is reported as 0.0).
1592
1592
1593 If -t is given, an additional -N<N> option can be given, where <N>
1593 If -t is given, an additional -N<N> option can be given, where <N>
1594 must be an integer indicating how many times you want the script to
1594 must be an integer indicating how many times you want the script to
1595 run. The final timing report will include total and per run results.
1595 run. The final timing report will include total and per run results.
1596
1596
1597 For example (testing the script uniq_stable.py):
1597 For example (testing the script uniq_stable.py):
1598
1598
1599 In [1]: run -t uniq_stable
1599 In [1]: run -t uniq_stable
1600
1600
1601 IPython CPU timings (estimated):\\
1601 IPython CPU timings (estimated):\\
1602 User : 0.19597 s.\\
1602 User : 0.19597 s.\\
1603 System: 0.0 s.\\
1603 System: 0.0 s.\\
1604
1604
1605 In [2]: run -t -N5 uniq_stable
1605 In [2]: run -t -N5 uniq_stable
1606
1606
1607 IPython CPU timings (estimated):\\
1607 IPython CPU timings (estimated):\\
1608 Total runs performed: 5\\
1608 Total runs performed: 5\\
1609 Times : Total Per run\\
1609 Times : Total Per run\\
1610 User : 0.910862 s, 0.1821724 s.\\
1610 User : 0.910862 s, 0.1821724 s.\\
1611 System: 0.0 s, 0.0 s.
1611 System: 0.0 s, 0.0 s.
1612
1612
1613 -d: run your program under the control of pdb, the Python debugger.
1613 -d: run your program under the control of pdb, the Python debugger.
1614 This allows you to execute your program step by step, watch variables,
1614 This allows you to execute your program step by step, watch variables,
1615 etc. Internally, what IPython does is similar to calling:
1615 etc. Internally, what IPython does is similar to calling:
1616
1616
1617 pdb.run('execfile("YOURFILENAME")')
1617 pdb.run('execfile("YOURFILENAME")')
1618
1618
1619 with a breakpoint set on line 1 of your file. You can change the line
1619 with a breakpoint set on line 1 of your file. You can change the line
1620 number for this automatic breakpoint to be <N> by using the -bN option
1620 number for this automatic breakpoint to be <N> by using the -bN option
1621 (where N must be an integer). For example:
1621 (where N must be an integer). For example:
1622
1622
1623 %run -d -b40 myscript
1623 %run -d -b40 myscript
1624
1624
1625 will set the first breakpoint at line 40 in myscript.py. Note that
1625 will set the first breakpoint at line 40 in myscript.py. Note that
1626 the first breakpoint must be set on a line which actually does
1626 the first breakpoint must be set on a line which actually does
1627 something (not a comment or docstring) for it to stop execution.
1627 something (not a comment or docstring) for it to stop execution.
1628
1628
1629 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1629 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1630 first enter 'c' (without qoutes) to start execution up to the first
1630 first enter 'c' (without qoutes) to start execution up to the first
1631 breakpoint.
1631 breakpoint.
1632
1632
1633 Entering 'help' gives information about the use of the debugger. You
1633 Entering 'help' gives information about the use of the debugger. You
1634 can easily see pdb's full documentation with "import pdb;pdb.help()"
1634 can easily see pdb's full documentation with "import pdb;pdb.help()"
1635 at a prompt.
1635 at a prompt.
1636
1636
1637 -p: run program under the control of the Python profiler module (which
1637 -p: run program under the control of the Python profiler module (which
1638 prints a detailed report of execution times, function calls, etc).
1638 prints a detailed report of execution times, function calls, etc).
1639
1639
1640 You can pass other options after -p which affect the behavior of the
1640 You can pass other options after -p which affect the behavior of the
1641 profiler itself. See the docs for %prun for details.
1641 profiler itself. See the docs for %prun for details.
1642
1642
1643 In this mode, the program's variables do NOT propagate back to the
1643 In this mode, the program's variables do NOT propagate back to the
1644 IPython interactive namespace (because they remain in the namespace
1644 IPython interactive namespace (because they remain in the namespace
1645 where the profiler executes them).
1645 where the profiler executes them).
1646
1646
1647 Internally this triggers a call to %prun, see its documentation for
1647 Internally this triggers a call to %prun, see its documentation for
1648 details on the options available specifically for profiling.
1648 details on the options available specifically for profiling.
1649
1649
1650 There is one special usage for which the text above doesn't apply:
1650 There is one special usage for which the text above doesn't apply:
1651 if the filename ends with .ipy, the file is run as ipython script,
1651 if the filename ends with .ipy, the file is run as ipython script,
1652 just as if the commands were written on IPython prompt.
1652 just as if the commands were written on IPython prompt.
1653 """
1653 """
1654
1654
1655 # get arguments and set sys.argv for program to be run.
1655 # get arguments and set sys.argv for program to be run.
1656 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1656 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1657 mode='list',list_all=1)
1657 mode='list',list_all=1)
1658
1658
1659 try:
1659 try:
1660 filename = file_finder(arg_lst[0])
1660 filename = file_finder(arg_lst[0])
1661 except IndexError:
1661 except IndexError:
1662 warn('you must provide at least a filename.')
1662 warn('you must provide at least a filename.')
1663 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1663 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1664 return
1664 return
1665 except IOError,msg:
1665 except IOError,msg:
1666 error(msg)
1666 error(msg)
1667 return
1667 return
1668
1668
1669 if filename.lower().endswith('.ipy'):
1669 if filename.lower().endswith('.ipy'):
1670 self.shell.safe_execfile_ipy(filename)
1670 self.shell.safe_execfile_ipy(filename)
1671 return
1671 return
1672
1672
1673 # Control the response to exit() calls made by the script being run
1673 # Control the response to exit() calls made by the script being run
1674 exit_ignore = opts.has_key('e')
1674 exit_ignore = opts.has_key('e')
1675
1675
1676 # Make sure that the running script gets a proper sys.argv as if it
1676 # Make sure that the running script gets a proper sys.argv as if it
1677 # were run from a system shell.
1677 # were run from a system shell.
1678 save_argv = sys.argv # save it for later restoring
1678 save_argv = sys.argv # save it for later restoring
1679 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1679 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1680
1680
1681 if opts.has_key('i'):
1681 if opts.has_key('i'):
1682 # Run in user's interactive namespace
1682 # Run in user's interactive namespace
1683 prog_ns = self.shell.user_ns
1683 prog_ns = self.shell.user_ns
1684 __name__save = self.shell.user_ns['__name__']
1684 __name__save = self.shell.user_ns['__name__']
1685 prog_ns['__name__'] = '__main__'
1685 prog_ns['__name__'] = '__main__'
1686 main_mod = self.shell.new_main_mod(prog_ns)
1686 main_mod = self.shell.new_main_mod(prog_ns)
1687 else:
1687 else:
1688 # Run in a fresh, empty namespace
1688 # Run in a fresh, empty namespace
1689 if opts.has_key('n'):
1689 if opts.has_key('n'):
1690 name = os.path.splitext(os.path.basename(filename))[0]
1690 name = os.path.splitext(os.path.basename(filename))[0]
1691 else:
1691 else:
1692 name = '__main__'
1692 name = '__main__'
1693
1693
1694 main_mod = self.shell.new_main_mod()
1694 main_mod = self.shell.new_main_mod()
1695 prog_ns = main_mod.__dict__
1695 prog_ns = main_mod.__dict__
1696 prog_ns['__name__'] = name
1696 prog_ns['__name__'] = name
1697
1697
1698 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1698 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1699 # set the __file__ global in the script's namespace
1699 # set the __file__ global in the script's namespace
1700 prog_ns['__file__'] = filename
1700 prog_ns['__file__'] = filename
1701
1701
1702 # pickle fix. See iplib for an explanation. But we need to make sure
1702 # pickle fix. See iplib for an explanation. But we need to make sure
1703 # that, if we overwrite __main__, we replace it at the end
1703 # that, if we overwrite __main__, we replace it at the end
1704 main_mod_name = prog_ns['__name__']
1704 main_mod_name = prog_ns['__name__']
1705
1705
1706 if main_mod_name == '__main__':
1706 if main_mod_name == '__main__':
1707 restore_main = sys.modules['__main__']
1707 restore_main = sys.modules['__main__']
1708 else:
1708 else:
1709 restore_main = False
1709 restore_main = False
1710
1710
1711 # This needs to be undone at the end to prevent holding references to
1711 # This needs to be undone at the end to prevent holding references to
1712 # every single object ever created.
1712 # every single object ever created.
1713 sys.modules[main_mod_name] = main_mod
1713 sys.modules[main_mod_name] = main_mod
1714
1714
1715 stats = None
1715 stats = None
1716 try:
1716 try:
1717 self.shell.savehist()
1717 self.shell.savehist()
1718
1718
1719 if opts.has_key('p'):
1719 if opts.has_key('p'):
1720 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1720 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1721 else:
1721 else:
1722 if opts.has_key('d'):
1722 if opts.has_key('d'):
1723 deb = debugger.Pdb(self.shell.colors)
1723 deb = debugger.Pdb(self.shell.colors)
1724 # reset Breakpoint state, which is moronically kept
1724 # reset Breakpoint state, which is moronically kept
1725 # in a class
1725 # in a class
1726 bdb.Breakpoint.next = 1
1726 bdb.Breakpoint.next = 1
1727 bdb.Breakpoint.bplist = {}
1727 bdb.Breakpoint.bplist = {}
1728 bdb.Breakpoint.bpbynumber = [None]
1728 bdb.Breakpoint.bpbynumber = [None]
1729 # Set an initial breakpoint to stop execution
1729 # Set an initial breakpoint to stop execution
1730 maxtries = 10
1730 maxtries = 10
1731 bp = int(opts.get('b',[1])[0])
1731 bp = int(opts.get('b',[1])[0])
1732 checkline = deb.checkline(filename,bp)
1732 checkline = deb.checkline(filename,bp)
1733 if not checkline:
1733 if not checkline:
1734 for bp in range(bp+1,bp+maxtries+1):
1734 for bp in range(bp+1,bp+maxtries+1):
1735 if deb.checkline(filename,bp):
1735 if deb.checkline(filename,bp):
1736 break
1736 break
1737 else:
1737 else:
1738 msg = ("\nI failed to find a valid line to set "
1738 msg = ("\nI failed to find a valid line to set "
1739 "a breakpoint\n"
1739 "a breakpoint\n"
1740 "after trying up to line: %s.\n"
1740 "after trying up to line: %s.\n"
1741 "Please set a valid breakpoint manually "
1741 "Please set a valid breakpoint manually "
1742 "with the -b option." % bp)
1742 "with the -b option." % bp)
1743 error(msg)
1743 error(msg)
1744 return
1744 return
1745 # if we find a good linenumber, set the breakpoint
1745 # if we find a good linenumber, set the breakpoint
1746 deb.do_break('%s:%s' % (filename,bp))
1746 deb.do_break('%s:%s' % (filename,bp))
1747 # Start file run
1747 # Start file run
1748 print "NOTE: Enter 'c' at the",
1748 print "NOTE: Enter 'c' at the",
1749 print "%s prompt to start your script." % deb.prompt
1749 print "%s prompt to start your script." % deb.prompt
1750 try:
1750 try:
1751 deb.run('execfile("%s")' % filename,prog_ns)
1751 deb.run('execfile("%s")' % filename,prog_ns)
1752
1752
1753 except:
1753 except:
1754 etype, value, tb = sys.exc_info()
1754 etype, value, tb = sys.exc_info()
1755 # Skip three frames in the traceback: the %run one,
1755 # Skip three frames in the traceback: the %run one,
1756 # one inside bdb.py, and the command-line typed by the
1756 # one inside bdb.py, and the command-line typed by the
1757 # user (run by exec in pdb itself).
1757 # user (run by exec in pdb itself).
1758 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1758 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1759 else:
1759 else:
1760 if runner is None:
1760 if runner is None:
1761 runner = self.shell.safe_execfile
1761 runner = self.shell.safe_execfile
1762 if opts.has_key('t'):
1762 if opts.has_key('t'):
1763 # timed execution
1763 # timed execution
1764 try:
1764 try:
1765 nruns = int(opts['N'][0])
1765 nruns = int(opts['N'][0])
1766 if nruns < 1:
1766 if nruns < 1:
1767 error('Number of runs must be >=1')
1767 error('Number of runs must be >=1')
1768 return
1768 return
1769 except (KeyError):
1769 except (KeyError):
1770 nruns = 1
1770 nruns = 1
1771 if nruns == 1:
1771 if nruns == 1:
1772 t0 = clock2()
1772 t0 = clock2()
1773 runner(filename,prog_ns,prog_ns,
1773 runner(filename,prog_ns,prog_ns,
1774 exit_ignore=exit_ignore)
1774 exit_ignore=exit_ignore)
1775 t1 = clock2()
1775 t1 = clock2()
1776 t_usr = t1[0]-t0[0]
1776 t_usr = t1[0]-t0[0]
1777 t_sys = t1[1]-t0[1]
1777 t_sys = t1[1]-t0[1]
1778 print "\nIPython CPU timings (estimated):"
1778 print "\nIPython CPU timings (estimated):"
1779 print " User : %10s s." % t_usr
1779 print " User : %10s s." % t_usr
1780 print " System: %10s s." % t_sys
1780 print " System: %10s s." % t_sys
1781 else:
1781 else:
1782 runs = range(nruns)
1782 runs = range(nruns)
1783 t0 = clock2()
1783 t0 = clock2()
1784 for nr in runs:
1784 for nr in runs:
1785 runner(filename,prog_ns,prog_ns,
1785 runner(filename,prog_ns,prog_ns,
1786 exit_ignore=exit_ignore)
1786 exit_ignore=exit_ignore)
1787 t1 = clock2()
1787 t1 = clock2()
1788 t_usr = t1[0]-t0[0]
1788 t_usr = t1[0]-t0[0]
1789 t_sys = t1[1]-t0[1]
1789 t_sys = t1[1]-t0[1]
1790 print "\nIPython CPU timings (estimated):"
1790 print "\nIPython CPU timings (estimated):"
1791 print "Total runs performed:",nruns
1791 print "Total runs performed:",nruns
1792 print " Times : %10s %10s" % ('Total','Per run')
1792 print " Times : %10s %10s" % ('Total','Per run')
1793 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1793 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1794 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1794 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1795
1795
1796 else:
1796 else:
1797 # regular execution
1797 # regular execution
1798 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1798 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1799
1799
1800 if opts.has_key('i'):
1800 if opts.has_key('i'):
1801 self.shell.user_ns['__name__'] = __name__save
1801 self.shell.user_ns['__name__'] = __name__save
1802 else:
1802 else:
1803 # The shell MUST hold a reference to prog_ns so after %run
1803 # The shell MUST hold a reference to prog_ns so after %run
1804 # exits, the python deletion mechanism doesn't zero it out
1804 # exits, the python deletion mechanism doesn't zero it out
1805 # (leaving dangling references).
1805 # (leaving dangling references).
1806 self.shell.cache_main_mod(prog_ns,filename)
1806 self.shell.cache_main_mod(prog_ns,filename)
1807 # update IPython interactive namespace
1807 # update IPython interactive namespace
1808
1808
1809 # Some forms of read errors on the file may mean the
1809 # Some forms of read errors on the file may mean the
1810 # __name__ key was never set; using pop we don't have to
1810 # __name__ key was never set; using pop we don't have to
1811 # worry about a possible KeyError.
1811 # worry about a possible KeyError.
1812 prog_ns.pop('__name__', None)
1812 prog_ns.pop('__name__', None)
1813
1813
1814 self.shell.user_ns.update(prog_ns)
1814 self.shell.user_ns.update(prog_ns)
1815 finally:
1815 finally:
1816 # It's a bit of a mystery why, but __builtins__ can change from
1816 # It's a bit of a mystery why, but __builtins__ can change from
1817 # being a module to becoming a dict missing some key data after
1817 # being a module to becoming a dict missing some key data after
1818 # %run. As best I can see, this is NOT something IPython is doing
1818 # %run. As best I can see, this is NOT something IPython is doing
1819 # at all, and similar problems have been reported before:
1819 # at all, and similar problems have been reported before:
1820 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1820 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1821 # Since this seems to be done by the interpreter itself, the best
1821 # Since this seems to be done by the interpreter itself, the best
1822 # we can do is to at least restore __builtins__ for the user on
1822 # we can do is to at least restore __builtins__ for the user on
1823 # exit.
1823 # exit.
1824 self.shell.user_ns['__builtins__'] = __builtin__
1824 self.shell.user_ns['__builtins__'] = __builtin__
1825
1825
1826 # Ensure key global structures are restored
1826 # Ensure key global structures are restored
1827 sys.argv = save_argv
1827 sys.argv = save_argv
1828 if restore_main:
1828 if restore_main:
1829 sys.modules['__main__'] = restore_main
1829 sys.modules['__main__'] = restore_main
1830 else:
1830 else:
1831 # Remove from sys.modules the reference to main_mod we'd
1831 # Remove from sys.modules the reference to main_mod we'd
1832 # added. Otherwise it will trap references to objects
1832 # added. Otherwise it will trap references to objects
1833 # contained therein.
1833 # contained therein.
1834 del sys.modules[main_mod_name]
1834 del sys.modules[main_mod_name]
1835
1835
1836 self.shell.reloadhist()
1836 self.shell.reloadhist()
1837
1837
1838 return stats
1838 return stats
1839
1839
1840 @testdec.skip_doctest
1840 @testdec.skip_doctest
1841 def magic_timeit(self, parameter_s =''):
1841 def magic_timeit(self, parameter_s =''):
1842 """Time execution of a Python statement or expression
1842 """Time execution of a Python statement or expression
1843
1843
1844 Usage:\\
1844 Usage:\\
1845 %timeit [-n<N> -r<R> [-t|-c]] statement
1845 %timeit [-n<N> -r<R> [-t|-c]] statement
1846
1846
1847 Time execution of a Python statement or expression using the timeit
1847 Time execution of a Python statement or expression using the timeit
1848 module.
1848 module.
1849
1849
1850 Options:
1850 Options:
1851 -n<N>: execute the given statement <N> times in a loop. If this value
1851 -n<N>: execute the given statement <N> times in a loop. If this value
1852 is not given, a fitting value is chosen.
1852 is not given, a fitting value is chosen.
1853
1853
1854 -r<R>: repeat the loop iteration <R> times and take the best result.
1854 -r<R>: repeat the loop iteration <R> times and take the best result.
1855 Default: 3
1855 Default: 3
1856
1856
1857 -t: use time.time to measure the time, which is the default on Unix.
1857 -t: use time.time to measure the time, which is the default on Unix.
1858 This function measures wall time.
1858 This function measures wall time.
1859
1859
1860 -c: use time.clock to measure the time, which is the default on
1860 -c: use time.clock to measure the time, which is the default on
1861 Windows and measures wall time. On Unix, resource.getrusage is used
1861 Windows and measures wall time. On Unix, resource.getrusage is used
1862 instead and returns the CPU user time.
1862 instead and returns the CPU user time.
1863
1863
1864 -p<P>: use a precision of <P> digits to display the timing result.
1864 -p<P>: use a precision of <P> digits to display the timing result.
1865 Default: 3
1865 Default: 3
1866
1866
1867
1867
1868 Examples:
1868 Examples:
1869
1869
1870 In [1]: %timeit pass
1870 In [1]: %timeit pass
1871 10000000 loops, best of 3: 53.3 ns per loop
1871 10000000 loops, best of 3: 53.3 ns per loop
1872
1872
1873 In [2]: u = None
1873 In [2]: u = None
1874
1874
1875 In [3]: %timeit u is None
1875 In [3]: %timeit u is None
1876 10000000 loops, best of 3: 184 ns per loop
1876 10000000 loops, best of 3: 184 ns per loop
1877
1877
1878 In [4]: %timeit -r 4 u == None
1878 In [4]: %timeit -r 4 u == None
1879 1000000 loops, best of 4: 242 ns per loop
1879 1000000 loops, best of 4: 242 ns per loop
1880
1880
1881 In [5]: import time
1881 In [5]: import time
1882
1882
1883 In [6]: %timeit -n1 time.sleep(2)
1883 In [6]: %timeit -n1 time.sleep(2)
1884 1 loops, best of 3: 2 s per loop
1884 1 loops, best of 3: 2 s per loop
1885
1885
1886
1886
1887 The times reported by %timeit will be slightly higher than those
1887 The times reported by %timeit will be slightly higher than those
1888 reported by the timeit.py script when variables are accessed. This is
1888 reported by the timeit.py script when variables are accessed. This is
1889 due to the fact that %timeit executes the statement in the namespace
1889 due to the fact that %timeit executes the statement in the namespace
1890 of the shell, compared with timeit.py, which uses a single setup
1890 of the shell, compared with timeit.py, which uses a single setup
1891 statement to import function or create variables. Generally, the bias
1891 statement to import function or create variables. Generally, the bias
1892 does not matter as long as results from timeit.py are not mixed with
1892 does not matter as long as results from timeit.py are not mixed with
1893 those from %timeit."""
1893 those from %timeit."""
1894
1894
1895 import timeit
1895 import timeit
1896 import math
1896 import math
1897
1897
1898 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1898 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1899 # certain terminals. Until we figure out a robust way of
1899 # certain terminals. Until we figure out a robust way of
1900 # auto-detecting if the terminal can deal with it, use plain 'us' for
1900 # auto-detecting if the terminal can deal with it, use plain 'us' for
1901 # microseconds. I am really NOT happy about disabling the proper
1901 # microseconds. I am really NOT happy about disabling the proper
1902 # 'micro' prefix, but crashing is worse... If anyone knows what the
1902 # 'micro' prefix, but crashing is worse... If anyone knows what the
1903 # right solution for this is, I'm all ears...
1903 # right solution for this is, I'm all ears...
1904 #
1904 #
1905 # Note: using
1905 # Note: using
1906 #
1906 #
1907 # s = u'\xb5'
1907 # s = u'\xb5'
1908 # s.encode(sys.getdefaultencoding())
1908 # s.encode(sys.getdefaultencoding())
1909 #
1909 #
1910 # is not sufficient, as I've seen terminals where that fails but
1910 # is not sufficient, as I've seen terminals where that fails but
1911 # print s
1911 # print s
1912 #
1912 #
1913 # succeeds
1913 # succeeds
1914 #
1914 #
1915 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1915 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1916
1916
1917 #units = [u"s", u"ms",u'\xb5',"ns"]
1917 #units = [u"s", u"ms",u'\xb5',"ns"]
1918 units = [u"s", u"ms",u'us',"ns"]
1918 units = [u"s", u"ms",u'us',"ns"]
1919
1919
1920 scaling = [1, 1e3, 1e6, 1e9]
1920 scaling = [1, 1e3, 1e6, 1e9]
1921
1921
1922 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1922 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1923 posix=False)
1923 posix=False)
1924 if stmt == "":
1924 if stmt == "":
1925 return
1925 return
1926 timefunc = timeit.default_timer
1926 timefunc = timeit.default_timer
1927 number = int(getattr(opts, "n", 0))
1927 number = int(getattr(opts, "n", 0))
1928 repeat = int(getattr(opts, "r", timeit.default_repeat))
1928 repeat = int(getattr(opts, "r", timeit.default_repeat))
1929 precision = int(getattr(opts, "p", 3))
1929 precision = int(getattr(opts, "p", 3))
1930 if hasattr(opts, "t"):
1930 if hasattr(opts, "t"):
1931 timefunc = time.time
1931 timefunc = time.time
1932 if hasattr(opts, "c"):
1932 if hasattr(opts, "c"):
1933 timefunc = clock
1933 timefunc = clock
1934
1934
1935 timer = timeit.Timer(timer=timefunc)
1935 timer = timeit.Timer(timer=timefunc)
1936 # this code has tight coupling to the inner workings of timeit.Timer,
1936 # this code has tight coupling to the inner workings of timeit.Timer,
1937 # but is there a better way to achieve that the code stmt has access
1937 # but is there a better way to achieve that the code stmt has access
1938 # to the shell namespace?
1938 # to the shell namespace?
1939
1939
1940 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1940 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1941 'setup': "pass"}
1941 'setup': "pass"}
1942 # Track compilation time so it can be reported if too long
1942 # Track compilation time so it can be reported if too long
1943 # Minimum time above which compilation time will be reported
1943 # Minimum time above which compilation time will be reported
1944 tc_min = 0.1
1944 tc_min = 0.1
1945
1945
1946 t0 = clock()
1946 t0 = clock()
1947 code = compile(src, "<magic-timeit>", "exec")
1947 code = compile(src, "<magic-timeit>", "exec")
1948 tc = clock()-t0
1948 tc = clock()-t0
1949
1949
1950 ns = {}
1950 ns = {}
1951 exec code in self.shell.user_ns, ns
1951 exec code in self.shell.user_ns, ns
1952 timer.inner = ns["inner"]
1952 timer.inner = ns["inner"]
1953
1953
1954 if number == 0:
1954 if number == 0:
1955 # determine number so that 0.2 <= total time < 2.0
1955 # determine number so that 0.2 <= total time < 2.0
1956 number = 1
1956 number = 1
1957 for i in range(1, 10):
1957 for i in range(1, 10):
1958 if timer.timeit(number) >= 0.2:
1958 if timer.timeit(number) >= 0.2:
1959 break
1959 break
1960 number *= 10
1960 number *= 10
1961
1961
1962 best = min(timer.repeat(repeat, number)) / number
1962 best = min(timer.repeat(repeat, number)) / number
1963
1963
1964 if best > 0.0 and best < 1000.0:
1964 if best > 0.0 and best < 1000.0:
1965 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1965 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1966 elif best >= 1000.0:
1966 elif best >= 1000.0:
1967 order = 0
1967 order = 0
1968 else:
1968 else:
1969 order = 3
1969 order = 3
1970 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1970 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1971 precision,
1971 precision,
1972 best * scaling[order],
1972 best * scaling[order],
1973 units[order])
1973 units[order])
1974 if tc > tc_min:
1974 if tc > tc_min:
1975 print "Compiler time: %.2f s" % tc
1975 print "Compiler time: %.2f s" % tc
1976
1976
1977 @testdec.skip_doctest
1977 @testdec.skip_doctest
1978 def magic_time(self,parameter_s = ''):
1978 def magic_time(self,parameter_s = ''):
1979 """Time execution of a Python statement or expression.
1979 """Time execution of a Python statement or expression.
1980
1980
1981 The CPU and wall clock times are printed, and the value of the
1981 The CPU and wall clock times are printed, and the value of the
1982 expression (if any) is returned. Note that under Win32, system time
1982 expression (if any) is returned. Note that under Win32, system time
1983 is always reported as 0, since it can not be measured.
1983 is always reported as 0, since it can not be measured.
1984
1984
1985 This function provides very basic timing functionality. In Python
1985 This function provides very basic timing functionality. In Python
1986 2.3, the timeit module offers more control and sophistication, so this
1986 2.3, the timeit module offers more control and sophistication, so this
1987 could be rewritten to use it (patches welcome).
1987 could be rewritten to use it (patches welcome).
1988
1988
1989 Some examples:
1989 Some examples:
1990
1990
1991 In [1]: time 2**128
1991 In [1]: time 2**128
1992 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1992 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1993 Wall time: 0.00
1993 Wall time: 0.00
1994 Out[1]: 340282366920938463463374607431768211456L
1994 Out[1]: 340282366920938463463374607431768211456L
1995
1995
1996 In [2]: n = 1000000
1996 In [2]: n = 1000000
1997
1997
1998 In [3]: time sum(range(n))
1998 In [3]: time sum(range(n))
1999 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1999 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
2000 Wall time: 1.37
2000 Wall time: 1.37
2001 Out[3]: 499999500000L
2001 Out[3]: 499999500000L
2002
2002
2003 In [4]: time print 'hello world'
2003 In [4]: time print 'hello world'
2004 hello world
2004 hello world
2005 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2005 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2006 Wall time: 0.00
2006 Wall time: 0.00
2007
2007
2008 Note that the time needed by Python to compile the given expression
2008 Note that the time needed by Python to compile the given expression
2009 will be reported if it is more than 0.1s. In this example, the
2009 will be reported if it is more than 0.1s. In this example, the
2010 actual exponentiation is done by Python at compilation time, so while
2010 actual exponentiation is done by Python at compilation time, so while
2011 the expression can take a noticeable amount of time to compute, that
2011 the expression can take a noticeable amount of time to compute, that
2012 time is purely due to the compilation:
2012 time is purely due to the compilation:
2013
2013
2014 In [5]: time 3**9999;
2014 In [5]: time 3**9999;
2015 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2015 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2016 Wall time: 0.00 s
2016 Wall time: 0.00 s
2017
2017
2018 In [6]: time 3**999999;
2018 In [6]: time 3**999999;
2019 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2019 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
2020 Wall time: 0.00 s
2020 Wall time: 0.00 s
2021 Compiler : 0.78 s
2021 Compiler : 0.78 s
2022 """
2022 """
2023
2023
2024 # fail immediately if the given expression can't be compiled
2024 # fail immediately if the given expression can't be compiled
2025
2025
2026 expr = self.shell.prefilter(parameter_s,False)
2026 expr = self.shell.prefilter(parameter_s,False)
2027
2027
2028 # Minimum time above which compilation time will be reported
2028 # Minimum time above which compilation time will be reported
2029 tc_min = 0.1
2029 tc_min = 0.1
2030
2030
2031 try:
2031 try:
2032 mode = 'eval'
2032 mode = 'eval'
2033 t0 = clock()
2033 t0 = clock()
2034 code = compile(expr,'<timed eval>',mode)
2034 code = compile(expr,'<timed eval>',mode)
2035 tc = clock()-t0
2035 tc = clock()-t0
2036 except SyntaxError:
2036 except SyntaxError:
2037 mode = 'exec'
2037 mode = 'exec'
2038 t0 = clock()
2038 t0 = clock()
2039 code = compile(expr,'<timed exec>',mode)
2039 code = compile(expr,'<timed exec>',mode)
2040 tc = clock()-t0
2040 tc = clock()-t0
2041 # skew measurement as little as possible
2041 # skew measurement as little as possible
2042 glob = self.shell.user_ns
2042 glob = self.shell.user_ns
2043 clk = clock2
2043 clk = clock2
2044 wtime = time.time
2044 wtime = time.time
2045 # time execution
2045 # time execution
2046 wall_st = wtime()
2046 wall_st = wtime()
2047 if mode=='eval':
2047 if mode=='eval':
2048 st = clk()
2048 st = clk()
2049 out = eval(code,glob)
2049 out = eval(code,glob)
2050 end = clk()
2050 end = clk()
2051 else:
2051 else:
2052 st = clk()
2052 st = clk()
2053 exec code in glob
2053 exec code in glob
2054 end = clk()
2054 end = clk()
2055 out = None
2055 out = None
2056 wall_end = wtime()
2056 wall_end = wtime()
2057 # Compute actual times and report
2057 # Compute actual times and report
2058 wall_time = wall_end-wall_st
2058 wall_time = wall_end-wall_st
2059 cpu_user = end[0]-st[0]
2059 cpu_user = end[0]-st[0]
2060 cpu_sys = end[1]-st[1]
2060 cpu_sys = end[1]-st[1]
2061 cpu_tot = cpu_user+cpu_sys
2061 cpu_tot = cpu_user+cpu_sys
2062 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
2062 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
2063 (cpu_user,cpu_sys,cpu_tot)
2063 (cpu_user,cpu_sys,cpu_tot)
2064 print "Wall time: %.2f s" % wall_time
2064 print "Wall time: %.2f s" % wall_time
2065 if tc > tc_min:
2065 if tc > tc_min:
2066 print "Compiler : %.2f s" % tc
2066 print "Compiler : %.2f s" % tc
2067 return out
2067 return out
2068
2068
2069 @testdec.skip_doctest
2069 @testdec.skip_doctest
2070 def magic_macro(self,parameter_s = ''):
2070 def magic_macro(self,parameter_s = ''):
2071 """Define a set of input lines as a macro for future re-execution.
2071 """Define a set of input lines as a macro for future re-execution.
2072
2072
2073 Usage:\\
2073 Usage:\\
2074 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
2074 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
2075
2075
2076 Options:
2076 Options:
2077
2077
2078 -r: use 'raw' input. By default, the 'processed' history is used,
2078 -r: use 'raw' input. By default, the 'processed' history is used,
2079 so that magics are loaded in their transformed version to valid
2079 so that magics are loaded in their transformed version to valid
2080 Python. If this option is given, the raw input as typed as the
2080 Python. If this option is given, the raw input as typed as the
2081 command line is used instead.
2081 command line is used instead.
2082
2082
2083 This will define a global variable called `name` which is a string
2083 This will define a global variable called `name` which is a string
2084 made of joining the slices and lines you specify (n1,n2,... numbers
2084 made of joining the slices and lines you specify (n1,n2,... numbers
2085 above) from your input history into a single string. This variable
2085 above) from your input history into a single string. This variable
2086 acts like an automatic function which re-executes those lines as if
2086 acts like an automatic function which re-executes those lines as if
2087 you had typed them. You just type 'name' at the prompt and the code
2087 you had typed them. You just type 'name' at the prompt and the code
2088 executes.
2088 executes.
2089
2089
2090 The notation for indicating number ranges is: n1-n2 means 'use line
2090 The notation for indicating number ranges is: n1-n2 means 'use line
2091 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
2091 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
2092 using the lines numbered 5,6 and 7.
2092 using the lines numbered 5,6 and 7.
2093
2093
2094 Note: as a 'hidden' feature, you can also use traditional python slice
2094 Note: as a 'hidden' feature, you can also use traditional python slice
2095 notation, where N:M means numbers N through M-1.
2095 notation, where N:M means numbers N through M-1.
2096
2096
2097 For example, if your history contains (%hist prints it):
2097 For example, if your history contains (%hist prints it):
2098
2098
2099 44: x=1
2099 44: x=1
2100 45: y=3
2100 45: y=3
2101 46: z=x+y
2101 46: z=x+y
2102 47: print x
2102 47: print x
2103 48: a=5
2103 48: a=5
2104 49: print 'x',x,'y',y
2104 49: print 'x',x,'y',y
2105
2105
2106 you can create a macro with lines 44 through 47 (included) and line 49
2106 you can create a macro with lines 44 through 47 (included) and line 49
2107 called my_macro with:
2107 called my_macro with:
2108
2108
2109 In [55]: %macro my_macro 44-47 49
2109 In [55]: %macro my_macro 44-47 49
2110
2110
2111 Now, typing `my_macro` (without quotes) will re-execute all this code
2111 Now, typing `my_macro` (without quotes) will re-execute all this code
2112 in one pass.
2112 in one pass.
2113
2113
2114 You don't need to give the line-numbers in order, and any given line
2114 You don't need to give the line-numbers in order, and any given line
2115 number can appear multiple times. You can assemble macros with any
2115 number can appear multiple times. You can assemble macros with any
2116 lines from your input history in any order.
2116 lines from your input history in any order.
2117
2117
2118 The macro is a simple object which holds its value in an attribute,
2118 The macro is a simple object which holds its value in an attribute,
2119 but IPython's display system checks for macros and executes them as
2119 but IPython's display system checks for macros and executes them as
2120 code instead of printing them when you type their name.
2120 code instead of printing them when you type their name.
2121
2121
2122 You can view a macro's contents by explicitly printing it with:
2122 You can view a macro's contents by explicitly printing it with:
2123
2123
2124 'print macro_name'.
2124 'print macro_name'.
2125
2125
2126 For one-off cases which DON'T contain magic function calls in them you
2126 For one-off cases which DON'T contain magic function calls in them you
2127 can obtain similar results by explicitly executing slices from your
2127 can obtain similar results by explicitly executing slices from your
2128 input history with:
2128 input history with:
2129
2129
2130 In [60]: exec In[44:48]+In[49]"""
2130 In [60]: exec In[44:48]+In[49]"""
2131
2131
2132 opts,args = self.parse_options(parameter_s,'r',mode='list')
2132 opts,args = self.parse_options(parameter_s,'r',mode='list')
2133 if not args:
2133 if not args:
2134 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2134 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2135 macs.sort()
2135 macs.sort()
2136 return macs
2136 return macs
2137 if len(args) == 1:
2137 if len(args) == 1:
2138 raise UsageError(
2138 raise UsageError(
2139 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2139 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2140 name,ranges = args[0], args[1:]
2140 name,ranges = args[0], args[1:]
2141
2141
2142 #print 'rng',ranges # dbg
2142 #print 'rng',ranges # dbg
2143 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2143 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2144 macro = Macro(lines)
2144 macro = Macro(lines)
2145 self.shell.define_macro(name, macro)
2145 self.shell.define_macro(name, macro)
2146 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2146 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2147 print 'Macro contents:'
2147 print 'Macro contents:'
2148 print macro,
2148 print macro,
2149
2149
2150 def magic_save(self,parameter_s = ''):
2150 def magic_save(self,parameter_s = ''):
2151 """Save a set of lines to a given filename.
2151 """Save a set of lines to a given filename.
2152
2152
2153 Usage:\\
2153 Usage:\\
2154 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2154 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2155
2155
2156 Options:
2156 Options:
2157
2157
2158 -r: use 'raw' input. By default, the 'processed' history is used,
2158 -r: use 'raw' input. By default, the 'processed' history is used,
2159 so that magics are loaded in their transformed version to valid
2159 so that magics are loaded in their transformed version to valid
2160 Python. If this option is given, the raw input as typed as the
2160 Python. If this option is given, the raw input as typed as the
2161 command line is used instead.
2161 command line is used instead.
2162
2162
2163 This function uses the same syntax as %macro for line extraction, but
2163 This function uses the same syntax as %macro for line extraction, but
2164 instead of creating a macro it saves the resulting string to the
2164 instead of creating a macro it saves the resulting string to the
2165 filename you specify.
2165 filename you specify.
2166
2166
2167 It adds a '.py' extension to the file if you don't do so yourself, and
2167 It adds a '.py' extension to the file if you don't do so yourself, and
2168 it asks for confirmation before overwriting existing files."""
2168 it asks for confirmation before overwriting existing files."""
2169
2169
2170 opts,args = self.parse_options(parameter_s,'r',mode='list')
2170 opts,args = self.parse_options(parameter_s,'r',mode='list')
2171 fname,ranges = args[0], args[1:]
2171 fname,ranges = args[0], args[1:]
2172 if not fname.endswith('.py'):
2172 if not fname.endswith('.py'):
2173 fname += '.py'
2173 fname += '.py'
2174 if os.path.isfile(fname):
2174 if os.path.isfile(fname):
2175 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2175 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2176 if ans.lower() not in ['y','yes']:
2176 if ans.lower() not in ['y','yes']:
2177 print 'Operation cancelled.'
2177 print 'Operation cancelled.'
2178 return
2178 return
2179 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2179 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2180 f = file(fname,'w')
2180 f = file(fname,'w')
2181 f.write(cmds)
2181 f.write(cmds)
2182 f.close()
2182 f.close()
2183 print 'The following commands were written to file `%s`:' % fname
2183 print 'The following commands were written to file `%s`:' % fname
2184 print cmds
2184 print cmds
2185
2185
2186 def _edit_macro(self,mname,macro):
2186 def _edit_macro(self,mname,macro):
2187 """open an editor with the macro data in a file"""
2187 """open an editor with the macro data in a file"""
2188 filename = self.shell.mktempfile(macro.value)
2188 filename = self.shell.mktempfile(macro.value)
2189 self.shell.hooks.editor(filename)
2189 self.shell.hooks.editor(filename)
2190
2190
2191 # and make a new macro object, to replace the old one
2191 # and make a new macro object, to replace the old one
2192 mfile = open(filename)
2192 mfile = open(filename)
2193 mvalue = mfile.read()
2193 mvalue = mfile.read()
2194 mfile.close()
2194 mfile.close()
2195 self.shell.user_ns[mname] = Macro(mvalue)
2195 self.shell.user_ns[mname] = Macro(mvalue)
2196
2196
2197 def magic_ed(self,parameter_s=''):
2197 def magic_ed(self,parameter_s=''):
2198 """Alias to %edit."""
2198 """Alias to %edit."""
2199 return self.magic_edit(parameter_s)
2199 return self.magic_edit(parameter_s)
2200
2200
2201 @testdec.skip_doctest
2201 @testdec.skip_doctest
2202 def magic_edit(self,parameter_s='',last_call=['','']):
2202 def magic_edit(self,parameter_s='',last_call=['','']):
2203 """Bring up an editor and execute the resulting code.
2203 """Bring up an editor and execute the resulting code.
2204
2204
2205 Usage:
2205 Usage:
2206 %edit [options] [args]
2206 %edit [options] [args]
2207
2207
2208 %edit runs IPython's editor hook. The default version of this hook is
2208 %edit runs IPython's editor hook. The default version of this hook is
2209 set to call the __IPYTHON__.rc.editor command. This is read from your
2209 set to call the __IPYTHON__.rc.editor command. This is read from your
2210 environment variable $EDITOR. If this isn't found, it will default to
2210 environment variable $EDITOR. If this isn't found, it will default to
2211 vi under Linux/Unix and to notepad under Windows. See the end of this
2211 vi under Linux/Unix and to notepad under Windows. See the end of this
2212 docstring for how to change the editor hook.
2212 docstring for how to change the editor hook.
2213
2213
2214 You can also set the value of this editor via the command line option
2214 You can also set the value of this editor via the command line option
2215 '-editor' or in your ipythonrc file. This is useful if you wish to use
2215 '-editor' or in your ipythonrc file. This is useful if you wish to use
2216 specifically for IPython an editor different from your typical default
2216 specifically for IPython an editor different from your typical default
2217 (and for Windows users who typically don't set environment variables).
2217 (and for Windows users who typically don't set environment variables).
2218
2218
2219 This command allows you to conveniently edit multi-line code right in
2219 This command allows you to conveniently edit multi-line code right in
2220 your IPython session.
2220 your IPython session.
2221
2221
2222 If called without arguments, %edit opens up an empty editor with a
2222 If called without arguments, %edit opens up an empty editor with a
2223 temporary file and will execute the contents of this file when you
2223 temporary file and will execute the contents of this file when you
2224 close it (don't forget to save it!).
2224 close it (don't forget to save it!).
2225
2225
2226
2226
2227 Options:
2227 Options:
2228
2228
2229 -n <number>: open the editor at a specified line number. By default,
2229 -n <number>: open the editor at a specified line number. By default,
2230 the IPython editor hook uses the unix syntax 'editor +N filename', but
2230 the IPython editor hook uses the unix syntax 'editor +N filename', but
2231 you can configure this by providing your own modified hook if your
2231 you can configure this by providing your own modified hook if your
2232 favorite editor supports line-number specifications with a different
2232 favorite editor supports line-number specifications with a different
2233 syntax.
2233 syntax.
2234
2234
2235 -p: this will call the editor with the same data as the previous time
2235 -p: this will call the editor with the same data as the previous time
2236 it was used, regardless of how long ago (in your current session) it
2236 it was used, regardless of how long ago (in your current session) it
2237 was.
2237 was.
2238
2238
2239 -r: use 'raw' input. This option only applies to input taken from the
2239 -r: use 'raw' input. This option only applies to input taken from the
2240 user's history. By default, the 'processed' history is used, so that
2240 user's history. By default, the 'processed' history is used, so that
2241 magics are loaded in their transformed version to valid Python. If
2241 magics are loaded in their transformed version to valid Python. If
2242 this option is given, the raw input as typed as the command line is
2242 this option is given, the raw input as typed as the command line is
2243 used instead. When you exit the editor, it will be executed by
2243 used instead. When you exit the editor, it will be executed by
2244 IPython's own processor.
2244 IPython's own processor.
2245
2245
2246 -x: do not execute the edited code immediately upon exit. This is
2246 -x: do not execute the edited code immediately upon exit. This is
2247 mainly useful if you are editing programs which need to be called with
2247 mainly useful if you are editing programs which need to be called with
2248 command line arguments, which you can then do using %run.
2248 command line arguments, which you can then do using %run.
2249
2249
2250
2250
2251 Arguments:
2251 Arguments:
2252
2252
2253 If arguments are given, the following possibilites exist:
2253 If arguments are given, the following possibilites exist:
2254
2254
2255 - The arguments are numbers or pairs of colon-separated numbers (like
2255 - The arguments are numbers or pairs of colon-separated numbers (like
2256 1 4:8 9). These are interpreted as lines of previous input to be
2256 1 4:8 9). These are interpreted as lines of previous input to be
2257 loaded into the editor. The syntax is the same of the %macro command.
2257 loaded into the editor. The syntax is the same of the %macro command.
2258
2258
2259 - If the argument doesn't start with a number, it is evaluated as a
2259 - If the argument doesn't start with a number, it is evaluated as a
2260 variable and its contents loaded into the editor. You can thus edit
2260 variable and its contents loaded into the editor. You can thus edit
2261 any string which contains python code (including the result of
2261 any string which contains python code (including the result of
2262 previous edits).
2262 previous edits).
2263
2263
2264 - If the argument is the name of an object (other than a string),
2264 - If the argument is the name of an object (other than a string),
2265 IPython will try to locate the file where it was defined and open the
2265 IPython will try to locate the file where it was defined and open the
2266 editor at the point where it is defined. You can use `%edit function`
2266 editor at the point where it is defined. You can use `%edit function`
2267 to load an editor exactly at the point where 'function' is defined,
2267 to load an editor exactly at the point where 'function' is defined,
2268 edit it and have the file be executed automatically.
2268 edit it and have the file be executed automatically.
2269
2269
2270 If the object is a macro (see %macro for details), this opens up your
2270 If the object is a macro (see %macro for details), this opens up your
2271 specified editor with a temporary file containing the macro's data.
2271 specified editor with a temporary file containing the macro's data.
2272 Upon exit, the macro is reloaded with the contents of the file.
2272 Upon exit, the macro is reloaded with the contents of the file.
2273
2273
2274 Note: opening at an exact line is only supported under Unix, and some
2274 Note: opening at an exact line is only supported under Unix, and some
2275 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2275 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2276 '+NUMBER' parameter necessary for this feature. Good editors like
2276 '+NUMBER' parameter necessary for this feature. Good editors like
2277 (X)Emacs, vi, jed, pico and joe all do.
2277 (X)Emacs, vi, jed, pico and joe all do.
2278
2278
2279 - If the argument is not found as a variable, IPython will look for a
2279 - If the argument is not found as a variable, IPython will look for a
2280 file with that name (adding .py if necessary) and load it into the
2280 file with that name (adding .py if necessary) and load it into the
2281 editor. It will execute its contents with execfile() when you exit,
2281 editor. It will execute its contents with execfile() when you exit,
2282 loading any code in the file into your interactive namespace.
2282 loading any code in the file into your interactive namespace.
2283
2283
2284 After executing your code, %edit will return as output the code you
2284 After executing your code, %edit will return as output the code you
2285 typed in the editor (except when it was an existing file). This way
2285 typed in the editor (except when it was an existing file). This way
2286 you can reload the code in further invocations of %edit as a variable,
2286 you can reload the code in further invocations of %edit as a variable,
2287 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2287 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2288 the output.
2288 the output.
2289
2289
2290 Note that %edit is also available through the alias %ed.
2290 Note that %edit is also available through the alias %ed.
2291
2291
2292 This is an example of creating a simple function inside the editor and
2292 This is an example of creating a simple function inside the editor and
2293 then modifying it. First, start up the editor:
2293 then modifying it. First, start up the editor:
2294
2294
2295 In [1]: ed
2295 In [1]: ed
2296 Editing... done. Executing edited code...
2296 Editing... done. Executing edited code...
2297 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2297 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2298
2298
2299 We can then call the function foo():
2299 We can then call the function foo():
2300
2300
2301 In [2]: foo()
2301 In [2]: foo()
2302 foo() was defined in an editing session
2302 foo() was defined in an editing session
2303
2303
2304 Now we edit foo. IPython automatically loads the editor with the
2304 Now we edit foo. IPython automatically loads the editor with the
2305 (temporary) file where foo() was previously defined:
2305 (temporary) file where foo() was previously defined:
2306
2306
2307 In [3]: ed foo
2307 In [3]: ed foo
2308 Editing... done. Executing edited code...
2308 Editing... done. Executing edited code...
2309
2309
2310 And if we call foo() again we get the modified version:
2310 And if we call foo() again we get the modified version:
2311
2311
2312 In [4]: foo()
2312 In [4]: foo()
2313 foo() has now been changed!
2313 foo() has now been changed!
2314
2314
2315 Here is an example of how to edit a code snippet successive
2315 Here is an example of how to edit a code snippet successive
2316 times. First we call the editor:
2316 times. First we call the editor:
2317
2317
2318 In [5]: ed
2318 In [5]: ed
2319 Editing... done. Executing edited code...
2319 Editing... done. Executing edited code...
2320 hello
2320 hello
2321 Out[5]: "print 'hello'n"
2321 Out[5]: "print 'hello'n"
2322
2322
2323 Now we call it again with the previous output (stored in _):
2323 Now we call it again with the previous output (stored in _):
2324
2324
2325 In [6]: ed _
2325 In [6]: ed _
2326 Editing... done. Executing edited code...
2326 Editing... done. Executing edited code...
2327 hello world
2327 hello world
2328 Out[6]: "print 'hello world'n"
2328 Out[6]: "print 'hello world'n"
2329
2329
2330 Now we call it with the output #8 (stored in _8, also as Out[8]):
2330 Now we call it with the output #8 (stored in _8, also as Out[8]):
2331
2331
2332 In [7]: ed _8
2332 In [7]: ed _8
2333 Editing... done. Executing edited code...
2333 Editing... done. Executing edited code...
2334 hello again
2334 hello again
2335 Out[7]: "print 'hello again'n"
2335 Out[7]: "print 'hello again'n"
2336
2336
2337
2337
2338 Changing the default editor hook:
2338 Changing the default editor hook:
2339
2339
2340 If you wish to write your own editor hook, you can put it in a
2340 If you wish to write your own editor hook, you can put it in a
2341 configuration file which you load at startup time. The default hook
2341 configuration file which you load at startup time. The default hook
2342 is defined in the IPython.core.hooks module, and you can use that as a
2342 is defined in the IPython.core.hooks module, and you can use that as a
2343 starting example for further modifications. That file also has
2343 starting example for further modifications. That file also has
2344 general instructions on how to set a new hook for use once you've
2344 general instructions on how to set a new hook for use once you've
2345 defined it."""
2345 defined it."""
2346
2346
2347 # FIXME: This function has become a convoluted mess. It needs a
2347 # FIXME: This function has become a convoluted mess. It needs a
2348 # ground-up rewrite with clean, simple logic.
2348 # ground-up rewrite with clean, simple logic.
2349
2349
2350 def make_filename(arg):
2350 def make_filename(arg):
2351 "Make a filename from the given args"
2351 "Make a filename from the given args"
2352 try:
2352 try:
2353 filename = get_py_filename(arg)
2353 filename = get_py_filename(arg)
2354 except IOError:
2354 except IOError:
2355 if args.endswith('.py'):
2355 if args.endswith('.py'):
2356 filename = arg
2356 filename = arg
2357 else:
2357 else:
2358 filename = None
2358 filename = None
2359 return filename
2359 return filename
2360
2360
2361 # custom exceptions
2361 # custom exceptions
2362 class DataIsObject(Exception): pass
2362 class DataIsObject(Exception): pass
2363
2363
2364 opts,args = self.parse_options(parameter_s,'prxn:')
2364 opts,args = self.parse_options(parameter_s,'prxn:')
2365 # Set a few locals from the options for convenience:
2365 # Set a few locals from the options for convenience:
2366 opts_p = opts.has_key('p')
2366 opts_p = opts.has_key('p')
2367 opts_r = opts.has_key('r')
2367 opts_r = opts.has_key('r')
2368
2368
2369 # Default line number value
2369 # Default line number value
2370 lineno = opts.get('n',None)
2370 lineno = opts.get('n',None)
2371
2371
2372 if opts_p:
2372 if opts_p:
2373 args = '_%s' % last_call[0]
2373 args = '_%s' % last_call[0]
2374 if not self.shell.user_ns.has_key(args):
2374 if not self.shell.user_ns.has_key(args):
2375 args = last_call[1]
2375 args = last_call[1]
2376
2376
2377 # use last_call to remember the state of the previous call, but don't
2377 # use last_call to remember the state of the previous call, but don't
2378 # let it be clobbered by successive '-p' calls.
2378 # let it be clobbered by successive '-p' calls.
2379 try:
2379 try:
2380 last_call[0] = self.shell.outputcache.prompt_count
2380 last_call[0] = self.shell.outputcache.prompt_count
2381 if not opts_p:
2381 if not opts_p:
2382 last_call[1] = parameter_s
2382 last_call[1] = parameter_s
2383 except:
2383 except:
2384 pass
2384 pass
2385
2385
2386 # by default this is done with temp files, except when the given
2386 # by default this is done with temp files, except when the given
2387 # arg is a filename
2387 # arg is a filename
2388 use_temp = 1
2388 use_temp = 1
2389
2389
2390 if re.match(r'\d',args):
2390 if re.match(r'\d',args):
2391 # Mode where user specifies ranges of lines, like in %macro.
2391 # Mode where user specifies ranges of lines, like in %macro.
2392 # This means that you can't edit files whose names begin with
2392 # This means that you can't edit files whose names begin with
2393 # numbers this way. Tough.
2393 # numbers this way. Tough.
2394 ranges = args.split()
2394 ranges = args.split()
2395 data = ''.join(self.extract_input_slices(ranges,opts_r))
2395 data = ''.join(self.extract_input_slices(ranges,opts_r))
2396 elif args.endswith('.py'):
2396 elif args.endswith('.py'):
2397 filename = make_filename(args)
2397 filename = make_filename(args)
2398 data = ''
2398 data = ''
2399 use_temp = 0
2399 use_temp = 0
2400 elif args:
2400 elif args:
2401 try:
2401 try:
2402 # Load the parameter given as a variable. If not a string,
2402 # Load the parameter given as a variable. If not a string,
2403 # process it as an object instead (below)
2403 # process it as an object instead (below)
2404
2404
2405 #print '*** args',args,'type',type(args) # dbg
2405 #print '*** args',args,'type',type(args) # dbg
2406 data = eval(args,self.shell.user_ns)
2406 data = eval(args,self.shell.user_ns)
2407 if not type(data) in StringTypes:
2407 if not type(data) in StringTypes:
2408 raise DataIsObject
2408 raise DataIsObject
2409
2409
2410 except (NameError,SyntaxError):
2410 except (NameError,SyntaxError):
2411 # given argument is not a variable, try as a filename
2411 # given argument is not a variable, try as a filename
2412 filename = make_filename(args)
2412 filename = make_filename(args)
2413 if filename is None:
2413 if filename is None:
2414 warn("Argument given (%s) can't be found as a variable "
2414 warn("Argument given (%s) can't be found as a variable "
2415 "or as a filename." % args)
2415 "or as a filename." % args)
2416 return
2416 return
2417
2417
2418 data = ''
2418 data = ''
2419 use_temp = 0
2419 use_temp = 0
2420 except DataIsObject:
2420 except DataIsObject:
2421
2421
2422 # macros have a special edit function
2422 # macros have a special edit function
2423 if isinstance(data,Macro):
2423 if isinstance(data,Macro):
2424 self._edit_macro(args,data)
2424 self._edit_macro(args,data)
2425 return
2425 return
2426
2426
2427 # For objects, try to edit the file where they are defined
2427 # For objects, try to edit the file where they are defined
2428 try:
2428 try:
2429 filename = inspect.getabsfile(data)
2429 filename = inspect.getabsfile(data)
2430 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2430 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2431 # class created by %edit? Try to find source
2431 # class created by %edit? Try to find source
2432 # by looking for method definitions instead, the
2432 # by looking for method definitions instead, the
2433 # __module__ in those classes is FakeModule.
2433 # __module__ in those classes is FakeModule.
2434 attrs = [getattr(data, aname) for aname in dir(data)]
2434 attrs = [getattr(data, aname) for aname in dir(data)]
2435 for attr in attrs:
2435 for attr in attrs:
2436 if not inspect.ismethod(attr):
2436 if not inspect.ismethod(attr):
2437 continue
2437 continue
2438 filename = inspect.getabsfile(attr)
2438 filename = inspect.getabsfile(attr)
2439 if filename and 'fakemodule' not in filename.lower():
2439 if filename and 'fakemodule' not in filename.lower():
2440 # change the attribute to be the edit target instead
2440 # change the attribute to be the edit target instead
2441 data = attr
2441 data = attr
2442 break
2442 break
2443
2443
2444 datafile = 1
2444 datafile = 1
2445 except TypeError:
2445 except TypeError:
2446 filename = make_filename(args)
2446 filename = make_filename(args)
2447 datafile = 1
2447 datafile = 1
2448 warn('Could not find file where `%s` is defined.\n'
2448 warn('Could not find file where `%s` is defined.\n'
2449 'Opening a file named `%s`' % (args,filename))
2449 'Opening a file named `%s`' % (args,filename))
2450 # Now, make sure we can actually read the source (if it was in
2450 # Now, make sure we can actually read the source (if it was in
2451 # a temp file it's gone by now).
2451 # a temp file it's gone by now).
2452 if datafile:
2452 if datafile:
2453 try:
2453 try:
2454 if lineno is None:
2454 if lineno is None:
2455 lineno = inspect.getsourcelines(data)[1]
2455 lineno = inspect.getsourcelines(data)[1]
2456 except IOError:
2456 except IOError:
2457 filename = make_filename(args)
2457 filename = make_filename(args)
2458 if filename is None:
2458 if filename is None:
2459 warn('The file `%s` where `%s` was defined cannot '
2459 warn('The file `%s` where `%s` was defined cannot '
2460 'be read.' % (filename,data))
2460 'be read.' % (filename,data))
2461 return
2461 return
2462 use_temp = 0
2462 use_temp = 0
2463 else:
2463 else:
2464 data = ''
2464 data = ''
2465
2465
2466 if use_temp:
2466 if use_temp:
2467 filename = self.shell.mktempfile(data)
2467 filename = self.shell.mktempfile(data)
2468 print 'IPython will make a temporary file named:',filename
2468 print 'IPython will make a temporary file named:',filename
2469
2469
2470 # do actual editing here
2470 # do actual editing here
2471 print 'Editing...',
2471 print 'Editing...',
2472 sys.stdout.flush()
2472 sys.stdout.flush()
2473 try:
2473 try:
2474 # Quote filenames that may have spaces in them
2474 # Quote filenames that may have spaces in them
2475 if ' ' in filename:
2475 if ' ' in filename:
2476 filename = "%s" % filename
2476 filename = "%s" % filename
2477 self.shell.hooks.editor(filename,lineno)
2477 self.shell.hooks.editor(filename,lineno)
2478 except TryNext:
2478 except TryNext:
2479 warn('Could not open editor')
2479 warn('Could not open editor')
2480 return
2480 return
2481
2481
2482 # XXX TODO: should this be generalized for all string vars?
2482 # XXX TODO: should this be generalized for all string vars?
2483 # For now, this is special-cased to blocks created by cpaste
2483 # For now, this is special-cased to blocks created by cpaste
2484 if args.strip() == 'pasted_block':
2484 if args.strip() == 'pasted_block':
2485 self.shell.user_ns['pasted_block'] = file_read(filename)
2485 self.shell.user_ns['pasted_block'] = file_read(filename)
2486
2486
2487 if opts.has_key('x'): # -x prevents actual execution
2487 if opts.has_key('x'): # -x prevents actual execution
2488 print
2488 print
2489 else:
2489 else:
2490 print 'done. Executing edited code...'
2490 print 'done. Executing edited code...'
2491 if opts_r:
2491 if opts_r:
2492 self.shell.runlines(file_read(filename))
2492 self.shell.runlines(file_read(filename))
2493 else:
2493 else:
2494 self.shell.safe_execfile(filename,self.shell.user_ns,
2494 self.shell.safe_execfile(filename,self.shell.user_ns,
2495 self.shell.user_ns)
2495 self.shell.user_ns)
2496
2496
2497
2497
2498 if use_temp:
2498 if use_temp:
2499 try:
2499 try:
2500 return open(filename).read()
2500 return open(filename).read()
2501 except IOError,msg:
2501 except IOError,msg:
2502 if msg.filename == filename:
2502 if msg.filename == filename:
2503 warn('File not found. Did you forget to save?')
2503 warn('File not found. Did you forget to save?')
2504 return
2504 return
2505 else:
2505 else:
2506 self.shell.showtraceback()
2506 self.shell.showtraceback()
2507
2507
2508 def magic_xmode(self,parameter_s = ''):
2508 def magic_xmode(self,parameter_s = ''):
2509 """Switch modes for the exception handlers.
2509 """Switch modes for the exception handlers.
2510
2510
2511 Valid modes: Plain, Context and Verbose.
2511 Valid modes: Plain, Context and Verbose.
2512
2512
2513 If called without arguments, acts as a toggle."""
2513 If called without arguments, acts as a toggle."""
2514
2514
2515 def xmode_switch_err(name):
2515 def xmode_switch_err(name):
2516 warn('Error changing %s exception modes.\n%s' %
2516 warn('Error changing %s exception modes.\n%s' %
2517 (name,sys.exc_info()[1]))
2517 (name,sys.exc_info()[1]))
2518
2518
2519 shell = self.shell
2519 shell = self.shell
2520 new_mode = parameter_s.strip().capitalize()
2520 new_mode = parameter_s.strip().capitalize()
2521 try:
2521 try:
2522 shell.InteractiveTB.set_mode(mode=new_mode)
2522 shell.InteractiveTB.set_mode(mode=new_mode)
2523 print 'Exception reporting mode:',shell.InteractiveTB.mode
2523 print 'Exception reporting mode:',shell.InteractiveTB.mode
2524 except:
2524 except:
2525 xmode_switch_err('user')
2525 xmode_switch_err('user')
2526
2526
2527 # threaded shells use a special handler in sys.excepthook
2527 # threaded shells use a special handler in sys.excepthook
2528 if shell.isthreaded:
2528 if shell.isthreaded:
2529 try:
2529 try:
2530 shell.sys_excepthook.set_mode(mode=new_mode)
2530 shell.sys_excepthook.set_mode(mode=new_mode)
2531 except:
2531 except:
2532 xmode_switch_err('threaded')
2532 xmode_switch_err('threaded')
2533
2533
2534 def magic_colors(self,parameter_s = ''):
2534 def magic_colors(self,parameter_s = ''):
2535 """Switch color scheme for prompts, info system and exception handlers.
2535 """Switch color scheme for prompts, info system and exception handlers.
2536
2536
2537 Currently implemented schemes: NoColor, Linux, LightBG.
2537 Currently implemented schemes: NoColor, Linux, LightBG.
2538
2538
2539 Color scheme names are not case-sensitive."""
2539 Color scheme names are not case-sensitive."""
2540
2540
2541 def color_switch_err(name):
2541 def color_switch_err(name):
2542 warn('Error changing %s color schemes.\n%s' %
2542 warn('Error changing %s color schemes.\n%s' %
2543 (name,sys.exc_info()[1]))
2543 (name,sys.exc_info()[1]))
2544
2544
2545
2545
2546 new_scheme = parameter_s.strip()
2546 new_scheme = parameter_s.strip()
2547 if not new_scheme:
2547 if not new_scheme:
2548 raise UsageError(
2548 raise UsageError(
2549 "%colors: you must specify a color scheme. See '%colors?'")
2549 "%colors: you must specify a color scheme. See '%colors?'")
2550 return
2550 return
2551 # local shortcut
2551 # local shortcut
2552 shell = self.shell
2552 shell = self.shell
2553
2553
2554 import IPython.utils.rlineimpl as readline
2554 import IPython.utils.rlineimpl as readline
2555
2555
2556 if not readline.have_readline and sys.platform == "win32":
2556 if not readline.have_readline and sys.platform == "win32":
2557 msg = """\
2557 msg = """\
2558 Proper color support under MS Windows requires the pyreadline library.
2558 Proper color support under MS Windows requires the pyreadline library.
2559 You can find it at:
2559 You can find it at:
2560 http://ipython.scipy.org/moin/PyReadline/Intro
2560 http://ipython.scipy.org/moin/PyReadline/Intro
2561 Gary's readline needs the ctypes module, from:
2561 Gary's readline needs the ctypes module, from:
2562 http://starship.python.net/crew/theller/ctypes
2562 http://starship.python.net/crew/theller/ctypes
2563 (Note that ctypes is already part of Python versions 2.5 and newer).
2563 (Note that ctypes is already part of Python versions 2.5 and newer).
2564
2564
2565 Defaulting color scheme to 'NoColor'"""
2565 Defaulting color scheme to 'NoColor'"""
2566 new_scheme = 'NoColor'
2566 new_scheme = 'NoColor'
2567 warn(msg)
2567 warn(msg)
2568
2568
2569 # readline option is 0
2569 # readline option is 0
2570 if not shell.has_readline:
2570 if not shell.has_readline:
2571 new_scheme = 'NoColor'
2571 new_scheme = 'NoColor'
2572
2572
2573 # Set prompt colors
2573 # Set prompt colors
2574 try:
2574 try:
2575 shell.outputcache.set_colors(new_scheme)
2575 shell.outputcache.set_colors(new_scheme)
2576 except:
2576 except:
2577 color_switch_err('prompt')
2577 color_switch_err('prompt')
2578 else:
2578 else:
2579 shell.colors = \
2579 shell.colors = \
2580 shell.outputcache.color_table.active_scheme_name
2580 shell.outputcache.color_table.active_scheme_name
2581 # Set exception colors
2581 # Set exception colors
2582 try:
2582 try:
2583 shell.InteractiveTB.set_colors(scheme = new_scheme)
2583 shell.InteractiveTB.set_colors(scheme = new_scheme)
2584 shell.SyntaxTB.set_colors(scheme = new_scheme)
2584 shell.SyntaxTB.set_colors(scheme = new_scheme)
2585 except:
2585 except:
2586 color_switch_err('exception')
2586 color_switch_err('exception')
2587
2587
2588 # threaded shells use a verbose traceback in sys.excepthook
2588 # threaded shells use a verbose traceback in sys.excepthook
2589 if shell.isthreaded:
2589 if shell.isthreaded:
2590 try:
2590 try:
2591 shell.sys_excepthook.set_colors(scheme=new_scheme)
2591 shell.sys_excepthook.set_colors(scheme=new_scheme)
2592 except:
2592 except:
2593 color_switch_err('system exception handler')
2593 color_switch_err('system exception handler')
2594
2594
2595 # Set info (for 'object?') colors
2595 # Set info (for 'object?') colors
2596 if shell.color_info:
2596 if shell.color_info:
2597 try:
2597 try:
2598 shell.inspector.set_active_scheme(new_scheme)
2598 shell.inspector.set_active_scheme(new_scheme)
2599 except:
2599 except:
2600 color_switch_err('object inspector')
2600 color_switch_err('object inspector')
2601 else:
2601 else:
2602 shell.inspector.set_active_scheme('NoColor')
2602 shell.inspector.set_active_scheme('NoColor')
2603
2603
2604 def magic_color_info(self,parameter_s = ''):
2604 def magic_color_info(self,parameter_s = ''):
2605 """Toggle color_info.
2605 """Toggle color_info.
2606
2606
2607 The color_info configuration parameter controls whether colors are
2607 The color_info configuration parameter controls whether colors are
2608 used for displaying object details (by things like %psource, %pfile or
2608 used for displaying object details (by things like %psource, %pfile or
2609 the '?' system). This function toggles this value with each call.
2609 the '?' system). This function toggles this value with each call.
2610
2610
2611 Note that unless you have a fairly recent pager (less works better
2611 Note that unless you have a fairly recent pager (less works better
2612 than more) in your system, using colored object information displays
2612 than more) in your system, using colored object information displays
2613 will not work properly. Test it and see."""
2613 will not work properly. Test it and see."""
2614
2614
2615 self.shell.color_info = not self.shell.color_info
2615 self.shell.color_info = not self.shell.color_info
2616 self.magic_colors(self.shell.colors)
2616 self.magic_colors(self.shell.colors)
2617 print 'Object introspection functions have now coloring:',
2617 print 'Object introspection functions have now coloring:',
2618 print ['OFF','ON'][int(self.shell.color_info)]
2618 print ['OFF','ON'][int(self.shell.color_info)]
2619
2619
2620 def magic_Pprint(self, parameter_s=''):
2620 def magic_Pprint(self, parameter_s=''):
2621 """Toggle pretty printing on/off."""
2621 """Toggle pretty printing on/off."""
2622
2622
2623 self.shell.pprint = 1 - self.shell.pprint
2623 self.shell.pprint = 1 - self.shell.pprint
2624 print 'Pretty printing has been turned', \
2624 print 'Pretty printing has been turned', \
2625 ['OFF','ON'][self.shell.pprint]
2625 ['OFF','ON'][self.shell.pprint]
2626
2626
2627 def magic_Exit(self, parameter_s=''):
2627 def magic_Exit(self, parameter_s=''):
2628 """Exit IPython without confirmation."""
2628 """Exit IPython without confirmation."""
2629
2629
2630 self.shell.ask_exit()
2630 self.shell.ask_exit()
2631
2631
2632 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2632 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2633 magic_exit = magic_quit = magic_Quit = magic_Exit
2633 magic_exit = magic_quit = magic_Quit = magic_Exit
2634
2634
2635 #......................................................................
2635 #......................................................................
2636 # Functions to implement unix shell-type things
2636 # Functions to implement unix shell-type things
2637
2637
2638 @testdec.skip_doctest
2638 @testdec.skip_doctest
2639 def magic_alias(self, parameter_s = ''):
2639 def magic_alias(self, parameter_s = ''):
2640 """Define an alias for a system command.
2640 """Define an alias for a system command.
2641
2641
2642 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2642 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2643
2643
2644 Then, typing 'alias_name params' will execute the system command 'cmd
2644 Then, typing 'alias_name params' will execute the system command 'cmd
2645 params' (from your underlying operating system).
2645 params' (from your underlying operating system).
2646
2646
2647 Aliases have lower precedence than magic functions and Python normal
2647 Aliases have lower precedence than magic functions and Python normal
2648 variables, so if 'foo' is both a Python variable and an alias, the
2648 variables, so if 'foo' is both a Python variable and an alias, the
2649 alias can not be executed until 'del foo' removes the Python variable.
2649 alias can not be executed until 'del foo' removes the Python variable.
2650
2650
2651 You can use the %l specifier in an alias definition to represent the
2651 You can use the %l specifier in an alias definition to represent the
2652 whole line when the alias is called. For example:
2652 whole line when the alias is called. For example:
2653
2653
2654 In [2]: alias all echo "Input in brackets: <%l>"
2654 In [2]: alias all echo "Input in brackets: <%l>"
2655 In [3]: all hello world
2655 In [3]: all hello world
2656 Input in brackets: <hello world>
2656 Input in brackets: <hello world>
2657
2657
2658 You can also define aliases with parameters using %s specifiers (one
2658 You can also define aliases with parameters using %s specifiers (one
2659 per parameter):
2659 per parameter):
2660
2660
2661 In [1]: alias parts echo first %s second %s
2661 In [1]: alias parts echo first %s second %s
2662 In [2]: %parts A B
2662 In [2]: %parts A B
2663 first A second B
2663 first A second B
2664 In [3]: %parts A
2664 In [3]: %parts A
2665 Incorrect number of arguments: 2 expected.
2665 Incorrect number of arguments: 2 expected.
2666 parts is an alias to: 'echo first %s second %s'
2666 parts is an alias to: 'echo first %s second %s'
2667
2667
2668 Note that %l and %s are mutually exclusive. You can only use one or
2668 Note that %l and %s are mutually exclusive. You can only use one or
2669 the other in your aliases.
2669 the other in your aliases.
2670
2670
2671 Aliases expand Python variables just like system calls using ! or !!
2671 Aliases expand Python variables just like system calls using ! or !!
2672 do: all expressions prefixed with '$' get expanded. For details of
2672 do: all expressions prefixed with '$' get expanded. For details of
2673 the semantic rules, see PEP-215:
2673 the semantic rules, see PEP-215:
2674 http://www.python.org/peps/pep-0215.html. This is the library used by
2674 http://www.python.org/peps/pep-0215.html. This is the library used by
2675 IPython for variable expansion. If you want to access a true shell
2675 IPython for variable expansion. If you want to access a true shell
2676 variable, an extra $ is necessary to prevent its expansion by IPython:
2676 variable, an extra $ is necessary to prevent its expansion by IPython:
2677
2677
2678 In [6]: alias show echo
2678 In [6]: alias show echo
2679 In [7]: PATH='A Python string'
2679 In [7]: PATH='A Python string'
2680 In [8]: show $PATH
2680 In [8]: show $PATH
2681 A Python string
2681 A Python string
2682 In [9]: show $$PATH
2682 In [9]: show $$PATH
2683 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2683 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2684
2684
2685 You can use the alias facility to acess all of $PATH. See the %rehash
2685 You can use the alias facility to acess all of $PATH. See the %rehash
2686 and %rehashx functions, which automatically create aliases for the
2686 and %rehashx functions, which automatically create aliases for the
2687 contents of your $PATH.
2687 contents of your $PATH.
2688
2688
2689 If called with no parameters, %alias prints the current alias table."""
2689 If called with no parameters, %alias prints the current alias table."""
2690
2690
2691 par = parameter_s.strip()
2691 par = parameter_s.strip()
2692 if not par:
2692 if not par:
2693 stored = self.db.get('stored_aliases', {} )
2693 stored = self.db.get('stored_aliases', {} )
2694 aliases = sorted(self.shell.alias_manager.aliases)
2694 aliases = sorted(self.shell.alias_manager.aliases)
2695 # for k, v in stored:
2695 # for k, v in stored:
2696 # atab.append(k, v[0])
2696 # atab.append(k, v[0])
2697
2697
2698 print "Total number of aliases:", len(aliases)
2698 print "Total number of aliases:", len(aliases)
2699 return aliases
2699 return aliases
2700
2700
2701 # Now try to define a new one
2701 # Now try to define a new one
2702 try:
2702 try:
2703 alias,cmd = par.split(None, 1)
2703 alias,cmd = par.split(None, 1)
2704 except:
2704 except:
2705 print oinspect.getdoc(self.magic_alias)
2705 print oinspect.getdoc(self.magic_alias)
2706 else:
2706 else:
2707 self.shell.alias_manager.soft_define_alias(alias, cmd)
2707 self.shell.alias_manager.soft_define_alias(alias, cmd)
2708 # end magic_alias
2708 # end magic_alias
2709
2709
2710 def magic_unalias(self, parameter_s = ''):
2710 def magic_unalias(self, parameter_s = ''):
2711 """Remove an alias"""
2711 """Remove an alias"""
2712
2712
2713 aname = parameter_s.strip()
2713 aname = parameter_s.strip()
2714 self.shell.alias_manager.undefine_alias(aname)
2714 self.shell.alias_manager.undefine_alias(aname)
2715 stored = self.db.get('stored_aliases', {} )
2715 stored = self.db.get('stored_aliases', {} )
2716 if aname in stored:
2716 if aname in stored:
2717 print "Removing %stored alias",aname
2717 print "Removing %stored alias",aname
2718 del stored[aname]
2718 del stored[aname]
2719 self.db['stored_aliases'] = stored
2719 self.db['stored_aliases'] = stored
2720
2720
2721
2721
2722 def magic_rehashx(self, parameter_s = ''):
2722 def magic_rehashx(self, parameter_s = ''):
2723 """Update the alias table with all executable files in $PATH.
2723 """Update the alias table with all executable files in $PATH.
2724
2724
2725 This version explicitly checks that every entry in $PATH is a file
2725 This version explicitly checks that every entry in $PATH is a file
2726 with execute access (os.X_OK), so it is much slower than %rehash.
2726 with execute access (os.X_OK), so it is much slower than %rehash.
2727
2727
2728 Under Windows, it checks executability as a match agains a
2728 Under Windows, it checks executability as a match agains a
2729 '|'-separated string of extensions, stored in the IPython config
2729 '|'-separated string of extensions, stored in the IPython config
2730 variable win_exec_ext. This defaults to 'exe|com|bat'.
2730 variable win_exec_ext. This defaults to 'exe|com|bat'.
2731
2731
2732 This function also resets the root module cache of module completer,
2732 This function also resets the root module cache of module completer,
2733 used on slow filesystems.
2733 used on slow filesystems.
2734 """
2734 """
2735 from IPython.core.alias import InvalidAliasError
2735 from IPython.core.alias import InvalidAliasError
2736
2736
2737 # for the benefit of module completer in ipy_completers.py
2737 # for the benefit of module completer in ipy_completers.py
2738 del self.db['rootmodules']
2738 del self.db['rootmodules']
2739
2739
2740 path = [os.path.abspath(os.path.expanduser(p)) for p in
2740 path = [os.path.abspath(os.path.expanduser(p)) for p in
2741 os.environ.get('PATH','').split(os.pathsep)]
2741 os.environ.get('PATH','').split(os.pathsep)]
2742 path = filter(os.path.isdir,path)
2742 path = filter(os.path.isdir,path)
2743
2743
2744 syscmdlist = []
2744 syscmdlist = []
2745 # Now define isexec in a cross platform manner.
2745 # Now define isexec in a cross platform manner.
2746 if os.name == 'posix':
2746 if os.name == 'posix':
2747 isexec = lambda fname:os.path.isfile(fname) and \
2747 isexec = lambda fname:os.path.isfile(fname) and \
2748 os.access(fname,os.X_OK)
2748 os.access(fname,os.X_OK)
2749 else:
2749 else:
2750 try:
2750 try:
2751 winext = os.environ['pathext'].replace(';','|').replace('.','')
2751 winext = os.environ['pathext'].replace(';','|').replace('.','')
2752 except KeyError:
2752 except KeyError:
2753 winext = 'exe|com|bat|py'
2753 winext = 'exe|com|bat|py'
2754 if 'py' not in winext:
2754 if 'py' not in winext:
2755 winext += '|py'
2755 winext += '|py'
2756 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2756 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2757 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2757 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2758 savedir = os.getcwd()
2758 savedir = os.getcwd()
2759
2759
2760 # Now walk the paths looking for executables to alias.
2760 # Now walk the paths looking for executables to alias.
2761 try:
2761 try:
2762 # write the whole loop for posix/Windows so we don't have an if in
2762 # write the whole loop for posix/Windows so we don't have an if in
2763 # the innermost part
2763 # the innermost part
2764 if os.name == 'posix':
2764 if os.name == 'posix':
2765 for pdir in path:
2765 for pdir in path:
2766 os.chdir(pdir)
2766 os.chdir(pdir)
2767 for ff in os.listdir(pdir):
2767 for ff in os.listdir(pdir):
2768 if isexec(ff):
2768 if isexec(ff):
2769 try:
2769 try:
2770 # Removes dots from the name since ipython
2770 # Removes dots from the name since ipython
2771 # will assume names with dots to be python.
2771 # will assume names with dots to be python.
2772 self.shell.alias_manager.define_alias(
2772 self.shell.alias_manager.define_alias(
2773 ff.replace('.',''), ff)
2773 ff.replace('.',''), ff)
2774 except InvalidAliasError:
2774 except InvalidAliasError:
2775 pass
2775 pass
2776 else:
2776 else:
2777 syscmdlist.append(ff)
2777 syscmdlist.append(ff)
2778 else:
2778 else:
2779 no_alias = self.shell.alias_manager.no_alias
2779 no_alias = self.shell.alias_manager.no_alias
2780 for pdir in path:
2780 for pdir in path:
2781 os.chdir(pdir)
2781 os.chdir(pdir)
2782 for ff in os.listdir(pdir):
2782 for ff in os.listdir(pdir):
2783 base, ext = os.path.splitext(ff)
2783 base, ext = os.path.splitext(ff)
2784 if isexec(ff) and base.lower() not in no_alias:
2784 if isexec(ff) and base.lower() not in no_alias:
2785 if ext.lower() == '.exe':
2785 if ext.lower() == '.exe':
2786 ff = base
2786 ff = base
2787 try:
2787 try:
2788 # Removes dots from the name since ipython
2788 # Removes dots from the name since ipython
2789 # will assume names with dots to be python.
2789 # will assume names with dots to be python.
2790 self.shell.alias_manager.define_alias(
2790 self.shell.alias_manager.define_alias(
2791 base.lower().replace('.',''), ff)
2791 base.lower().replace('.',''), ff)
2792 except InvalidAliasError:
2792 except InvalidAliasError:
2793 pass
2793 pass
2794 syscmdlist.append(ff)
2794 syscmdlist.append(ff)
2795 db = self.db
2795 db = self.db
2796 db['syscmdlist'] = syscmdlist
2796 db['syscmdlist'] = syscmdlist
2797 finally:
2797 finally:
2798 os.chdir(savedir)
2798 os.chdir(savedir)
2799
2799
2800 def magic_pwd(self, parameter_s = ''):
2800 def magic_pwd(self, parameter_s = ''):
2801 """Return the current working directory path."""
2801 """Return the current working directory path."""
2802 return os.getcwd()
2802 return os.getcwd()
2803
2803
2804 def magic_cd(self, parameter_s=''):
2804 def magic_cd(self, parameter_s=''):
2805 """Change the current working directory.
2805 """Change the current working directory.
2806
2806
2807 This command automatically maintains an internal list of directories
2807 This command automatically maintains an internal list of directories
2808 you visit during your IPython session, in the variable _dh. The
2808 you visit during your IPython session, in the variable _dh. The
2809 command %dhist shows this history nicely formatted. You can also
2809 command %dhist shows this history nicely formatted. You can also
2810 do 'cd -<tab>' to see directory history conveniently.
2810 do 'cd -<tab>' to see directory history conveniently.
2811
2811
2812 Usage:
2812 Usage:
2813
2813
2814 cd 'dir': changes to directory 'dir'.
2814 cd 'dir': changes to directory 'dir'.
2815
2815
2816 cd -: changes to the last visited directory.
2816 cd -: changes to the last visited directory.
2817
2817
2818 cd -<n>: changes to the n-th directory in the directory history.
2818 cd -<n>: changes to the n-th directory in the directory history.
2819
2819
2820 cd --foo: change to directory that matches 'foo' in history
2820 cd --foo: change to directory that matches 'foo' in history
2821
2821
2822 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2822 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2823 (note: cd <bookmark_name> is enough if there is no
2823 (note: cd <bookmark_name> is enough if there is no
2824 directory <bookmark_name>, but a bookmark with the name exists.)
2824 directory <bookmark_name>, but a bookmark with the name exists.)
2825 'cd -b <tab>' allows you to tab-complete bookmark names.
2825 'cd -b <tab>' allows you to tab-complete bookmark names.
2826
2826
2827 Options:
2827 Options:
2828
2828
2829 -q: quiet. Do not print the working directory after the cd command is
2829 -q: quiet. Do not print the working directory after the cd command is
2830 executed. By default IPython's cd command does print this directory,
2830 executed. By default IPython's cd command does print this directory,
2831 since the default prompts do not display path information.
2831 since the default prompts do not display path information.
2832
2832
2833 Note that !cd doesn't work for this purpose because the shell where
2833 Note that !cd doesn't work for this purpose because the shell where
2834 !command runs is immediately discarded after executing 'command'."""
2834 !command runs is immediately discarded after executing 'command'."""
2835
2835
2836 parameter_s = parameter_s.strip()
2836 parameter_s = parameter_s.strip()
2837 #bkms = self.shell.persist.get("bookmarks",{})
2837 #bkms = self.shell.persist.get("bookmarks",{})
2838
2838
2839 oldcwd = os.getcwd()
2839 oldcwd = os.getcwd()
2840 numcd = re.match(r'(-)(\d+)$',parameter_s)
2840 numcd = re.match(r'(-)(\d+)$',parameter_s)
2841 # jump in directory history by number
2841 # jump in directory history by number
2842 if numcd:
2842 if numcd:
2843 nn = int(numcd.group(2))
2843 nn = int(numcd.group(2))
2844 try:
2844 try:
2845 ps = self.shell.user_ns['_dh'][nn]
2845 ps = self.shell.user_ns['_dh'][nn]
2846 except IndexError:
2846 except IndexError:
2847 print 'The requested directory does not exist in history.'
2847 print 'The requested directory does not exist in history.'
2848 return
2848 return
2849 else:
2849 else:
2850 opts = {}
2850 opts = {}
2851 elif parameter_s.startswith('--'):
2851 elif parameter_s.startswith('--'):
2852 ps = None
2852 ps = None
2853 fallback = None
2853 fallback = None
2854 pat = parameter_s[2:]
2854 pat = parameter_s[2:]
2855 dh = self.shell.user_ns['_dh']
2855 dh = self.shell.user_ns['_dh']
2856 # first search only by basename (last component)
2856 # first search only by basename (last component)
2857 for ent in reversed(dh):
2857 for ent in reversed(dh):
2858 if pat in os.path.basename(ent) and os.path.isdir(ent):
2858 if pat in os.path.basename(ent) and os.path.isdir(ent):
2859 ps = ent
2859 ps = ent
2860 break
2860 break
2861
2861
2862 if fallback is None and pat in ent and os.path.isdir(ent):
2862 if fallback is None and pat in ent and os.path.isdir(ent):
2863 fallback = ent
2863 fallback = ent
2864
2864
2865 # if we have no last part match, pick the first full path match
2865 # if we have no last part match, pick the first full path match
2866 if ps is None:
2866 if ps is None:
2867 ps = fallback
2867 ps = fallback
2868
2868
2869 if ps is None:
2869 if ps is None:
2870 print "No matching entry in directory history"
2870 print "No matching entry in directory history"
2871 return
2871 return
2872 else:
2872 else:
2873 opts = {}
2873 opts = {}
2874
2874
2875
2875
2876 else:
2876 else:
2877 #turn all non-space-escaping backslashes to slashes,
2877 #turn all non-space-escaping backslashes to slashes,
2878 # for c:\windows\directory\names\
2878 # for c:\windows\directory\names\
2879 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2879 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2880 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2880 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2881 # jump to previous
2881 # jump to previous
2882 if ps == '-':
2882 if ps == '-':
2883 try:
2883 try:
2884 ps = self.shell.user_ns['_dh'][-2]
2884 ps = self.shell.user_ns['_dh'][-2]
2885 except IndexError:
2885 except IndexError:
2886 raise UsageError('%cd -: No previous directory to change to.')
2886 raise UsageError('%cd -: No previous directory to change to.')
2887 # jump to bookmark if needed
2887 # jump to bookmark if needed
2888 else:
2888 else:
2889 if not os.path.isdir(ps) or opts.has_key('b'):
2889 if not os.path.isdir(ps) or opts.has_key('b'):
2890 bkms = self.db.get('bookmarks', {})
2890 bkms = self.db.get('bookmarks', {})
2891
2891
2892 if bkms.has_key(ps):
2892 if bkms.has_key(ps):
2893 target = bkms[ps]
2893 target = bkms[ps]
2894 print '(bookmark:%s) -> %s' % (ps,target)
2894 print '(bookmark:%s) -> %s' % (ps,target)
2895 ps = target
2895 ps = target
2896 else:
2896 else:
2897 if opts.has_key('b'):
2897 if opts.has_key('b'):
2898 raise UsageError("Bookmark '%s' not found. "
2898 raise UsageError("Bookmark '%s' not found. "
2899 "Use '%%bookmark -l' to see your bookmarks." % ps)
2899 "Use '%%bookmark -l' to see your bookmarks." % ps)
2900
2900
2901 # at this point ps should point to the target dir
2901 # at this point ps should point to the target dir
2902 if ps:
2902 if ps:
2903 try:
2903 try:
2904 os.chdir(os.path.expanduser(ps))
2904 os.chdir(os.path.expanduser(ps))
2905 if self.shell.term_title:
2905 if self.shell.term_title:
2906 set_term_title('IPython: ' + abbrev_cwd())
2906 set_term_title('IPython: ' + abbrev_cwd())
2907 except OSError:
2907 except OSError:
2908 print sys.exc_info()[1]
2908 print sys.exc_info()[1]
2909 else:
2909 else:
2910 cwd = os.getcwd()
2910 cwd = os.getcwd()
2911 dhist = self.shell.user_ns['_dh']
2911 dhist = self.shell.user_ns['_dh']
2912 if oldcwd != cwd:
2912 if oldcwd != cwd:
2913 dhist.append(cwd)
2913 dhist.append(cwd)
2914 self.db['dhist'] = compress_dhist(dhist)[-100:]
2914 self.db['dhist'] = compress_dhist(dhist)[-100:]
2915
2915
2916 else:
2916 else:
2917 os.chdir(self.shell.home_dir)
2917 os.chdir(self.shell.home_dir)
2918 if self.shell.term_title:
2918 if self.shell.term_title:
2919 set_term_title('IPython: ' + '~')
2919 set_term_title('IPython: ' + '~')
2920 cwd = os.getcwd()
2920 cwd = os.getcwd()
2921 dhist = self.shell.user_ns['_dh']
2921 dhist = self.shell.user_ns['_dh']
2922
2922
2923 if oldcwd != cwd:
2923 if oldcwd != cwd:
2924 dhist.append(cwd)
2924 dhist.append(cwd)
2925 self.db['dhist'] = compress_dhist(dhist)[-100:]
2925 self.db['dhist'] = compress_dhist(dhist)[-100:]
2926 if not 'q' in opts and self.shell.user_ns['_dh']:
2926 if not 'q' in opts and self.shell.user_ns['_dh']:
2927 print self.shell.user_ns['_dh'][-1]
2927 print self.shell.user_ns['_dh'][-1]
2928
2928
2929
2929
2930 def magic_env(self, parameter_s=''):
2930 def magic_env(self, parameter_s=''):
2931 """List environment variables."""
2931 """List environment variables."""
2932
2932
2933 return os.environ.data
2933 return os.environ.data
2934
2934
2935 def magic_pushd(self, parameter_s=''):
2935 def magic_pushd(self, parameter_s=''):
2936 """Place the current dir on stack and change directory.
2936 """Place the current dir on stack and change directory.
2937
2937
2938 Usage:\\
2938 Usage:\\
2939 %pushd ['dirname']
2939 %pushd ['dirname']
2940 """
2940 """
2941
2941
2942 dir_s = self.shell.dir_stack
2942 dir_s = self.shell.dir_stack
2943 tgt = os.path.expanduser(parameter_s)
2943 tgt = os.path.expanduser(parameter_s)
2944 cwd = os.getcwd().replace(self.home_dir,'~')
2944 cwd = os.getcwd().replace(self.home_dir,'~')
2945 if tgt:
2945 if tgt:
2946 self.magic_cd(parameter_s)
2946 self.magic_cd(parameter_s)
2947 dir_s.insert(0,cwd)
2947 dir_s.insert(0,cwd)
2948 return self.magic_dirs()
2948 return self.magic_dirs()
2949
2949
2950 def magic_popd(self, parameter_s=''):
2950 def magic_popd(self, parameter_s=''):
2951 """Change to directory popped off the top of the stack.
2951 """Change to directory popped off the top of the stack.
2952 """
2952 """
2953 if not self.shell.dir_stack:
2953 if not self.shell.dir_stack:
2954 raise UsageError("%popd on empty stack")
2954 raise UsageError("%popd on empty stack")
2955 top = self.shell.dir_stack.pop(0)
2955 top = self.shell.dir_stack.pop(0)
2956 self.magic_cd(top)
2956 self.magic_cd(top)
2957 print "popd ->",top
2957 print "popd ->",top
2958
2958
2959 def magic_dirs(self, parameter_s=''):
2959 def magic_dirs(self, parameter_s=''):
2960 """Return the current directory stack."""
2960 """Return the current directory stack."""
2961
2961
2962 return self.shell.dir_stack
2962 return self.shell.dir_stack
2963
2963
2964 def magic_dhist(self, parameter_s=''):
2964 def magic_dhist(self, parameter_s=''):
2965 """Print your history of visited directories.
2965 """Print your history of visited directories.
2966
2966
2967 %dhist -> print full history\\
2967 %dhist -> print full history\\
2968 %dhist n -> print last n entries only\\
2968 %dhist n -> print last n entries only\\
2969 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2969 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2970
2970
2971 This history is automatically maintained by the %cd command, and
2971 This history is automatically maintained by the %cd command, and
2972 always available as the global list variable _dh. You can use %cd -<n>
2972 always available as the global list variable _dh. You can use %cd -<n>
2973 to go to directory number <n>.
2973 to go to directory number <n>.
2974
2974
2975 Note that most of time, you should view directory history by entering
2975 Note that most of time, you should view directory history by entering
2976 cd -<TAB>.
2976 cd -<TAB>.
2977
2977
2978 """
2978 """
2979
2979
2980 dh = self.shell.user_ns['_dh']
2980 dh = self.shell.user_ns['_dh']
2981 if parameter_s:
2981 if parameter_s:
2982 try:
2982 try:
2983 args = map(int,parameter_s.split())
2983 args = map(int,parameter_s.split())
2984 except:
2984 except:
2985 self.arg_err(Magic.magic_dhist)
2985 self.arg_err(Magic.magic_dhist)
2986 return
2986 return
2987 if len(args) == 1:
2987 if len(args) == 1:
2988 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2988 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2989 elif len(args) == 2:
2989 elif len(args) == 2:
2990 ini,fin = args
2990 ini,fin = args
2991 else:
2991 else:
2992 self.arg_err(Magic.magic_dhist)
2992 self.arg_err(Magic.magic_dhist)
2993 return
2993 return
2994 else:
2994 else:
2995 ini,fin = 0,len(dh)
2995 ini,fin = 0,len(dh)
2996 nlprint(dh,
2996 nlprint(dh,
2997 header = 'Directory history (kept in _dh)',
2997 header = 'Directory history (kept in _dh)',
2998 start=ini,stop=fin)
2998 start=ini,stop=fin)
2999
2999
3000 @testdec.skip_doctest
3000 @testdec.skip_doctest
3001 def magic_sc(self, parameter_s=''):
3001 def magic_sc(self, parameter_s=''):
3002 """Shell capture - execute a shell command and capture its output.
3002 """Shell capture - execute a shell command and capture its output.
3003
3003
3004 DEPRECATED. Suboptimal, retained for backwards compatibility.
3004 DEPRECATED. Suboptimal, retained for backwards compatibility.
3005
3005
3006 You should use the form 'var = !command' instead. Example:
3006 You should use the form 'var = !command' instead. Example:
3007
3007
3008 "%sc -l myfiles = ls ~" should now be written as
3008 "%sc -l myfiles = ls ~" should now be written as
3009
3009
3010 "myfiles = !ls ~"
3010 "myfiles = !ls ~"
3011
3011
3012 myfiles.s, myfiles.l and myfiles.n still apply as documented
3012 myfiles.s, myfiles.l and myfiles.n still apply as documented
3013 below.
3013 below.
3014
3014
3015 --
3015 --
3016 %sc [options] varname=command
3016 %sc [options] varname=command
3017
3017
3018 IPython will run the given command using commands.getoutput(), and
3018 IPython will run the given command using commands.getoutput(), and
3019 will then update the user's interactive namespace with a variable
3019 will then update the user's interactive namespace with a variable
3020 called varname, containing the value of the call. Your command can
3020 called varname, containing the value of the call. Your command can
3021 contain shell wildcards, pipes, etc.
3021 contain shell wildcards, pipes, etc.
3022
3022
3023 The '=' sign in the syntax is mandatory, and the variable name you
3023 The '=' sign in the syntax is mandatory, and the variable name you
3024 supply must follow Python's standard conventions for valid names.
3024 supply must follow Python's standard conventions for valid names.
3025
3025
3026 (A special format without variable name exists for internal use)
3026 (A special format without variable name exists for internal use)
3027
3027
3028 Options:
3028 Options:
3029
3029
3030 -l: list output. Split the output on newlines into a list before
3030 -l: list output. Split the output on newlines into a list before
3031 assigning it to the given variable. By default the output is stored
3031 assigning it to the given variable. By default the output is stored
3032 as a single string.
3032 as a single string.
3033
3033
3034 -v: verbose. Print the contents of the variable.
3034 -v: verbose. Print the contents of the variable.
3035
3035
3036 In most cases you should not need to split as a list, because the
3036 In most cases you should not need to split as a list, because the
3037 returned value is a special type of string which can automatically
3037 returned value is a special type of string which can automatically
3038 provide its contents either as a list (split on newlines) or as a
3038 provide its contents either as a list (split on newlines) or as a
3039 space-separated string. These are convenient, respectively, either
3039 space-separated string. These are convenient, respectively, either
3040 for sequential processing or to be passed to a shell command.
3040 for sequential processing or to be passed to a shell command.
3041
3041
3042 For example:
3042 For example:
3043
3043
3044 # all-random
3044 # all-random
3045
3045
3046 # Capture into variable a
3046 # Capture into variable a
3047 In [1]: sc a=ls *py
3047 In [1]: sc a=ls *py
3048
3048
3049 # a is a string with embedded newlines
3049 # a is a string with embedded newlines
3050 In [2]: a
3050 In [2]: a
3051 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3051 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3052
3052
3053 # which can be seen as a list:
3053 # which can be seen as a list:
3054 In [3]: a.l
3054 In [3]: a.l
3055 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3055 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3056
3056
3057 # or as a whitespace-separated string:
3057 # or as a whitespace-separated string:
3058 In [4]: a.s
3058 In [4]: a.s
3059 Out[4]: 'setup.py win32_manual_post_install.py'
3059 Out[4]: 'setup.py win32_manual_post_install.py'
3060
3060
3061 # a.s is useful to pass as a single command line:
3061 # a.s is useful to pass as a single command line:
3062 In [5]: !wc -l $a.s
3062 In [5]: !wc -l $a.s
3063 146 setup.py
3063 146 setup.py
3064 130 win32_manual_post_install.py
3064 130 win32_manual_post_install.py
3065 276 total
3065 276 total
3066
3066
3067 # while the list form is useful to loop over:
3067 # while the list form is useful to loop over:
3068 In [6]: for f in a.l:
3068 In [6]: for f in a.l:
3069 ...: !wc -l $f
3069 ...: !wc -l $f
3070 ...:
3070 ...:
3071 146 setup.py
3071 146 setup.py
3072 130 win32_manual_post_install.py
3072 130 win32_manual_post_install.py
3073
3073
3074 Similiarly, the lists returned by the -l option are also special, in
3074 Similiarly, the lists returned by the -l option are also special, in
3075 the sense that you can equally invoke the .s attribute on them to
3075 the sense that you can equally invoke the .s attribute on them to
3076 automatically get a whitespace-separated string from their contents:
3076 automatically get a whitespace-separated string from their contents:
3077
3077
3078 In [7]: sc -l b=ls *py
3078 In [7]: sc -l b=ls *py
3079
3079
3080 In [8]: b
3080 In [8]: b
3081 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3081 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3082
3082
3083 In [9]: b.s
3083 In [9]: b.s
3084 Out[9]: 'setup.py win32_manual_post_install.py'
3084 Out[9]: 'setup.py win32_manual_post_install.py'
3085
3085
3086 In summary, both the lists and strings used for ouptut capture have
3086 In summary, both the lists and strings used for ouptut capture have
3087 the following special attributes:
3087 the following special attributes:
3088
3088
3089 .l (or .list) : value as list.
3089 .l (or .list) : value as list.
3090 .n (or .nlstr): value as newline-separated string.
3090 .n (or .nlstr): value as newline-separated string.
3091 .s (or .spstr): value as space-separated string.
3091 .s (or .spstr): value as space-separated string.
3092 """
3092 """
3093
3093
3094 opts,args = self.parse_options(parameter_s,'lv')
3094 opts,args = self.parse_options(parameter_s,'lv')
3095 # Try to get a variable name and command to run
3095 # Try to get a variable name and command to run
3096 try:
3096 try:
3097 # the variable name must be obtained from the parse_options
3097 # the variable name must be obtained from the parse_options
3098 # output, which uses shlex.split to strip options out.
3098 # output, which uses shlex.split to strip options out.
3099 var,_ = args.split('=',1)
3099 var,_ = args.split('=',1)
3100 var = var.strip()
3100 var = var.strip()
3101 # But the the command has to be extracted from the original input
3101 # But the the command has to be extracted from the original input
3102 # parameter_s, not on what parse_options returns, to avoid the
3102 # parameter_s, not on what parse_options returns, to avoid the
3103 # quote stripping which shlex.split performs on it.
3103 # quote stripping which shlex.split performs on it.
3104 _,cmd = parameter_s.split('=',1)
3104 _,cmd = parameter_s.split('=',1)
3105 except ValueError:
3105 except ValueError:
3106 var,cmd = '',''
3106 var,cmd = '',''
3107 # If all looks ok, proceed
3107 # If all looks ok, proceed
3108 out,err = self.shell.getoutputerror(cmd)
3108 out,err = self.shell.getoutputerror(cmd)
3109 if err:
3109 if err:
3110 print >> Term.cerr,err
3110 print >> Term.cerr,err
3111 if opts.has_key('l'):
3111 if opts.has_key('l'):
3112 out = SList(out.split('\n'))
3112 out = SList(out.split('\n'))
3113 else:
3113 else:
3114 out = LSString(out)
3114 out = LSString(out)
3115 if opts.has_key('v'):
3115 if opts.has_key('v'):
3116 print '%s ==\n%s' % (var,pformat(out))
3116 print '%s ==\n%s' % (var,pformat(out))
3117 if var:
3117 if var:
3118 self.shell.user_ns.update({var:out})
3118 self.shell.user_ns.update({var:out})
3119 else:
3119 else:
3120 return out
3120 return out
3121
3121
3122 def magic_sx(self, parameter_s=''):
3122 def magic_sx(self, parameter_s=''):
3123 """Shell execute - run a shell command and capture its output.
3123 """Shell execute - run a shell command and capture its output.
3124
3124
3125 %sx command
3125 %sx command
3126
3126
3127 IPython will run the given command using commands.getoutput(), and
3127 IPython will run the given command using commands.getoutput(), and
3128 return the result formatted as a list (split on '\\n'). Since the
3128 return the result formatted as a list (split on '\\n'). Since the
3129 output is _returned_, it will be stored in ipython's regular output
3129 output is _returned_, it will be stored in ipython's regular output
3130 cache Out[N] and in the '_N' automatic variables.
3130 cache Out[N] and in the '_N' automatic variables.
3131
3131
3132 Notes:
3132 Notes:
3133
3133
3134 1) If an input line begins with '!!', then %sx is automatically
3134 1) If an input line begins with '!!', then %sx is automatically
3135 invoked. That is, while:
3135 invoked. That is, while:
3136 !ls
3136 !ls
3137 causes ipython to simply issue system('ls'), typing
3137 causes ipython to simply issue system('ls'), typing
3138 !!ls
3138 !!ls
3139 is a shorthand equivalent to:
3139 is a shorthand equivalent to:
3140 %sx ls
3140 %sx ls
3141
3141
3142 2) %sx differs from %sc in that %sx automatically splits into a list,
3142 2) %sx differs from %sc in that %sx automatically splits into a list,
3143 like '%sc -l'. The reason for this is to make it as easy as possible
3143 like '%sc -l'. The reason for this is to make it as easy as possible
3144 to process line-oriented shell output via further python commands.
3144 to process line-oriented shell output via further python commands.
3145 %sc is meant to provide much finer control, but requires more
3145 %sc is meant to provide much finer control, but requires more
3146 typing.
3146 typing.
3147
3147
3148 3) Just like %sc -l, this is a list with special attributes:
3148 3) Just like %sc -l, this is a list with special attributes:
3149
3149
3150 .l (or .list) : value as list.
3150 .l (or .list) : value as list.
3151 .n (or .nlstr): value as newline-separated string.
3151 .n (or .nlstr): value as newline-separated string.
3152 .s (or .spstr): value as whitespace-separated string.
3152 .s (or .spstr): value as whitespace-separated string.
3153
3153
3154 This is very useful when trying to use such lists as arguments to
3154 This is very useful when trying to use such lists as arguments to
3155 system commands."""
3155 system commands."""
3156
3156
3157 if parameter_s:
3157 if parameter_s:
3158 out,err = self.shell.getoutputerror(parameter_s)
3158 out,err = self.shell.getoutputerror(parameter_s)
3159 if err:
3159 if err:
3160 print >> Term.cerr,err
3160 print >> Term.cerr,err
3161 return SList(out.split('\n'))
3161 return SList(out.split('\n'))
3162
3162
3163 def magic_bg(self, parameter_s=''):
3163 def magic_bg(self, parameter_s=''):
3164 """Run a job in the background, in a separate thread.
3164 """Run a job in the background, in a separate thread.
3165
3165
3166 For example,
3166 For example,
3167
3167
3168 %bg myfunc(x,y,z=1)
3168 %bg myfunc(x,y,z=1)
3169
3169
3170 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3170 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3171 execution starts, a message will be printed indicating the job
3171 execution starts, a message will be printed indicating the job
3172 number. If your job number is 5, you can use
3172 number. If your job number is 5, you can use
3173
3173
3174 myvar = jobs.result(5) or myvar = jobs[5].result
3174 myvar = jobs.result(5) or myvar = jobs[5].result
3175
3175
3176 to assign this result to variable 'myvar'.
3176 to assign this result to variable 'myvar'.
3177
3177
3178 IPython has a job manager, accessible via the 'jobs' object. You can
3178 IPython has a job manager, accessible via the 'jobs' object. You can
3179 type jobs? to get more information about it, and use jobs.<TAB> to see
3179 type jobs? to get more information about it, and use jobs.<TAB> to see
3180 its attributes. All attributes not starting with an underscore are
3180 its attributes. All attributes not starting with an underscore are
3181 meant for public use.
3181 meant for public use.
3182
3182
3183 In particular, look at the jobs.new() method, which is used to create
3183 In particular, look at the jobs.new() method, which is used to create
3184 new jobs. This magic %bg function is just a convenience wrapper
3184 new jobs. This magic %bg function is just a convenience wrapper
3185 around jobs.new(), for expression-based jobs. If you want to create a
3185 around jobs.new(), for expression-based jobs. If you want to create a
3186 new job with an explicit function object and arguments, you must call
3186 new job with an explicit function object and arguments, you must call
3187 jobs.new() directly.
3187 jobs.new() directly.
3188
3188
3189 The jobs.new docstring also describes in detail several important
3189 The jobs.new docstring also describes in detail several important
3190 caveats associated with a thread-based model for background job
3190 caveats associated with a thread-based model for background job
3191 execution. Type jobs.new? for details.
3191 execution. Type jobs.new? for details.
3192
3192
3193 You can check the status of all jobs with jobs.status().
3193 You can check the status of all jobs with jobs.status().
3194
3194
3195 The jobs variable is set by IPython into the Python builtin namespace.
3195 The jobs variable is set by IPython into the Python builtin namespace.
3196 If you ever declare a variable named 'jobs', you will shadow this
3196 If you ever declare a variable named 'jobs', you will shadow this
3197 name. You can either delete your global jobs variable to regain
3197 name. You can either delete your global jobs variable to regain
3198 access to the job manager, or make a new name and assign it manually
3198 access to the job manager, or make a new name and assign it manually
3199 to the manager (stored in IPython's namespace). For example, to
3199 to the manager (stored in IPython's namespace). For example, to
3200 assign the job manager to the Jobs name, use:
3200 assign the job manager to the Jobs name, use:
3201
3201
3202 Jobs = __builtins__.jobs"""
3202 Jobs = __builtins__.jobs"""
3203
3203
3204 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3204 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3205
3205
3206 def magic_r(self, parameter_s=''):
3206 def magic_r(self, parameter_s=''):
3207 """Repeat previous input.
3207 """Repeat previous input.
3208
3208
3209 Note: Consider using the more powerfull %rep instead!
3209 Note: Consider using the more powerfull %rep instead!
3210
3210
3211 If given an argument, repeats the previous command which starts with
3211 If given an argument, repeats the previous command which starts with
3212 the same string, otherwise it just repeats the previous input.
3212 the same string, otherwise it just repeats the previous input.
3213
3213
3214 Shell escaped commands (with ! as first character) are not recognized
3214 Shell escaped commands (with ! as first character) are not recognized
3215 by this system, only pure python code and magic commands.
3215 by this system, only pure python code and magic commands.
3216 """
3216 """
3217
3217
3218 start = parameter_s.strip()
3218 start = parameter_s.strip()
3219 esc_magic = ESC_MAGIC
3219 esc_magic = ESC_MAGIC
3220 # Identify magic commands even if automagic is on (which means
3220 # Identify magic commands even if automagic is on (which means
3221 # the in-memory version is different from that typed by the user).
3221 # the in-memory version is different from that typed by the user).
3222 if self.shell.automagic:
3222 if self.shell.automagic:
3223 start_magic = esc_magic+start
3223 start_magic = esc_magic+start
3224 else:
3224 else:
3225 start_magic = start
3225 start_magic = start
3226 # Look through the input history in reverse
3226 # Look through the input history in reverse
3227 for n in range(len(self.shell.input_hist)-2,0,-1):
3227 for n in range(len(self.shell.input_hist)-2,0,-1):
3228 input = self.shell.input_hist[n]
3228 input = self.shell.input_hist[n]
3229 # skip plain 'r' lines so we don't recurse to infinity
3229 # skip plain 'r' lines so we don't recurse to infinity
3230 if input != '_ip.magic("r")\n' and \
3230 if input != '_ip.magic("r")\n' and \
3231 (input.startswith(start) or input.startswith(start_magic)):
3231 (input.startswith(start) or input.startswith(start_magic)):
3232 #print 'match',`input` # dbg
3232 #print 'match',`input` # dbg
3233 print 'Executing:',input,
3233 print 'Executing:',input,
3234 self.shell.runlines(input)
3234 self.shell.runlines(input)
3235 return
3235 return
3236 print 'No previous input matching `%s` found.' % start
3236 print 'No previous input matching `%s` found.' % start
3237
3237
3238
3238
3239 def magic_bookmark(self, parameter_s=''):
3239 def magic_bookmark(self, parameter_s=''):
3240 """Manage IPython's bookmark system.
3240 """Manage IPython's bookmark system.
3241
3241
3242 %bookmark <name> - set bookmark to current dir
3242 %bookmark <name> - set bookmark to current dir
3243 %bookmark <name> <dir> - set bookmark to <dir>
3243 %bookmark <name> <dir> - set bookmark to <dir>
3244 %bookmark -l - list all bookmarks
3244 %bookmark -l - list all bookmarks
3245 %bookmark -d <name> - remove bookmark
3245 %bookmark -d <name> - remove bookmark
3246 %bookmark -r - remove all bookmarks
3246 %bookmark -r - remove all bookmarks
3247
3247
3248 You can later on access a bookmarked folder with:
3248 You can later on access a bookmarked folder with:
3249 %cd -b <name>
3249 %cd -b <name>
3250 or simply '%cd <name>' if there is no directory called <name> AND
3250 or simply '%cd <name>' if there is no directory called <name> AND
3251 there is such a bookmark defined.
3251 there is such a bookmark defined.
3252
3252
3253 Your bookmarks persist through IPython sessions, but they are
3253 Your bookmarks persist through IPython sessions, but they are
3254 associated with each profile."""
3254 associated with each profile."""
3255
3255
3256 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3256 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3257 if len(args) > 2:
3257 if len(args) > 2:
3258 raise UsageError("%bookmark: too many arguments")
3258 raise UsageError("%bookmark: too many arguments")
3259
3259
3260 bkms = self.db.get('bookmarks',{})
3260 bkms = self.db.get('bookmarks',{})
3261
3261
3262 if opts.has_key('d'):
3262 if opts.has_key('d'):
3263 try:
3263 try:
3264 todel = args[0]
3264 todel = args[0]
3265 except IndexError:
3265 except IndexError:
3266 raise UsageError(
3266 raise UsageError(
3267 "%bookmark -d: must provide a bookmark to delete")
3267 "%bookmark -d: must provide a bookmark to delete")
3268 else:
3268 else:
3269 try:
3269 try:
3270 del bkms[todel]
3270 del bkms[todel]
3271 except KeyError:
3271 except KeyError:
3272 raise UsageError(
3272 raise UsageError(
3273 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3273 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3274
3274
3275 elif opts.has_key('r'):
3275 elif opts.has_key('r'):
3276 bkms = {}
3276 bkms = {}
3277 elif opts.has_key('l'):
3277 elif opts.has_key('l'):
3278 bks = bkms.keys()
3278 bks = bkms.keys()
3279 bks.sort()
3279 bks.sort()
3280 if bks:
3280 if bks:
3281 size = max(map(len,bks))
3281 size = max(map(len,bks))
3282 else:
3282 else:
3283 size = 0
3283 size = 0
3284 fmt = '%-'+str(size)+'s -> %s'
3284 fmt = '%-'+str(size)+'s -> %s'
3285 print 'Current bookmarks:'
3285 print 'Current bookmarks:'
3286 for bk in bks:
3286 for bk in bks:
3287 print fmt % (bk,bkms[bk])
3287 print fmt % (bk,bkms[bk])
3288 else:
3288 else:
3289 if not args:
3289 if not args:
3290 raise UsageError("%bookmark: You must specify the bookmark name")
3290 raise UsageError("%bookmark: You must specify the bookmark name")
3291 elif len(args)==1:
3291 elif len(args)==1:
3292 bkms[args[0]] = os.getcwd()
3292 bkms[args[0]] = os.getcwd()
3293 elif len(args)==2:
3293 elif len(args)==2:
3294 bkms[args[0]] = args[1]
3294 bkms[args[0]] = args[1]
3295 self.db['bookmarks'] = bkms
3295 self.db['bookmarks'] = bkms
3296
3296
3297 def magic_pycat(self, parameter_s=''):
3297 def magic_pycat(self, parameter_s=''):
3298 """Show a syntax-highlighted file through a pager.
3298 """Show a syntax-highlighted file through a pager.
3299
3299
3300 This magic is similar to the cat utility, but it will assume the file
3300 This magic is similar to the cat utility, but it will assume the file
3301 to be Python source and will show it with syntax highlighting. """
3301 to be Python source and will show it with syntax highlighting. """
3302
3302
3303 try:
3303 try:
3304 filename = get_py_filename(parameter_s)
3304 filename = get_py_filename(parameter_s)
3305 cont = file_read(filename)
3305 cont = file_read(filename)
3306 except IOError:
3306 except IOError:
3307 try:
3307 try:
3308 cont = eval(parameter_s,self.user_ns)
3308 cont = eval(parameter_s,self.user_ns)
3309 except NameError:
3309 except NameError:
3310 cont = None
3310 cont = None
3311 if cont is None:
3311 if cont is None:
3312 print "Error: no such file or variable"
3312 print "Error: no such file or variable"
3313 return
3313 return
3314
3314
3315 page(self.shell.pycolorize(cont),
3315 page(self.shell.pycolorize(cont),
3316 screen_lines=self.shell.usable_screen_length)
3316 screen_lines=self.shell.usable_screen_length)
3317
3317
3318 def _rerun_pasted(self):
3318 def _rerun_pasted(self):
3319 """ Rerun a previously pasted command.
3319 """ Rerun a previously pasted command.
3320 """
3320 """
3321 b = self.user_ns.get('pasted_block', None)
3321 b = self.user_ns.get('pasted_block', None)
3322 if b is None:
3322 if b is None:
3323 raise UsageError('No previous pasted block available')
3323 raise UsageError('No previous pasted block available')
3324 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3324 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3325 exec b in self.user_ns
3325 exec b in self.user_ns
3326
3326
3327 def _get_pasted_lines(self, sentinel):
3327 def _get_pasted_lines(self, sentinel):
3328 """ Yield pasted lines until the user enters the given sentinel value.
3328 """ Yield pasted lines until the user enters the given sentinel value.
3329 """
3329 """
3330 from IPython.core import iplib
3330 from IPython.core import iplib
3331 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3331 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3332 while True:
3332 while True:
3333 l = iplib.raw_input_original(':')
3333 l = iplib.raw_input_original(':')
3334 if l == sentinel:
3334 if l == sentinel:
3335 return
3335 return
3336 else:
3336 else:
3337 yield l
3337 yield l
3338
3338
3339 def _strip_pasted_lines_for_code(self, raw_lines):
3339 def _strip_pasted_lines_for_code(self, raw_lines):
3340 """ Strip non-code parts of a sequence of lines to return a block of
3340 """ Strip non-code parts of a sequence of lines to return a block of
3341 code.
3341 code.
3342 """
3342 """
3343 # Regular expressions that declare text we strip from the input:
3343 # Regular expressions that declare text we strip from the input:
3344 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3344 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3345 r'^\s*(\s?>)+', # Python input prompt
3345 r'^\s*(\s?>)+', # Python input prompt
3346 r'^\s*\.{3,}', # Continuation prompts
3346 r'^\s*\.{3,}', # Continuation prompts
3347 r'^\++',
3347 r'^\++',
3348 ]
3348 ]
3349
3349
3350 strip_from_start = map(re.compile,strip_re)
3350 strip_from_start = map(re.compile,strip_re)
3351
3351
3352 lines = []
3352 lines = []
3353 for l in raw_lines:
3353 for l in raw_lines:
3354 for pat in strip_from_start:
3354 for pat in strip_from_start:
3355 l = pat.sub('',l)
3355 l = pat.sub('',l)
3356 lines.append(l)
3356 lines.append(l)
3357
3357
3358 block = "\n".join(lines) + '\n'
3358 block = "\n".join(lines) + '\n'
3359 #print "block:\n",block
3359 #print "block:\n",block
3360 return block
3360 return block
3361
3361
3362 def _execute_block(self, block, par):
3362 def _execute_block(self, block, par):
3363 """ Execute a block, or store it in a variable, per the user's request.
3363 """ Execute a block, or store it in a variable, per the user's request.
3364 """
3364 """
3365 if not par:
3365 if not par:
3366 b = textwrap.dedent(block)
3366 b = textwrap.dedent(block)
3367 self.user_ns['pasted_block'] = b
3367 self.user_ns['pasted_block'] = b
3368 exec b in self.user_ns
3368 exec b in self.user_ns
3369 else:
3369 else:
3370 self.user_ns[par] = SList(block.splitlines())
3370 self.user_ns[par] = SList(block.splitlines())
3371 print "Block assigned to '%s'" % par
3371 print "Block assigned to '%s'" % par
3372
3372
3373 def magic_cpaste(self, parameter_s=''):
3373 def magic_cpaste(self, parameter_s=''):
3374 """Allows you to paste & execute a pre-formatted code block from clipboard.
3374 """Allows you to paste & execute a pre-formatted code block from clipboard.
3375
3375
3376 You must terminate the block with '--' (two minus-signs) alone on the
3376 You must terminate the block with '--' (two minus-signs) alone on the
3377 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3377 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3378 is the new sentinel for this operation)
3378 is the new sentinel for this operation)
3379
3379
3380 The block is dedented prior to execution to enable execution of method
3380 The block is dedented prior to execution to enable execution of method
3381 definitions. '>' and '+' characters at the beginning of a line are
3381 definitions. '>' and '+' characters at the beginning of a line are
3382 ignored, to allow pasting directly from e-mails, diff files and
3382 ignored, to allow pasting directly from e-mails, diff files and
3383 doctests (the '...' continuation prompt is also stripped). The
3383 doctests (the '...' continuation prompt is also stripped). The
3384 executed block is also assigned to variable named 'pasted_block' for
3384 executed block is also assigned to variable named 'pasted_block' for
3385 later editing with '%edit pasted_block'.
3385 later editing with '%edit pasted_block'.
3386
3386
3387 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3387 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3388 This assigns the pasted block to variable 'foo' as string, without
3388 This assigns the pasted block to variable 'foo' as string, without
3389 dedenting or executing it (preceding >>> and + is still stripped)
3389 dedenting or executing it (preceding >>> and + is still stripped)
3390
3390
3391 '%cpaste -r' re-executes the block previously entered by cpaste.
3391 '%cpaste -r' re-executes the block previously entered by cpaste.
3392
3392
3393 Do not be alarmed by garbled output on Windows (it's a readline bug).
3393 Do not be alarmed by garbled output on Windows (it's a readline bug).
3394 Just press enter and type -- (and press enter again) and the block
3394 Just press enter and type -- (and press enter again) and the block
3395 will be what was just pasted.
3395 will be what was just pasted.
3396
3396
3397 IPython statements (magics, shell escapes) are not supported (yet).
3397 IPython statements (magics, shell escapes) are not supported (yet).
3398
3398
3399 See also
3399 See also
3400 --------
3400 --------
3401 paste: automatically pull code from clipboard.
3401 paste: automatically pull code from clipboard.
3402 """
3402 """
3403
3403
3404 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3404 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3405 par = args.strip()
3405 par = args.strip()
3406 if opts.has_key('r'):
3406 if opts.has_key('r'):
3407 self._rerun_pasted()
3407 self._rerun_pasted()
3408 return
3408 return
3409
3409
3410 sentinel = opts.get('s','--')
3410 sentinel = opts.get('s','--')
3411
3411
3412 block = self._strip_pasted_lines_for_code(
3412 block = self._strip_pasted_lines_for_code(
3413 self._get_pasted_lines(sentinel))
3413 self._get_pasted_lines(sentinel))
3414
3414
3415 self._execute_block(block, par)
3415 self._execute_block(block, par)
3416
3416
3417 def magic_paste(self, parameter_s=''):
3417 def magic_paste(self, parameter_s=''):
3418 """Allows you to paste & execute a pre-formatted code block from clipboard.
3418 """Allows you to paste & execute a pre-formatted code block from clipboard.
3419
3419
3420 The text is pulled directly from the clipboard without user
3420 The text is pulled directly from the clipboard without user
3421 intervention and printed back on the screen before execution (unless
3421 intervention and printed back on the screen before execution (unless
3422 the -q flag is given to force quiet mode).
3422 the -q flag is given to force quiet mode).
3423
3423
3424 The block is dedented prior to execution to enable execution of method
3424 The block is dedented prior to execution to enable execution of method
3425 definitions. '>' and '+' characters at the beginning of a line are
3425 definitions. '>' and '+' characters at the beginning of a line are
3426 ignored, to allow pasting directly from e-mails, diff files and
3426 ignored, to allow pasting directly from e-mails, diff files and
3427 doctests (the '...' continuation prompt is also stripped). The
3427 doctests (the '...' continuation prompt is also stripped). The
3428 executed block is also assigned to variable named 'pasted_block' for
3428 executed block is also assigned to variable named 'pasted_block' for
3429 later editing with '%edit pasted_block'.
3429 later editing with '%edit pasted_block'.
3430
3430
3431 You can also pass a variable name as an argument, e.g. '%paste foo'.
3431 You can also pass a variable name as an argument, e.g. '%paste foo'.
3432 This assigns the pasted block to variable 'foo' as string, without
3432 This assigns the pasted block to variable 'foo' as string, without
3433 dedenting or executing it (preceding >>> and + is still stripped)
3433 dedenting or executing it (preceding >>> and + is still stripped)
3434
3434
3435 Options
3435 Options
3436 -------
3436 -------
3437
3437
3438 -r: re-executes the block previously entered by cpaste.
3438 -r: re-executes the block previously entered by cpaste.
3439
3439
3440 -q: quiet mode: do not echo the pasted text back to the terminal.
3440 -q: quiet mode: do not echo the pasted text back to the terminal.
3441
3441
3442 IPython statements (magics, shell escapes) are not supported (yet).
3442 IPython statements (magics, shell escapes) are not supported (yet).
3443
3443
3444 See also
3444 See also
3445 --------
3445 --------
3446 cpaste: manually paste code into terminal until you mark its end.
3446 cpaste: manually paste code into terminal until you mark its end.
3447 """
3447 """
3448 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3448 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3449 par = args.strip()
3449 par = args.strip()
3450 if opts.has_key('r'):
3450 if opts.has_key('r'):
3451 self._rerun_pasted()
3451 self._rerun_pasted()
3452 return
3452 return
3453
3453
3454 text = self.shell.hooks.clipboard_get()
3454 text = self.shell.hooks.clipboard_get()
3455 block = self._strip_pasted_lines_for_code(text.splitlines())
3455 block = self._strip_pasted_lines_for_code(text.splitlines())
3456
3456
3457 # By default, echo back to terminal unless quiet mode is requested
3457 # By default, echo back to terminal unless quiet mode is requested
3458 if not opts.has_key('q'):
3458 if not opts.has_key('q'):
3459 write = self.shell.write
3459 write = self.shell.write
3460 write(self.shell.pycolorize(block))
3460 write(self.shell.pycolorize(block))
3461 if not block.endswith('\n'):
3461 if not block.endswith('\n'):
3462 write('\n')
3462 write('\n')
3463 write("## -- End pasted text --\n")
3463 write("## -- End pasted text --\n")
3464
3464
3465 self._execute_block(block, par)
3465 self._execute_block(block, par)
3466
3466
3467 def magic_quickref(self,arg):
3467 def magic_quickref(self,arg):
3468 """ Show a quick reference sheet """
3468 """ Show a quick reference sheet """
3469 import IPython.core.usage
3469 import IPython.core.usage
3470 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3470 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3471
3471
3472 page(qr)
3472 page(qr)
3473
3473
3474 def magic_doctest_mode(self,parameter_s=''):
3474 def magic_doctest_mode(self,parameter_s=''):
3475 """Toggle doctest mode on and off.
3475 """Toggle doctest mode on and off.
3476
3476
3477 This mode allows you to toggle the prompt behavior between normal
3477 This mode allows you to toggle the prompt behavior between normal
3478 IPython prompts and ones that are as similar to the default IPython
3478 IPython prompts and ones that are as similar to the default IPython
3479 interpreter as possible.
3479 interpreter as possible.
3480
3480
3481 It also supports the pasting of code snippets that have leading '>>>'
3481 It also supports the pasting of code snippets that have leading '>>>'
3482 and '...' prompts in them. This means that you can paste doctests from
3482 and '...' prompts in them. This means that you can paste doctests from
3483 files or docstrings (even if they have leading whitespace), and the
3483 files or docstrings (even if they have leading whitespace), and the
3484 code will execute correctly. You can then use '%history -tn' to see
3484 code will execute correctly. You can then use '%history -tn' to see
3485 the translated history without line numbers; this will give you the
3485 the translated history without line numbers; this will give you the
3486 input after removal of all the leading prompts and whitespace, which
3486 input after removal of all the leading prompts and whitespace, which
3487 can be pasted back into an editor.
3487 can be pasted back into an editor.
3488
3488
3489 With these features, you can switch into this mode easily whenever you
3489 With these features, you can switch into this mode easily whenever you
3490 need to do testing and changes to doctests, without having to leave
3490 need to do testing and changes to doctests, without having to leave
3491 your existing IPython session.
3491 your existing IPython session.
3492 """
3492 """
3493
3493
3494 from IPython.utils.ipstruct import Struct
3494 from IPython.utils.ipstruct import Struct
3495
3495
3496 # Shorthands
3496 # Shorthands
3497 shell = self.shell
3497 shell = self.shell
3498 oc = shell.outputcache
3498 oc = shell.outputcache
3499 meta = shell.meta
3499 meta = shell.meta
3500 # dstore is a data store kept in the instance metadata bag to track any
3500 # dstore is a data store kept in the instance metadata bag to track any
3501 # changes we make, so we can undo them later.
3501 # changes we make, so we can undo them later.
3502 dstore = meta.setdefault('doctest_mode',Struct())
3502 dstore = meta.setdefault('doctest_mode',Struct())
3503 save_dstore = dstore.setdefault
3503 save_dstore = dstore.setdefault
3504
3504
3505 # save a few values we'll need to recover later
3505 # save a few values we'll need to recover later
3506 mode = save_dstore('mode',False)
3506 mode = save_dstore('mode',False)
3507 save_dstore('rc_pprint',shell.pprint)
3507 save_dstore('rc_pprint',shell.pprint)
3508 save_dstore('xmode',shell.InteractiveTB.mode)
3508 save_dstore('xmode',shell.InteractiveTB.mode)
3509 save_dstore('rc_separate_out',shell.separate_out)
3509 save_dstore('rc_separate_out',shell.separate_out)
3510 save_dstore('rc_separate_out2',shell.separate_out2)
3510 save_dstore('rc_separate_out2',shell.separate_out2)
3511 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3511 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3512 save_dstore('rc_separate_in',shell.separate_in)
3512 save_dstore('rc_separate_in',shell.separate_in)
3513
3513
3514 if mode == False:
3514 if mode == False:
3515 # turn on
3515 # turn on
3516 oc.prompt1.p_template = '>>> '
3516 oc.prompt1.p_template = '>>> '
3517 oc.prompt2.p_template = '... '
3517 oc.prompt2.p_template = '... '
3518 oc.prompt_out.p_template = ''
3518 oc.prompt_out.p_template = ''
3519
3519
3520 # Prompt separators like plain python
3520 # Prompt separators like plain python
3521 oc.input_sep = oc.prompt1.sep = ''
3521 oc.input_sep = oc.prompt1.sep = ''
3522 oc.output_sep = ''
3522 oc.output_sep = ''
3523 oc.output_sep2 = ''
3523 oc.output_sep2 = ''
3524
3524
3525 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3525 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3526 oc.prompt_out.pad_left = False
3526 oc.prompt_out.pad_left = False
3527
3527
3528 shell.pprint = False
3528 shell.pprint = False
3529
3529
3530 shell.magic_xmode('Plain')
3530 shell.magic_xmode('Plain')
3531
3531
3532 else:
3532 else:
3533 # turn off
3533 # turn off
3534 oc.prompt1.p_template = shell.prompt_in1
3534 oc.prompt1.p_template = shell.prompt_in1
3535 oc.prompt2.p_template = shell.prompt_in2
3535 oc.prompt2.p_template = shell.prompt_in2
3536 oc.prompt_out.p_template = shell.prompt_out
3536 oc.prompt_out.p_template = shell.prompt_out
3537
3537
3538 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3538 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3539
3539
3540 oc.output_sep = dstore.rc_separate_out
3540 oc.output_sep = dstore.rc_separate_out
3541 oc.output_sep2 = dstore.rc_separate_out2
3541 oc.output_sep2 = dstore.rc_separate_out2
3542
3542
3543 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3543 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3544 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3544 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3545
3545
3546 shell.pprint = dstore.rc_pprint
3546 shell.pprint = dstore.rc_pprint
3547
3547
3548 shell.magic_xmode(dstore.xmode)
3548 shell.magic_xmode(dstore.xmode)
3549
3549
3550 # Store new mode and inform
3550 # Store new mode and inform
3551 dstore.mode = bool(1-int(mode))
3551 dstore.mode = bool(1-int(mode))
3552 print 'Doctest mode is:',
3552 print 'Doctest mode is:',
3553 print ['OFF','ON'][dstore.mode]
3553 print ['OFF','ON'][dstore.mode]
3554
3554
3555 def magic_gui(self, parameter_s=''):
3555 def magic_gui(self, parameter_s=''):
3556 """Enable or disable IPython GUI event loop integration.
3556 """Enable or disable IPython GUI event loop integration.
3557
3557
3558 %gui [-a] [GUINAME]
3558 %gui [-a] [GUINAME]
3559
3559
3560 This magic replaces IPython's threaded shells that were activated
3560 This magic replaces IPython's threaded shells that were activated
3561 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3561 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3562 can now be enabled, disabled and swtiched at runtime and keyboard
3562 can now be enabled, disabled and swtiched at runtime and keyboard
3563 interrupts should work without any problems. The following toolkits
3563 interrupts should work without any problems. The following toolkits
3564 are supported: wxPython, PyQt4, PyGTK, and Tk::
3564 are supported: wxPython, PyQt4, PyGTK, and Tk::
3565
3565
3566 %gui wx # enable wxPython event loop integration
3566 %gui wx # enable wxPython event loop integration
3567 %gui qt4|qt # enable PyQt4 event loop integration
3567 %gui qt4|qt # enable PyQt4 event loop integration
3568 %gui gtk # enable PyGTK event loop integration
3568 %gui gtk # enable PyGTK event loop integration
3569 %gui tk # enable Tk event loop integration
3569 %gui tk # enable Tk event loop integration
3570 %gui # disable all event loop integration
3570 %gui # disable all event loop integration
3571
3571
3572 WARNING: after any of these has been called you can simply create
3572 WARNING: after any of these has been called you can simply create
3573 an application object, but DO NOT start the event loop yourself, as
3573 an application object, but DO NOT start the event loop yourself, as
3574 we have already handled that.
3574 we have already handled that.
3575
3575
3576 If you want us to create an appropriate application object add the
3576 If you want us to create an appropriate application object add the
3577 "-a" flag to your command::
3577 "-a" flag to your command::
3578
3578
3579 %gui -a wx
3579 %gui -a wx
3580
3580
3581 This is highly recommended for most users.
3581 This is highly recommended for most users.
3582 """
3582 """
3583 opts, arg = self.parse_options(parameter_s,'a')
3583 opts, arg = self.parse_options(parameter_s,'a')
3584 if arg=='': arg = None
3584 if arg=='': arg = None
3585 return enable_gui(arg, 'a' in opts)
3585 return enable_gui(arg, 'a' in opts)
3586
3586
3587 def magic_load_ext(self, module_str):
3587 def magic_load_ext(self, module_str):
3588 """Load an IPython extension by its module name."""
3588 """Load an IPython extension by its module name."""
3589 return self.load_extension(module_str)
3589 return self.extension_manager.load_extension(module_str)
3590
3590
3591 def magic_unload_ext(self, module_str):
3591 def magic_unload_ext(self, module_str):
3592 """Unload an IPython extension by its module name."""
3592 """Unload an IPython extension by its module name."""
3593 self.unload_extension(module_str)
3593 self.extension_manager.unload_extension(module_str)
3594
3594
3595 def magic_reload_ext(self, module_str):
3595 def magic_reload_ext(self, module_str):
3596 """Reload an IPython extension by its module name."""
3596 """Reload an IPython extension by its module name."""
3597 self.reload_extension(module_str)
3597 self.extension_manager.reload_extension(module_str)
3598
3598
3599 @testdec.skip_doctest
3599 @testdec.skip_doctest
3600 def magic_install_profiles(self, s):
3600 def magic_install_profiles(self, s):
3601 """Install the default IPython profiles into the .ipython dir.
3601 """Install the default IPython profiles into the .ipython dir.
3602
3602
3603 If the default profiles have already been installed, they will not
3603 If the default profiles have already been installed, they will not
3604 be overwritten. You can force overwriting them by using the ``-o``
3604 be overwritten. You can force overwriting them by using the ``-o``
3605 option::
3605 option::
3606
3606
3607 In [1]: %install_profiles -o
3607 In [1]: %install_profiles -o
3608 """
3608 """
3609 if '-o' in s:
3609 if '-o' in s:
3610 overwrite = True
3610 overwrite = True
3611 else:
3611 else:
3612 overwrite = False
3612 overwrite = False
3613 from IPython.config import profile
3613 from IPython.config import profile
3614 profile_dir = os.path.split(profile.__file__)[0]
3614 profile_dir = os.path.split(profile.__file__)[0]
3615 ipython_dir = self.ipython_dir
3615 ipython_dir = self.ipython_dir
3616 files = os.listdir(profile_dir)
3616 files = os.listdir(profile_dir)
3617
3617
3618 to_install = []
3618 to_install = []
3619 for f in files:
3619 for f in files:
3620 if f.startswith('ipython_config'):
3620 if f.startswith('ipython_config'):
3621 src = os.path.join(profile_dir, f)
3621 src = os.path.join(profile_dir, f)
3622 dst = os.path.join(ipython_dir, f)
3622 dst = os.path.join(ipython_dir, f)
3623 if (not os.path.isfile(dst)) or overwrite:
3623 if (not os.path.isfile(dst)) or overwrite:
3624 to_install.append((f, src, dst))
3624 to_install.append((f, src, dst))
3625 if len(to_install)>0:
3625 if len(to_install)>0:
3626 print "Installing profiles to: ", ipython_dir
3626 print "Installing profiles to: ", ipython_dir
3627 for (f, src, dst) in to_install:
3627 for (f, src, dst) in to_install:
3628 shutil.copy(src, dst)
3628 shutil.copy(src, dst)
3629 print " %s" % f
3629 print " %s" % f
3630
3630
3631 def magic_install_default_config(self, s):
3631 def magic_install_default_config(self, s):
3632 """Install IPython's default config file into the .ipython dir.
3632 """Install IPython's default config file into the .ipython dir.
3633
3633
3634 If the default config file (:file:`ipython_config.py`) is already
3634 If the default config file (:file:`ipython_config.py`) is already
3635 installed, it will not be overwritten. You can force overwriting
3635 installed, it will not be overwritten. You can force overwriting
3636 by using the ``-o`` option::
3636 by using the ``-o`` option::
3637
3637
3638 In [1]: %install_default_config
3638 In [1]: %install_default_config
3639 """
3639 """
3640 if '-o' in s:
3640 if '-o' in s:
3641 overwrite = True
3641 overwrite = True
3642 else:
3642 else:
3643 overwrite = False
3643 overwrite = False
3644 from IPython.config import default
3644 from IPython.config import default
3645 config_dir = os.path.split(default.__file__)[0]
3645 config_dir = os.path.split(default.__file__)[0]
3646 ipython_dir = self.ipython_dir
3646 ipython_dir = self.ipython_dir
3647 default_config_file_name = 'ipython_config.py'
3647 default_config_file_name = 'ipython_config.py'
3648 src = os.path.join(config_dir, default_config_file_name)
3648 src = os.path.join(config_dir, default_config_file_name)
3649 dst = os.path.join(ipython_dir, default_config_file_name)
3649 dst = os.path.join(ipython_dir, default_config_file_name)
3650 if (not os.path.isfile(dst)) or overwrite:
3650 if (not os.path.isfile(dst)) or overwrite:
3651 shutil.copy(src, dst)
3651 shutil.copy(src, dst)
3652 print "Installing default config file: %s" % dst
3652 print "Installing default config file: %s" % dst
3653
3653
3654 # Pylab support: simple wrappers that activate pylab, load gui input
3654 # Pylab support: simple wrappers that activate pylab, load gui input
3655 # handling and modify slightly %run
3655 # handling and modify slightly %run
3656
3656
3657 @testdec.skip_doctest
3657 @testdec.skip_doctest
3658 def _pylab_magic_run(self, parameter_s=''):
3658 def _pylab_magic_run(self, parameter_s=''):
3659 Magic.magic_run(self, parameter_s,
3659 Magic.magic_run(self, parameter_s,
3660 runner=mpl_runner(self.shell.safe_execfile))
3660 runner=mpl_runner(self.shell.safe_execfile))
3661
3661
3662 _pylab_magic_run.__doc__ = magic_run.__doc__
3662 _pylab_magic_run.__doc__ = magic_run.__doc__
3663
3663
3664 @testdec.skip_doctest
3664 @testdec.skip_doctest
3665 def magic_pylab(self, s):
3665 def magic_pylab(self, s):
3666 """Load numpy and matplotlib to work interactively.
3666 """Load numpy and matplotlib to work interactively.
3667
3667
3668 %pylab [GUINAME]
3668 %pylab [GUINAME]
3669
3669
3670 This function lets you activate pylab (matplotlib, numpy and
3670 This function lets you activate pylab (matplotlib, numpy and
3671 interactive support) at any point during an IPython session.
3671 interactive support) at any point during an IPython session.
3672
3672
3673 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3673 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3674 pylab and mlab, as well as all names from numpy and pylab.
3674 pylab and mlab, as well as all names from numpy and pylab.
3675
3675
3676 Parameters
3676 Parameters
3677 ----------
3677 ----------
3678 guiname : optional
3678 guiname : optional
3679 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3679 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3680 'tk'). If given, the corresponding Matplotlib backend is used,
3680 'tk'). If given, the corresponding Matplotlib backend is used,
3681 otherwise matplotlib's default (which you can override in your
3681 otherwise matplotlib's default (which you can override in your
3682 matplotlib config file) is used.
3682 matplotlib config file) is used.
3683
3683
3684 Examples
3684 Examples
3685 --------
3685 --------
3686 In this case, where the MPL default is TkAgg:
3686 In this case, where the MPL default is TkAgg:
3687 In [2]: %pylab
3687 In [2]: %pylab
3688
3688
3689 Welcome to pylab, a matplotlib-based Python environment.
3689 Welcome to pylab, a matplotlib-based Python environment.
3690 Backend in use: TkAgg
3690 Backend in use: TkAgg
3691 For more information, type 'help(pylab)'.
3691 For more information, type 'help(pylab)'.
3692
3692
3693 But you can explicitly request a different backend:
3693 But you can explicitly request a different backend:
3694 In [3]: %pylab qt
3694 In [3]: %pylab qt
3695
3695
3696 Welcome to pylab, a matplotlib-based Python environment.
3696 Welcome to pylab, a matplotlib-based Python environment.
3697 Backend in use: Qt4Agg
3697 Backend in use: Qt4Agg
3698 For more information, type 'help(pylab)'.
3698 For more information, type 'help(pylab)'.
3699 """
3699 """
3700 self.shell.enable_pylab(s)
3700 self.shell.enable_pylab(s)
3701
3701
3702 def magic_tb(self, s):
3702 def magic_tb(self, s):
3703 """Print the last traceback with the currently active exception mode.
3703 """Print the last traceback with the currently active exception mode.
3704
3704
3705 See %xmode for changing exception reporting modes."""
3705 See %xmode for changing exception reporting modes."""
3706 self.shell.showtraceback()
3706 self.shell.showtraceback()
3707
3707
3708 # end Magic
3708 # end Magic
@@ -1,1050 +1,1022 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Prefiltering components.
4 Prefiltering components.
5
5
6 Prefilters transform user input before it is exec'd by Python. These
6 Prefilters transform user input before it is exec'd by Python. These
7 transforms are used to implement additional syntax such as !ls and %magic.
7 transforms are used to implement additional syntax such as !ls and %magic.
8
8
9 Authors:
9 Authors:
10
10
11 * Brian Granger
11 * Brian Granger
12 * Fernando Perez
12 * Fernando Perez
13 * Dan Milstein
13 * Dan Milstein
14 * Ville Vainio
14 * Ville Vainio
15 """
15 """
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Copyright (C) 2008-2009 The IPython Development Team
18 # Copyright (C) 2008-2009 The IPython Development Team
19 #
19 #
20 # Distributed under the terms of the BSD License. The full license is in
20 # Distributed under the terms of the BSD License. The full license is in
21 # the file COPYING, distributed as part of this software.
21 # the file COPYING, distributed as part of this software.
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Imports
25 # Imports
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 import __builtin__
28 import __builtin__
29 import codeop
29 import codeop
30 import re
30 import re
31
31
32 from IPython.core.alias import AliasManager
32 from IPython.core.alias import AliasManager
33 from IPython.core.autocall import IPyAutocall
33 from IPython.core.autocall import IPyAutocall
34 from IPython.core.component import Component
34 from IPython.config.configurable import Configurable
35 from IPython.core.splitinput import split_user_input
35 from IPython.core.splitinput import split_user_input
36 from IPython.core.page import page
36 from IPython.core.page import page
37
37
38 from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool
38 from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool, Instance
39 from IPython.utils.io import Term
39 from IPython.utils.io import Term
40 from IPython.utils.text import make_quoted_expr
40 from IPython.utils.text import make_quoted_expr
41 from IPython.utils.autoattr import auto_attr
41 from IPython.utils.autoattr import auto_attr
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Global utilities, errors and constants
44 # Global utilities, errors and constants
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 # Warning, these cannot be changed unless various regular expressions
47 # Warning, these cannot be changed unless various regular expressions
48 # are updated in a number of places. Not great, but at least we told you.
48 # are updated in a number of places. Not great, but at least we told you.
49 ESC_SHELL = '!'
49 ESC_SHELL = '!'
50 ESC_SH_CAP = '!!'
50 ESC_SH_CAP = '!!'
51 ESC_HELP = '?'
51 ESC_HELP = '?'
52 ESC_MAGIC = '%'
52 ESC_MAGIC = '%'
53 ESC_QUOTE = ','
53 ESC_QUOTE = ','
54 ESC_QUOTE2 = ';'
54 ESC_QUOTE2 = ';'
55 ESC_PAREN = '/'
55 ESC_PAREN = '/'
56
56
57
57
58 class PrefilterError(Exception):
58 class PrefilterError(Exception):
59 pass
59 pass
60
60
61
61
62 # RegExp to identify potential function names
62 # RegExp to identify potential function names
63 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
63 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
64
64
65 # RegExp to exclude strings with this start from autocalling. In
65 # RegExp to exclude strings with this start from autocalling. In
66 # particular, all binary operators should be excluded, so that if foo is
66 # particular, all binary operators should be excluded, so that if foo is
67 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
67 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
68 # characters '!=()' don't need to be checked for, as the checkPythonChars
68 # characters '!=()' don't need to be checked for, as the checkPythonChars
69 # routine explicitely does so, to catch direct calls and rebindings of
69 # routine explicitely does so, to catch direct calls and rebindings of
70 # existing names.
70 # existing names.
71
71
72 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
72 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
73 # it affects the rest of the group in square brackets.
73 # it affects the rest of the group in square brackets.
74 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
74 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
75 r'|^is |^not |^in |^and |^or ')
75 r'|^is |^not |^in |^and |^or ')
76
76
77 # try to catch also methods for stuff in lists/tuples/dicts: off
77 # try to catch also methods for stuff in lists/tuples/dicts: off
78 # (experimental). For this to work, the line_split regexp would need
78 # (experimental). For this to work, the line_split regexp would need
79 # to be modified so it wouldn't break things at '['. That line is
79 # to be modified so it wouldn't break things at '['. That line is
80 # nasty enough that I shouldn't change it until I can test it _well_.
80 # nasty enough that I shouldn't change it until I can test it _well_.
81 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
81 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
82
82
83
83
84 # Handler Check Utilities
84 # Handler Check Utilities
85 def is_shadowed(identifier, ip):
85 def is_shadowed(identifier, ip):
86 """Is the given identifier defined in one of the namespaces which shadow
86 """Is the given identifier defined in one of the namespaces which shadow
87 the alias and magic namespaces? Note that an identifier is different
87 the alias and magic namespaces? Note that an identifier is different
88 than ifun, because it can not contain a '.' character."""
88 than ifun, because it can not contain a '.' character."""
89 # This is much safer than calling ofind, which can change state
89 # This is much safer than calling ofind, which can change state
90 return (identifier in ip.user_ns \
90 return (identifier in ip.user_ns \
91 or identifier in ip.internal_ns \
91 or identifier in ip.internal_ns \
92 or identifier in ip.ns_table['builtin'])
92 or identifier in ip.ns_table['builtin'])
93
93
94
94
95 #-----------------------------------------------------------------------------
95 #-----------------------------------------------------------------------------
96 # The LineInfo class used throughout
96 # The LineInfo class used throughout
97 #-----------------------------------------------------------------------------
97 #-----------------------------------------------------------------------------
98
98
99
99
100 class LineInfo(object):
100 class LineInfo(object):
101 """A single line of input and associated info.
101 """A single line of input and associated info.
102
102
103 Includes the following as properties:
103 Includes the following as properties:
104
104
105 line
105 line
106 The original, raw line
106 The original, raw line
107
107
108 continue_prompt
108 continue_prompt
109 Is this line a continuation in a sequence of multiline input?
109 Is this line a continuation in a sequence of multiline input?
110
110
111 pre
111 pre
112 The initial esc character or whitespace.
112 The initial esc character or whitespace.
113
113
114 pre_char
114 pre_char
115 The escape character(s) in pre or the empty string if there isn't one.
115 The escape character(s) in pre or the empty string if there isn't one.
116 Note that '!!' is a possible value for pre_char. Otherwise it will
116 Note that '!!' is a possible value for pre_char. Otherwise it will
117 always be a single character.
117 always be a single character.
118
118
119 pre_whitespace
119 pre_whitespace
120 The leading whitespace from pre if it exists. If there is a pre_char,
120 The leading whitespace from pre if it exists. If there is a pre_char,
121 this is just ''.
121 this is just ''.
122
122
123 ifun
123 ifun
124 The 'function part', which is basically the maximal initial sequence
124 The 'function part', which is basically the maximal initial sequence
125 of valid python identifiers and the '.' character. This is what is
125 of valid python identifiers and the '.' character. This is what is
126 checked for alias and magic transformations, used for auto-calling,
126 checked for alias and magic transformations, used for auto-calling,
127 etc.
127 etc.
128
128
129 the_rest
129 the_rest
130 Everything else on the line.
130 Everything else on the line.
131 """
131 """
132 def __init__(self, line, continue_prompt):
132 def __init__(self, line, continue_prompt):
133 self.line = line
133 self.line = line
134 self.continue_prompt = continue_prompt
134 self.continue_prompt = continue_prompt
135 self.pre, self.ifun, self.the_rest = split_user_input(line)
135 self.pre, self.ifun, self.the_rest = split_user_input(line)
136
136
137 self.pre_char = self.pre.strip()
137 self.pre_char = self.pre.strip()
138 if self.pre_char:
138 if self.pre_char:
139 self.pre_whitespace = '' # No whitespace allowd before esc chars
139 self.pre_whitespace = '' # No whitespace allowd before esc chars
140 else:
140 else:
141 self.pre_whitespace = self.pre
141 self.pre_whitespace = self.pre
142
142
143 self._oinfo = None
143 self._oinfo = None
144
144
145 def ofind(self, ip):
145 def ofind(self, ip):
146 """Do a full, attribute-walking lookup of the ifun in the various
146 """Do a full, attribute-walking lookup of the ifun in the various
147 namespaces for the given IPython InteractiveShell instance.
147 namespaces for the given IPython InteractiveShell instance.
148
148
149 Return a dict with keys: found,obj,ospace,ismagic
149 Return a dict with keys: found,obj,ospace,ismagic
150
150
151 Note: can cause state changes because of calling getattr, but should
151 Note: can cause state changes because of calling getattr, but should
152 only be run if autocall is on and if the line hasn't matched any
152 only be run if autocall is on and if the line hasn't matched any
153 other, less dangerous handlers.
153 other, less dangerous handlers.
154
154
155 Does cache the results of the call, so can be called multiple times
155 Does cache the results of the call, so can be called multiple times
156 without worrying about *further* damaging state.
156 without worrying about *further* damaging state.
157 """
157 """
158 if not self._oinfo:
158 if not self._oinfo:
159 # ip.shell._ofind is actually on the Magic class!
159 # ip.shell._ofind is actually on the Magic class!
160 self._oinfo = ip.shell._ofind(self.ifun)
160 self._oinfo = ip.shell._ofind(self.ifun)
161 return self._oinfo
161 return self._oinfo
162
162
163 def __str__(self):
163 def __str__(self):
164 return "Lineinfo [%s|%s|%s]" %(self.pre, self.ifun, self.the_rest)
164 return "Lineinfo [%s|%s|%s]" %(self.pre, self.ifun, self.the_rest)
165
165
166
166
167 #-----------------------------------------------------------------------------
167 #-----------------------------------------------------------------------------
168 # Main Prefilter manager
168 # Main Prefilter manager
169 #-----------------------------------------------------------------------------
169 #-----------------------------------------------------------------------------
170
170
171
171
172 class PrefilterManager(Component):
172 class PrefilterManager(Configurable):
173 """Main prefilter component.
173 """Main prefilter component.
174
174
175 The IPython prefilter is run on all user input before it is run. The
175 The IPython prefilter is run on all user input before it is run. The
176 prefilter consumes lines of input and produces transformed lines of
176 prefilter consumes lines of input and produces transformed lines of
177 input.
177 input.
178
178
179 The iplementation consists of two phases:
179 The iplementation consists of two phases:
180
180
181 1. Transformers
181 1. Transformers
182 2. Checkers and handlers
182 2. Checkers and handlers
183
183
184 Over time, we plan on deprecating the checkers and handlers and doing
184 Over time, we plan on deprecating the checkers and handlers and doing
185 everything in the transformers.
185 everything in the transformers.
186
186
187 The transformers are instances of :class:`PrefilterTransformer` and have
187 The transformers are instances of :class:`PrefilterTransformer` and have
188 a single method :meth:`transform` that takes a line and returns a
188 a single method :meth:`transform` that takes a line and returns a
189 transformed line. The transformation can be accomplished using any
189 transformed line. The transformation can be accomplished using any
190 tool, but our current ones use regular expressions for speed. We also
190 tool, but our current ones use regular expressions for speed. We also
191 ship :mod:`pyparsing` in :mod:`IPython.external` for use in transformers.
191 ship :mod:`pyparsing` in :mod:`IPython.external` for use in transformers.
192
192
193 After all the transformers have been run, the line is fed to the checkers,
193 After all the transformers have been run, the line is fed to the checkers,
194 which are instances of :class:`PrefilterChecker`. The line is passed to
194 which are instances of :class:`PrefilterChecker`. The line is passed to
195 the :meth:`check` method, which either returns `None` or a
195 the :meth:`check` method, which either returns `None` or a
196 :class:`PrefilterHandler` instance. If `None` is returned, the other
196 :class:`PrefilterHandler` instance. If `None` is returned, the other
197 checkers are tried. If an :class:`PrefilterHandler` instance is returned,
197 checkers are tried. If an :class:`PrefilterHandler` instance is returned,
198 the line is passed to the :meth:`handle` method of the returned
198 the line is passed to the :meth:`handle` method of the returned
199 handler and no further checkers are tried.
199 handler and no further checkers are tried.
200
200
201 Both transformers and checkers have a `priority` attribute, that determines
201 Both transformers and checkers have a `priority` attribute, that determines
202 the order in which they are called. Smaller priorities are tried first.
202 the order in which they are called. Smaller priorities are tried first.
203
203
204 Both transformers and checkers also have `enabled` attribute, which is
204 Both transformers and checkers also have `enabled` attribute, which is
205 a boolean that determines if the instance is used.
205 a boolean that determines if the instance is used.
206
206
207 Users or developers can change the priority or enabled attribute of
207 Users or developers can change the priority or enabled attribute of
208 transformers or checkers, but they must call the :meth:`sort_checkers`
208 transformers or checkers, but they must call the :meth:`sort_checkers`
209 or :meth:`sort_transformers` method after changing the priority.
209 or :meth:`sort_transformers` method after changing the priority.
210 """
210 """
211
211
212 multi_line_specials = CBool(True, config=True)
212 multi_line_specials = CBool(True, config=True)
213 shell = Instance('IPython.core.iplib.InteractiveShellABC')
213
214
214 def __init__(self, parent, config=None):
215 def __init__(self, shell=None, config=None):
215 super(PrefilterManager, self).__init__(parent, config=config)
216 super(PrefilterManager, self).__init__(shell=shell, config=config)
217 self.shell = shell
216 self.init_transformers()
218 self.init_transformers()
217 self.init_handlers()
219 self.init_handlers()
218 self.init_checkers()
220 self.init_checkers()
219
221
220 @auto_attr
221 def shell(self):
222 return Component.get_instances(
223 root=self.root,
224 klass='IPython.core.iplib.InteractiveShell')[0]
225
226 #-------------------------------------------------------------------------
222 #-------------------------------------------------------------------------
227 # API for managing transformers
223 # API for managing transformers
228 #-------------------------------------------------------------------------
224 #-------------------------------------------------------------------------
229
225
230 def init_transformers(self):
226 def init_transformers(self):
231 """Create the default transformers."""
227 """Create the default transformers."""
232 self._transformers = []
228 self._transformers = []
233 for transformer_cls in _default_transformers:
229 for transformer_cls in _default_transformers:
234 transformer_cls(self, config=self.config)
230 transformer_cls(
231 shell=self.shell, prefilter_manager=self, config=self.config
232 )
235
233
236 def sort_transformers(self):
234 def sort_transformers(self):
237 """Sort the transformers by priority.
235 """Sort the transformers by priority.
238
236
239 This must be called after the priority of a transformer is changed.
237 This must be called after the priority of a transformer is changed.
240 The :meth:`register_transformer` method calls this automatically.
238 The :meth:`register_transformer` method calls this automatically.
241 """
239 """
242 self._transformers.sort(cmp=lambda x,y: x.priority-y.priority)
240 self._transformers.sort(cmp=lambda x,y: x.priority-y.priority)
243
241
244 @property
242 @property
245 def transformers(self):
243 def transformers(self):
246 """Return a list of checkers, sorted by priority."""
244 """Return a list of checkers, sorted by priority."""
247 return self._transformers
245 return self._transformers
248
246
249 def register_transformer(self, transformer):
247 def register_transformer(self, transformer):
250 """Register a transformer instance."""
248 """Register a transformer instance."""
251 if transformer not in self._transformers:
249 if transformer not in self._transformers:
252 self._transformers.append(transformer)
250 self._transformers.append(transformer)
253 self.sort_transformers()
251 self.sort_transformers()
254
252
255 def unregister_transformer(self, transformer):
253 def unregister_transformer(self, transformer):
256 """Unregister a transformer instance."""
254 """Unregister a transformer instance."""
257 if transformer in self._transformers:
255 if transformer in self._transformers:
258 self._transformers.remove(transformer)
256 self._transformers.remove(transformer)
259
257
260 #-------------------------------------------------------------------------
258 #-------------------------------------------------------------------------
261 # API for managing checkers
259 # API for managing checkers
262 #-------------------------------------------------------------------------
260 #-------------------------------------------------------------------------
263
261
264 def init_checkers(self):
262 def init_checkers(self):
265 """Create the default checkers."""
263 """Create the default checkers."""
266 self._checkers = []
264 self._checkers = []
267 for checker in _default_checkers:
265 for checker in _default_checkers:
268 checker(self, config=self.config)
266 checker(
267 shell=self.shell, prefilter_manager=self, config=self.config
268 )
269
269
270 def sort_checkers(self):
270 def sort_checkers(self):
271 """Sort the checkers by priority.
271 """Sort the checkers by priority.
272
272
273 This must be called after the priority of a checker is changed.
273 This must be called after the priority of a checker is changed.
274 The :meth:`register_checker` method calls this automatically.
274 The :meth:`register_checker` method calls this automatically.
275 """
275 """
276 self._checkers.sort(cmp=lambda x,y: x.priority-y.priority)
276 self._checkers.sort(cmp=lambda x,y: x.priority-y.priority)
277
277
278 @property
278 @property
279 def checkers(self):
279 def checkers(self):
280 """Return a list of checkers, sorted by priority."""
280 """Return a list of checkers, sorted by priority."""
281 return self._checkers
281 return self._checkers
282
282
283 def register_checker(self, checker):
283 def register_checker(self, checker):
284 """Register a checker instance."""
284 """Register a checker instance."""
285 if checker not in self._checkers:
285 if checker not in self._checkers:
286 self._checkers.append(checker)
286 self._checkers.append(checker)
287 self.sort_checkers()
287 self.sort_checkers()
288
288
289 def unregister_checker(self, checker):
289 def unregister_checker(self, checker):
290 """Unregister a checker instance."""
290 """Unregister a checker instance."""
291 if checker in self._checkers:
291 if checker in self._checkers:
292 self._checkers.remove(checker)
292 self._checkers.remove(checker)
293
293
294 #-------------------------------------------------------------------------
294 #-------------------------------------------------------------------------
295 # API for managing checkers
295 # API for managing checkers
296 #-------------------------------------------------------------------------
296 #-------------------------------------------------------------------------
297
297
298 def init_handlers(self):
298 def init_handlers(self):
299 """Create the default handlers."""
299 """Create the default handlers."""
300 self._handlers = {}
300 self._handlers = {}
301 self._esc_handlers = {}
301 self._esc_handlers = {}
302 for handler in _default_handlers:
302 for handler in _default_handlers:
303 handler(self, config=self.config)
303 handler(
304 shell=self.shell, prefilter_manager=self, config=self.config
305 )
304
306
305 @property
307 @property
306 def handlers(self):
308 def handlers(self):
307 """Return a dict of all the handlers."""
309 """Return a dict of all the handlers."""
308 return self._handlers
310 return self._handlers
309
311
310 def register_handler(self, name, handler, esc_strings):
312 def register_handler(self, name, handler, esc_strings):
311 """Register a handler instance by name with esc_strings."""
313 """Register a handler instance by name with esc_strings."""
312 self._handlers[name] = handler
314 self._handlers[name] = handler
313 for esc_str in esc_strings:
315 for esc_str in esc_strings:
314 self._esc_handlers[esc_str] = handler
316 self._esc_handlers[esc_str] = handler
315
317
316 def unregister_handler(self, name, handler, esc_strings):
318 def unregister_handler(self, name, handler, esc_strings):
317 """Unregister a handler instance by name with esc_strings."""
319 """Unregister a handler instance by name with esc_strings."""
318 try:
320 try:
319 del self._handlers[name]
321 del self._handlers[name]
320 except KeyError:
322 except KeyError:
321 pass
323 pass
322 for esc_str in esc_strings:
324 for esc_str in esc_strings:
323 h = self._esc_handlers.get(esc_str)
325 h = self._esc_handlers.get(esc_str)
324 if h is handler:
326 if h is handler:
325 del self._esc_handlers[esc_str]
327 del self._esc_handlers[esc_str]
326
328
327 def get_handler_by_name(self, name):
329 def get_handler_by_name(self, name):
328 """Get a handler by its name."""
330 """Get a handler by its name."""
329 return self._handlers.get(name)
331 return self._handlers.get(name)
330
332
331 def get_handler_by_esc(self, esc_str):
333 def get_handler_by_esc(self, esc_str):
332 """Get a handler by its escape string."""
334 """Get a handler by its escape string."""
333 return self._esc_handlers.get(esc_str)
335 return self._esc_handlers.get(esc_str)
334
336
335 #-------------------------------------------------------------------------
337 #-------------------------------------------------------------------------
336 # Main prefiltering API
338 # Main prefiltering API
337 #-------------------------------------------------------------------------
339 #-------------------------------------------------------------------------
338
340
339 def prefilter_line_info(self, line_info):
341 def prefilter_line_info(self, line_info):
340 """Prefilter a line that has been converted to a LineInfo object.
342 """Prefilter a line that has been converted to a LineInfo object.
341
343
342 This implements the checker/handler part of the prefilter pipe.
344 This implements the checker/handler part of the prefilter pipe.
343 """
345 """
344 # print "prefilter_line_info: ", line_info
346 # print "prefilter_line_info: ", line_info
345 handler = self.find_handler(line_info)
347 handler = self.find_handler(line_info)
346 return handler.handle(line_info)
348 return handler.handle(line_info)
347
349
348 def find_handler(self, line_info):
350 def find_handler(self, line_info):
349 """Find a handler for the line_info by trying checkers."""
351 """Find a handler for the line_info by trying checkers."""
350 for checker in self.checkers:
352 for checker in self.checkers:
351 if checker.enabled:
353 if checker.enabled:
352 handler = checker.check(line_info)
354 handler = checker.check(line_info)
353 if handler:
355 if handler:
354 return handler
356 return handler
355 return self.get_handler_by_name('normal')
357 return self.get_handler_by_name('normal')
356
358
357 def transform_line(self, line, continue_prompt):
359 def transform_line(self, line, continue_prompt):
358 """Calls the enabled transformers in order of increasing priority."""
360 """Calls the enabled transformers in order of increasing priority."""
359 for transformer in self.transformers:
361 for transformer in self.transformers:
360 if transformer.enabled:
362 if transformer.enabled:
361 line = transformer.transform(line, continue_prompt)
363 line = transformer.transform(line, continue_prompt)
362 return line
364 return line
363
365
364 def prefilter_line(self, line, continue_prompt=False):
366 def prefilter_line(self, line, continue_prompt=False):
365 """Prefilter a single input line as text.
367 """Prefilter a single input line as text.
366
368
367 This method prefilters a single line of text by calling the
369 This method prefilters a single line of text by calling the
368 transformers and then the checkers/handlers.
370 transformers and then the checkers/handlers.
369 """
371 """
370
372
371 # print "prefilter_line: ", line, continue_prompt
373 # print "prefilter_line: ", line, continue_prompt
372 # All handlers *must* return a value, even if it's blank ('').
374 # All handlers *must* return a value, even if it's blank ('').
373
375
374 # Lines are NOT logged here. Handlers should process the line as
376 # Lines are NOT logged here. Handlers should process the line as
375 # needed, update the cache AND log it (so that the input cache array
377 # needed, update the cache AND log it (so that the input cache array
376 # stays synced).
378 # stays synced).
377
379
378 # save the line away in case we crash, so the post-mortem handler can
380 # save the line away in case we crash, so the post-mortem handler can
379 # record it
381 # record it
380 self.shell._last_input_line = line
382 self.shell._last_input_line = line
381
383
382 if not line:
384 if not line:
383 # Return immediately on purely empty lines, so that if the user
385 # Return immediately on purely empty lines, so that if the user
384 # previously typed some whitespace that started a continuation
386 # previously typed some whitespace that started a continuation
385 # prompt, he can break out of that loop with just an empty line.
387 # prompt, he can break out of that loop with just an empty line.
386 # This is how the default python prompt works.
388 # This is how the default python prompt works.
387
389
388 # Only return if the accumulated input buffer was just whitespace!
390 # Only return if the accumulated input buffer was just whitespace!
389 if ''.join(self.shell.buffer).isspace():
391 if ''.join(self.shell.buffer).isspace():
390 self.shell.buffer[:] = []
392 self.shell.buffer[:] = []
391 return ''
393 return ''
392
394
393 # At this point, we invoke our transformers.
395 # At this point, we invoke our transformers.
394 if not continue_prompt or (continue_prompt and self.multi_line_specials):
396 if not continue_prompt or (continue_prompt and self.multi_line_specials):
395 line = self.transform_line(line, continue_prompt)
397 line = self.transform_line(line, continue_prompt)
396
398
397 # Now we compute line_info for the checkers and handlers
399 # Now we compute line_info for the checkers and handlers
398 line_info = LineInfo(line, continue_prompt)
400 line_info = LineInfo(line, continue_prompt)
399
401
400 # the input history needs to track even empty lines
402 # the input history needs to track even empty lines
401 stripped = line.strip()
403 stripped = line.strip()
402
404
403 normal_handler = self.get_handler_by_name('normal')
405 normal_handler = self.get_handler_by_name('normal')
404 if not stripped:
406 if not stripped:
405 if not continue_prompt:
407 if not continue_prompt:
406 self.shell.outputcache.prompt_count -= 1
408 self.shell.outputcache.prompt_count -= 1
407
409
408 return normal_handler.handle(line_info)
410 return normal_handler.handle(line_info)
409
411
410 # special handlers are only allowed for single line statements
412 # special handlers are only allowed for single line statements
411 if continue_prompt and not self.multi_line_specials:
413 if continue_prompt and not self.multi_line_specials:
412 return normal_handler.handle(line_info)
414 return normal_handler.handle(line_info)
413
415
414 prefiltered = self.prefilter_line_info(line_info)
416 prefiltered = self.prefilter_line_info(line_info)
415 # print "prefiltered line: %r" % prefiltered
417 # print "prefiltered line: %r" % prefiltered
416 return prefiltered
418 return prefiltered
417
419
418 def prefilter_lines(self, lines, continue_prompt=False):
420 def prefilter_lines(self, lines, continue_prompt=False):
419 """Prefilter multiple input lines of text.
421 """Prefilter multiple input lines of text.
420
422
421 This is the main entry point for prefiltering multiple lines of
423 This is the main entry point for prefiltering multiple lines of
422 input. This simply calls :meth:`prefilter_line` for each line of
424 input. This simply calls :meth:`prefilter_line` for each line of
423 input.
425 input.
424
426
425 This covers cases where there are multiple lines in the user entry,
427 This covers cases where there are multiple lines in the user entry,
426 which is the case when the user goes back to a multiline history
428 which is the case when the user goes back to a multiline history
427 entry and presses enter.
429 entry and presses enter.
428 """
430 """
429 llines = lines.rstrip('\n').split('\n')
431 llines = lines.rstrip('\n').split('\n')
430 # We can get multiple lines in one shot, where multiline input 'blends'
432 # We can get multiple lines in one shot, where multiline input 'blends'
431 # into one line, in cases like recalling from the readline history
433 # into one line, in cases like recalling from the readline history
432 # buffer. We need to make sure that in such cases, we correctly
434 # buffer. We need to make sure that in such cases, we correctly
433 # communicate downstream which line is first and which are continuation
435 # communicate downstream which line is first and which are continuation
434 # ones.
436 # ones.
435 if len(llines) > 1:
437 if len(llines) > 1:
436 out = '\n'.join([self.prefilter_line(line, lnum>0)
438 out = '\n'.join([self.prefilter_line(line, lnum>0)
437 for lnum, line in enumerate(llines) ])
439 for lnum, line in enumerate(llines) ])
438 else:
440 else:
439 out = self.prefilter_line(llines[0], continue_prompt)
441 out = self.prefilter_line(llines[0], continue_prompt)
440
442
441 return out
443 return out
442
444
443 #-----------------------------------------------------------------------------
445 #-----------------------------------------------------------------------------
444 # Prefilter transformers
446 # Prefilter transformers
445 #-----------------------------------------------------------------------------
447 #-----------------------------------------------------------------------------
446
448
447
449
448 class PrefilterTransformer(Component):
450 class PrefilterTransformer(Configurable):
449 """Transform a line of user input."""
451 """Transform a line of user input."""
450
452
451 priority = Int(100, config=True)
453 priority = Int(100, config=True)
452 shell = Any
454 # Transformers don't currently use shell or prefilter_manager, but as we
453 prefilter_manager = Any
455 # move away from checkers and handlers, they will need them.
456 shell = Instance('IPython.core.iplib.InteractiveShellABC')
457 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
454 enabled = Bool(True, config=True)
458 enabled = Bool(True, config=True)
455
459
456 def __init__(self, parent, config=None):
460 def __init__(self, shell=None, prefilter_manager=None, config=None):
457 super(PrefilterTransformer, self).__init__(parent, config=config)
461 super(PrefilterTransformer, self).__init__(
462 shell=shell, prefilter_manager=prefilter_manager, config=config
463 )
458 self.prefilter_manager.register_transformer(self)
464 self.prefilter_manager.register_transformer(self)
459
465
460 @auto_attr
461 def shell(self):
462 return Component.get_instances(
463 root=self.root,
464 klass='IPython.core.iplib.InteractiveShell')[0]
465
466 @auto_attr
467 def prefilter_manager(self):
468 return PrefilterManager.get_instances(root=self.root)[0]
469
470 def transform(self, line, continue_prompt):
466 def transform(self, line, continue_prompt):
471 """Transform a line, returning the new one."""
467 """Transform a line, returning the new one."""
472 return None
468 return None
473
469
474 def __repr__(self):
470 def __repr__(self):
475 return "<%s(priority=%r, enabled=%r)>" % (
471 return "<%s(priority=%r, enabled=%r)>" % (
476 self.__class__.__name__, self.priority, self.enabled)
472 self.__class__.__name__, self.priority, self.enabled)
477
473
478
474
479 _assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
475 _assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
480 r'\s*=\s*!(?P<cmd>.*)')
476 r'\s*=\s*!(?P<cmd>.*)')
481
477
482
478
483 class AssignSystemTransformer(PrefilterTransformer):
479 class AssignSystemTransformer(PrefilterTransformer):
484 """Handle the `files = !ls` syntax."""
480 """Handle the `files = !ls` syntax."""
485
481
486 priority = Int(100, config=True)
482 priority = Int(100, config=True)
487
483
488 def transform(self, line, continue_prompt):
484 def transform(self, line, continue_prompt):
489 m = _assign_system_re.match(line)
485 m = _assign_system_re.match(line)
490 if m is not None:
486 if m is not None:
491 cmd = m.group('cmd')
487 cmd = m.group('cmd')
492 lhs = m.group('lhs')
488 lhs = m.group('lhs')
493 expr = make_quoted_expr("sc -l =%s" % cmd)
489 expr = make_quoted_expr("sc -l =%s" % cmd)
494 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
490 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
495 return new_line
491 return new_line
496 return line
492 return line
497
493
498
494
499 _assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
495 _assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
500 r'\s*=\s*%(?P<cmd>.*)')
496 r'\s*=\s*%(?P<cmd>.*)')
501
497
502 class AssignMagicTransformer(PrefilterTransformer):
498 class AssignMagicTransformer(PrefilterTransformer):
503 """Handle the `a = %who` syntax."""
499 """Handle the `a = %who` syntax."""
504
500
505 priority = Int(200, config=True)
501 priority = Int(200, config=True)
506
502
507 def transform(self, line, continue_prompt):
503 def transform(self, line, continue_prompt):
508 m = _assign_magic_re.match(line)
504 m = _assign_magic_re.match(line)
509 if m is not None:
505 if m is not None:
510 cmd = m.group('cmd')
506 cmd = m.group('cmd')
511 lhs = m.group('lhs')
507 lhs = m.group('lhs')
512 expr = make_quoted_expr(cmd)
508 expr = make_quoted_expr(cmd)
513 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
509 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
514 return new_line
510 return new_line
515 return line
511 return line
516
512
517
513
518 _classic_prompt_re = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
514 _classic_prompt_re = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
519
515
520 class PyPromptTransformer(PrefilterTransformer):
516 class PyPromptTransformer(PrefilterTransformer):
521 """Handle inputs that start with '>>> ' syntax."""
517 """Handle inputs that start with '>>> ' syntax."""
522
518
523 priority = Int(50, config=True)
519 priority = Int(50, config=True)
524
520
525 def transform(self, line, continue_prompt):
521 def transform(self, line, continue_prompt):
526
522
527 if not line or line.isspace() or line.strip() == '...':
523 if not line or line.isspace() or line.strip() == '...':
528 # This allows us to recognize multiple input prompts separated by
524 # This allows us to recognize multiple input prompts separated by
529 # blank lines and pasted in a single chunk, very common when
525 # blank lines and pasted in a single chunk, very common when
530 # pasting doctests or long tutorial passages.
526 # pasting doctests or long tutorial passages.
531 return ''
527 return ''
532 m = _classic_prompt_re.match(line)
528 m = _classic_prompt_re.match(line)
533 if m:
529 if m:
534 return line[len(m.group(0)):]
530 return line[len(m.group(0)):]
535 else:
531 else:
536 return line
532 return line
537
533
538
534
539 _ipy_prompt_re = re.compile(r'(^[ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )')
535 _ipy_prompt_re = re.compile(r'(^[ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )')
540
536
541 class IPyPromptTransformer(PrefilterTransformer):
537 class IPyPromptTransformer(PrefilterTransformer):
542 """Handle inputs that start classic IPython prompt syntax."""
538 """Handle inputs that start classic IPython prompt syntax."""
543
539
544 priority = Int(50, config=True)
540 priority = Int(50, config=True)
545
541
546 def transform(self, line, continue_prompt):
542 def transform(self, line, continue_prompt):
547
543
548 if not line or line.isspace() or line.strip() == '...':
544 if not line or line.isspace() or line.strip() == '...':
549 # This allows us to recognize multiple input prompts separated by
545 # This allows us to recognize multiple input prompts separated by
550 # blank lines and pasted in a single chunk, very common when
546 # blank lines and pasted in a single chunk, very common when
551 # pasting doctests or long tutorial passages.
547 # pasting doctests or long tutorial passages.
552 return ''
548 return ''
553 m = _ipy_prompt_re.match(line)
549 m = _ipy_prompt_re.match(line)
554 if m:
550 if m:
555 return line[len(m.group(0)):]
551 return line[len(m.group(0)):]
556 else:
552 else:
557 return line
553 return line
558
554
559 #-----------------------------------------------------------------------------
555 #-----------------------------------------------------------------------------
560 # Prefilter checkers
556 # Prefilter checkers
561 #-----------------------------------------------------------------------------
557 #-----------------------------------------------------------------------------
562
558
563
559
564 class PrefilterChecker(Component):
560 class PrefilterChecker(Configurable):
565 """Inspect an input line and return a handler for that line."""
561 """Inspect an input line and return a handler for that line."""
566
562
567 priority = Int(100, config=True)
563 priority = Int(100, config=True)
568 shell = Any
564 shell = Instance('IPython.core.iplib.InteractiveShellABC')
569 prefilter_manager = Any
565 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
570 enabled = Bool(True, config=True)
566 enabled = Bool(True, config=True)
571
567
572 def __init__(self, parent, config=None):
568 def __init__(self, shell=None, prefilter_manager=None, config=None):
573 super(PrefilterChecker, self).__init__(parent, config=config)
569 super(PrefilterChecker, self).__init__(
570 shell=shell, prefilter_manager=prefilter_manager, config=config
571 )
574 self.prefilter_manager.register_checker(self)
572 self.prefilter_manager.register_checker(self)
575
573
576 @auto_attr
577 def shell(self):
578 return Component.get_instances(
579 root=self.root,
580 klass='IPython.core.iplib.InteractiveShell')[0]
581
582 @auto_attr
583 def prefilter_manager(self):
584 return PrefilterManager.get_instances(root=self.root)[0]
585
586 def check(self, line_info):
574 def check(self, line_info):
587 """Inspect line_info and return a handler instance or None."""
575 """Inspect line_info and return a handler instance or None."""
588 return None
576 return None
589
577
590 def __repr__(self):
578 def __repr__(self):
591 return "<%s(priority=%r, enabled=%r)>" % (
579 return "<%s(priority=%r, enabled=%r)>" % (
592 self.__class__.__name__, self.priority, self.enabled)
580 self.__class__.__name__, self.priority, self.enabled)
593
581
594
582
595 class EmacsChecker(PrefilterChecker):
583 class EmacsChecker(PrefilterChecker):
596
584
597 priority = Int(100, config=True)
585 priority = Int(100, config=True)
598 enabled = Bool(False, config=True)
586 enabled = Bool(False, config=True)
599
587
600 def check(self, line_info):
588 def check(self, line_info):
601 "Emacs ipython-mode tags certain input lines."
589 "Emacs ipython-mode tags certain input lines."
602 if line_info.line.endswith('# PYTHON-MODE'):
590 if line_info.line.endswith('# PYTHON-MODE'):
603 return self.prefilter_manager.get_handler_by_name('emacs')
591 return self.prefilter_manager.get_handler_by_name('emacs')
604 else:
592 else:
605 return None
593 return None
606
594
607
595
608 class ShellEscapeChecker(PrefilterChecker):
596 class ShellEscapeChecker(PrefilterChecker):
609
597
610 priority = Int(200, config=True)
598 priority = Int(200, config=True)
611
599
612 def check(self, line_info):
600 def check(self, line_info):
613 if line_info.line.lstrip().startswith(ESC_SHELL):
601 if line_info.line.lstrip().startswith(ESC_SHELL):
614 return self.prefilter_manager.get_handler_by_name('shell')
602 return self.prefilter_manager.get_handler_by_name('shell')
615
603
616
604
617 class IPyAutocallChecker(PrefilterChecker):
605 class IPyAutocallChecker(PrefilterChecker):
618
606
619 priority = Int(300, config=True)
607 priority = Int(300, config=True)
620
608
621 def check(self, line_info):
609 def check(self, line_info):
622 "Instances of IPyAutocall in user_ns get autocalled immediately"
610 "Instances of IPyAutocall in user_ns get autocalled immediately"
623 obj = self.shell.user_ns.get(line_info.ifun, None)
611 obj = self.shell.user_ns.get(line_info.ifun, None)
624 if isinstance(obj, IPyAutocall):
612 if isinstance(obj, IPyAutocall):
625 obj.set_ip(self.shell)
613 obj.set_ip(self.shell)
626 return self.prefilter_manager.get_handler_by_name('auto')
614 return self.prefilter_manager.get_handler_by_name('auto')
627 else:
615 else:
628 return None
616 return None
629
617
630
618
631 class MultiLineMagicChecker(PrefilterChecker):
619 class MultiLineMagicChecker(PrefilterChecker):
632
620
633 priority = Int(400, config=True)
621 priority = Int(400, config=True)
634
622
635 def check(self, line_info):
623 def check(self, line_info):
636 "Allow ! and !! in multi-line statements if multi_line_specials is on"
624 "Allow ! and !! in multi-line statements if multi_line_specials is on"
637 # Note that this one of the only places we check the first character of
625 # Note that this one of the only places we check the first character of
638 # ifun and *not* the pre_char. Also note that the below test matches
626 # ifun and *not* the pre_char. Also note that the below test matches
639 # both ! and !!.
627 # both ! and !!.
640 if line_info.continue_prompt \
628 if line_info.continue_prompt \
641 and self.prefilter_manager.multi_line_specials:
629 and self.prefilter_manager.multi_line_specials:
642 if line_info.ifun.startswith(ESC_MAGIC):
630 if line_info.ifun.startswith(ESC_MAGIC):
643 return self.prefilter_manager.get_handler_by_name('magic')
631 return self.prefilter_manager.get_handler_by_name('magic')
644 else:
632 else:
645 return None
633 return None
646
634
647
635
648 class EscCharsChecker(PrefilterChecker):
636 class EscCharsChecker(PrefilterChecker):
649
637
650 priority = Int(500, config=True)
638 priority = Int(500, config=True)
651
639
652 def check(self, line_info):
640 def check(self, line_info):
653 """Check for escape character and return either a handler to handle it,
641 """Check for escape character and return either a handler to handle it,
654 or None if there is no escape char."""
642 or None if there is no escape char."""
655 if line_info.line[-1] == ESC_HELP \
643 if line_info.line[-1] == ESC_HELP \
656 and line_info.pre_char != ESC_SHELL \
644 and line_info.pre_char != ESC_SHELL \
657 and line_info.pre_char != ESC_SH_CAP:
645 and line_info.pre_char != ESC_SH_CAP:
658 # the ? can be at the end, but *not* for either kind of shell escape,
646 # the ? can be at the end, but *not* for either kind of shell escape,
659 # because a ? can be a vaild final char in a shell cmd
647 # because a ? can be a vaild final char in a shell cmd
660 return self.prefilter_manager.get_handler_by_name('help')
648 return self.prefilter_manager.get_handler_by_name('help')
661 else:
649 else:
662 # This returns None like it should if no handler exists
650 # This returns None like it should if no handler exists
663 return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
651 return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
664
652
665
653
666 class AssignmentChecker(PrefilterChecker):
654 class AssignmentChecker(PrefilterChecker):
667
655
668 priority = Int(600, config=True)
656 priority = Int(600, config=True)
669
657
670 def check(self, line_info):
658 def check(self, line_info):
671 """Check to see if user is assigning to a var for the first time, in
659 """Check to see if user is assigning to a var for the first time, in
672 which case we want to avoid any sort of automagic / autocall games.
660 which case we want to avoid any sort of automagic / autocall games.
673
661
674 This allows users to assign to either alias or magic names true python
662 This allows users to assign to either alias or magic names true python
675 variables (the magic/alias systems always take second seat to true
663 variables (the magic/alias systems always take second seat to true
676 python code). E.g. ls='hi', or ls,that=1,2"""
664 python code). E.g. ls='hi', or ls,that=1,2"""
677 if line_info.the_rest:
665 if line_info.the_rest:
678 if line_info.the_rest[0] in '=,':
666 if line_info.the_rest[0] in '=,':
679 return self.prefilter_manager.get_handler_by_name('normal')
667 return self.prefilter_manager.get_handler_by_name('normal')
680 else:
668 else:
681 return None
669 return None
682
670
683
671
684 class AutoMagicChecker(PrefilterChecker):
672 class AutoMagicChecker(PrefilterChecker):
685
673
686 priority = Int(700, config=True)
674 priority = Int(700, config=True)
687
675
688 def check(self, line_info):
676 def check(self, line_info):
689 """If the ifun is magic, and automagic is on, run it. Note: normal,
677 """If the ifun is magic, and automagic is on, run it. Note: normal,
690 non-auto magic would already have been triggered via '%' in
678 non-auto magic would already have been triggered via '%' in
691 check_esc_chars. This just checks for automagic. Also, before
679 check_esc_chars. This just checks for automagic. Also, before
692 triggering the magic handler, make sure that there is nothing in the
680 triggering the magic handler, make sure that there is nothing in the
693 user namespace which could shadow it."""
681 user namespace which could shadow it."""
694 if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun):
682 if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun):
695 return None
683 return None
696
684
697 # We have a likely magic method. Make sure we should actually call it.
685 # We have a likely magic method. Make sure we should actually call it.
698 if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials:
686 if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials:
699 return None
687 return None
700
688
701 head = line_info.ifun.split('.',1)[0]
689 head = line_info.ifun.split('.',1)[0]
702 if is_shadowed(head, self.shell):
690 if is_shadowed(head, self.shell):
703 return None
691 return None
704
692
705 return self.prefilter_manager.get_handler_by_name('magic')
693 return self.prefilter_manager.get_handler_by_name('magic')
706
694
707
695
708 class AliasChecker(PrefilterChecker):
696 class AliasChecker(PrefilterChecker):
709
697
710 priority = Int(800, config=True)
698 priority = Int(800, config=True)
711
699
712 @auto_attr
713 def alias_manager(self):
714 return AliasManager.get_instances(root=self.root)[0]
715
716 def check(self, line_info):
700 def check(self, line_info):
717 "Check if the initital identifier on the line is an alias."
701 "Check if the initital identifier on the line is an alias."
718 # Note: aliases can not contain '.'
702 # Note: aliases can not contain '.'
719 head = line_info.ifun.split('.',1)[0]
703 head = line_info.ifun.split('.',1)[0]
720 if line_info.ifun not in self.alias_manager \
704 if line_info.ifun not in self.shell.alias_manager \
721 or head not in self.alias_manager \
705 or head not in self.shell.alias_manager \
722 or is_shadowed(head, self.shell):
706 or is_shadowed(head, self.shell):
723 return None
707 return None
724
708
725 return self.prefilter_manager.get_handler_by_name('alias')
709 return self.prefilter_manager.get_handler_by_name('alias')
726
710
727
711
728 class PythonOpsChecker(PrefilterChecker):
712 class PythonOpsChecker(PrefilterChecker):
729
713
730 priority = Int(900, config=True)
714 priority = Int(900, config=True)
731
715
732 def check(self, line_info):
716 def check(self, line_info):
733 """If the 'rest' of the line begins with a function call or pretty much
717 """If the 'rest' of the line begins with a function call or pretty much
734 any python operator, we should simply execute the line (regardless of
718 any python operator, we should simply execute the line (regardless of
735 whether or not there's a possible autocall expansion). This avoids
719 whether or not there's a possible autocall expansion). This avoids
736 spurious (and very confusing) geattr() accesses."""
720 spurious (and very confusing) geattr() accesses."""
737 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
721 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
738 return self.prefilter_manager.get_handler_by_name('normal')
722 return self.prefilter_manager.get_handler_by_name('normal')
739 else:
723 else:
740 return None
724 return None
741
725
742
726
743 class AutocallChecker(PrefilterChecker):
727 class AutocallChecker(PrefilterChecker):
744
728
745 priority = Int(1000, config=True)
729 priority = Int(1000, config=True)
746
730
747 def check(self, line_info):
731 def check(self, line_info):
748 "Check if the initial word/function is callable and autocall is on."
732 "Check if the initial word/function is callable and autocall is on."
749 if not self.shell.autocall:
733 if not self.shell.autocall:
750 return None
734 return None
751
735
752 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
736 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
753 if not oinfo['found']:
737 if not oinfo['found']:
754 return None
738 return None
755
739
756 if callable(oinfo['obj']) \
740 if callable(oinfo['obj']) \
757 and (not re_exclude_auto.match(line_info.the_rest)) \
741 and (not re_exclude_auto.match(line_info.the_rest)) \
758 and re_fun_name.match(line_info.ifun):
742 and re_fun_name.match(line_info.ifun):
759 return self.prefilter_manager.get_handler_by_name('auto')
743 return self.prefilter_manager.get_handler_by_name('auto')
760 else:
744 else:
761 return None
745 return None
762
746
763
747
764 #-----------------------------------------------------------------------------
748 #-----------------------------------------------------------------------------
765 # Prefilter handlers
749 # Prefilter handlers
766 #-----------------------------------------------------------------------------
750 #-----------------------------------------------------------------------------
767
751
768
752
769 class PrefilterHandler(Component):
753 class PrefilterHandler(Configurable):
770
754
771 handler_name = Str('normal')
755 handler_name = Str('normal')
772 esc_strings = List([])
756 esc_strings = List([])
773 shell = Any
757 shell = Instance('IPython.core.iplib.InteractiveShellABC')
774 prefilter_manager = Any
758 prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
775
759
776 def __init__(self, parent, config=None):
760 def __init__(self, shell=None, prefilter_manager=None, config=None):
777 super(PrefilterHandler, self).__init__(parent, config=config)
761 super(PrefilterHandler, self).__init__(
762 shell=shell, prefilter_manager=prefilter_manager, config=config
763 )
778 self.prefilter_manager.register_handler(
764 self.prefilter_manager.register_handler(
779 self.handler_name,
765 self.handler_name,
780 self,
766 self,
781 self.esc_strings
767 self.esc_strings
782 )
768 )
783
769
784 @auto_attr
785 def shell(self):
786 return Component.get_instances(
787 root=self.root,
788 klass='IPython.core.iplib.InteractiveShell')[0]
789
790 @auto_attr
791 def prefilter_manager(self):
792 return PrefilterManager.get_instances(root=self.root)[0]
793
794 def handle(self, line_info):
770 def handle(self, line_info):
795 # print "normal: ", line_info
771 # print "normal: ", line_info
796 """Handle normal input lines. Use as a template for handlers."""
772 """Handle normal input lines. Use as a template for handlers."""
797
773
798 # With autoindent on, we need some way to exit the input loop, and I
774 # With autoindent on, we need some way to exit the input loop, and I
799 # don't want to force the user to have to backspace all the way to
775 # don't want to force the user to have to backspace all the way to
800 # clear the line. The rule will be in this case, that either two
776 # clear the line. The rule will be in this case, that either two
801 # lines of pure whitespace in a row, or a line of pure whitespace but
777 # lines of pure whitespace in a row, or a line of pure whitespace but
802 # of a size different to the indent level, will exit the input loop.
778 # of a size different to the indent level, will exit the input loop.
803 line = line_info.line
779 line = line_info.line
804 continue_prompt = line_info.continue_prompt
780 continue_prompt = line_info.continue_prompt
805
781
806 if (continue_prompt and
782 if (continue_prompt and
807 self.shell.autoindent and
783 self.shell.autoindent and
808 line.isspace() and
784 line.isspace() and
809
785
810 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2
786 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2
811 or
787 or
812 not self.shell.buffer
788 not self.shell.buffer
813 or
789 or
814 (self.shell.buffer[-1]).isspace()
790 (self.shell.buffer[-1]).isspace()
815 )
791 )
816 ):
792 ):
817 line = ''
793 line = ''
818
794
819 self.shell.log(line, line, continue_prompt)
795 self.shell.log(line, line, continue_prompt)
820 return line
796 return line
821
797
822 def __str__(self):
798 def __str__(self):
823 return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
799 return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
824
800
825
801
826 class AliasHandler(PrefilterHandler):
802 class AliasHandler(PrefilterHandler):
827
803
828 handler_name = Str('alias')
804 handler_name = Str('alias')
829
805
830 @auto_attr
831 def alias_manager(self):
832 return AliasManager.get_instances(root=self.root)[0]
833
834 def handle(self, line_info):
806 def handle(self, line_info):
835 """Handle alias input lines. """
807 """Handle alias input lines. """
836 transformed = self.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
808 transformed = self.shell.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
837 # pre is needed, because it carries the leading whitespace. Otherwise
809 # pre is needed, because it carries the leading whitespace. Otherwise
838 # aliases won't work in indented sections.
810 # aliases won't work in indented sections.
839 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
811 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
840 make_quoted_expr(transformed))
812 make_quoted_expr(transformed))
841
813
842 self.shell.log(line_info.line, line_out, line_info.continue_prompt)
814 self.shell.log(line_info.line, line_out, line_info.continue_prompt)
843 return line_out
815 return line_out
844
816
845
817
846 class ShellEscapeHandler(PrefilterHandler):
818 class ShellEscapeHandler(PrefilterHandler):
847
819
848 handler_name = Str('shell')
820 handler_name = Str('shell')
849 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
821 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
850
822
851 def handle(self, line_info):
823 def handle(self, line_info):
852 """Execute the line in a shell, empty return value"""
824 """Execute the line in a shell, empty return value"""
853 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
825 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
854
826
855 line = line_info.line
827 line = line_info.line
856 if line.lstrip().startswith(ESC_SH_CAP):
828 if line.lstrip().startswith(ESC_SH_CAP):
857 # rewrite LineInfo's line, ifun and the_rest to properly hold the
829 # rewrite LineInfo's line, ifun and the_rest to properly hold the
858 # call to %sx and the actual command to be executed, so
830 # call to %sx and the actual command to be executed, so
859 # handle_magic can work correctly. Note that this works even if
831 # handle_magic can work correctly. Note that this works even if
860 # the line is indented, so it handles multi_line_specials
832 # the line is indented, so it handles multi_line_specials
861 # properly.
833 # properly.
862 new_rest = line.lstrip()[2:]
834 new_rest = line.lstrip()[2:]
863 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
835 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
864 line_info.ifun = 'sx'
836 line_info.ifun = 'sx'
865 line_info.the_rest = new_rest
837 line_info.the_rest = new_rest
866 return magic_handler.handle(line_info)
838 return magic_handler.handle(line_info)
867 else:
839 else:
868 cmd = line.lstrip().lstrip(ESC_SHELL)
840 cmd = line.lstrip().lstrip(ESC_SHELL)
869 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
841 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
870 make_quoted_expr(cmd))
842 make_quoted_expr(cmd))
871 # update cache/log and return
843 # update cache/log and return
872 self.shell.log(line, line_out, line_info.continue_prompt)
844 self.shell.log(line, line_out, line_info.continue_prompt)
873 return line_out
845 return line_out
874
846
875
847
876 class MagicHandler(PrefilterHandler):
848 class MagicHandler(PrefilterHandler):
877
849
878 handler_name = Str('magic')
850 handler_name = Str('magic')
879 esc_strings = List([ESC_MAGIC])
851 esc_strings = List([ESC_MAGIC])
880
852
881 def handle(self, line_info):
853 def handle(self, line_info):
882 """Execute magic functions."""
854 """Execute magic functions."""
883 ifun = line_info.ifun
855 ifun = line_info.ifun
884 the_rest = line_info.the_rest
856 the_rest = line_info.the_rest
885 cmd = '%sget_ipython().magic(%s)' % (line_info.pre_whitespace,
857 cmd = '%sget_ipython().magic(%s)' % (line_info.pre_whitespace,
886 make_quoted_expr(ifun + " " + the_rest))
858 make_quoted_expr(ifun + " " + the_rest))
887 self.shell.log(line_info.line, cmd, line_info.continue_prompt)
859 self.shell.log(line_info.line, cmd, line_info.continue_prompt)
888 return cmd
860 return cmd
889
861
890
862
891 class AutoHandler(PrefilterHandler):
863 class AutoHandler(PrefilterHandler):
892
864
893 handler_name = Str('auto')
865 handler_name = Str('auto')
894 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
866 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
895
867
896 def handle(self, line_info):
868 def handle(self, line_info):
897 """Hande lines which can be auto-executed, quoting if requested."""
869 """Handle lines which can be auto-executed, quoting if requested."""
898 line = line_info.line
870 line = line_info.line
899 ifun = line_info.ifun
871 ifun = line_info.ifun
900 the_rest = line_info.the_rest
872 the_rest = line_info.the_rest
901 pre = line_info.pre
873 pre = line_info.pre
902 continue_prompt = line_info.continue_prompt
874 continue_prompt = line_info.continue_prompt
903 obj = line_info.ofind(self)['obj']
875 obj = line_info.ofind(self)['obj']
904 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
876 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
905
877
906 # This should only be active for single-line input!
878 # This should only be active for single-line input!
907 if continue_prompt:
879 if continue_prompt:
908 self.shell.log(line,line,continue_prompt)
880 self.shell.log(line,line,continue_prompt)
909 return line
881 return line
910
882
911 force_auto = isinstance(obj, IPyAutocall)
883 force_auto = isinstance(obj, IPyAutocall)
912 auto_rewrite = True
884 auto_rewrite = True
913
885
914 if pre == ESC_QUOTE:
886 if pre == ESC_QUOTE:
915 # Auto-quote splitting on whitespace
887 # Auto-quote splitting on whitespace
916 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
888 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
917 elif pre == ESC_QUOTE2:
889 elif pre == ESC_QUOTE2:
918 # Auto-quote whole string
890 # Auto-quote whole string
919 newcmd = '%s("%s")' % (ifun,the_rest)
891 newcmd = '%s("%s")' % (ifun,the_rest)
920 elif pre == ESC_PAREN:
892 elif pre == ESC_PAREN:
921 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
893 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
922 else:
894 else:
923 # Auto-paren.
895 # Auto-paren.
924 # We only apply it to argument-less calls if the autocall
896 # We only apply it to argument-less calls if the autocall
925 # parameter is set to 2. We only need to check that autocall is <
897 # parameter is set to 2. We only need to check that autocall is <
926 # 2, since this function isn't called unless it's at least 1.
898 # 2, since this function isn't called unless it's at least 1.
927 if not the_rest and (self.shell.autocall < 2) and not force_auto:
899 if not the_rest and (self.shell.autocall < 2) and not force_auto:
928 newcmd = '%s %s' % (ifun,the_rest)
900 newcmd = '%s %s' % (ifun,the_rest)
929 auto_rewrite = False
901 auto_rewrite = False
930 else:
902 else:
931 if not force_auto and the_rest.startswith('['):
903 if not force_auto and the_rest.startswith('['):
932 if hasattr(obj,'__getitem__'):
904 if hasattr(obj,'__getitem__'):
933 # Don't autocall in this case: item access for an object
905 # Don't autocall in this case: item access for an object
934 # which is BOTH callable and implements __getitem__.
906 # which is BOTH callable and implements __getitem__.
935 newcmd = '%s %s' % (ifun,the_rest)
907 newcmd = '%s %s' % (ifun,the_rest)
936 auto_rewrite = False
908 auto_rewrite = False
937 else:
909 else:
938 # if the object doesn't support [] access, go ahead and
910 # if the object doesn't support [] access, go ahead and
939 # autocall
911 # autocall
940 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
912 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
941 elif the_rest.endswith(';'):
913 elif the_rest.endswith(';'):
942 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
914 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
943 else:
915 else:
944 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
916 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
945
917
946 if auto_rewrite:
918 if auto_rewrite:
947 rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd
919 rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd
948
920
949 try:
921 try:
950 # plain ascii works better w/ pyreadline, on some machines, so
922 # plain ascii works better w/ pyreadline, on some machines, so
951 # we use it and only print uncolored rewrite if we have unicode
923 # we use it and only print uncolored rewrite if we have unicode
952 rw = str(rw)
924 rw = str(rw)
953 print >>Term.cout, rw
925 print >>Term.cout, rw
954 except UnicodeEncodeError:
926 except UnicodeEncodeError:
955 print "-------------->" + newcmd
927 print "-------------->" + newcmd
956
928
957 # log what is now valid Python, not the actual user input (without the
929 # log what is now valid Python, not the actual user input (without the
958 # final newline)
930 # final newline)
959 self.shell.log(line,newcmd,continue_prompt)
931 self.shell.log(line,newcmd,continue_prompt)
960 return newcmd
932 return newcmd
961
933
962
934
963 class HelpHandler(PrefilterHandler):
935 class HelpHandler(PrefilterHandler):
964
936
965 handler_name = Str('help')
937 handler_name = Str('help')
966 esc_strings = List([ESC_HELP])
938 esc_strings = List([ESC_HELP])
967
939
968 def handle(self, line_info):
940 def handle(self, line_info):
969 """Try to get some help for the object.
941 """Try to get some help for the object.
970
942
971 obj? or ?obj -> basic information.
943 obj? or ?obj -> basic information.
972 obj?? or ??obj -> more details.
944 obj?? or ??obj -> more details.
973 """
945 """
974 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
946 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
975 line = line_info.line
947 line = line_info.line
976 # We need to make sure that we don't process lines which would be
948 # We need to make sure that we don't process lines which would be
977 # otherwise valid python, such as "x=1 # what?"
949 # otherwise valid python, such as "x=1 # what?"
978 try:
950 try:
979 codeop.compile_command(line)
951 codeop.compile_command(line)
980 except SyntaxError:
952 except SyntaxError:
981 # We should only handle as help stuff which is NOT valid syntax
953 # We should only handle as help stuff which is NOT valid syntax
982 if line[0]==ESC_HELP:
954 if line[0]==ESC_HELP:
983 line = line[1:]
955 line = line[1:]
984 elif line[-1]==ESC_HELP:
956 elif line[-1]==ESC_HELP:
985 line = line[:-1]
957 line = line[:-1]
986 self.shell.log(line, '#?'+line, line_info.continue_prompt)
958 self.shell.log(line, '#?'+line, line_info.continue_prompt)
987 if line:
959 if line:
988 #print 'line:<%r>' % line # dbg
960 #print 'line:<%r>' % line # dbg
989 self.shell.magic_pinfo(line)
961 self.shell.magic_pinfo(line)
990 else:
962 else:
991 page(self.shell.usage, screen_lines=self.shell.usable_screen_length)
963 page(self.shell.usage, screen_lines=self.shell.usable_screen_length)
992 return '' # Empty string is needed here!
964 return '' # Empty string is needed here!
993 except:
965 except:
994 raise
966 raise
995 # Pass any other exceptions through to the normal handler
967 # Pass any other exceptions through to the normal handler
996 return normal_handler.handle(line_info)
968 return normal_handler.handle(line_info)
997 else:
969 else:
998 # If the code compiles ok, we should handle it normally
970 # If the code compiles ok, we should handle it normally
999 return normal_handler.handle(line_info)
971 return normal_handler.handle(line_info)
1000
972
1001
973
1002 class EmacsHandler(PrefilterHandler):
974 class EmacsHandler(PrefilterHandler):
1003
975
1004 handler_name = Str('emacs')
976 handler_name = Str('emacs')
1005 esc_strings = List([])
977 esc_strings = List([])
1006
978
1007 def handle(self, line_info):
979 def handle(self, line_info):
1008 """Handle input lines marked by python-mode."""
980 """Handle input lines marked by python-mode."""
1009
981
1010 # Currently, nothing is done. Later more functionality can be added
982 # Currently, nothing is done. Later more functionality can be added
1011 # here if needed.
983 # here if needed.
1012
984
1013 # The input cache shouldn't be updated
985 # The input cache shouldn't be updated
1014 return line_info.line
986 return line_info.line
1015
987
1016
988
1017 #-----------------------------------------------------------------------------
989 #-----------------------------------------------------------------------------
1018 # Defaults
990 # Defaults
1019 #-----------------------------------------------------------------------------
991 #-----------------------------------------------------------------------------
1020
992
1021
993
1022 _default_transformers = [
994 _default_transformers = [
1023 AssignSystemTransformer,
995 AssignSystemTransformer,
1024 AssignMagicTransformer,
996 AssignMagicTransformer,
1025 PyPromptTransformer,
997 PyPromptTransformer,
1026 IPyPromptTransformer,
998 IPyPromptTransformer,
1027 ]
999 ]
1028
1000
1029 _default_checkers = [
1001 _default_checkers = [
1030 EmacsChecker,
1002 EmacsChecker,
1031 ShellEscapeChecker,
1003 ShellEscapeChecker,
1032 IPyAutocallChecker,
1004 IPyAutocallChecker,
1033 MultiLineMagicChecker,
1005 MultiLineMagicChecker,
1034 EscCharsChecker,
1006 EscCharsChecker,
1035 AssignmentChecker,
1007 AssignmentChecker,
1036 AutoMagicChecker,
1008 AutoMagicChecker,
1037 AliasChecker,
1009 AliasChecker,
1038 PythonOpsChecker,
1010 PythonOpsChecker,
1039 AutocallChecker
1011 AutocallChecker
1040 ]
1012 ]
1041
1013
1042 _default_handlers = [
1014 _default_handlers = [
1043 PrefilterHandler,
1015 PrefilterHandler,
1044 AliasHandler,
1016 AliasHandler,
1045 ShellEscapeHandler,
1017 ShellEscapeHandler,
1046 MagicHandler,
1018 MagicHandler,
1047 AutoHandler,
1019 AutoHandler,
1048 HelpHandler,
1020 HelpHandler,
1049 EmacsHandler
1021 EmacsHandler
1050 ]
1022 ]
@@ -1,41 +1,47 b''
1 """Minimal script to reproduce our nasty reference counting bug.
1 """Minimal script to reproduce our nasty reference counting bug.
2
2
3 The problem is related to https://bugs.launchpad.net/ipython/+bug/269966
3 The problem is related to https://bugs.launchpad.net/ipython/+bug/269966
4
4
5 The original fix for that appeared to work, but John D. Hunter found a
5 The original fix for that appeared to work, but John D. Hunter found a
6 matplotlib example which, when run twice in a row, would break. The problem
6 matplotlib example which, when run twice in a row, would break. The problem
7 were references held by open figures to internals of Tkinter.
7 were references held by open figures to internals of Tkinter.
8
8
9 This code reproduces the problem that John saw, without matplotlib.
9 This code reproduces the problem that John saw, without matplotlib.
10
10
11 This script is meant to be called by other parts of the test suite that call it
11 This script is meant to be called by other parts of the test suite that call it
12 via %run as if it were executed interactively by the user. As of 2009-04-13,
12 via %run as if it were executed interactively by the user. As of 2009-04-13,
13 test_magic.py calls it.
13 test_magic.py calls it.
14 """
14 """
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Module imports
17 # Module imports
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 import sys
19 import sys
20
20
21 from IPython.core import ipapi
21 from IPython.core import ipapi
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Globals
24 # Globals
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
27 # This needs to be here because nose and other test runners will import
28 # this module. Importing this module has potential side effects that we
29 # want to prevent.
30 if __name__ == '__main__':
31
26 ip = ipapi.get()
32 ip = ipapi.get()
27
33
28 if not '_refbug_cache' in ip.user_ns:
34 if not '_refbug_cache' in ip.user_ns:
29 ip.user_ns['_refbug_cache'] = []
35 ip.user_ns['_refbug_cache'] = []
30
36
31
37
32 aglobal = 'Hello'
38 aglobal = 'Hello'
33 def f():
39 def f():
34 return aglobal
40 return aglobal
35
41
36 cache = ip.user_ns['_refbug_cache']
42 cache = ip.user_ns['_refbug_cache']
37 cache.append(f)
43 cache.append(f)
38
44
39 def call_f():
45 def call_f():
40 for func in cache:
46 for func in cache:
41 print 'lowercased:',func().lower()
47 print 'lowercased:',func().lower()
@@ -1,13 +1,2 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """This directory is meant for special-purpose extensions to IPython.
2 """This directory is meant for IPython extensions."""
3
4 This can include things which alter the syntax processing stage (see
5 PhysicalQ_Input for an example of how to do this).
6
7 Any file located here can be called with an 'execfile =' option as
8
9 execfile = extensions/filename.py
10
11 since the IPython directory itself is already part of the search path for
12 files listed as 'execfile ='.
13 """
@@ -1,209 +1,201 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3
3
4 """Magic command interface for interactive parallel work."""
4 """Magic command interface for interactive parallel work."""
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
8 #
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 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import new
17 import new
18
18
19 from IPython.core.component import Component
19 from IPython.core.plugin import Plugin
20 from IPython.utils.traitlets import Bool, Any
20 from IPython.utils.traitlets import Bool, Any, Instance
21 from IPython.utils.autoattr import auto_attr
21 from IPython.utils.autoattr import auto_attr
22 from IPython.testing import decorators as testdec
22 from IPython.testing import decorators as testdec
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Definitions of magic functions for use with IPython
25 # Definitions of magic functions for use with IPython
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28
28
29 NO_ACTIVE_MULTIENGINE_CLIENT = """
29 NO_ACTIVE_MULTIENGINE_CLIENT = """
30 Use activate() on a MultiEngineClient object to activate it for magics.
30 Use activate() on a MultiEngineClient object to activate it for magics.
31 """
31 """
32
32
33
33
34 class ParalleMagicComponent(Component):
34 class ParalleMagic(Plugin):
35 """A component to manage the %result, %px and %autopx magics."""
35 """A component to manage the %result, %px and %autopx magics."""
36
36
37 active_multiengine_client = Any()
37 active_multiengine_client = Any()
38 verbose = Bool(False, config=True)
38 verbose = Bool(False, config=True)
39 shell = Instance('IPython.core.iplib.InteractiveShellABC')
39
40
40 def __init__(self, parent, name=None, config=None):
41 def __init__(self, shell=None, config=None):
41 super(ParalleMagicComponent, self).__init__(parent, name=name, config=config)
42 super(ParalleMagic, self).__init__(shell=shell, config=config)
42 self._define_magics()
43 self._define_magics()
43 # A flag showing if autopx is activated or not
44 # A flag showing if autopx is activated or not
44 self.autopx = False
45 self.autopx = False
45
46
46 # Access other components like this rather than by a regular attribute.
47 # This won't lookup the InteractiveShell object until it is used and
48 # then it is cached. This is both efficient and couples this class
49 # more loosely to InteractiveShell.
50 @auto_attr
51 def shell(self):
52 return Component.get_instances(
53 root=self.root,
54 klass='IPython.core.iplib.InteractiveShell')[0]
55
56 def _define_magics(self):
47 def _define_magics(self):
57 """Define the magic functions."""
48 """Define the magic functions."""
58 self.shell.define_magic('result', self.magic_result)
49 self.shell.define_magic('result', self.magic_result)
59 self.shell.define_magic('px', self.magic_px)
50 self.shell.define_magic('px', self.magic_px)
60 self.shell.define_magic('autopx', self.magic_autopx)
51 self.shell.define_magic('autopx', self.magic_autopx)
61
52
62 @testdec.skip_doctest
53 @testdec.skip_doctest
63 def magic_result(self, ipself, parameter_s=''):
54 def magic_result(self, ipself, parameter_s=''):
64 """Print the result of command i on all engines..
55 """Print the result of command i on all engines..
65
56
66 To use this a :class:`MultiEngineClient` instance must be created
57 To use this a :class:`MultiEngineClient` instance must be created
67 and then activated by calling its :meth:`activate` method.
58 and then activated by calling its :meth:`activate` method.
68
59
69 Then you can do the following::
60 Then you can do the following::
70
61
71 In [23]: %result
62 In [23]: %result
72 Out[23]:
63 Out[23]:
73 <Results List>
64 <Results List>
74 [0] In [6]: a = 10
65 [0] In [6]: a = 10
75 [1] In [6]: a = 10
66 [1] In [6]: a = 10
76
67
77 In [22]: %result 6
68 In [22]: %result 6
78 Out[22]:
69 Out[22]:
79 <Results List>
70 <Results List>
80 [0] In [6]: a = 10
71 [0] In [6]: a = 10
81 [1] In [6]: a = 10
72 [1] In [6]: a = 10
82 """
73 """
83 if self.active_multiengine_client is None:
74 if self.active_multiengine_client is None:
84 print NO_ACTIVE_MULTIENGINE_CLIENT
75 print NO_ACTIVE_MULTIENGINE_CLIENT
85 return
76 return
86
77
87 try:
78 try:
88 index = int(parameter_s)
79 index = int(parameter_s)
89 except:
80 except:
90 index = None
81 index = None
91 result = self.active_multiengine_client.get_result(index)
82 result = self.active_multiengine_client.get_result(index)
92 return result
83 return result
93
84
94 @testdec.skip_doctest
85 @testdec.skip_doctest
95 def magic_px(self, ipself, parameter_s=''):
86 def magic_px(self, ipself, parameter_s=''):
96 """Executes the given python command in parallel.
87 """Executes the given python command in parallel.
97
88
98 To use this a :class:`MultiEngineClient` instance must be created
89 To use this a :class:`MultiEngineClient` instance must be created
99 and then activated by calling its :meth:`activate` method.
90 and then activated by calling its :meth:`activate` method.
100
91
101 Then you can do the following::
92 Then you can do the following::
102
93
103 In [24]: %px a = 5
94 In [24]: %px a = 5
104 Parallel execution on engines: all
95 Parallel execution on engines: all
105 Out[24]:
96 Out[24]:
106 <Results List>
97 <Results List>
107 [0] In [7]: a = 5
98 [0] In [7]: a = 5
108 [1] In [7]: a = 5
99 [1] In [7]: a = 5
109 """
100 """
110
101
111 if self.active_multiengine_client is None:
102 if self.active_multiengine_client is None:
112 print NO_ACTIVE_MULTIENGINE_CLIENT
103 print NO_ACTIVE_MULTIENGINE_CLIENT
113 return
104 return
114 print "Parallel execution on engines: %s" % self.active_multiengine_client.targets
105 print "Parallel execution on engines: %s" % self.active_multiengine_client.targets
115 result = self.active_multiengine_client.execute(parameter_s)
106 result = self.active_multiengine_client.execute(parameter_s)
116 return result
107 return result
117
108
118 @testdec.skip_doctest
109 @testdec.skip_doctest
119 def magic_autopx(self, ipself, parameter_s=''):
110 def magic_autopx(self, ipself, parameter_s=''):
120 """Toggles auto parallel mode.
111 """Toggles auto parallel mode.
121
112
122 To use this a :class:`MultiEngineClient` instance must be created
113 To use this a :class:`MultiEngineClient` instance must be created
123 and then activated by calling its :meth:`activate` method. Once this
114 and then activated by calling its :meth:`activate` method. Once this
124 is called, all commands typed at the command line are send to
115 is called, all commands typed at the command line are send to
125 the engines to be executed in parallel. To control which engine
116 the engines to be executed in parallel. To control which engine
126 are used, set the ``targets`` attributed of the multiengine client
117 are used, set the ``targets`` attributed of the multiengine client
127 before entering ``%autopx`` mode.
118 before entering ``%autopx`` mode.
128
119
129 Then you can do the following::
120 Then you can do the following::
130
121
131 In [25]: %autopx
122 In [25]: %autopx
132 %autopx to enabled
123 %autopx to enabled
133
124
134 In [26]: a = 10
125 In [26]: a = 10
135 <Results List>
126 <Results List>
136 [0] In [8]: a = 10
127 [0] In [8]: a = 10
137 [1] In [8]: a = 10
128 [1] In [8]: a = 10
138
129
139
130
140 In [27]: %autopx
131 In [27]: %autopx
141 %autopx disabled
132 %autopx disabled
142 """
133 """
143 if self.autopx:
134 if self.autopx:
144 self._disable_autopx()
135 self._disable_autopx()
145 else:
136 else:
146 self._enable_autopx()
137 self._enable_autopx()
147
138
148 def _enable_autopx(self):
139 def _enable_autopx(self):
149 """Enable %autopx mode by saving the original runsource and installing
140 """Enable %autopx mode by saving the original runsource and installing
150 pxrunsource.
141 pxrunsource.
151 """
142 """
152 if self.active_multiengine_client is None:
143 if self.active_multiengine_client is None:
153 print NO_ACTIVE_MULTIENGINE_CLIENT
144 print NO_ACTIVE_MULTIENGINE_CLIENT
154 return
145 return
155
146
156 self._original_runsource = self.shell.runsource
147 self._original_runsource = self.shell.runsource
157 self.shell.runsource = new.instancemethod(
148 self.shell.runsource = new.instancemethod(
158 self.pxrunsource, self.shell, self.shell.__class__
149 self.pxrunsource, self.shell, self.shell.__class__
159 )
150 )
160 self.autopx = True
151 self.autopx = True
161 print "%autopx enabled"
152 print "%autopx enabled"
162
153
163 def _disable_autopx(self):
154 def _disable_autopx(self):
164 """Disable %autopx by restoring the original InteractiveShell.runsource."""
155 """Disable %autopx by restoring the original InteractiveShell.runsource."""
165 if self.autopx:
156 if self.autopx:
166 self.shell.runsource = self._original_runsource
157 self.shell.runsource = self._original_runsource
167 self.autopx = False
158 self.autopx = False
168 print "%autopx disabled"
159 print "%autopx disabled"
169
160
170 def pxrunsource(self, ipself, source, filename="<input>", symbol="single"):
161 def pxrunsource(self, ipself, source, filename="<input>", symbol="single"):
171 """A parallel replacement for InteractiveShell.runsource."""
162 """A parallel replacement for InteractiveShell.runsource."""
172
163
173 try:
164 try:
174 code = ipself.compile(source, filename, symbol)
165 code = ipself.compile(source, filename, symbol)
175 except (OverflowError, SyntaxError, ValueError):
166 except (OverflowError, SyntaxError, ValueError):
176 # Case 1
167 # Case 1
177 ipself.showsyntaxerror(filename)
168 ipself.showsyntaxerror(filename)
178 return None
169 return None
179
170
180 if code is None:
171 if code is None:
181 # Case 2
172 # Case 2
182 return True
173 return True
183
174
184 # Case 3
175 # Case 3
185 # Because autopx is enabled, we now call executeAll or disable autopx if
176 # Because autopx is enabled, we now call executeAll or disable autopx if
186 # %autopx or autopx has been called
177 # %autopx or autopx has been called
187 if 'get_ipython().magic("%autopx' in source or 'get_ipython().magic("autopx' in source:
178 if 'get_ipython().magic("%autopx' in source or 'get_ipython().magic("autopx' in source:
188 self._disable_autopx()
179 self._disable_autopx()
189 return False
180 return False
190 else:
181 else:
191 try:
182 try:
192 result = self.active_multiengine_client.execute(source)
183 result = self.active_multiengine_client.execute(source)
193 except:
184 except:
194 ipself.showtraceback()
185 ipself.showtraceback()
195 else:
186 else:
196 print result.__repr__()
187 print result.__repr__()
197 return False
188 return False
198
189
199
190
200 _loaded = False
191 _loaded = False
201
192
202
193
203 def load_ipython_extension(ip):
194 def load_ipython_extension(ip):
204 """Load the extension in IPython."""
195 """Load the extension in IPython."""
205 global _loaded
196 global _loaded
206 if not _loaded:
197 if not _loaded:
207 prd = ParalleMagicComponent(ip, name='parallel_magic')
198 plugin = ParalleMagic(shell=ip, config=ip.config)
199 ip.plugin_manager.register_plugin('parallel_magic', plugin)
208 _loaded = True
200 _loaded = True
209
201
@@ -1,166 +1,157 b''
1 """Use pretty.py for configurable pretty-printing.
1 """Use pretty.py for configurable pretty-printing.
2
2
3 To enable this extension in your configuration
3 To enable this extension in your configuration
4 file, add the following to :file:`ipython_config.py`::
4 file, add the following to :file:`ipython_config.py`::
5
5
6 c.Global.extensions = ['IPython.extensions.pretty']
6 c.Global.extensions = ['IPython.extensions.pretty']
7 def dict_pprinter(obj, p, cycle):
7 def dict_pprinter(obj, p, cycle):
8 return p.text("<dict>")
8 return p.text("<dict>")
9 c.PrettyResultDisplay.verbose = True
9 c.PrettyResultDisplay.verbose = True
10 c.PrettyResultDisplay.defaults_for_type = [
10 c.PrettyResultDisplay.defaults_for_type = [
11 (dict, dict_pprinter)
11 (dict, dict_pprinter)
12 ]
12 ]
13 c.PrettyResultDisplay.defaults_for_type_by_name = [
13 c.PrettyResultDisplay.defaults_for_type_by_name = [
14 ('numpy', 'dtype', 'IPython.extensions.pretty.dtype_pprinter')
14 ('numpy', 'dtype', 'IPython.extensions.pretty.dtype_pprinter')
15 ]
15 ]
16
16
17 This extension can also be loaded by using the ``%load_ext`` magic::
17 This extension can also be loaded by using the ``%load_ext`` magic::
18
18
19 %load_ext IPython.extensions.pretty
19 %load_ext IPython.extensions.pretty
20
20
21 If this extension is enabled, you can always add additional pretty printers
21 If this extension is enabled, you can always add additional pretty printers
22 by doing::
22 by doing::
23
23
24 ip = get_ipython()
24 ip = get_ipython()
25 prd = ip.get_component('pretty_result_display')
25 prd = ip.get_component('pretty_result_display')
26 import numpy
26 import numpy
27 from IPython.extensions.pretty import dtype_pprinter
27 from IPython.extensions.pretty import dtype_pprinter
28 prd.for_type(numpy.dtype, dtype_pprinter)
28 prd.for_type(numpy.dtype, dtype_pprinter)
29
29
30 # If you don't want to have numpy imported until it needs to be:
30 # If you don't want to have numpy imported until it needs to be:
31 prd.for_type_by_name('numpy', 'dtype', dtype_pprinter)
31 prd.for_type_by_name('numpy', 'dtype', dtype_pprinter)
32 """
32 """
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Imports
35 # Imports
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38 from IPython.core.error import TryNext
38 from IPython.core.error import TryNext
39 from IPython.external import pretty
39 from IPython.external import pretty
40 from IPython.core.component import Component
40 from IPython.core.plugin import Plugin
41 from IPython.utils.traitlets import Bool, List
41 from IPython.utils.traitlets import Bool, List, Instance
42 from IPython.utils.io import Term
42 from IPython.utils.io import Term
43 from IPython.utils.autoattr import auto_attr
43 from IPython.utils.autoattr import auto_attr
44 from IPython.utils.importstring import import_item
44 from IPython.utils.importstring import import_item
45
45
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47 # Code
47 # Code
48 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
49
49
50
50
51 _loaded = False
51 _loaded = False
52
52
53
53
54 class PrettyResultDisplay(Component):
54 class PrettyResultDisplay(Plugin):
55 """A component for pretty printing on steroids."""
55 """A component for pretty printing on steroids."""
56
56
57 verbose = Bool(False, config=True)
57 verbose = Bool(False, config=True)
58 shell = Instance('IPython.core.iplib.InteractiveShellABC')
58
59
59 # A list of (type, func_name), like
60 # A list of (type, func_name), like
60 # [(dict, 'my_dict_printer')]
61 # [(dict, 'my_dict_printer')]
61 # The final argument can also be a callable
62 # The final argument can also be a callable
62 defaults_for_type = List(default_value=[], config=True)
63 defaults_for_type = List(default_value=[], config=True)
63
64
64 # A list of (module_name, type_name, func_name), like
65 # A list of (module_name, type_name, func_name), like
65 # [('numpy', 'dtype', 'IPython.extensions.pretty.dtype_pprinter')]
66 # [('numpy', 'dtype', 'IPython.extensions.pretty.dtype_pprinter')]
66 # The final argument can also be a callable
67 # The final argument can also be a callable
67 defaults_for_type_by_name = List(default_value=[], config=True)
68 defaults_for_type_by_name = List(default_value=[], config=True)
68
69
69 def __init__(self, parent, name=None, config=None):
70 def __init__(self, shell=None, config=None):
70 super(PrettyResultDisplay, self).__init__(parent, name=name, config=config)
71 super(PrettyResultDisplay, self).__init__(shell=shell, config=config)
71 self._setup_defaults()
72 self._setup_defaults()
72
73
73 def _setup_defaults(self):
74 def _setup_defaults(self):
74 """Initialize the default pretty printers."""
75 """Initialize the default pretty printers."""
75 for typ, func_name in self.defaults_for_type:
76 for typ, func_name in self.defaults_for_type:
76 func = self._resolve_func_name(func_name)
77 func = self._resolve_func_name(func_name)
77 self.for_type(typ, func)
78 self.for_type(typ, func)
78 for type_module, type_name, func_name in self.defaults_for_type_by_name:
79 for type_module, type_name, func_name in self.defaults_for_type_by_name:
79 func = self._resolve_func_name(func_name)
80 func = self._resolve_func_name(func_name)
80 self.for_type_by_name(type_module, type_name, func)
81 self.for_type_by_name(type_module, type_name, func)
81
82
82 def _resolve_func_name(self, func_name):
83 def _resolve_func_name(self, func_name):
83 if callable(func_name):
84 if callable(func_name):
84 return func_name
85 return func_name
85 elif isinstance(func_name, basestring):
86 elif isinstance(func_name, basestring):
86 return import_item(func_name)
87 return import_item(func_name)
87 else:
88 else:
88 raise TypeError('func_name must be a str or callable, got: %r' % func_name)
89 raise TypeError('func_name must be a str or callable, got: %r' % func_name)
89
90
90 # Access other components like this rather than by a regular attribute.
91 # This won't lookup the InteractiveShell object until it is used and
92 # then it is cached. This is both efficient and couples this class
93 # more loosely to InteractiveShell.
94 @auto_attr
95 def shell(self):
96 return Component.get_instances(
97 root=self.root,
98 klass='IPython.core.iplib.InteractiveShell')[0]
99
100 def __call__(self, otherself, arg):
91 def __call__(self, otherself, arg):
101 """Uber-pretty-printing display hook.
92 """Uber-pretty-printing display hook.
102
93
103 Called for displaying the result to the user.
94 Called for displaying the result to the user.
104 """
95 """
105
96
106 if self.shell.pprint:
97 if self.shell.pprint:
107 out = pretty.pretty(arg, verbose=self.verbose)
98 out = pretty.pretty(arg, verbose=self.verbose)
108 if '\n' in out:
99 if '\n' in out:
109 # So that multi-line strings line up with the left column of
100 # So that multi-line strings line up with the left column of
110 # the screen, instead of having the output prompt mess up
101 # the screen, instead of having the output prompt mess up
111 # their first line.
102 # their first line.
112 Term.cout.write('\n')
103 Term.cout.write('\n')
113 print >>Term.cout, out
104 print >>Term.cout, out
114 else:
105 else:
115 raise TryNext
106 raise TryNext
116
107
117 def for_type(self, typ, func):
108 def for_type(self, typ, func):
118 """Add a pretty printer for a type."""
109 """Add a pretty printer for a type."""
119 return pretty.for_type(typ, func)
110 return pretty.for_type(typ, func)
120
111
121 def for_type_by_name(self, type_module, type_name, func):
112 def for_type_by_name(self, type_module, type_name, func):
122 """Add a pretty printer for a type by its name and module name."""
113 """Add a pretty printer for a type by its name and module name."""
123 return pretty.for_type_by_name(type_module, type_name, func)
114 return pretty.for_type_by_name(type_module, type_name, func)
124
115
125
116
126 #-----------------------------------------------------------------------------
117 #-----------------------------------------------------------------------------
127 # Initialization code for the extension
118 # Initialization code for the extension
128 #-----------------------------------------------------------------------------
119 #-----------------------------------------------------------------------------
129
120
130
121
131 def load_ipython_extension(ip):
122 def load_ipython_extension(ip):
132 """Load the extension in IPython as a hook."""
123 """Load the extension in IPython as a hook."""
133 global _loaded
124 global _loaded
134 if not _loaded:
125 if not _loaded:
135 prd = PrettyResultDisplay(ip, name='pretty_result_display')
126 plugin = PrettyResultDisplay(shell=ip, config=ip.config)
136 ip.set_hook('result_display', prd, priority=99)
127 ip.set_hook('result_display', plugin, priority=99)
137 _loaded = True
128 _loaded = True
138 return prd
129 ip.plugin_manager.register_plugin('pretty_result_display', plugin)
139
130
140 def unload_ipython_extension(ip):
131 def unload_ipython_extension(ip):
141 """Unload the extension."""
132 """Unload the extension."""
142 # The hook system does not have a way to remove a hook so this is a pass
133 # The hook system does not have a way to remove a hook so this is a pass
143 pass
134 pass
144
135
145
136
146 #-----------------------------------------------------------------------------
137 #-----------------------------------------------------------------------------
147 # Example pretty printers
138 # Example pretty printers
148 #-----------------------------------------------------------------------------
139 #-----------------------------------------------------------------------------
149
140
150
141
151 def dtype_pprinter(obj, p, cycle):
142 def dtype_pprinter(obj, p, cycle):
152 """ A pretty-printer for numpy dtype objects.
143 """ A pretty-printer for numpy dtype objects.
153 """
144 """
154 if cycle:
145 if cycle:
155 return p.text('dtype(...)')
146 return p.text('dtype(...)')
156 if hasattr(obj, 'fields'):
147 if hasattr(obj, 'fields'):
157 if obj.fields is None:
148 if obj.fields is None:
158 p.text(repr(obj))
149 p.text(repr(obj))
159 else:
150 else:
160 p.begin_group(7, 'dtype([')
151 p.begin_group(7, 'dtype([')
161 for i, field in enumerate(obj.descr):
152 for i, field in enumerate(obj.descr):
162 if i > 0:
153 if i > 0:
163 p.text(',')
154 p.text(',')
164 p.breakable()
155 p.breakable()
165 p.pretty(field)
156 p.pretty(field)
166 p.end_group(7, '])')
157 p.end_group(7, '])')
@@ -1,101 +1,100 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Simple tests for :mod:`IPython.extensions.pretty`.
4 Simple tests for :mod:`IPython.extensions.pretty`.
5 """
5 """
6
6
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2008-2009 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
17
18 from unittest import TestCase
18 from unittest import TestCase
19
19
20 from IPython.core.component import Component, masquerade_as
20 from IPython.config.configurable import Configurable
21 from IPython.core.iplib import InteractiveShell
21 from IPython.core.iplib import InteractiveShellABC
22 from IPython.extensions import pretty as pretty_ext
22 from IPython.extensions import pretty as pretty_ext
23 from IPython.external import pretty
23 from IPython.external import pretty
24 from IPython.testing import decorators as dec
24 from IPython.testing import decorators as dec
25 from IPython.testing import tools as tt
25 from IPython.testing import tools as tt
26 from IPython.utils.traitlets import Bool
26 from IPython.utils.traitlets import Bool
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Tests
29 # Tests
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 class InteractiveShellStub(Component):
32 class InteractiveShellStub(Configurable):
33 pprint = Bool(True)
33 pprint = Bool(True)
34
34
35 InteractiveShellABC.register(InteractiveShellStub)
36
35 class A(object):
37 class A(object):
36 pass
38 pass
37
39
38 def a_pprinter(o, p, c):
40 def a_pprinter(o, p, c):
39 return p.text("<A>")
41 return p.text("<A>")
40
42
41 class TestPrettyResultDisplay(TestCase):
43 class TestPrettyResultDisplay(TestCase):
42
44
43 def setUp(self):
45 def setUp(self):
44 self.ip = InteractiveShellStub(None)
46 self.ip = InteractiveShellStub()
45 # This allows our stub to be retrieved instead of the real
47 self.prd = pretty_ext.PrettyResultDisplay(shell=self.ip, config=None)
46 # InteractiveShell
47 masquerade_as(self.ip, InteractiveShell)
48 self.prd = pretty_ext.PrettyResultDisplay(self.ip,
49 name='pretty_result_display')
50
48
51 def test_for_type(self):
49 def test_for_type(self):
52 self.prd.for_type(A, a_pprinter)
50 self.prd.for_type(A, a_pprinter)
53 a = A()
51 a = A()
54 result = pretty.pretty(a)
52 result = pretty.pretty(a)
55 self.assertEquals(result, "<A>")
53 self.assertEquals(result, "<A>")
56
54
57 ipy_src = """
55 ipy_src = """
58 class A(object):
56 class A(object):
59 def __repr__(self):
57 def __repr__(self):
60 return 'A()'
58 return 'A()'
61
59
62 class B(object):
60 class B(object):
63 def __repr__(self):
61 def __repr__(self):
64 return 'B()'
62 return 'B()'
65
63
66 a = A()
64 a = A()
67 b = B()
65 b = B()
68
66
69 def a_pretty_printer(obj, p, cycle):
67 def a_pretty_printer(obj, p, cycle):
70 p.text('<A>')
68 p.text('<A>')
71
69
72 def b_pretty_printer(obj, p, cycle):
70 def b_pretty_printer(obj, p, cycle):
73 p.text('<B>')
71 p.text('<B>')
74
72
75
73
76 a
74 a
77 b
75 b
78
76
79 ip = get_ipython()
77 ip = get_ipython()
80 prd = ip.load_extension('pretty')
78 ip.extension_manager.load_extension('pretty')
79 prd = ip.plugin_manager.get_plugin('pretty_result_display')
81 prd.for_type(A, a_pretty_printer)
80 prd.for_type(A, a_pretty_printer)
82 prd.for_type_by_name(B.__module__, B.__name__, b_pretty_printer)
81 prd.for_type_by_name(B.__module__, B.__name__, b_pretty_printer)
83
82
84 a
83 a
85 b
84 b
86 """
85 """
87 ipy_out = """
86 ipy_out = """
88 A()
87 A()
89 B()
88 B()
90 <A>
89 <A>
91 <B>
90 <B>
92 """
91 """
93
92
94 class TestPrettyInteractively(tt.TempFileMixin):
93 class TestPrettyInteractively(tt.TempFileMixin):
95
94
96 # XXX Unfortunately, ipexec_validate fails under win32. If someone helps
95 # XXX Unfortunately, ipexec_validate fails under win32. If someone helps
97 # us write a win32-compatible version, we can reactivate this test.
96 # us write a win32-compatible version, we can reactivate this test.
98 @dec.skip_win32
97 @dec.skip_win32
99 def test_printers(self):
98 def test_printers(self):
100 self.mktmp(ipy_src, '.ipy')
99 self.mktmp(ipy_src, '.ipy')
101 tt.ipexec_validate(self.fname, ipy_out)
100 tt.ipexec_validate(self.fname, ipy_out)
@@ -1,171 +1,184 b''
1 """ Defines a KernelManager that provides signals and slots.
1 """ Defines a KernelManager that provides signals and slots.
2 """
2 """
3
3
4 # System library imports.
4 # System library imports.
5 from PyQt4 import QtCore
5 from PyQt4 import QtCore
6 import zmq
6 import zmq
7
7
8 # IPython imports.
8 # IPython imports.
9 from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
9 from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
10 XReqSocketChannel, RepSocketChannel
10 XReqSocketChannel, RepSocketChannel
11 from util import MetaQObjectHasTraits
11 from util import MetaQObjectHasTraits
12
12
13 # When doing multiple inheritance from QtCore.QObject and other classes
14 # the calling of the parent __init__'s is a subtle issue:
15 # * QtCore.QObject does not call super so you can't use super and put
16 # QObject first in the inheritance list.
17 # * QtCore.QObject.__init__ takes 1 argument, the parent. So if you are going
18 # to use super, any class that comes before QObject must pass it something
19 # reasonable.
20 # In summary, I don't think using super in these situations will work.
21 # Instead we will need to call the __init__ methods of both parents
22 # by hand. Not pretty, but it works.
13
23
14 class QtSubSocketChannel(SubSocketChannel, QtCore.QObject):
24 class QtSubSocketChannel(SubSocketChannel, QtCore.QObject):
15
25
16 # Emitted when any message is received.
26 # Emitted when any message is received.
17 message_received = QtCore.pyqtSignal(object)
27 message_received = QtCore.pyqtSignal(object)
18
28
19 # Emitted when a message of type 'pyout' or 'stdout' is received.
29 # Emitted when a message of type 'pyout' or 'stdout' is received.
20 output_received = QtCore.pyqtSignal(object)
30 output_received = QtCore.pyqtSignal(object)
21
31
22 # Emitted when a message of type 'pyerr' or 'stderr' is received.
32 # Emitted when a message of type 'pyerr' or 'stderr' is received.
23 error_received = QtCore.pyqtSignal(object)
33 error_received = QtCore.pyqtSignal(object)
24
34
25 #---------------------------------------------------------------------------
35 #---------------------------------------------------------------------------
26 # 'object' interface
36 # 'object' interface
27 #---------------------------------------------------------------------------
37 #---------------------------------------------------------------------------
28
38
29 def __init__(self, *args, **kw):
39 def __init__(self, *args, **kw):
30 """ Reimplemented to ensure that QtCore.QObject is initialized first.
40 """ Reimplemented to ensure that QtCore.QObject is initialized first.
31 """
41 """
32 QtCore.QObject.__init__(self)
42 QtCore.QObject.__init__(self)
33 SubSocketChannel.__init__(self, *args, **kw)
43 SubSocketChannel.__init__(self, *args, **kw)
34
44
35 #---------------------------------------------------------------------------
45 #---------------------------------------------------------------------------
36 # 'SubSocketChannel' interface
46 # 'SubSocketChannel' interface
37 #---------------------------------------------------------------------------
47 #---------------------------------------------------------------------------
38
48
39 def call_handlers(self, msg):
49 def call_handlers(self, msg):
40 """ Reimplemented to emit signals instead of making callbacks.
50 """ Reimplemented to emit signals instead of making callbacks.
41 """
51 """
42 # Emit the generic signal.
52 # Emit the generic signal.
43 self.message_received.emit(msg)
53 self.message_received.emit(msg)
44
54
45 # Emit signals for specialized message types.
55 # Emit signals for specialized message types.
46 msg_type = msg['msg_type']
56 msg_type = msg['msg_type']
47 if msg_type in ('pyout', 'stdout'):
57 if msg_type in ('pyout', 'stdout'):
48 self.output_received.emit(msg)
58 self.output_received.emit(msg)
49 elif msg_type in ('pyerr', 'stderr'):
59 elif msg_type in ('pyerr', 'stderr'):
50 self.error_received.emit(msg)
60 self.error_received.emit(msg)
51
61
52 def flush(self):
62 def flush(self):
53 """ Reimplemented to ensure that signals are dispatched immediately.
63 """ Reimplemented to ensure that signals are dispatched immediately.
54 """
64 """
55 super(QtSubSocketChannel, self).flush()
65 super(QtSubSocketChannel, self).flush()
56 QtCore.QCoreApplication.instance().processEvents()
66 QtCore.QCoreApplication.instance().processEvents()
57
67
58
68
59 class QtXReqSocketChannel(XReqSocketChannel, QtCore.QObject):
69 class QtXReqSocketChannel(XReqSocketChannel, QtCore.QObject):
60
70
61 # Emitted when any message is received.
71 # Emitted when any message is received.
62 message_received = QtCore.pyqtSignal(object)
72 message_received = QtCore.pyqtSignal(object)
63
73
64 # Emitted when a reply has been received for the corresponding request type.
74 # Emitted when a reply has been received for the corresponding request type.
65 execute_reply = QtCore.pyqtSignal(object)
75 execute_reply = QtCore.pyqtSignal(object)
66 complete_reply = QtCore.pyqtSignal(object)
76 complete_reply = QtCore.pyqtSignal(object)
67 object_info_reply = QtCore.pyqtSignal(object)
77 object_info_reply = QtCore.pyqtSignal(object)
68
78
69 #---------------------------------------------------------------------------
79 #---------------------------------------------------------------------------
70 # 'object' interface
80 # 'object' interface
71 #---------------------------------------------------------------------------
81 #---------------------------------------------------------------------------
72
82
73 def __init__(self, *args, **kw):
83 def __init__(self, *args, **kw):
74 """ Reimplemented to ensure that QtCore.QObject is initialized first.
84 """ Reimplemented to ensure that QtCore.QObject is initialized first.
75 """
85 """
76 QtCore.QObject.__init__(self)
86 QtCore.QObject.__init__(self)
77 XReqSocketChannel.__init__(self, *args, **kw)
87 XReqSocketChannel.__init__(self, *args, **kw)
78
88
79 #---------------------------------------------------------------------------
89 #---------------------------------------------------------------------------
80 # 'XReqSocketChannel' interface
90 # 'XReqSocketChannel' interface
81 #---------------------------------------------------------------------------
91 #---------------------------------------------------------------------------
82
92
83 def call_handlers(self, msg):
93 def call_handlers(self, msg):
84 """ Reimplemented to emit signals instead of making callbacks.
94 """ Reimplemented to emit signals instead of making callbacks.
85 """
95 """
86 # Emit the generic signal.
96 # Emit the generic signal.
87 self.message_received.emit(msg)
97 self.message_received.emit(msg)
88
98
89 # Emit signals for specialized message types.
99 # Emit signals for specialized message types.
90 msg_type = msg['msg_type']
100 msg_type = msg['msg_type']
91 signal = getattr(self, msg_type, None)
101 signal = getattr(self, msg_type, None)
92 if signal:
102 if signal:
93 signal.emit(msg)
103 signal.emit(msg)
94
104
95
105
96 class QtRepSocketChannel(RepSocketChannel, QtCore.QObject):
106 class QtRepSocketChannel(RepSocketChannel, QtCore.QObject):
97
107
98 # Emitted when any message is received.
108 # Emitted when any message is received.
99 message_received = QtCore.pyqtSignal(object)
109 message_received = QtCore.pyqtSignal(object)
100
110
101 # Emitted when an input request is received.
111 # Emitted when an input request is received.
102 input_requested = QtCore.pyqtSignal(object)
112 input_requested = QtCore.pyqtSignal(object)
103
113
104 #---------------------------------------------------------------------------
114 #---------------------------------------------------------------------------
105 # 'object' interface
115 # 'object' interface
106 #---------------------------------------------------------------------------
116 #---------------------------------------------------------------------------
107
117
108 def __init__(self, *args, **kw):
118 def __init__(self, *args, **kw):
109 """ Reimplemented to ensure that QtCore.QObject is initialized first.
119 """ Reimplemented to ensure that QtCore.QObject is initialized first.
110 """
120 """
111 QtCore.QObject.__init__(self)
121 QtCore.QObject.__init__(self)
112 RepSocketChannel.__init__(self, *args, **kw)
122 RepSocketChannel.__init__(self, *args, **kw)
113
123
114 #---------------------------------------------------------------------------
124 #---------------------------------------------------------------------------
115 # 'RepSocketChannel' interface
125 # 'RepSocketChannel' interface
116 #---------------------------------------------------------------------------
126 #---------------------------------------------------------------------------
117
127
118 def call_handlers(self, msg):
128 def call_handlers(self, msg):
119 """ Reimplemented to emit signals instead of making callbacks.
129 """ Reimplemented to emit signals instead of making callbacks.
120 """
130 """
121 # Emit the generic signal.
131 # Emit the generic signal.
122 self.message_received.emit(msg)
132 self.message_received.emit(msg)
123
133
124 # Emit signals for specialized message types.
134 # Emit signals for specialized message types.
125 msg_type = msg['msg_type']
135 msg_type = msg['msg_type']
126 if msg_type == 'input_request':
136 if msg_type == 'input_request':
127 self.input_requested.emit(msg)
137 self.input_requested.emit(msg)
128
138
129
130 class QtKernelManager(KernelManager, QtCore.QObject):
139 class QtKernelManager(KernelManager, QtCore.QObject):
131 """ A KernelManager that provides signals and slots.
140 """ A KernelManager that provides signals and slots.
132 """
141 """
133
142
134 __metaclass__ = MetaQObjectHasTraits
143 __metaclass__ = MetaQObjectHasTraits
135
144
136 # Emitted when the kernel manager has started listening.
145 # Emitted when the kernel manager has started listening.
137 started_channels = QtCore.pyqtSignal()
146 started_channels = QtCore.pyqtSignal()
138
147
139 # Emitted when the kernel manager has stopped listening.
148 # Emitted when the kernel manager has stopped listening.
140 stopped_channels = QtCore.pyqtSignal()
149 stopped_channels = QtCore.pyqtSignal()
141
150
142 # Use Qt-specific channel classes that emit signals.
151 # Use Qt-specific channel classes that emit signals.
143 sub_channel_class = QtSubSocketChannel
152 sub_channel_class = QtSubSocketChannel
144 xreq_channel_class = QtXReqSocketChannel
153 xreq_channel_class = QtXReqSocketChannel
145 rep_channel_class = QtRepSocketChannel
154 rep_channel_class = QtRepSocketChannel
146
155
156 def __init__(self, *args, **kw):
157 QtCore.QObject.__init__(self)
158 KernelManager.__init__(self, *args, **kw)
159
147 #---------------------------------------------------------------------------
160 #---------------------------------------------------------------------------
148 # 'object' interface
161 # 'object' interface
149 #---------------------------------------------------------------------------
162 #---------------------------------------------------------------------------
150
163
151 def __init__(self, *args, **kw):
164 def __init__(self, *args, **kw):
152 """ Reimplemented to ensure that QtCore.QObject is initialized first.
165 """ Reimplemented to ensure that QtCore.QObject is initialized first.
153 """
166 """
154 QtCore.QObject.__init__(self)
167 QtCore.QObject.__init__(self)
155 KernelManager.__init__(self, *args, **kw)
168 KernelManager.__init__(self, *args, **kw)
156
169
157 #---------------------------------------------------------------------------
170 #---------------------------------------------------------------------------
158 # 'KernelManager' interface
171 # 'KernelManager' interface
159 #---------------------------------------------------------------------------
172 #---------------------------------------------------------------------------
160
173
161 def start_channels(self):
174 def start_channels(self):
162 """ Reimplemented to emit signal.
175 """ Reimplemented to emit signal.
163 """
176 """
164 super(QtKernelManager, self).start_channels()
177 super(QtKernelManager, self).start_channels()
165 self.started_channels.emit()
178 self.started_channels.emit()
166
179
167 def stop_channels(self):
180 def stop_channels(self):
168 """ Reimplemented to emit signal.
181 """ Reimplemented to emit signal.
169 """
182 """
170 super(QtKernelManager, self).stop_channels()
183 super(QtKernelManager, self).stop_channels()
171 self.stopped_channels.emit()
184 self.stopped_channels.emit()
@@ -1,63 +1,64 b''
1 """ Defines miscellaneous Qt-related helper classes and functions.
1 """ Defines miscellaneous Qt-related helper classes and functions.
2 """
2 """
3
3
4 # System library imports.
4 # System library imports.
5 from PyQt4 import QtCore, QtGui
5 from PyQt4 import QtCore, QtGui
6
6
7 # IPython imports.
7 # IPython imports.
8 from IPython.utils.traitlets import HasTraits
8 from IPython.utils.traitlets import HasTraits
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Metaclasses
11 # Metaclasses
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 MetaHasTraits = type(HasTraits)
14 MetaHasTraits = type(HasTraits)
15 MetaQObject = type(QtCore.QObject)
15 MetaQObject = type(QtCore.QObject)
16
16
17 # You can switch the order of the parents here and it doesn't seem to matter.
17 class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):
18 class MetaQObjectHasTraits(MetaQObject, MetaHasTraits):
18 """ A metaclass that inherits from the metaclasses of both HasTraits and
19 """ A metaclass that inherits from the metaclasses of both HasTraits and
19 QObject.
20 QObject.
20
21
21 Using this metaclass allows a class to inherit from both HasTraits and
22 Using this metaclass allows a class to inherit from both HasTraits and
22 QObject. See QtKernelManager for an example.
23 QObject. See QtKernelManager for an example.
23 """
24 """
24 pass
25 pass
25
26
26 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
27 # Functions
28 # Functions
28 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
29
30
30 def image_from_svg(string, size=None):
31 def image_from_svg(string, size=None):
31 """ Convert a string containing SVG data into a QImage.
32 """ Convert a string containing SVG data into a QImage.
32
33
33 Parameters:
34 Parameters:
34 -----------
35 -----------
35 string : str
36 string : str
36 A Python string containing the SVG data.
37 A Python string containing the SVG data.
37
38
38 size : QSize or None [default None]
39 size : QSize or None [default None]
39 The size of the image that is produced. If not specified, the SVG data's
40 The size of the image that is produced. If not specified, the SVG data's
40 default size is used.
41 default size is used.
41
42
42 Raises:
43 Raises:
43 -------
44 -------
44 ValueError
45 ValueError
45 If an invalid SVG string is provided.
46 If an invalid SVG string is provided.
46
47
47 Returns:
48 Returns:
48 --------
49 --------
49 A QImage with format QImage.Format_ARGB32_Premultiplied.
50 A QImage with format QImage.Format_ARGB32_Premultiplied.
50 """
51 """
51 from PyQt4 import QtSvg
52 from PyQt4 import QtSvg
52
53
53 bytes = QtCore.QByteArray(string)
54 bytes = QtCore.QByteArray(string)
54 renderer = QtSvg.QSvgRenderer(bytes)
55 renderer = QtSvg.QSvgRenderer(bytes)
55 if not renderer.isValid():
56 if not renderer.isValid():
56 raise ValueError('Invalid SVG data.')
57 raise ValueError('Invalid SVG data.')
57
58
58 if size is None:
59 if size is None:
59 size = renderer.defaultSize()
60 size = renderer.defaultSize()
60 image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32_Premultiplied)
61 image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32_Premultiplied)
61 painter = QtGui.QPainter(image)
62 painter = QtGui.QPainter(image)
62 renderer.render(painter)
63 renderer.render(painter)
63 return image
64 return image
@@ -1,539 +1,538 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 The IPython cluster directory
4 The IPython cluster directory
5 """
5 """
6
6
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2008-2009 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
17
18 from __future__ import with_statement
18 from __future__ import with_statement
19
19
20 import os
20 import os
21 import shutil
21 import shutil
22 import sys
22 import sys
23 import warnings
23 import warnings
24
24
25 from twisted.python import log
25 from twisted.python import log
26
26
27 from IPython.config.loader import PyFileConfigLoader
27 from IPython.config.loader import PyFileConfigLoader
28 from IPython.core.application import Application, BaseAppConfigLoader
28 from IPython.core.application import Application, BaseAppConfigLoader
29 from IPython.core.component import Component
29 from IPython.config.configurable import Configurable
30 from IPython.core.crashhandler import CrashHandler
30 from IPython.core.crashhandler import CrashHandler
31 from IPython.core import release
31 from IPython.core import release
32 from IPython.utils.path import (
32 from IPython.utils.path import (
33 get_ipython_package_dir,
33 get_ipython_package_dir,
34 expand_path
34 expand_path
35 )
35 )
36 from IPython.utils.traitlets import Unicode
36 from IPython.utils.traitlets import Unicode
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Warnings control
39 # Warnings control
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Twisted generates annoying warnings with Python 2.6, as will do other code
41 # Twisted generates annoying warnings with Python 2.6, as will do other code
42 # that imports 'sets' as of today
42 # that imports 'sets' as of today
43 warnings.filterwarnings('ignore', 'the sets module is deprecated',
43 warnings.filterwarnings('ignore', 'the sets module is deprecated',
44 DeprecationWarning )
44 DeprecationWarning )
45
45
46 # This one also comes from Twisted
46 # This one also comes from Twisted
47 warnings.filterwarnings('ignore', 'the sha module is deprecated',
47 warnings.filterwarnings('ignore', 'the sha module is deprecated',
48 DeprecationWarning)
48 DeprecationWarning)
49
49
50 #-----------------------------------------------------------------------------
50 #-----------------------------------------------------------------------------
51 # Module errors
51 # Module errors
52 #-----------------------------------------------------------------------------
52 #-----------------------------------------------------------------------------
53
53
54 class ClusterDirError(Exception):
54 class ClusterDirError(Exception):
55 pass
55 pass
56
56
57
57
58 class PIDFileError(Exception):
58 class PIDFileError(Exception):
59 pass
59 pass
60
60
61
61
62 #-----------------------------------------------------------------------------
62 #-----------------------------------------------------------------------------
63 # Class for managing cluster directories
63 # Class for managing cluster directories
64 #-----------------------------------------------------------------------------
64 #-----------------------------------------------------------------------------
65
65
66 class ClusterDir(Component):
66 class ClusterDir(Configurable):
67 """An object to manage the cluster directory and its resources.
67 """An object to manage the cluster directory and its resources.
68
68
69 The cluster directory is used by :command:`ipcontroller`,
69 The cluster directory is used by :command:`ipcontroller`,
70 :command:`ipcontroller` and :command:`ipcontroller` to manage the
70 :command:`ipcontroller` and :command:`ipcontroller` to manage the
71 configuration, logging and security of these applications.
71 configuration, logging and security of these applications.
72
72
73 This object knows how to find, create and manage these directories. This
73 This object knows how to find, create and manage these directories. This
74 should be used by any code that want's to handle cluster directories.
74 should be used by any code that want's to handle cluster directories.
75 """
75 """
76
76
77 security_dir_name = Unicode('security')
77 security_dir_name = Unicode('security')
78 log_dir_name = Unicode('log')
78 log_dir_name = Unicode('log')
79 pid_dir_name = Unicode('pid')
79 pid_dir_name = Unicode('pid')
80 security_dir = Unicode(u'')
80 security_dir = Unicode(u'')
81 log_dir = Unicode(u'')
81 log_dir = Unicode(u'')
82 pid_dir = Unicode(u'')
82 pid_dir = Unicode(u'')
83 location = Unicode(u'')
83 location = Unicode(u'')
84
84
85 def __init__(self, location):
85 def __init__(self, location=u''):
86 super(ClusterDir, self).__init__(None)
86 super(ClusterDir, self).__init__(location=location)
87 self.location = location
88
87
89 def _location_changed(self, name, old, new):
88 def _location_changed(self, name, old, new):
90 if not os.path.isdir(new):
89 if not os.path.isdir(new):
91 os.makedirs(new)
90 os.makedirs(new)
92 self.security_dir = os.path.join(new, self.security_dir_name)
91 self.security_dir = os.path.join(new, self.security_dir_name)
93 self.log_dir = os.path.join(new, self.log_dir_name)
92 self.log_dir = os.path.join(new, self.log_dir_name)
94 self.pid_dir = os.path.join(new, self.pid_dir_name)
93 self.pid_dir = os.path.join(new, self.pid_dir_name)
95 self.check_dirs()
94 self.check_dirs()
96
95
97 def _log_dir_changed(self, name, old, new):
96 def _log_dir_changed(self, name, old, new):
98 self.check_log_dir()
97 self.check_log_dir()
99
98
100 def check_log_dir(self):
99 def check_log_dir(self):
101 if not os.path.isdir(self.log_dir):
100 if not os.path.isdir(self.log_dir):
102 os.mkdir(self.log_dir)
101 os.mkdir(self.log_dir)
103
102
104 def _security_dir_changed(self, name, old, new):
103 def _security_dir_changed(self, name, old, new):
105 self.check_security_dir()
104 self.check_security_dir()
106
105
107 def check_security_dir(self):
106 def check_security_dir(self):
108 if not os.path.isdir(self.security_dir):
107 if not os.path.isdir(self.security_dir):
109 os.mkdir(self.security_dir, 0700)
108 os.mkdir(self.security_dir, 0700)
110 os.chmod(self.security_dir, 0700)
109 os.chmod(self.security_dir, 0700)
111
110
112 def _pid_dir_changed(self, name, old, new):
111 def _pid_dir_changed(self, name, old, new):
113 self.check_pid_dir()
112 self.check_pid_dir()
114
113
115 def check_pid_dir(self):
114 def check_pid_dir(self):
116 if not os.path.isdir(self.pid_dir):
115 if not os.path.isdir(self.pid_dir):
117 os.mkdir(self.pid_dir, 0700)
116 os.mkdir(self.pid_dir, 0700)
118 os.chmod(self.pid_dir, 0700)
117 os.chmod(self.pid_dir, 0700)
119
118
120 def check_dirs(self):
119 def check_dirs(self):
121 self.check_security_dir()
120 self.check_security_dir()
122 self.check_log_dir()
121 self.check_log_dir()
123 self.check_pid_dir()
122 self.check_pid_dir()
124
123
125 def load_config_file(self, filename):
124 def load_config_file(self, filename):
126 """Load a config file from the top level of the cluster dir.
125 """Load a config file from the top level of the cluster dir.
127
126
128 Parameters
127 Parameters
129 ----------
128 ----------
130 filename : unicode or str
129 filename : unicode or str
131 The filename only of the config file that must be located in
130 The filename only of the config file that must be located in
132 the top-level of the cluster directory.
131 the top-level of the cluster directory.
133 """
132 """
134 loader = PyFileConfigLoader(filename, self.location)
133 loader = PyFileConfigLoader(filename, self.location)
135 return loader.load_config()
134 return loader.load_config()
136
135
137 def copy_config_file(self, config_file, path=None, overwrite=False):
136 def copy_config_file(self, config_file, path=None, overwrite=False):
138 """Copy a default config file into the active cluster directory.
137 """Copy a default config file into the active cluster directory.
139
138
140 Default configuration files are kept in :mod:`IPython.config.default`.
139 Default configuration files are kept in :mod:`IPython.config.default`.
141 This function moves these from that location to the working cluster
140 This function moves these from that location to the working cluster
142 directory.
141 directory.
143 """
142 """
144 if path is None:
143 if path is None:
145 import IPython.config.default
144 import IPython.config.default
146 path = IPython.config.default.__file__.split(os.path.sep)[:-1]
145 path = IPython.config.default.__file__.split(os.path.sep)[:-1]
147 path = os.path.sep.join(path)
146 path = os.path.sep.join(path)
148 src = os.path.join(path, config_file)
147 src = os.path.join(path, config_file)
149 dst = os.path.join(self.location, config_file)
148 dst = os.path.join(self.location, config_file)
150 if not os.path.isfile(dst) or overwrite:
149 if not os.path.isfile(dst) or overwrite:
151 shutil.copy(src, dst)
150 shutil.copy(src, dst)
152
151
153 def copy_all_config_files(self, path=None, overwrite=False):
152 def copy_all_config_files(self, path=None, overwrite=False):
154 """Copy all config files into the active cluster directory."""
153 """Copy all config files into the active cluster directory."""
155 for f in [u'ipcontroller_config.py', u'ipengine_config.py',
154 for f in [u'ipcontroller_config.py', u'ipengine_config.py',
156 u'ipcluster_config.py']:
155 u'ipcluster_config.py']:
157 self.copy_config_file(f, path=path, overwrite=overwrite)
156 self.copy_config_file(f, path=path, overwrite=overwrite)
158
157
159 @classmethod
158 @classmethod
160 def create_cluster_dir(csl, cluster_dir):
159 def create_cluster_dir(csl, cluster_dir):
161 """Create a new cluster directory given a full path.
160 """Create a new cluster directory given a full path.
162
161
163 Parameters
162 Parameters
164 ----------
163 ----------
165 cluster_dir : str
164 cluster_dir : str
166 The full path to the cluster directory. If it does exist, it will
165 The full path to the cluster directory. If it does exist, it will
167 be used. If not, it will be created.
166 be used. If not, it will be created.
168 """
167 """
169 return ClusterDir(cluster_dir)
168 return ClusterDir(location=cluster_dir)
170
169
171 @classmethod
170 @classmethod
172 def create_cluster_dir_by_profile(cls, path, profile=u'default'):
171 def create_cluster_dir_by_profile(cls, path, profile=u'default'):
173 """Create a cluster dir by profile name and path.
172 """Create a cluster dir by profile name and path.
174
173
175 Parameters
174 Parameters
176 ----------
175 ----------
177 path : str
176 path : str
178 The path (directory) to put the cluster directory in.
177 The path (directory) to put the cluster directory in.
179 profile : str
178 profile : str
180 The name of the profile. The name of the cluster directory will
179 The name of the profile. The name of the cluster directory will
181 be "cluster_<profile>".
180 be "cluster_<profile>".
182 """
181 """
183 if not os.path.isdir(path):
182 if not os.path.isdir(path):
184 raise ClusterDirError('Directory not found: %s' % path)
183 raise ClusterDirError('Directory not found: %s' % path)
185 cluster_dir = os.path.join(path, u'cluster_' + profile)
184 cluster_dir = os.path.join(path, u'cluster_' + profile)
186 return ClusterDir(cluster_dir)
185 return ClusterDir(location=cluster_dir)
187
186
188 @classmethod
187 @classmethod
189 def find_cluster_dir_by_profile(cls, ipython_dir, profile=u'default'):
188 def find_cluster_dir_by_profile(cls, ipython_dir, profile=u'default'):
190 """Find an existing cluster dir by profile name, return its ClusterDir.
189 """Find an existing cluster dir by profile name, return its ClusterDir.
191
190
192 This searches through a sequence of paths for a cluster dir. If it
191 This searches through a sequence of paths for a cluster dir. If it
193 is not found, a :class:`ClusterDirError` exception will be raised.
192 is not found, a :class:`ClusterDirError` exception will be raised.
194
193
195 The search path algorithm is:
194 The search path algorithm is:
196 1. ``os.getcwd()``
195 1. ``os.getcwd()``
197 2. ``ipython_dir``
196 2. ``ipython_dir``
198 3. The directories found in the ":" separated
197 3. The directories found in the ":" separated
199 :env:`IPCLUSTER_DIR_PATH` environment variable.
198 :env:`IPCLUSTER_DIR_PATH` environment variable.
200
199
201 Parameters
200 Parameters
202 ----------
201 ----------
203 ipython_dir : unicode or str
202 ipython_dir : unicode or str
204 The IPython directory to use.
203 The IPython directory to use.
205 profile : unicode or str
204 profile : unicode or str
206 The name of the profile. The name of the cluster directory
205 The name of the profile. The name of the cluster directory
207 will be "cluster_<profile>".
206 will be "cluster_<profile>".
208 """
207 """
209 dirname = u'cluster_' + profile
208 dirname = u'cluster_' + profile
210 cluster_dir_paths = os.environ.get('IPCLUSTER_DIR_PATH','')
209 cluster_dir_paths = os.environ.get('IPCLUSTER_DIR_PATH','')
211 if cluster_dir_paths:
210 if cluster_dir_paths:
212 cluster_dir_paths = cluster_dir_paths.split(':')
211 cluster_dir_paths = cluster_dir_paths.split(':')
213 else:
212 else:
214 cluster_dir_paths = []
213 cluster_dir_paths = []
215 paths = [os.getcwd(), ipython_dir] + cluster_dir_paths
214 paths = [os.getcwd(), ipython_dir] + cluster_dir_paths
216 for p in paths:
215 for p in paths:
217 cluster_dir = os.path.join(p, dirname)
216 cluster_dir = os.path.join(p, dirname)
218 if os.path.isdir(cluster_dir):
217 if os.path.isdir(cluster_dir):
219 return ClusterDir(cluster_dir)
218 return ClusterDir(location=cluster_dir)
220 else:
219 else:
221 raise ClusterDirError('Cluster directory not found in paths: %s' % dirname)
220 raise ClusterDirError('Cluster directory not found in paths: %s' % dirname)
222
221
223 @classmethod
222 @classmethod
224 def find_cluster_dir(cls, cluster_dir):
223 def find_cluster_dir(cls, cluster_dir):
225 """Find/create a cluster dir and return its ClusterDir.
224 """Find/create a cluster dir and return its ClusterDir.
226
225
227 This will create the cluster directory if it doesn't exist.
226 This will create the cluster directory if it doesn't exist.
228
227
229 Parameters
228 Parameters
230 ----------
229 ----------
231 cluster_dir : unicode or str
230 cluster_dir : unicode or str
232 The path of the cluster directory. This is expanded using
231 The path of the cluster directory. This is expanded using
233 :func:`IPython.utils.genutils.expand_path`.
232 :func:`IPython.utils.genutils.expand_path`.
234 """
233 """
235 cluster_dir = expand_path(cluster_dir)
234 cluster_dir = expand_path(cluster_dir)
236 if not os.path.isdir(cluster_dir):
235 if not os.path.isdir(cluster_dir):
237 raise ClusterDirError('Cluster directory not found: %s' % cluster_dir)
236 raise ClusterDirError('Cluster directory not found: %s' % cluster_dir)
238 return ClusterDir(cluster_dir)
237 return ClusterDir(location=cluster_dir)
239
238
240
239
241 #-----------------------------------------------------------------------------
240 #-----------------------------------------------------------------------------
242 # Command line options
241 # Command line options
243 #-----------------------------------------------------------------------------
242 #-----------------------------------------------------------------------------
244
243
245 class ClusterDirConfigLoader(BaseAppConfigLoader):
244 class ClusterDirConfigLoader(BaseAppConfigLoader):
246
245
247 def _add_cluster_profile(self, parser):
246 def _add_cluster_profile(self, parser):
248 paa = parser.add_argument
247 paa = parser.add_argument
249 paa('-p', '--profile',
248 paa('-p', '--profile',
250 dest='Global.profile',type=unicode,
249 dest='Global.profile',type=unicode,
251 help=
250 help=
252 """The string name of the profile to be used. This determines the name
251 """The string name of the profile to be used. This determines the name
253 of the cluster dir as: cluster_<profile>. The default profile is named
252 of the cluster dir as: cluster_<profile>. The default profile is named
254 'default'. The cluster directory is resolve this way if the
253 'default'. The cluster directory is resolve this way if the
255 --cluster-dir option is not used.""",
254 --cluster-dir option is not used.""",
256 metavar='Global.profile')
255 metavar='Global.profile')
257
256
258 def _add_cluster_dir(self, parser):
257 def _add_cluster_dir(self, parser):
259 paa = parser.add_argument
258 paa = parser.add_argument
260 paa('--cluster-dir',
259 paa('--cluster-dir',
261 dest='Global.cluster_dir',type=unicode,
260 dest='Global.cluster_dir',type=unicode,
262 help="""Set the cluster dir. This overrides the logic used by the
261 help="""Set the cluster dir. This overrides the logic used by the
263 --profile option.""",
262 --profile option.""",
264 metavar='Global.cluster_dir')
263 metavar='Global.cluster_dir')
265
264
266 def _add_work_dir(self, parser):
265 def _add_work_dir(self, parser):
267 paa = parser.add_argument
266 paa = parser.add_argument
268 paa('--work-dir',
267 paa('--work-dir',
269 dest='Global.work_dir',type=unicode,
268 dest='Global.work_dir',type=unicode,
270 help='Set the working dir for the process.',
269 help='Set the working dir for the process.',
271 metavar='Global.work_dir')
270 metavar='Global.work_dir')
272
271
273 def _add_clean_logs(self, parser):
272 def _add_clean_logs(self, parser):
274 paa = parser.add_argument
273 paa = parser.add_argument
275 paa('--clean-logs',
274 paa('--clean-logs',
276 dest='Global.clean_logs', action='store_true',
275 dest='Global.clean_logs', action='store_true',
277 help='Delete old log flies before starting.')
276 help='Delete old log flies before starting.')
278
277
279 def _add_no_clean_logs(self, parser):
278 def _add_no_clean_logs(self, parser):
280 paa = parser.add_argument
279 paa = parser.add_argument
281 paa('--no-clean-logs',
280 paa('--no-clean-logs',
282 dest='Global.clean_logs', action='store_false',
281 dest='Global.clean_logs', action='store_false',
283 help="Don't Delete old log flies before starting.")
282 help="Don't Delete old log flies before starting.")
284
283
285 def _add_arguments(self):
284 def _add_arguments(self):
286 super(ClusterDirConfigLoader, self)._add_arguments()
285 super(ClusterDirConfigLoader, self)._add_arguments()
287 self._add_cluster_profile(self.parser)
286 self._add_cluster_profile(self.parser)
288 self._add_cluster_dir(self.parser)
287 self._add_cluster_dir(self.parser)
289 self._add_work_dir(self.parser)
288 self._add_work_dir(self.parser)
290 self._add_clean_logs(self.parser)
289 self._add_clean_logs(self.parser)
291 self._add_no_clean_logs(self.parser)
290 self._add_no_clean_logs(self.parser)
292
291
293
292
294 #-----------------------------------------------------------------------------
293 #-----------------------------------------------------------------------------
295 # Crash handler for this application
294 # Crash handler for this application
296 #-----------------------------------------------------------------------------
295 #-----------------------------------------------------------------------------
297
296
298
297
299 _message_template = """\
298 _message_template = """\
300 Oops, $self.app_name crashed. We do our best to make it stable, but...
299 Oops, $self.app_name crashed. We do our best to make it stable, but...
301
300
302 A crash report was automatically generated with the following information:
301 A crash report was automatically generated with the following information:
303 - A verbatim copy of the crash traceback.
302 - A verbatim copy of the crash traceback.
304 - Data on your current $self.app_name configuration.
303 - Data on your current $self.app_name configuration.
305
304
306 It was left in the file named:
305 It was left in the file named:
307 \t'$self.crash_report_fname'
306 \t'$self.crash_report_fname'
308 If you can email this file to the developers, the information in it will help
307 If you can email this file to the developers, the information in it will help
309 them in understanding and correcting the problem.
308 them in understanding and correcting the problem.
310
309
311 You can mail it to: $self.contact_name at $self.contact_email
310 You can mail it to: $self.contact_name at $self.contact_email
312 with the subject '$self.app_name Crash Report'.
311 with the subject '$self.app_name Crash Report'.
313
312
314 If you want to do it now, the following command will work (under Unix):
313 If you want to do it now, the following command will work (under Unix):
315 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
314 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
316
315
317 To ensure accurate tracking of this issue, please file a report about it at:
316 To ensure accurate tracking of this issue, please file a report about it at:
318 $self.bug_tracker
317 $self.bug_tracker
319 """
318 """
320
319
321 class ClusterDirCrashHandler(CrashHandler):
320 class ClusterDirCrashHandler(CrashHandler):
322 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
321 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
323
322
324 message_template = _message_template
323 message_template = _message_template
325
324
326 def __init__(self, app):
325 def __init__(self, app):
327 contact_name = release.authors['Brian'][0]
326 contact_name = release.authors['Brian'][0]
328 contact_email = release.authors['Brian'][1]
327 contact_email = release.authors['Brian'][1]
329 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
328 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
330 super(ClusterDirCrashHandler,self).__init__(
329 super(ClusterDirCrashHandler,self).__init__(
331 app, contact_name, contact_email, bug_tracker
330 app, contact_name, contact_email, bug_tracker
332 )
331 )
333
332
334
333
335 #-----------------------------------------------------------------------------
334 #-----------------------------------------------------------------------------
336 # Main application
335 # Main application
337 #-----------------------------------------------------------------------------
336 #-----------------------------------------------------------------------------
338
337
339 class ApplicationWithClusterDir(Application):
338 class ApplicationWithClusterDir(Application):
340 """An application that puts everything into a cluster directory.
339 """An application that puts everything into a cluster directory.
341
340
342 Instead of looking for things in the ipython_dir, this type of application
341 Instead of looking for things in the ipython_dir, this type of application
343 will use its own private directory called the "cluster directory"
342 will use its own private directory called the "cluster directory"
344 for things like config files, log files, etc.
343 for things like config files, log files, etc.
345
344
346 The cluster directory is resolved as follows:
345 The cluster directory is resolved as follows:
347
346
348 * If the ``--cluster-dir`` option is given, it is used.
347 * If the ``--cluster-dir`` option is given, it is used.
349 * If ``--cluster-dir`` is not given, the application directory is
348 * If ``--cluster-dir`` is not given, the application directory is
350 resolve using the profile name as ``cluster_<profile>``. The search
349 resolve using the profile name as ``cluster_<profile>``. The search
351 path for this directory is then i) cwd if it is found there
350 path for this directory is then i) cwd if it is found there
352 and ii) in ipython_dir otherwise.
351 and ii) in ipython_dir otherwise.
353
352
354 The config file for the application is to be put in the cluster
353 The config file for the application is to be put in the cluster
355 dir and named the value of the ``config_file_name`` class attribute.
354 dir and named the value of the ``config_file_name`` class attribute.
356 """
355 """
357
356
358 command_line_loader = ClusterDirConfigLoader
357 command_line_loader = ClusterDirConfigLoader
359 crash_handler_class = ClusterDirCrashHandler
358 crash_handler_class = ClusterDirCrashHandler
360 auto_create_cluster_dir = True
359 auto_create_cluster_dir = True
361
360
362 def create_default_config(self):
361 def create_default_config(self):
363 super(ApplicationWithClusterDir, self).create_default_config()
362 super(ApplicationWithClusterDir, self).create_default_config()
364 self.default_config.Global.profile = u'default'
363 self.default_config.Global.profile = u'default'
365 self.default_config.Global.cluster_dir = u''
364 self.default_config.Global.cluster_dir = u''
366 self.default_config.Global.work_dir = os.getcwd()
365 self.default_config.Global.work_dir = os.getcwd()
367 self.default_config.Global.log_to_file = False
366 self.default_config.Global.log_to_file = False
368 self.default_config.Global.clean_logs = False
367 self.default_config.Global.clean_logs = False
369
368
370 def find_resources(self):
369 def find_resources(self):
371 """This resolves the cluster directory.
370 """This resolves the cluster directory.
372
371
373 This tries to find the cluster directory and if successful, it will
372 This tries to find the cluster directory and if successful, it will
374 have done:
373 have done:
375 * Sets ``self.cluster_dir_obj`` to the :class:`ClusterDir` object for
374 * Sets ``self.cluster_dir_obj`` to the :class:`ClusterDir` object for
376 the application.
375 the application.
377 * Sets ``self.cluster_dir`` attribute of the application and config
376 * Sets ``self.cluster_dir`` attribute of the application and config
378 objects.
377 objects.
379
378
380 The algorithm used for this is as follows:
379 The algorithm used for this is as follows:
381 1. Try ``Global.cluster_dir``.
380 1. Try ``Global.cluster_dir``.
382 2. Try using ``Global.profile``.
381 2. Try using ``Global.profile``.
383 3. If both of these fail and ``self.auto_create_cluster_dir`` is
382 3. If both of these fail and ``self.auto_create_cluster_dir`` is
384 ``True``, then create the new cluster dir in the IPython directory.
383 ``True``, then create the new cluster dir in the IPython directory.
385 4. If all fails, then raise :class:`ClusterDirError`.
384 4. If all fails, then raise :class:`ClusterDirError`.
386 """
385 """
387
386
388 try:
387 try:
389 cluster_dir = self.command_line_config.Global.cluster_dir
388 cluster_dir = self.command_line_config.Global.cluster_dir
390 except AttributeError:
389 except AttributeError:
391 cluster_dir = self.default_config.Global.cluster_dir
390 cluster_dir = self.default_config.Global.cluster_dir
392 cluster_dir = expand_path(cluster_dir)
391 cluster_dir = expand_path(cluster_dir)
393 try:
392 try:
394 self.cluster_dir_obj = ClusterDir.find_cluster_dir(cluster_dir)
393 self.cluster_dir_obj = ClusterDir.find_cluster_dir(cluster_dir)
395 except ClusterDirError:
394 except ClusterDirError:
396 pass
395 pass
397 else:
396 else:
398 self.log.info('Using existing cluster dir: %s' % \
397 self.log.info('Using existing cluster dir: %s' % \
399 self.cluster_dir_obj.location
398 self.cluster_dir_obj.location
400 )
399 )
401 self.finish_cluster_dir()
400 self.finish_cluster_dir()
402 return
401 return
403
402
404 try:
403 try:
405 self.profile = self.command_line_config.Global.profile
404 self.profile = self.command_line_config.Global.profile
406 except AttributeError:
405 except AttributeError:
407 self.profile = self.default_config.Global.profile
406 self.profile = self.default_config.Global.profile
408 try:
407 try:
409 self.cluster_dir_obj = ClusterDir.find_cluster_dir_by_profile(
408 self.cluster_dir_obj = ClusterDir.find_cluster_dir_by_profile(
410 self.ipython_dir, self.profile)
409 self.ipython_dir, self.profile)
411 except ClusterDirError:
410 except ClusterDirError:
412 pass
411 pass
413 else:
412 else:
414 self.log.info('Using existing cluster dir: %s' % \
413 self.log.info('Using existing cluster dir: %s' % \
415 self.cluster_dir_obj.location
414 self.cluster_dir_obj.location
416 )
415 )
417 self.finish_cluster_dir()
416 self.finish_cluster_dir()
418 return
417 return
419
418
420 if self.auto_create_cluster_dir:
419 if self.auto_create_cluster_dir:
421 self.cluster_dir_obj = ClusterDir.create_cluster_dir_by_profile(
420 self.cluster_dir_obj = ClusterDir.create_cluster_dir_by_profile(
422 self.ipython_dir, self.profile
421 self.ipython_dir, self.profile
423 )
422 )
424 self.log.info('Creating new cluster dir: %s' % \
423 self.log.info('Creating new cluster dir: %s' % \
425 self.cluster_dir_obj.location
424 self.cluster_dir_obj.location
426 )
425 )
427 self.finish_cluster_dir()
426 self.finish_cluster_dir()
428 else:
427 else:
429 raise ClusterDirError('Could not find a valid cluster directory.')
428 raise ClusterDirError('Could not find a valid cluster directory.')
430
429
431 def finish_cluster_dir(self):
430 def finish_cluster_dir(self):
432 # Set the cluster directory
431 # Set the cluster directory
433 self.cluster_dir = self.cluster_dir_obj.location
432 self.cluster_dir = self.cluster_dir_obj.location
434
433
435 # These have to be set because they could be different from the one
434 # These have to be set because they could be different from the one
436 # that we just computed. Because command line has the highest
435 # that we just computed. Because command line has the highest
437 # priority, this will always end up in the master_config.
436 # priority, this will always end up in the master_config.
438 self.default_config.Global.cluster_dir = self.cluster_dir
437 self.default_config.Global.cluster_dir = self.cluster_dir
439 self.command_line_config.Global.cluster_dir = self.cluster_dir
438 self.command_line_config.Global.cluster_dir = self.cluster_dir
440
439
441 def find_config_file_name(self):
440 def find_config_file_name(self):
442 """Find the config file name for this application."""
441 """Find the config file name for this application."""
443 # For this type of Application it should be set as a class attribute.
442 # For this type of Application it should be set as a class attribute.
444 if not hasattr(self, 'default_config_file_name'):
443 if not hasattr(self, 'default_config_file_name'):
445 self.log.critical("No config filename found")
444 self.log.critical("No config filename found")
446 else:
445 else:
447 self.config_file_name = self.default_config_file_name
446 self.config_file_name = self.default_config_file_name
448
447
449 def find_config_file_paths(self):
448 def find_config_file_paths(self):
450 # Set the search path to to the cluster directory. We should NOT
449 # Set the search path to to the cluster directory. We should NOT
451 # include IPython.config.default here as the default config files
450 # include IPython.config.default here as the default config files
452 # are ALWAYS automatically moved to the cluster directory.
451 # are ALWAYS automatically moved to the cluster directory.
453 conf_dir = os.path.join(get_ipython_package_dir(), 'config', 'default')
452 conf_dir = os.path.join(get_ipython_package_dir(), 'config', 'default')
454 self.config_file_paths = (self.cluster_dir,)
453 self.config_file_paths = (self.cluster_dir,)
455
454
456 def pre_construct(self):
455 def pre_construct(self):
457 # The log and security dirs were set earlier, but here we put them
456 # The log and security dirs were set earlier, but here we put them
458 # into the config and log them.
457 # into the config and log them.
459 config = self.master_config
458 config = self.master_config
460 sdir = self.cluster_dir_obj.security_dir
459 sdir = self.cluster_dir_obj.security_dir
461 self.security_dir = config.Global.security_dir = sdir
460 self.security_dir = config.Global.security_dir = sdir
462 ldir = self.cluster_dir_obj.log_dir
461 ldir = self.cluster_dir_obj.log_dir
463 self.log_dir = config.Global.log_dir = ldir
462 self.log_dir = config.Global.log_dir = ldir
464 pdir = self.cluster_dir_obj.pid_dir
463 pdir = self.cluster_dir_obj.pid_dir
465 self.pid_dir = config.Global.pid_dir = pdir
464 self.pid_dir = config.Global.pid_dir = pdir
466 self.log.info("Cluster directory set to: %s" % self.cluster_dir)
465 self.log.info("Cluster directory set to: %s" % self.cluster_dir)
467 config.Global.work_dir = unicode(expand_path(config.Global.work_dir))
466 config.Global.work_dir = unicode(expand_path(config.Global.work_dir))
468 # Change to the working directory. We do this just before construct
467 # Change to the working directory. We do this just before construct
469 # is called so all the components there have the right working dir.
468 # is called so all the components there have the right working dir.
470 self.to_work_dir()
469 self.to_work_dir()
471
470
472 def to_work_dir(self):
471 def to_work_dir(self):
473 wd = self.master_config.Global.work_dir
472 wd = self.master_config.Global.work_dir
474 if unicode(wd) != unicode(os.getcwd()):
473 if unicode(wd) != unicode(os.getcwd()):
475 os.chdir(wd)
474 os.chdir(wd)
476 self.log.info("Changing to working dir: %s" % wd)
475 self.log.info("Changing to working dir: %s" % wd)
477
476
478 def start_logging(self):
477 def start_logging(self):
479 # Remove old log files
478 # Remove old log files
480 if self.master_config.Global.clean_logs:
479 if self.master_config.Global.clean_logs:
481 log_dir = self.master_config.Global.log_dir
480 log_dir = self.master_config.Global.log_dir
482 for f in os.listdir(log_dir):
481 for f in os.listdir(log_dir):
483 if f.startswith(self.name + u'-') and f.endswith('.log'):
482 if f.startswith(self.name + u'-') and f.endswith('.log'):
484 os.remove(os.path.join(log_dir, f))
483 os.remove(os.path.join(log_dir, f))
485 # Start logging to the new log file
484 # Start logging to the new log file
486 if self.master_config.Global.log_to_file:
485 if self.master_config.Global.log_to_file:
487 log_filename = self.name + u'-' + str(os.getpid()) + u'.log'
486 log_filename = self.name + u'-' + str(os.getpid()) + u'.log'
488 logfile = os.path.join(self.log_dir, log_filename)
487 logfile = os.path.join(self.log_dir, log_filename)
489 open_log_file = open(logfile, 'w')
488 open_log_file = open(logfile, 'w')
490 else:
489 else:
491 open_log_file = sys.stdout
490 open_log_file = sys.stdout
492 log.startLogging(open_log_file)
491 log.startLogging(open_log_file)
493
492
494 def write_pid_file(self, overwrite=False):
493 def write_pid_file(self, overwrite=False):
495 """Create a .pid file in the pid_dir with my pid.
494 """Create a .pid file in the pid_dir with my pid.
496
495
497 This must be called after pre_construct, which sets `self.pid_dir`.
496 This must be called after pre_construct, which sets `self.pid_dir`.
498 This raises :exc:`PIDFileError` if the pid file exists already.
497 This raises :exc:`PIDFileError` if the pid file exists already.
499 """
498 """
500 pid_file = os.path.join(self.pid_dir, self.name + u'.pid')
499 pid_file = os.path.join(self.pid_dir, self.name + u'.pid')
501 if os.path.isfile(pid_file):
500 if os.path.isfile(pid_file):
502 pid = self.get_pid_from_file()
501 pid = self.get_pid_from_file()
503 if not overwrite:
502 if not overwrite:
504 raise PIDFileError(
503 raise PIDFileError(
505 'The pid file [%s] already exists. \nThis could mean that this '
504 'The pid file [%s] already exists. \nThis could mean that this '
506 'server is already running with [pid=%s].' % (pid_file, pid)
505 'server is already running with [pid=%s].' % (pid_file, pid)
507 )
506 )
508 with open(pid_file, 'w') as f:
507 with open(pid_file, 'w') as f:
509 self.log.info("Creating pid file: %s" % pid_file)
508 self.log.info("Creating pid file: %s" % pid_file)
510 f.write(repr(os.getpid())+'\n')
509 f.write(repr(os.getpid())+'\n')
511
510
512 def remove_pid_file(self):
511 def remove_pid_file(self):
513 """Remove the pid file.
512 """Remove the pid file.
514
513
515 This should be called at shutdown by registering a callback with
514 This should be called at shutdown by registering a callback with
516 :func:`reactor.addSystemEventTrigger`. This needs to return
515 :func:`reactor.addSystemEventTrigger`. This needs to return
517 ``None``.
516 ``None``.
518 """
517 """
519 pid_file = os.path.join(self.pid_dir, self.name + u'.pid')
518 pid_file = os.path.join(self.pid_dir, self.name + u'.pid')
520 if os.path.isfile(pid_file):
519 if os.path.isfile(pid_file):
521 try:
520 try:
522 self.log.info("Removing pid file: %s" % pid_file)
521 self.log.info("Removing pid file: %s" % pid_file)
523 os.remove(pid_file)
522 os.remove(pid_file)
524 except:
523 except:
525 self.log.warn("Error removing the pid file: %s" % pid_file)
524 self.log.warn("Error removing the pid file: %s" % pid_file)
526
525
527 def get_pid_from_file(self):
526 def get_pid_from_file(self):
528 """Get the pid from the pid file.
527 """Get the pid from the pid file.
529
528
530 If the pid file doesn't exist a :exc:`PIDFileError` is raised.
529 If the pid file doesn't exist a :exc:`PIDFileError` is raised.
531 """
530 """
532 pid_file = os.path.join(self.pid_dir, self.name + u'.pid')
531 pid_file = os.path.join(self.pid_dir, self.name + u'.pid')
533 if os.path.isfile(pid_file):
532 if os.path.isfile(pid_file):
534 with open(pid_file, 'r') as f:
533 with open(pid_file, 'r') as f:
535 pid = int(f.read().strip())
534 pid = int(f.read().strip())
536 return pid
535 return pid
537 else:
536 else:
538 raise PIDFileError('pid file not found: %s' % pid_file)
537 raise PIDFileError('pid file not found: %s' % pid_file)
539
538
@@ -1,79 +1,79 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 A class for creating a Twisted service that is configured using IPython's
4 A class for creating a Twisted service that is configured using IPython's
5 configuration system.
5 configuration system.
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2009 The IPython Development Team
9 # Copyright (C) 2008-2009 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 import zope.interface as zi
19 import zope.interface as zi
20
20
21 from IPython.core.component import Component
21 from IPython.config.configurable import Configurable
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Code
24 # Code
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27
27
28 class IConfiguredObjectFactory(zi.Interface):
28 class IConfiguredObjectFactory(zi.Interface):
29 """I am a component that creates a configured object.
29 """I am a component that creates a configured object.
30
30
31 This class is useful if you want to configure a class that is not a
31 This class is useful if you want to configure a class that is not a
32 subclass of :class:`IPython.core.component.Component`.
32 subclass of :class:`IPython.config.configurable.Configurable`.
33 """
33 """
34
34
35 def __init__(config):
35 def __init__(config):
36 """Get ready to configure the object using config."""
36 """Get ready to configure the object using config."""
37
37
38 def create():
38 def create():
39 """Return an instance of the configured object."""
39 """Return an instance of the configured object."""
40
40
41
41
42 class ConfiguredObjectFactory(Component):
42 class ConfiguredObjectFactory(Configurable):
43
43
44 zi.implements(IConfiguredObjectFactory)
44 zi.implements(IConfiguredObjectFactory)
45
45
46 def __init__(self, config):
46 def __init__(self, config=None):
47 super(ConfiguredObjectFactory, self).__init__(None, config=config)
47 super(ConfiguredObjectFactory, self).__init__(config=config)
48
48
49 def create(self):
49 def create(self):
50 raise NotImplementedError('create must be implemented in a subclass')
50 raise NotImplementedError('create must be implemented in a subclass')
51
51
52
52
53 class IAdaptedConfiguredObjectFactory(zi.Interface):
53 class IAdaptedConfiguredObjectFactory(zi.Interface):
54 """I am a component that adapts and configures an object.
54 """I am a component that adapts and configures an object.
55
55
56 This class is useful if you have the adapt an instance and configure it.
56 This class is useful if you have the adapt an instance and configure it.
57 """
57 """
58
58
59 def __init__(config, adaptee=None):
59 def __init__(config=None, adaptee=None):
60 """Get ready to adapt adaptee and then configure it using config."""
60 """Get ready to adapt adaptee and then configure it using config."""
61
61
62 def create():
62 def create():
63 """Return an instance of the adapted and configured object."""
63 """Return an instance of the adapted and configured object."""
64
64
65
65
66 class AdaptedConfiguredObjectFactory(Component):
66 class AdaptedConfiguredObjectFactory(Configurable):
67
67
68 # zi.implements(IAdaptedConfiguredObjectFactory)
68 # zi.implements(IAdaptedConfiguredObjectFactory)
69
69
70 def __init__(self, config, adaptee):
70 def __init__(self, config=None, adaptee=None):
71 # print
71 # print
72 # print "config pre:", config
72 # print "config pre:", config
73 super(AdaptedConfiguredObjectFactory, self).__init__(None, config=config)
73 super(AdaptedConfiguredObjectFactory, self).__init__(config=config)
74 # print
74 # print
75 # print "config post:", config
75 # print "config post:", config
76 self.adaptee = adaptee
76 self.adaptee = adaptee
77
77
78 def create(self):
78 def create(self):
79 raise NotImplementedError('create must be implemented in a subclass')
79 raise NotImplementedError('create must be implemented in a subclass')
@@ -1,296 +1,296 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Foolscap related utilities.
4 Foolscap related utilities.
5 """
5 """
6
6
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2008-2009 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
17
18 from __future__ import with_statement
18 from __future__ import with_statement
19
19
20 import os
20 import os
21 import tempfile
21 import tempfile
22
22
23 from twisted.internet import reactor, defer
23 from twisted.internet import reactor, defer
24 from twisted.python import log
24 from twisted.python import log
25
25
26 import foolscap
26 import foolscap
27 try:
27 try:
28 from foolscap.api import Tub, UnauthenticatedTub
28 from foolscap.api import Tub, UnauthenticatedTub
29 except ImportError:
29 except ImportError:
30 from foolscap import Tub, UnauthenticatedTub
30 from foolscap import Tub, UnauthenticatedTub
31
31
32 from IPython.config.loader import Config
32 from IPython.config.loader import Config
33 from IPython.kernel.configobjfactory import AdaptedConfiguredObjectFactory
33 from IPython.kernel.configobjfactory import AdaptedConfiguredObjectFactory
34 from IPython.kernel.error import SecurityError
34 from IPython.kernel.error import SecurityError
35
35
36 from IPython.utils.importstring import import_item
36 from IPython.utils.importstring import import_item
37 from IPython.utils.path import expand_path
37 from IPython.utils.path import expand_path
38 from IPython.utils.traitlets import Int, Str, Bool, Instance
38 from IPython.utils.traitlets import Int, Str, Bool, Instance
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Code
41 # Code
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44
44
45 # We do this so if a user doesn't have OpenSSL installed, it will try to use
45 # We do this so if a user doesn't have OpenSSL installed, it will try to use
46 # an UnauthenticatedTub. But, they will still run into problems if they
46 # an UnauthenticatedTub. But, they will still run into problems if they
47 # try to use encrypted furls.
47 # try to use encrypted furls.
48 try:
48 try:
49 import OpenSSL
49 import OpenSSL
50 except:
50 except:
51 Tub = UnauthenticatedTub
51 Tub = UnauthenticatedTub
52 have_crypto = False
52 have_crypto = False
53 else:
53 else:
54 have_crypto = True
54 have_crypto = True
55
55
56
56
57 class FURLError(Exception):
57 class FURLError(Exception):
58 pass
58 pass
59
59
60
60
61 def check_furl_file_security(furl_file, secure):
61 def check_furl_file_security(furl_file, secure):
62 """Remove the old furl_file if changing security modes."""
62 """Remove the old furl_file if changing security modes."""
63 furl_file = expand_path(furl_file)
63 furl_file = expand_path(furl_file)
64 if os.path.isfile(furl_file):
64 if os.path.isfile(furl_file):
65 with open(furl_file, 'r') as f:
65 with open(furl_file, 'r') as f:
66 oldfurl = f.read().strip()
66 oldfurl = f.read().strip()
67 if (oldfurl.startswith('pb://') and not secure) or (oldfurl.startswith('pbu://') and secure):
67 if (oldfurl.startswith('pb://') and not secure) or (oldfurl.startswith('pbu://') and secure):
68 os.remove(furl_file)
68 os.remove(furl_file)
69
69
70
70
71 def is_secure(furl):
71 def is_secure(furl):
72 """Is the given FURL secure or not."""
72 """Is the given FURL secure or not."""
73 if is_valid_furl(furl):
73 if is_valid_furl(furl):
74 if furl.startswith("pb://"):
74 if furl.startswith("pb://"):
75 return True
75 return True
76 elif furl.startswith("pbu://"):
76 elif furl.startswith("pbu://"):
77 return False
77 return False
78 else:
78 else:
79 raise FURLError("invalid FURL: %s" % furl)
79 raise FURLError("invalid FURL: %s" % furl)
80
80
81
81
82 def is_valid_furl(furl):
82 def is_valid_furl(furl):
83 """Is the str a valid FURL or not."""
83 """Is the str a valid FURL or not."""
84 if isinstance(furl, str):
84 if isinstance(furl, str):
85 if furl.startswith("pb://") or furl.startswith("pbu://"):
85 if furl.startswith("pb://") or furl.startswith("pbu://"):
86 return True
86 return True
87 else:
87 else:
88 return False
88 return False
89 else:
89 else:
90 return False
90 return False
91
91
92
92
93 def is_valid_furl_file(furl_or_file):
93 def is_valid_furl_file(furl_or_file):
94 """See if furl_or_file exists and contains a valid FURL.
94 """See if furl_or_file exists and contains a valid FURL.
95
95
96 This doesn't try to read the contents because often we have to validate
96 This doesn't try to read the contents because often we have to validate
97 FURL files that are created, but don't yet have a FURL written to them.
97 FURL files that are created, but don't yet have a FURL written to them.
98 """
98 """
99 if isinstance(furl_or_file, (str, unicode)):
99 if isinstance(furl_or_file, (str, unicode)):
100 path, furl_filename = os.path.split(furl_or_file)
100 path, furl_filename = os.path.split(furl_or_file)
101 if os.path.isdir(path) and furl_filename.endswith('.furl'):
101 if os.path.isdir(path) and furl_filename.endswith('.furl'):
102 return True
102 return True
103 return False
103 return False
104
104
105
105
106 def find_furl(furl_or_file):
106 def find_furl(furl_or_file):
107 """Find, validate and return a FURL in a string or file.
107 """Find, validate and return a FURL in a string or file.
108
108
109 This calls :func:`IPython.utils.path.expand_path` on the argument to
109 This calls :func:`IPython.utils.path.expand_path` on the argument to
110 properly handle ``~`` and ``$`` variables in the path.
110 properly handle ``~`` and ``$`` variables in the path.
111 """
111 """
112 if is_valid_furl(furl_or_file):
112 if is_valid_furl(furl_or_file):
113 return furl_or_file
113 return furl_or_file
114 furl_or_file = expand_path(furl_or_file)
114 furl_or_file = expand_path(furl_or_file)
115 if is_valid_furl_file(furl_or_file):
115 if is_valid_furl_file(furl_or_file):
116 with open(furl_or_file, 'r') as f:
116 with open(furl_or_file, 'r') as f:
117 furl = f.read().strip()
117 furl = f.read().strip()
118 if is_valid_furl(furl):
118 if is_valid_furl(furl):
119 return furl
119 return furl
120 raise FURLError("Not a valid FURL or FURL file: %r" % furl_or_file)
120 raise FURLError("Not a valid FURL or FURL file: %r" % furl_or_file)
121
121
122
122
123 def is_valid_furl_or_file(furl_or_file):
123 def is_valid_furl_or_file(furl_or_file):
124 """Validate a FURL or a FURL file.
124 """Validate a FURL or a FURL file.
125
125
126 If ``furl_or_file`` looks like a file, we simply make sure its directory
126 If ``furl_or_file`` looks like a file, we simply make sure its directory
127 exists and that it has a ``.furl`` file extension. We don't try to see
127 exists and that it has a ``.furl`` file extension. We don't try to see
128 if the FURL file exists or to read its contents. This is useful for
128 if the FURL file exists or to read its contents. This is useful for
129 cases where auto re-connection is being used.
129 cases where auto re-connection is being used.
130 """
130 """
131 if is_valid_furl(furl_or_file) or is_valid_furl_file(furl_or_file):
131 if is_valid_furl(furl_or_file) or is_valid_furl_file(furl_or_file):
132 return True
132 return True
133 else:
133 else:
134 return False
134 return False
135
135
136
136
137 def validate_furl_or_file(furl_or_file):
137 def validate_furl_or_file(furl_or_file):
138 """Like :func:`is_valid_furl_or_file`, but raises an error."""
138 """Like :func:`is_valid_furl_or_file`, but raises an error."""
139 if not is_valid_furl_or_file(furl_or_file):
139 if not is_valid_furl_or_file(furl_or_file):
140 raise FURLError('Not a valid FURL or FURL file: %r' % furl_or_file)
140 raise FURLError('Not a valid FURL or FURL file: %r' % furl_or_file)
141
141
142
142
143 def get_temp_furlfile(filename):
143 def get_temp_furlfile(filename):
144 """Return a temporary FURL file."""
144 """Return a temporary FURL file."""
145 return tempfile.mktemp(dir=os.path.dirname(filename),
145 return tempfile.mktemp(dir=os.path.dirname(filename),
146 prefix=os.path.basename(filename))
146 prefix=os.path.basename(filename))
147
147
148
148
149 def make_tub(ip, port, secure, cert_file):
149 def make_tub(ip, port, secure, cert_file):
150 """Create a listening tub given an ip, port, and cert_file location.
150 """Create a listening tub given an ip, port, and cert_file location.
151
151
152 Parameters
152 Parameters
153 ----------
153 ----------
154 ip : str
154 ip : str
155 The ip address or hostname that the tub should listen on.
155 The ip address or hostname that the tub should listen on.
156 Empty means all interfaces.
156 Empty means all interfaces.
157 port : int
157 port : int
158 The port that the tub should listen on. A value of 0 means
158 The port that the tub should listen on. A value of 0 means
159 pick a random port
159 pick a random port
160 secure: bool
160 secure: bool
161 Will the connection be secure (in the Foolscap sense).
161 Will the connection be secure (in the Foolscap sense).
162 cert_file: str
162 cert_file: str
163 A filename of a file to be used for theSSL certificate.
163 A filename of a file to be used for theSSL certificate.
164
164
165 Returns
165 Returns
166 -------
166 -------
167 A tub, listener tuple.
167 A tub, listener tuple.
168 """
168 """
169 if secure:
169 if secure:
170 if have_crypto:
170 if have_crypto:
171 tub = Tub(certFile=cert_file)
171 tub = Tub(certFile=cert_file)
172 else:
172 else:
173 raise SecurityError("OpenSSL/pyOpenSSL is not available, so we "
173 raise SecurityError("OpenSSL/pyOpenSSL is not available, so we "
174 "can't run in secure mode. Try running without "
174 "can't run in secure mode. Try running without "
175 "security using 'ipcontroller -xy'.")
175 "security using 'ipcontroller -xy'.")
176 else:
176 else:
177 tub = UnauthenticatedTub()
177 tub = UnauthenticatedTub()
178
178
179 # Set the strport based on the ip and port and start listening
179 # Set the strport based on the ip and port and start listening
180 if ip == '':
180 if ip == '':
181 strport = "tcp:%i" % port
181 strport = "tcp:%i" % port
182 else:
182 else:
183 strport = "tcp:%i:interface=%s" % (port, ip)
183 strport = "tcp:%i:interface=%s" % (port, ip)
184 log.msg("Starting listener with [secure=%r] on: %s" % (secure, strport))
184 log.msg("Starting listener with [secure=%r] on: %s" % (secure, strport))
185 listener = tub.listenOn(strport)
185 listener = tub.listenOn(strport)
186
186
187 return tub, listener
187 return tub, listener
188
188
189
189
190 class FCServiceFactory(AdaptedConfiguredObjectFactory):
190 class FCServiceFactory(AdaptedConfiguredObjectFactory):
191 """This class creates a tub with various services running in it.
191 """This class creates a tub with various services running in it.
192
192
193 The basic idea is that :meth:`create` returns a running :class:`Tub`
193 The basic idea is that :meth:`create` returns a running :class:`Tub`
194 instance that has a number of Foolscap references registered in it.
194 instance that has a number of Foolscap references registered in it. This
195 This class is a subclass of :class:`IPython.core.component.Component`
195 class is a subclass of :class:`IPython.config.configurable.Configurable`
196 so the IPython configuration and component system are used.
196 so the IPython configuration system is used.
197
197
198 Attributes
198 Attributes
199 ----------
199 ----------
200 interfaces : Config
200 interfaces : Config
201 A Config instance whose values are sub-Config objects having two
201 A Config instance whose values are sub-Config objects having two
202 keys: furl_file and interface_chain.
202 keys: furl_file and interface_chain.
203
203
204 The other attributes are the standard ones for Foolscap.
204 The other attributes are the standard ones for Foolscap.
205 """
205 """
206
206
207 ip = Str('', config=True)
207 ip = Str('', config=True)
208 port = Int(0, config=True)
208 port = Int(0, config=True)
209 secure = Bool(True, config=True)
209 secure = Bool(True, config=True)
210 cert_file = Str('', config=True)
210 cert_file = Str('', config=True)
211 location = Str('', config=True)
211 location = Str('', config=True)
212 reuse_furls = Bool(False, config=True)
212 reuse_furls = Bool(False, config=True)
213 interfaces = Instance(klass=Config, kw={}, allow_none=False, config=True)
213 interfaces = Instance(klass=Config, kw={}, allow_none=False, config=True)
214
214
215 def __init__(self, config, adaptee):
215 def __init__(self, config, adaptee):
216 super(FCServiceFactory, self).__init__(config, adaptee)
216 super(FCServiceFactory, self).__init__(config, adaptee)
217 self._check_reuse_furls()
217 self._check_reuse_furls()
218
218
219 def _ip_changed(self, name, old, new):
219 def _ip_changed(self, name, old, new):
220 if new == 'localhost' or new == '127.0.0.1':
220 if new == 'localhost' or new == '127.0.0.1':
221 self.location = '127.0.0.1'
221 self.location = '127.0.0.1'
222
222
223 def _check_reuse_furls(self):
223 def _check_reuse_furls(self):
224 furl_files = [i.furl_file for i in self.interfaces.values()]
224 furl_files = [i.furl_file for i in self.interfaces.values()]
225 for ff in furl_files:
225 for ff in furl_files:
226 fullfile = self._get_security_file(ff)
226 fullfile = self._get_security_file(ff)
227 if self.reuse_furls:
227 if self.reuse_furls:
228 if self.port==0:
228 if self.port==0:
229 raise FURLError("You are trying to reuse the FURL file "
229 raise FURLError("You are trying to reuse the FURL file "
230 "for this connection, but the port for this connection "
230 "for this connection, but the port for this connection "
231 "is set to 0 (autoselect). To reuse the FURL file "
231 "is set to 0 (autoselect). To reuse the FURL file "
232 "you need to specify specific port to listen on."
232 "you need to specify specific port to listen on."
233 )
233 )
234 else:
234 else:
235 log.msg("Reusing FURL file: %s" % fullfile)
235 log.msg("Reusing FURL file: %s" % fullfile)
236 else:
236 else:
237 if os.path.isfile(fullfile):
237 if os.path.isfile(fullfile):
238 log.msg("Removing old FURL file: %s" % fullfile)
238 log.msg("Removing old FURL file: %s" % fullfile)
239 os.remove(fullfile)
239 os.remove(fullfile)
240
240
241 def _get_security_file(self, filename):
241 def _get_security_file(self, filename):
242 return os.path.join(self.config.Global.security_dir, filename)
242 return os.path.join(self.config.Global.security_dir, filename)
243
243
244 def create(self):
244 def create(self):
245 """Create and return the Foolscap tub with everything running."""
245 """Create and return the Foolscap tub with everything running."""
246
246
247 self.tub, self.listener = make_tub(
247 self.tub, self.listener = make_tub(
248 self.ip, self.port, self.secure,
248 self.ip, self.port, self.secure,
249 self._get_security_file(self.cert_file)
249 self._get_security_file(self.cert_file)
250 )
250 )
251 # log.msg("Interfaces to register [%r]: %r" % \
251 # log.msg("Interfaces to register [%r]: %r" % \
252 # (self.__class__, self.interfaces))
252 # (self.__class__, self.interfaces))
253 if not self.secure:
253 if not self.secure:
254 log.msg("WARNING: running with no security: %s" % \
254 log.msg("WARNING: running with no security: %s" % \
255 self.__class__.__name__)
255 self.__class__.__name__)
256 reactor.callWhenRunning(self.set_location_and_register)
256 reactor.callWhenRunning(self.set_location_and_register)
257 return self.tub
257 return self.tub
258
258
259 def set_location_and_register(self):
259 def set_location_and_register(self):
260 """Set the location for the tub and return a deferred."""
260 """Set the location for the tub and return a deferred."""
261
261
262 if self.location == '':
262 if self.location == '':
263 d = self.tub.setLocationAutomatically()
263 d = self.tub.setLocationAutomatically()
264 else:
264 else:
265 d = defer.maybeDeferred(self.tub.setLocation,
265 d = defer.maybeDeferred(self.tub.setLocation,
266 "%s:%i" % (self.location, self.listener.getPortnum()))
266 "%s:%i" % (self.location, self.listener.getPortnum()))
267 self.adapt_to_interfaces(d)
267 self.adapt_to_interfaces(d)
268
268
269 def adapt_to_interfaces(self, d):
269 def adapt_to_interfaces(self, d):
270 """Run through the interfaces, adapt and register."""
270 """Run through the interfaces, adapt and register."""
271
271
272 for ifname, ifconfig in self.interfaces.iteritems():
272 for ifname, ifconfig in self.interfaces.iteritems():
273 ff = self._get_security_file(ifconfig.furl_file)
273 ff = self._get_security_file(ifconfig.furl_file)
274 log.msg("Adapting [%s] to interface: %s" % \
274 log.msg("Adapting [%s] to interface: %s" % \
275 (self.adaptee.__class__.__name__, ifname))
275 (self.adaptee.__class__.__name__, ifname))
276 log.msg("Saving FURL for interface [%s] to file: %s" % (ifname, ff))
276 log.msg("Saving FURL for interface [%s] to file: %s" % (ifname, ff))
277 check_furl_file_security(ff, self.secure)
277 check_furl_file_security(ff, self.secure)
278 adaptee = self.adaptee
278 adaptee = self.adaptee
279 for i in ifconfig.interface_chain:
279 for i in ifconfig.interface_chain:
280 adaptee = import_item(i)(adaptee)
280 adaptee = import_item(i)(adaptee)
281 d.addCallback(self.register, adaptee, furl_file=ff)
281 d.addCallback(self.register, adaptee, furl_file=ff)
282
282
283 def register(self, empty, ref, furl_file):
283 def register(self, empty, ref, furl_file):
284 """Register the reference with the FURL file.
284 """Register the reference with the FURL file.
285
285
286 The FURL file is created and then moved to make sure that when the
286 The FURL file is created and then moved to make sure that when the
287 file appears, the buffer has been flushed and the file closed. This
287 file appears, the buffer has been flushed and the file closed. This
288 is not done if we are re-using FURLS however.
288 is not done if we are re-using FURLS however.
289 """
289 """
290 if self.reuse_furls:
290 if self.reuse_furls:
291 self.tub.registerReference(ref, furlFile=furl_file)
291 self.tub.registerReference(ref, furlFile=furl_file)
292 else:
292 else:
293 temp_furl_file = get_temp_furlfile(furl_file)
293 temp_furl_file = get_temp_furlfile(furl_file)
294 self.tub.registerReference(ref, furlFile=temp_furl_file)
294 self.tub.registerReference(ref, furlFile=temp_furl_file)
295 os.rename(temp_furl_file, furl_file)
295 os.rename(temp_furl_file, furl_file)
296
296
@@ -1,835 +1,834 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Facilities for launching IPython processes asynchronously.
4 Facilities for launching IPython processes asynchronously.
5 """
5 """
6
6
7 #-----------------------------------------------------------------------------
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2008-2009 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
17
18 import os
18 import os
19 import re
19 import re
20 import sys
20 import sys
21
21
22 from IPython.core.component import Component
22 from IPython.config.configurable import Configurable
23 from IPython.external import Itpl
23 from IPython.external import Itpl
24 from IPython.utils.traitlets import Str, Int, List, Unicode
24 from IPython.utils.traitlets import Str, Int, List, Unicode
25 from IPython.utils.path import get_ipython_module_path
25 from IPython.utils.path import get_ipython_module_path
26 from IPython.utils.process import find_cmd, pycmd2argv, FindCmdError
26 from IPython.utils.process import find_cmd, pycmd2argv, FindCmdError
27 from IPython.kernel.twistedutil import (
27 from IPython.kernel.twistedutil import (
28 gatherBoth,
28 gatherBoth,
29 make_deferred,
29 make_deferred,
30 sleep_deferred
30 sleep_deferred
31 )
31 )
32 from IPython.kernel.winhpcjob import (
32 from IPython.kernel.winhpcjob import (
33 IPControllerTask, IPEngineTask,
33 IPControllerTask, IPEngineTask,
34 IPControllerJob, IPEngineSetJob
34 IPControllerJob, IPEngineSetJob
35 )
35 )
36
36
37 from twisted.internet import reactor, defer
37 from twisted.internet import reactor, defer
38 from twisted.internet.defer import inlineCallbacks
38 from twisted.internet.defer import inlineCallbacks
39 from twisted.internet.protocol import ProcessProtocol
39 from twisted.internet.protocol import ProcessProtocol
40 from twisted.internet.utils import getProcessOutput
40 from twisted.internet.utils import getProcessOutput
41 from twisted.internet.error import ProcessDone, ProcessTerminated
41 from twisted.internet.error import ProcessDone, ProcessTerminated
42 from twisted.python import log
42 from twisted.python import log
43 from twisted.python.failure import Failure
43 from twisted.python.failure import Failure
44
44
45
45
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47 # Paths to the kernel apps
47 # Paths to the kernel apps
48 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
49
49
50
50
51 ipcluster_cmd_argv = pycmd2argv(get_ipython_module_path(
51 ipcluster_cmd_argv = pycmd2argv(get_ipython_module_path(
52 'IPython.kernel.ipclusterapp'
52 'IPython.kernel.ipclusterapp'
53 ))
53 ))
54
54
55 ipengine_cmd_argv = pycmd2argv(get_ipython_module_path(
55 ipengine_cmd_argv = pycmd2argv(get_ipython_module_path(
56 'IPython.kernel.ipengineapp'
56 'IPython.kernel.ipengineapp'
57 ))
57 ))
58
58
59 ipcontroller_cmd_argv = pycmd2argv(get_ipython_module_path(
59 ipcontroller_cmd_argv = pycmd2argv(get_ipython_module_path(
60 'IPython.kernel.ipcontrollerapp'
60 'IPython.kernel.ipcontrollerapp'
61 ))
61 ))
62
62
63 #-----------------------------------------------------------------------------
63 #-----------------------------------------------------------------------------
64 # Base launchers and errors
64 # Base launchers and errors
65 #-----------------------------------------------------------------------------
65 #-----------------------------------------------------------------------------
66
66
67
67
68 class LauncherError(Exception):
68 class LauncherError(Exception):
69 pass
69 pass
70
70
71
71
72 class ProcessStateError(LauncherError):
72 class ProcessStateError(LauncherError):
73 pass
73 pass
74
74
75
75
76 class UnknownStatus(LauncherError):
76 class UnknownStatus(LauncherError):
77 pass
77 pass
78
78
79
79
80 class BaseLauncher(Component):
80 class BaseLauncher(Configurable):
81 """An asbtraction for starting, stopping and signaling a process."""
81 """An asbtraction for starting, stopping and signaling a process."""
82
82
83 # In all of the launchers, the work_dir is where child processes will be
83 # In all of the launchers, the work_dir is where child processes will be
84 # run. This will usually be the cluster_dir, but may not be. any work_dir
84 # run. This will usually be the cluster_dir, but may not be. any work_dir
85 # passed into the __init__ method will override the config value.
85 # passed into the __init__ method will override the config value.
86 # This should not be used to set the work_dir for the actual engine
86 # This should not be used to set the work_dir for the actual engine
87 # and controller. Instead, use their own config files or the
87 # and controller. Instead, use their own config files or the
88 # controller_args, engine_args attributes of the launchers to add
88 # controller_args, engine_args attributes of the launchers to add
89 # the --work-dir option.
89 # the --work-dir option.
90 work_dir = Unicode(u'')
90 work_dir = Unicode(u'')
91
91
92 def __init__(self, work_dir, parent=None, name=None, config=None):
92 def __init__(self, work_dir=u'', config=None):
93 super(BaseLauncher, self).__init__(parent, name, config)
93 super(BaseLauncher, self).__init__(work_dir=work_dir, config=config)
94 self.work_dir = work_dir
95 self.state = 'before' # can be before, running, after
94 self.state = 'before' # can be before, running, after
96 self.stop_deferreds = []
95 self.stop_deferreds = []
97 self.start_data = None
96 self.start_data = None
98 self.stop_data = None
97 self.stop_data = None
99
98
100 @property
99 @property
101 def args(self):
100 def args(self):
102 """A list of cmd and args that will be used to start the process.
101 """A list of cmd and args that will be used to start the process.
103
102
104 This is what is passed to :func:`spawnProcess` and the first element
103 This is what is passed to :func:`spawnProcess` and the first element
105 will be the process name.
104 will be the process name.
106 """
105 """
107 return self.find_args()
106 return self.find_args()
108
107
109 def find_args(self):
108 def find_args(self):
110 """The ``.args`` property calls this to find the args list.
109 """The ``.args`` property calls this to find the args list.
111
110
112 Subcommand should implement this to construct the cmd and args.
111 Subcommand should implement this to construct the cmd and args.
113 """
112 """
114 raise NotImplementedError('find_args must be implemented in a subclass')
113 raise NotImplementedError('find_args must be implemented in a subclass')
115
114
116 @property
115 @property
117 def arg_str(self):
116 def arg_str(self):
118 """The string form of the program arguments."""
117 """The string form of the program arguments."""
119 return ' '.join(self.args)
118 return ' '.join(self.args)
120
119
121 @property
120 @property
122 def running(self):
121 def running(self):
123 """Am I running."""
122 """Am I running."""
124 if self.state == 'running':
123 if self.state == 'running':
125 return True
124 return True
126 else:
125 else:
127 return False
126 return False
128
127
129 def start(self):
128 def start(self):
130 """Start the process.
129 """Start the process.
131
130
132 This must return a deferred that fires with information about the
131 This must return a deferred that fires with information about the
133 process starting (like a pid, job id, etc.).
132 process starting (like a pid, job id, etc.).
134 """
133 """
135 return defer.fail(
134 return defer.fail(
136 Failure(NotImplementedError(
135 Failure(NotImplementedError(
137 'start must be implemented in a subclass')
136 'start must be implemented in a subclass')
138 )
137 )
139 )
138 )
140
139
141 def stop(self):
140 def stop(self):
142 """Stop the process and notify observers of stopping.
141 """Stop the process and notify observers of stopping.
143
142
144 This must return a deferred that fires with information about the
143 This must return a deferred that fires with information about the
145 processing stopping, like errors that occur while the process is
144 processing stopping, like errors that occur while the process is
146 attempting to be shut down. This deferred won't fire when the process
145 attempting to be shut down. This deferred won't fire when the process
147 actually stops. To observe the actual process stopping, see
146 actually stops. To observe the actual process stopping, see
148 :func:`observe_stop`.
147 :func:`observe_stop`.
149 """
148 """
150 return defer.fail(
149 return defer.fail(
151 Failure(NotImplementedError(
150 Failure(NotImplementedError(
152 'stop must be implemented in a subclass')
151 'stop must be implemented in a subclass')
153 )
152 )
154 )
153 )
155
154
156 def observe_stop(self):
155 def observe_stop(self):
157 """Get a deferred that will fire when the process stops.
156 """Get a deferred that will fire when the process stops.
158
157
159 The deferred will fire with data that contains information about
158 The deferred will fire with data that contains information about
160 the exit status of the process.
159 the exit status of the process.
161 """
160 """
162 if self.state=='after':
161 if self.state=='after':
163 return defer.succeed(self.stop_data)
162 return defer.succeed(self.stop_data)
164 else:
163 else:
165 d = defer.Deferred()
164 d = defer.Deferred()
166 self.stop_deferreds.append(d)
165 self.stop_deferreds.append(d)
167 return d
166 return d
168
167
169 def notify_start(self, data):
168 def notify_start(self, data):
170 """Call this to trigger startup actions.
169 """Call this to trigger startup actions.
171
170
172 This logs the process startup and sets the state to 'running'. It is
171 This logs the process startup and sets the state to 'running'. It is
173 a pass-through so it can be used as a callback.
172 a pass-through so it can be used as a callback.
174 """
173 """
175
174
176 log.msg('Process %r started: %r' % (self.args[0], data))
175 log.msg('Process %r started: %r' % (self.args[0], data))
177 self.start_data = data
176 self.start_data = data
178 self.state = 'running'
177 self.state = 'running'
179 return data
178 return data
180
179
181 def notify_stop(self, data):
180 def notify_stop(self, data):
182 """Call this to trigger process stop actions.
181 """Call this to trigger process stop actions.
183
182
184 This logs the process stopping and sets the state to 'after'. Call
183 This logs the process stopping and sets the state to 'after'. Call
185 this to trigger all the deferreds from :func:`observe_stop`."""
184 this to trigger all the deferreds from :func:`observe_stop`."""
186
185
187 log.msg('Process %r stopped: %r' % (self.args[0], data))
186 log.msg('Process %r stopped: %r' % (self.args[0], data))
188 self.stop_data = data
187 self.stop_data = data
189 self.state = 'after'
188 self.state = 'after'
190 for i in range(len(self.stop_deferreds)):
189 for i in range(len(self.stop_deferreds)):
191 d = self.stop_deferreds.pop()
190 d = self.stop_deferreds.pop()
192 d.callback(data)
191 d.callback(data)
193 return data
192 return data
194
193
195 def signal(self, sig):
194 def signal(self, sig):
196 """Signal the process.
195 """Signal the process.
197
196
198 Return a semi-meaningless deferred after signaling the process.
197 Return a semi-meaningless deferred after signaling the process.
199
198
200 Parameters
199 Parameters
201 ----------
200 ----------
202 sig : str or int
201 sig : str or int
203 'KILL', 'INT', etc., or any signal number
202 'KILL', 'INT', etc., or any signal number
204 """
203 """
205 return defer.fail(
204 return defer.fail(
206 Failure(NotImplementedError(
205 Failure(NotImplementedError(
207 'signal must be implemented in a subclass')
206 'signal must be implemented in a subclass')
208 )
207 )
209 )
208 )
210
209
211
210
212 #-----------------------------------------------------------------------------
211 #-----------------------------------------------------------------------------
213 # Local process launchers
212 # Local process launchers
214 #-----------------------------------------------------------------------------
213 #-----------------------------------------------------------------------------
215
214
216
215
217 class LocalProcessLauncherProtocol(ProcessProtocol):
216 class LocalProcessLauncherProtocol(ProcessProtocol):
218 """A ProcessProtocol to go with the LocalProcessLauncher."""
217 """A ProcessProtocol to go with the LocalProcessLauncher."""
219
218
220 def __init__(self, process_launcher):
219 def __init__(self, process_launcher):
221 self.process_launcher = process_launcher
220 self.process_launcher = process_launcher
222 self.pid = None
221 self.pid = None
223
222
224 def connectionMade(self):
223 def connectionMade(self):
225 self.pid = self.transport.pid
224 self.pid = self.transport.pid
226 self.process_launcher.notify_start(self.transport.pid)
225 self.process_launcher.notify_start(self.transport.pid)
227
226
228 def processEnded(self, status):
227 def processEnded(self, status):
229 value = status.value
228 value = status.value
230 if isinstance(value, ProcessDone):
229 if isinstance(value, ProcessDone):
231 self.process_launcher.notify_stop(
230 self.process_launcher.notify_stop(
232 {'exit_code':0,
231 {'exit_code':0,
233 'signal':None,
232 'signal':None,
234 'status':None,
233 'status':None,
235 'pid':self.pid
234 'pid':self.pid
236 }
235 }
237 )
236 )
238 elif isinstance(value, ProcessTerminated):
237 elif isinstance(value, ProcessTerminated):
239 self.process_launcher.notify_stop(
238 self.process_launcher.notify_stop(
240 {'exit_code':value.exitCode,
239 {'exit_code':value.exitCode,
241 'signal':value.signal,
240 'signal':value.signal,
242 'status':value.status,
241 'status':value.status,
243 'pid':self.pid
242 'pid':self.pid
244 }
243 }
245 )
244 )
246 else:
245 else:
247 raise UnknownStatus("Unknown exit status, this is probably a "
246 raise UnknownStatus("Unknown exit status, this is probably a "
248 "bug in Twisted")
247 "bug in Twisted")
249
248
250 def outReceived(self, data):
249 def outReceived(self, data):
251 log.msg(data)
250 log.msg(data)
252
251
253 def errReceived(self, data):
252 def errReceived(self, data):
254 log.err(data)
253 log.err(data)
255
254
256
255
257 class LocalProcessLauncher(BaseLauncher):
256 class LocalProcessLauncher(BaseLauncher):
258 """Start and stop an external process in an asynchronous manner.
257 """Start and stop an external process in an asynchronous manner.
259
258
260 This will launch the external process with a working directory of
259 This will launch the external process with a working directory of
261 ``self.work_dir``.
260 ``self.work_dir``.
262 """
261 """
263
262
264 # This is used to to construct self.args, which is passed to
263 # This is used to to construct self.args, which is passed to
265 # spawnProcess.
264 # spawnProcess.
266 cmd_and_args = List([])
265 cmd_and_args = List([])
267
266
268 def __init__(self, work_dir, parent=None, name=None, config=None):
267 def __init__(self, work_dir=u'', config=None):
269 super(LocalProcessLauncher, self).__init__(
268 super(LocalProcessLauncher, self).__init__(
270 work_dir, parent, name, config
269 work_dir=work_dir, config=config
271 )
270 )
272 self.process_protocol = None
271 self.process_protocol = None
273 self.start_deferred = None
272 self.start_deferred = None
274
273
275 def find_args(self):
274 def find_args(self):
276 return self.cmd_and_args
275 return self.cmd_and_args
277
276
278 def start(self):
277 def start(self):
279 if self.state == 'before':
278 if self.state == 'before':
280 self.process_protocol = LocalProcessLauncherProtocol(self)
279 self.process_protocol = LocalProcessLauncherProtocol(self)
281 self.start_deferred = defer.Deferred()
280 self.start_deferred = defer.Deferred()
282 self.process_transport = reactor.spawnProcess(
281 self.process_transport = reactor.spawnProcess(
283 self.process_protocol,
282 self.process_protocol,
284 str(self.args[0]), # twisted expects these to be str, not unicode
283 str(self.args[0]), # twisted expects these to be str, not unicode
285 [str(a) for a in self.args], # str expected, not unicode
284 [str(a) for a in self.args], # str expected, not unicode
286 env=os.environ,
285 env=os.environ,
287 path=self.work_dir # start in the work_dir
286 path=self.work_dir # start in the work_dir
288 )
287 )
289 return self.start_deferred
288 return self.start_deferred
290 else:
289 else:
291 s = 'The process was already started and has state: %r' % self.state
290 s = 'The process was already started and has state: %r' % self.state
292 return defer.fail(ProcessStateError(s))
291 return defer.fail(ProcessStateError(s))
293
292
294 def notify_start(self, data):
293 def notify_start(self, data):
295 super(LocalProcessLauncher, self).notify_start(data)
294 super(LocalProcessLauncher, self).notify_start(data)
296 self.start_deferred.callback(data)
295 self.start_deferred.callback(data)
297
296
298 def stop(self):
297 def stop(self):
299 return self.interrupt_then_kill()
298 return self.interrupt_then_kill()
300
299
301 @make_deferred
300 @make_deferred
302 def signal(self, sig):
301 def signal(self, sig):
303 if self.state == 'running':
302 if self.state == 'running':
304 self.process_transport.signalProcess(sig)
303 self.process_transport.signalProcess(sig)
305
304
306 @inlineCallbacks
305 @inlineCallbacks
307 def interrupt_then_kill(self, delay=2.0):
306 def interrupt_then_kill(self, delay=2.0):
308 """Send INT, wait a delay and then send KILL."""
307 """Send INT, wait a delay and then send KILL."""
309 yield self.signal('INT')
308 yield self.signal('INT')
310 yield sleep_deferred(delay)
309 yield sleep_deferred(delay)
311 yield self.signal('KILL')
310 yield self.signal('KILL')
312
311
313
312
314 class LocalControllerLauncher(LocalProcessLauncher):
313 class LocalControllerLauncher(LocalProcessLauncher):
315 """Launch a controller as a regular external process."""
314 """Launch a controller as a regular external process."""
316
315
317 controller_cmd = List(ipcontroller_cmd_argv, config=True)
316 controller_cmd = List(ipcontroller_cmd_argv, config=True)
318 # Command line arguments to ipcontroller.
317 # Command line arguments to ipcontroller.
319 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
318 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
320
319
321 def find_args(self):
320 def find_args(self):
322 return self.controller_cmd + self.controller_args
321 return self.controller_cmd + self.controller_args
323
322
324 def start(self, cluster_dir):
323 def start(self, cluster_dir):
325 """Start the controller by cluster_dir."""
324 """Start the controller by cluster_dir."""
326 self.controller_args.extend(['--cluster-dir', cluster_dir])
325 self.controller_args.extend(['--cluster-dir', cluster_dir])
327 self.cluster_dir = unicode(cluster_dir)
326 self.cluster_dir = unicode(cluster_dir)
328 log.msg("Starting LocalControllerLauncher: %r" % self.args)
327 log.msg("Starting LocalControllerLauncher: %r" % self.args)
329 return super(LocalControllerLauncher, self).start()
328 return super(LocalControllerLauncher, self).start()
330
329
331
330
332 class LocalEngineLauncher(LocalProcessLauncher):
331 class LocalEngineLauncher(LocalProcessLauncher):
333 """Launch a single engine as a regular externall process."""
332 """Launch a single engine as a regular externall process."""
334
333
335 engine_cmd = List(ipengine_cmd_argv, config=True)
334 engine_cmd = List(ipengine_cmd_argv, config=True)
336 # Command line arguments for ipengine.
335 # Command line arguments for ipengine.
337 engine_args = List(
336 engine_args = List(
338 ['--log-to-file','--log-level', '40'], config=True
337 ['--log-to-file','--log-level', '40'], config=True
339 )
338 )
340
339
341 def find_args(self):
340 def find_args(self):
342 return self.engine_cmd + self.engine_args
341 return self.engine_cmd + self.engine_args
343
342
344 def start(self, cluster_dir):
343 def start(self, cluster_dir):
345 """Start the engine by cluster_dir."""
344 """Start the engine by cluster_dir."""
346 self.engine_args.extend(['--cluster-dir', cluster_dir])
345 self.engine_args.extend(['--cluster-dir', cluster_dir])
347 self.cluster_dir = unicode(cluster_dir)
346 self.cluster_dir = unicode(cluster_dir)
348 return super(LocalEngineLauncher, self).start()
347 return super(LocalEngineLauncher, self).start()
349
348
350
349
351 class LocalEngineSetLauncher(BaseLauncher):
350 class LocalEngineSetLauncher(BaseLauncher):
352 """Launch a set of engines as regular external processes."""
351 """Launch a set of engines as regular external processes."""
353
352
354 # Command line arguments for ipengine.
353 # Command line arguments for ipengine.
355 engine_args = List(
354 engine_args = List(
356 ['--log-to-file','--log-level', '40'], config=True
355 ['--log-to-file','--log-level', '40'], config=True
357 )
356 )
358
357
359 def __init__(self, work_dir, parent=None, name=None, config=None):
358 def __init__(self, work_dir=u'', config=None):
360 super(LocalEngineSetLauncher, self).__init__(
359 super(LocalEngineSetLauncher, self).__init__(
361 work_dir, parent, name, config
360 work_dir=work_dir, config=config
362 )
361 )
363 self.launchers = []
362 self.launchers = []
364
363
365 def start(self, n, cluster_dir):
364 def start(self, n, cluster_dir):
366 """Start n engines by profile or cluster_dir."""
365 """Start n engines by profile or cluster_dir."""
367 self.cluster_dir = unicode(cluster_dir)
366 self.cluster_dir = unicode(cluster_dir)
368 dlist = []
367 dlist = []
369 for i in range(n):
368 for i in range(n):
370 el = LocalEngineLauncher(self.work_dir, self)
369 el = LocalEngineLauncher(work_dir=self.work_dir, config=self.config)
371 # Copy the engine args over to each engine launcher.
370 # Copy the engine args over to each engine launcher.
372 import copy
371 import copy
373 el.engine_args = copy.deepcopy(self.engine_args)
372 el.engine_args = copy.deepcopy(self.engine_args)
374 d = el.start(cluster_dir)
373 d = el.start(cluster_dir)
375 if i==0:
374 if i==0:
376 log.msg("Starting LocalEngineSetLauncher: %r" % el.args)
375 log.msg("Starting LocalEngineSetLauncher: %r" % el.args)
377 self.launchers.append(el)
376 self.launchers.append(el)
378 dlist.append(d)
377 dlist.append(d)
379 # The consumeErrors here could be dangerous
378 # The consumeErrors here could be dangerous
380 dfinal = gatherBoth(dlist, consumeErrors=True)
379 dfinal = gatherBoth(dlist, consumeErrors=True)
381 dfinal.addCallback(self.notify_start)
380 dfinal.addCallback(self.notify_start)
382 return dfinal
381 return dfinal
383
382
384 def find_args(self):
383 def find_args(self):
385 return ['engine set']
384 return ['engine set']
386
385
387 def signal(self, sig):
386 def signal(self, sig):
388 dlist = []
387 dlist = []
389 for el in self.launchers:
388 for el in self.launchers:
390 d = el.signal(sig)
389 d = el.signal(sig)
391 dlist.append(d)
390 dlist.append(d)
392 dfinal = gatherBoth(dlist, consumeErrors=True)
391 dfinal = gatherBoth(dlist, consumeErrors=True)
393 return dfinal
392 return dfinal
394
393
395 def interrupt_then_kill(self, delay=1.0):
394 def interrupt_then_kill(self, delay=1.0):
396 dlist = []
395 dlist = []
397 for el in self.launchers:
396 for el in self.launchers:
398 d = el.interrupt_then_kill(delay)
397 d = el.interrupt_then_kill(delay)
399 dlist.append(d)
398 dlist.append(d)
400 dfinal = gatherBoth(dlist, consumeErrors=True)
399 dfinal = gatherBoth(dlist, consumeErrors=True)
401 return dfinal
400 return dfinal
402
401
403 def stop(self):
402 def stop(self):
404 return self.interrupt_then_kill()
403 return self.interrupt_then_kill()
405
404
406 def observe_stop(self):
405 def observe_stop(self):
407 dlist = [el.observe_stop() for el in self.launchers]
406 dlist = [el.observe_stop() for el in self.launchers]
408 dfinal = gatherBoth(dlist, consumeErrors=False)
407 dfinal = gatherBoth(dlist, consumeErrors=False)
409 dfinal.addCallback(self.notify_stop)
408 dfinal.addCallback(self.notify_stop)
410 return dfinal
409 return dfinal
411
410
412
411
413 #-----------------------------------------------------------------------------
412 #-----------------------------------------------------------------------------
414 # MPIExec launchers
413 # MPIExec launchers
415 #-----------------------------------------------------------------------------
414 #-----------------------------------------------------------------------------
416
415
417
416
418 class MPIExecLauncher(LocalProcessLauncher):
417 class MPIExecLauncher(LocalProcessLauncher):
419 """Launch an external process using mpiexec."""
418 """Launch an external process using mpiexec."""
420
419
421 # The mpiexec command to use in starting the process.
420 # The mpiexec command to use in starting the process.
422 mpi_cmd = List(['mpiexec'], config=True)
421 mpi_cmd = List(['mpiexec'], config=True)
423 # The command line arguments to pass to mpiexec.
422 # The command line arguments to pass to mpiexec.
424 mpi_args = List([], config=True)
423 mpi_args = List([], config=True)
425 # The program to start using mpiexec.
424 # The program to start using mpiexec.
426 program = List(['date'], config=True)
425 program = List(['date'], config=True)
427 # The command line argument to the program.
426 # The command line argument to the program.
428 program_args = List([], config=True)
427 program_args = List([], config=True)
429 # The number of instances of the program to start.
428 # The number of instances of the program to start.
430 n = Int(1, config=True)
429 n = Int(1, config=True)
431
430
432 def find_args(self):
431 def find_args(self):
433 """Build self.args using all the fields."""
432 """Build self.args using all the fields."""
434 return self.mpi_cmd + ['-n', self.n] + self.mpi_args + \
433 return self.mpi_cmd + ['-n', self.n] + self.mpi_args + \
435 self.program + self.program_args
434 self.program + self.program_args
436
435
437 def start(self, n):
436 def start(self, n):
438 """Start n instances of the program using mpiexec."""
437 """Start n instances of the program using mpiexec."""
439 self.n = n
438 self.n = n
440 return super(MPIExecLauncher, self).start()
439 return super(MPIExecLauncher, self).start()
441
440
442
441
443 class MPIExecControllerLauncher(MPIExecLauncher):
442 class MPIExecControllerLauncher(MPIExecLauncher):
444 """Launch a controller using mpiexec."""
443 """Launch a controller using mpiexec."""
445
444
446 controller_cmd = List(ipcontroller_cmd_argv, config=True)
445 controller_cmd = List(ipcontroller_cmd_argv, config=True)
447 # Command line arguments to ipcontroller.
446 # Command line arguments to ipcontroller.
448 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
447 controller_args = List(['--log-to-file','--log-level', '40'], config=True)
449 n = Int(1, config=False)
448 n = Int(1, config=False)
450
449
451 def start(self, cluster_dir):
450 def start(self, cluster_dir):
452 """Start the controller by cluster_dir."""
451 """Start the controller by cluster_dir."""
453 self.controller_args.extend(['--cluster-dir', cluster_dir])
452 self.controller_args.extend(['--cluster-dir', cluster_dir])
454 self.cluster_dir = unicode(cluster_dir)
453 self.cluster_dir = unicode(cluster_dir)
455 log.msg("Starting MPIExecControllerLauncher: %r" % self.args)
454 log.msg("Starting MPIExecControllerLauncher: %r" % self.args)
456 return super(MPIExecControllerLauncher, self).start(1)
455 return super(MPIExecControllerLauncher, self).start(1)
457
456
458 def find_args(self):
457 def find_args(self):
459 return self.mpi_cmd + ['-n', self.n] + self.mpi_args + \
458 return self.mpi_cmd + ['-n', self.n] + self.mpi_args + \
460 self.controller_cmd + self.controller_args
459 self.controller_cmd + self.controller_args
461
460
462
461
463 class MPIExecEngineSetLauncher(MPIExecLauncher):
462 class MPIExecEngineSetLauncher(MPIExecLauncher):
464
463
465 engine_cmd = List(ipengine_cmd_argv, config=True)
464 engine_cmd = List(ipengine_cmd_argv, config=True)
466 # Command line arguments for ipengine.
465 # Command line arguments for ipengine.
467 engine_args = List(
466 engine_args = List(
468 ['--log-to-file','--log-level', '40'], config=True
467 ['--log-to-file','--log-level', '40'], config=True
469 )
468 )
470 n = Int(1, config=True)
469 n = Int(1, config=True)
471
470
472 def start(self, n, cluster_dir):
471 def start(self, n, cluster_dir):
473 """Start n engines by profile or cluster_dir."""
472 """Start n engines by profile or cluster_dir."""
474 self.engine_args.extend(['--cluster-dir', cluster_dir])
473 self.engine_args.extend(['--cluster-dir', cluster_dir])
475 self.cluster_dir = unicode(cluster_dir)
474 self.cluster_dir = unicode(cluster_dir)
476 self.n = n
475 self.n = n
477 log.msg('Starting MPIExecEngineSetLauncher: %r' % self.args)
476 log.msg('Starting MPIExecEngineSetLauncher: %r' % self.args)
478 return super(MPIExecEngineSetLauncher, self).start(n)
477 return super(MPIExecEngineSetLauncher, self).start(n)
479
478
480 def find_args(self):
479 def find_args(self):
481 return self.mpi_cmd + ['-n', self.n] + self.mpi_args + \
480 return self.mpi_cmd + ['-n', self.n] + self.mpi_args + \
482 self.engine_cmd + self.engine_args
481 self.engine_cmd + self.engine_args
483
482
484
483
485 #-----------------------------------------------------------------------------
484 #-----------------------------------------------------------------------------
486 # SSH launchers
485 # SSH launchers
487 #-----------------------------------------------------------------------------
486 #-----------------------------------------------------------------------------
488
487
489 # TODO: Get SSH Launcher working again.
488 # TODO: Get SSH Launcher working again.
490
489
491 class SSHLauncher(BaseLauncher):
490 class SSHLauncher(BaseLauncher):
492 """A minimal launcher for ssh.
491 """A minimal launcher for ssh.
493
492
494 To be useful this will probably have to be extended to use the ``sshx``
493 To be useful this will probably have to be extended to use the ``sshx``
495 idea for environment variables. There could be other things this needs
494 idea for environment variables. There could be other things this needs
496 as well.
495 as well.
497 """
496 """
498
497
499 ssh_cmd = List(['ssh'], config=True)
498 ssh_cmd = List(['ssh'], config=True)
500 ssh_args = List([], config=True)
499 ssh_args = List([], config=True)
501 program = List(['date'], config=True)
500 program = List(['date'], config=True)
502 program_args = List([], config=True)
501 program_args = List([], config=True)
503 hostname = Str('', config=True)
502 hostname = Str('', config=True)
504 user = Str('', config=True)
503 user = Str('', config=True)
505 location = Str('')
504 location = Str('')
506
505
507 def _hostname_changed(self, name, old, new):
506 def _hostname_changed(self, name, old, new):
508 self.location = '%s@%s' % (self.user, new)
507 self.location = '%s@%s' % (self.user, new)
509
508
510 def _user_changed(self, name, old, new):
509 def _user_changed(self, name, old, new):
511 self.location = '%s@%s' % (new, self.hostname)
510 self.location = '%s@%s' % (new, self.hostname)
512
511
513 def find_args(self):
512 def find_args(self):
514 return self.ssh_cmd + self.ssh_args + [self.location] + \
513 return self.ssh_cmd + self.ssh_args + [self.location] + \
515 self.program + self.program_args
514 self.program + self.program_args
516
515
517 def start(self, n, hostname=None, user=None):
516 def start(self, n, hostname=None, user=None):
518 if hostname is not None:
517 if hostname is not None:
519 self.hostname = hostname
518 self.hostname = hostname
520 if user is not None:
519 if user is not None:
521 self.user = user
520 self.user = user
522 return super(SSHLauncher, self).start()
521 return super(SSHLauncher, self).start()
523
522
524
523
525 class SSHControllerLauncher(SSHLauncher):
524 class SSHControllerLauncher(SSHLauncher):
526 pass
525 pass
527
526
528
527
529 class SSHEngineSetLauncher(BaseLauncher):
528 class SSHEngineSetLauncher(BaseLauncher):
530 pass
529 pass
531
530
532
531
533 #-----------------------------------------------------------------------------
532 #-----------------------------------------------------------------------------
534 # Windows HPC Server 2008 scheduler launchers
533 # Windows HPC Server 2008 scheduler launchers
535 #-----------------------------------------------------------------------------
534 #-----------------------------------------------------------------------------
536
535
537
536
538 # This is only used on Windows.
537 # This is only used on Windows.
539 def find_job_cmd():
538 def find_job_cmd():
540 if os.name=='nt':
539 if os.name=='nt':
541 try:
540 try:
542 return find_cmd('job')
541 return find_cmd('job')
543 except FindCmdError:
542 except FindCmdError:
544 return 'job'
543 return 'job'
545 else:
544 else:
546 return 'job'
545 return 'job'
547
546
548
547
549 class WindowsHPCLauncher(BaseLauncher):
548 class WindowsHPCLauncher(BaseLauncher):
550
549
551 # A regular expression used to get the job id from the output of the
550 # A regular expression used to get the job id from the output of the
552 # submit_command.
551 # submit_command.
553 job_id_regexp = Str(r'\d+', config=True)
552 job_id_regexp = Str(r'\d+', config=True)
554 # The filename of the instantiated job script.
553 # The filename of the instantiated job script.
555 job_file_name = Unicode(u'ipython_job.xml', config=True)
554 job_file_name = Unicode(u'ipython_job.xml', config=True)
556 # The full path to the instantiated job script. This gets made dynamically
555 # The full path to the instantiated job script. This gets made dynamically
557 # by combining the work_dir with the job_file_name.
556 # by combining the work_dir with the job_file_name.
558 job_file = Unicode(u'')
557 job_file = Unicode(u'')
559 # The hostname of the scheduler to submit the job to
558 # The hostname of the scheduler to submit the job to
560 scheduler = Str('', config=True)
559 scheduler = Str('', config=True)
561 job_cmd = Str(find_job_cmd(), config=True)
560 job_cmd = Str(find_job_cmd(), config=True)
562
561
563 def __init__(self, work_dir, parent=None, name=None, config=None):
562 def __init__(self, work_dir=u'', config=None):
564 super(WindowsHPCLauncher, self).__init__(
563 super(WindowsHPCLauncher, self).__init__(
565 work_dir, parent, name, config
564 work_dir=work_dir, config=config
566 )
565 )
567
566
568 @property
567 @property
569 def job_file(self):
568 def job_file(self):
570 return os.path.join(self.work_dir, self.job_file_name)
569 return os.path.join(self.work_dir, self.job_file_name)
571
570
572 def write_job_file(self, n):
571 def write_job_file(self, n):
573 raise NotImplementedError("Implement write_job_file in a subclass.")
572 raise NotImplementedError("Implement write_job_file in a subclass.")
574
573
575 def find_args(self):
574 def find_args(self):
576 return ['job.exe']
575 return ['job.exe']
577
576
578 def parse_job_id(self, output):
577 def parse_job_id(self, output):
579 """Take the output of the submit command and return the job id."""
578 """Take the output of the submit command and return the job id."""
580 m = re.search(self.job_id_regexp, output)
579 m = re.search(self.job_id_regexp, output)
581 if m is not None:
580 if m is not None:
582 job_id = m.group()
581 job_id = m.group()
583 else:
582 else:
584 raise LauncherError("Job id couldn't be determined: %s" % output)
583 raise LauncherError("Job id couldn't be determined: %s" % output)
585 self.job_id = job_id
584 self.job_id = job_id
586 log.msg('Job started with job id: %r' % job_id)
585 log.msg('Job started with job id: %r' % job_id)
587 return job_id
586 return job_id
588
587
589 @inlineCallbacks
588 @inlineCallbacks
590 def start(self, n):
589 def start(self, n):
591 """Start n copies of the process using the Win HPC job scheduler."""
590 """Start n copies of the process using the Win HPC job scheduler."""
592 self.write_job_file(n)
591 self.write_job_file(n)
593 args = [
592 args = [
594 'submit',
593 'submit',
595 '/jobfile:%s' % self.job_file,
594 '/jobfile:%s' % self.job_file,
596 '/scheduler:%s' % self.scheduler
595 '/scheduler:%s' % self.scheduler
597 ]
596 ]
598 log.msg("Starting Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
597 log.msg("Starting Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
599 # Twisted will raise DeprecationWarnings if we try to pass unicode to this
598 # Twisted will raise DeprecationWarnings if we try to pass unicode to this
600 output = yield getProcessOutput(str(self.job_cmd),
599 output = yield getProcessOutput(str(self.job_cmd),
601 [str(a) for a in args],
600 [str(a) for a in args],
602 env=dict((str(k),str(v)) for k,v in os.environ.items()),
601 env=dict((str(k),str(v)) for k,v in os.environ.items()),
603 path=self.work_dir
602 path=self.work_dir
604 )
603 )
605 job_id = self.parse_job_id(output)
604 job_id = self.parse_job_id(output)
606 self.notify_start(job_id)
605 self.notify_start(job_id)
607 defer.returnValue(job_id)
606 defer.returnValue(job_id)
608
607
609 @inlineCallbacks
608 @inlineCallbacks
610 def stop(self):
609 def stop(self):
611 args = [
610 args = [
612 'cancel',
611 'cancel',
613 self.job_id,
612 self.job_id,
614 '/scheduler:%s' % self.scheduler
613 '/scheduler:%s' % self.scheduler
615 ]
614 ]
616 log.msg("Stopping Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
615 log.msg("Stopping Win HPC Job: %s" % (self.job_cmd + ' ' + ' '.join(args),))
617 try:
616 try:
618 # Twisted will raise DeprecationWarnings if we try to pass unicode to this
617 # Twisted will raise DeprecationWarnings if we try to pass unicode to this
619 output = yield getProcessOutput(str(self.job_cmd),
618 output = yield getProcessOutput(str(self.job_cmd),
620 [str(a) for a in args],
619 [str(a) for a in args],
621 env=dict((str(k),str(v)) for k,v in os.environ.items()),
620 env=dict((str(k),str(v)) for k,v in os.environ.items()),
622 path=self.work_dir
621 path=self.work_dir
623 )
622 )
624 except:
623 except:
625 output = 'The job already appears to be stoppped: %r' % self.job_id
624 output = 'The job already appears to be stoppped: %r' % self.job_id
626 self.notify_stop(output) # Pass the output of the kill cmd
625 self.notify_stop(output) # Pass the output of the kill cmd
627 defer.returnValue(output)
626 defer.returnValue(output)
628
627
629
628
630 class WindowsHPCControllerLauncher(WindowsHPCLauncher):
629 class WindowsHPCControllerLauncher(WindowsHPCLauncher):
631
630
632 job_file_name = Unicode(u'ipcontroller_job.xml', config=True)
631 job_file_name = Unicode(u'ipcontroller_job.xml', config=True)
633 extra_args = List([], config=False)
632 extra_args = List([], config=False)
634
633
635 def write_job_file(self, n):
634 def write_job_file(self, n):
636 job = IPControllerJob(self)
635 job = IPControllerJob(config=self.config)
637
636
638 t = IPControllerTask(self)
637 t = IPControllerTask(config=self.config)
639 # The tasks work directory is *not* the actual work directory of
638 # The tasks work directory is *not* the actual work directory of
640 # the controller. It is used as the base path for the stdout/stderr
639 # the controller. It is used as the base path for the stdout/stderr
641 # files that the scheduler redirects to.
640 # files that the scheduler redirects to.
642 t.work_directory = self.cluster_dir
641 t.work_directory = self.cluster_dir
643 # Add the --cluster-dir and from self.start().
642 # Add the --cluster-dir and from self.start().
644 t.controller_args.extend(self.extra_args)
643 t.controller_args.extend(self.extra_args)
645 job.add_task(t)
644 job.add_task(t)
646
645
647 log.msg("Writing job description file: %s" % self.job_file)
646 log.msg("Writing job description file: %s" % self.job_file)
648 job.write(self.job_file)
647 job.write(self.job_file)
649
648
650 @property
649 @property
651 def job_file(self):
650 def job_file(self):
652 return os.path.join(self.cluster_dir, self.job_file_name)
651 return os.path.join(self.cluster_dir, self.job_file_name)
653
652
654 def start(self, cluster_dir):
653 def start(self, cluster_dir):
655 """Start the controller by cluster_dir."""
654 """Start the controller by cluster_dir."""
656 self.extra_args = ['--cluster-dir', cluster_dir]
655 self.extra_args = ['--cluster-dir', cluster_dir]
657 self.cluster_dir = unicode(cluster_dir)
656 self.cluster_dir = unicode(cluster_dir)
658 return super(WindowsHPCControllerLauncher, self).start(1)
657 return super(WindowsHPCControllerLauncher, self).start(1)
659
658
660
659
661 class WindowsHPCEngineSetLauncher(WindowsHPCLauncher):
660 class WindowsHPCEngineSetLauncher(WindowsHPCLauncher):
662
661
663 job_file_name = Unicode(u'ipengineset_job.xml', config=True)
662 job_file_name = Unicode(u'ipengineset_job.xml', config=True)
664 extra_args = List([], config=False)
663 extra_args = List([], config=False)
665
664
666 def write_job_file(self, n):
665 def write_job_file(self, n):
667 job = IPEngineSetJob(self)
666 job = IPEngineSetJob(config=self.config)
668
667
669 for i in range(n):
668 for i in range(n):
670 t = IPEngineTask(self)
669 t = IPEngineTask(config=self.config)
671 # The tasks work directory is *not* the actual work directory of
670 # The tasks work directory is *not* the actual work directory of
672 # the engine. It is used as the base path for the stdout/stderr
671 # the engine. It is used as the base path for the stdout/stderr
673 # files that the scheduler redirects to.
672 # files that the scheduler redirects to.
674 t.work_directory = self.cluster_dir
673 t.work_directory = self.cluster_dir
675 # Add the --cluster-dir and from self.start().
674 # Add the --cluster-dir and from self.start().
676 t.engine_args.extend(self.extra_args)
675 t.engine_args.extend(self.extra_args)
677 job.add_task(t)
676 job.add_task(t)
678
677
679 log.msg("Writing job description file: %s" % self.job_file)
678 log.msg("Writing job description file: %s" % self.job_file)
680 job.write(self.job_file)
679 job.write(self.job_file)
681
680
682 @property
681 @property
683 def job_file(self):
682 def job_file(self):
684 return os.path.join(self.cluster_dir, self.job_file_name)
683 return os.path.join(self.cluster_dir, self.job_file_name)
685
684
686 def start(self, n, cluster_dir):
685 def start(self, n, cluster_dir):
687 """Start the controller by cluster_dir."""
686 """Start the controller by cluster_dir."""
688 self.extra_args = ['--cluster-dir', cluster_dir]
687 self.extra_args = ['--cluster-dir', cluster_dir]
689 self.cluster_dir = unicode(cluster_dir)
688 self.cluster_dir = unicode(cluster_dir)
690 return super(WindowsHPCEngineSetLauncher, self).start(n)
689 return super(WindowsHPCEngineSetLauncher, self).start(n)
691
690
692
691
693 #-----------------------------------------------------------------------------
692 #-----------------------------------------------------------------------------
694 # Batch (PBS) system launchers
693 # Batch (PBS) system launchers
695 #-----------------------------------------------------------------------------
694 #-----------------------------------------------------------------------------
696
695
697 # TODO: Get PBS launcher working again.
696 # TODO: Get PBS launcher working again.
698
697
699 class BatchSystemLauncher(BaseLauncher):
698 class BatchSystemLauncher(BaseLauncher):
700 """Launch an external process using a batch system.
699 """Launch an external process using a batch system.
701
700
702 This class is designed to work with UNIX batch systems like PBS, LSF,
701 This class is designed to work with UNIX batch systems like PBS, LSF,
703 GridEngine, etc. The overall model is that there are different commands
702 GridEngine, etc. The overall model is that there are different commands
704 like qsub, qdel, etc. that handle the starting and stopping of the process.
703 like qsub, qdel, etc. that handle the starting and stopping of the process.
705
704
706 This class also has the notion of a batch script. The ``batch_template``
705 This class also has the notion of a batch script. The ``batch_template``
707 attribute can be set to a string that is a template for the batch script.
706 attribute can be set to a string that is a template for the batch script.
708 This template is instantiated using Itpl. Thus the template can use
707 This template is instantiated using Itpl. Thus the template can use
709 ${n} fot the number of instances. Subclasses can add additional variables
708 ${n} fot the number of instances. Subclasses can add additional variables
710 to the template dict.
709 to the template dict.
711 """
710 """
712
711
713 # Subclasses must fill these in. See PBSEngineSet
712 # Subclasses must fill these in. See PBSEngineSet
714 # The name of the command line program used to submit jobs.
713 # The name of the command line program used to submit jobs.
715 submit_command = Str('', config=True)
714 submit_command = Str('', config=True)
716 # The name of the command line program used to delete jobs.
715 # The name of the command line program used to delete jobs.
717 delete_command = Str('', config=True)
716 delete_command = Str('', config=True)
718 # A regular expression used to get the job id from the output of the
717 # A regular expression used to get the job id from the output of the
719 # submit_command.
718 # submit_command.
720 job_id_regexp = Str('', config=True)
719 job_id_regexp = Str('', config=True)
721 # The string that is the batch script template itself.
720 # The string that is the batch script template itself.
722 batch_template = Str('', config=True)
721 batch_template = Str('', config=True)
723 # The filename of the instantiated batch script.
722 # The filename of the instantiated batch script.
724 batch_file_name = Unicode(u'batch_script', config=True)
723 batch_file_name = Unicode(u'batch_script', config=True)
725 # The full path to the instantiated batch script.
724 # The full path to the instantiated batch script.
726 batch_file = Unicode(u'')
725 batch_file = Unicode(u'')
727
726
728 def __init__(self, work_dir, parent=None, name=None, config=None):
727 def __init__(self, work_dir=u'', config=None):
729 super(BatchSystemLauncher, self).__init__(
728 super(BatchSystemLauncher, self).__init__(
730 work_dir, parent, name, config
729 work_dir=work_dir, config=config
731 )
730 )
732 self.batch_file = os.path.join(self.work_dir, self.batch_file_name)
731 self.batch_file = os.path.join(self.work_dir, self.batch_file_name)
733 self.context = {}
732 self.context = {}
734
733
735 def parse_job_id(self, output):
734 def parse_job_id(self, output):
736 """Take the output of the submit command and return the job id."""
735 """Take the output of the submit command and return the job id."""
737 m = re.match(self.job_id_regexp, output)
736 m = re.match(self.job_id_regexp, output)
738 if m is not None:
737 if m is not None:
739 job_id = m.group()
738 job_id = m.group()
740 else:
739 else:
741 raise LauncherError("Job id couldn't be determined: %s" % output)
740 raise LauncherError("Job id couldn't be determined: %s" % output)
742 self.job_id = job_id
741 self.job_id = job_id
743 log.msg('Job started with job id: %r' % job_id)
742 log.msg('Job started with job id: %r' % job_id)
744 return job_id
743 return job_id
745
744
746 def write_batch_script(self, n):
745 def write_batch_script(self, n):
747 """Instantiate and write the batch script to the work_dir."""
746 """Instantiate and write the batch script to the work_dir."""
748 self.context['n'] = n
747 self.context['n'] = n
749 script_as_string = Itpl.itplns(self.batch_template, self.context)
748 script_as_string = Itpl.itplns(self.batch_template, self.context)
750 log.msg('Writing instantiated batch script: %s' % self.batch_file)
749 log.msg('Writing instantiated batch script: %s' % self.batch_file)
751 f = open(self.batch_file, 'w')
750 f = open(self.batch_file, 'w')
752 f.write(script_as_string)
751 f.write(script_as_string)
753 f.close()
752 f.close()
754
753
755 @inlineCallbacks
754 @inlineCallbacks
756 def start(self, n):
755 def start(self, n):
757 """Start n copies of the process using a batch system."""
756 """Start n copies of the process using a batch system."""
758 self.write_batch_script(n)
757 self.write_batch_script(n)
759 output = yield getProcessOutput(self.submit_command,
758 output = yield getProcessOutput(self.submit_command,
760 [self.batch_file], env=os.environ)
759 [self.batch_file], env=os.environ)
761 job_id = self.parse_job_id(output)
760 job_id = self.parse_job_id(output)
762 self.notify_start(job_id)
761 self.notify_start(job_id)
763 defer.returnValue(job_id)
762 defer.returnValue(job_id)
764
763
765 @inlineCallbacks
764 @inlineCallbacks
766 def stop(self):
765 def stop(self):
767 output = yield getProcessOutput(self.delete_command,
766 output = yield getProcessOutput(self.delete_command,
768 [self.job_id], env=os.environ
767 [self.job_id], env=os.environ
769 )
768 )
770 self.notify_stop(output) # Pass the output of the kill cmd
769 self.notify_stop(output) # Pass the output of the kill cmd
771 defer.returnValue(output)
770 defer.returnValue(output)
772
771
773
772
774 class PBSLauncher(BatchSystemLauncher):
773 class PBSLauncher(BatchSystemLauncher):
775 """A BatchSystemLauncher subclass for PBS."""
774 """A BatchSystemLauncher subclass for PBS."""
776
775
777 submit_command = Str('qsub', config=True)
776 submit_command = Str('qsub', config=True)
778 delete_command = Str('qdel', config=True)
777 delete_command = Str('qdel', config=True)
779 job_id_regexp = Str(r'\d+', config=True)
778 job_id_regexp = Str(r'\d+', config=True)
780 batch_template = Str('', config=True)
779 batch_template = Str('', config=True)
781 batch_file_name = Unicode(u'pbs_batch_script', config=True)
780 batch_file_name = Unicode(u'pbs_batch_script', config=True)
782 batch_file = Unicode(u'')
781 batch_file = Unicode(u'')
783
782
784
783
785 class PBSControllerLauncher(PBSLauncher):
784 class PBSControllerLauncher(PBSLauncher):
786 """Launch a controller using PBS."""
785 """Launch a controller using PBS."""
787
786
788 batch_file_name = Unicode(u'pbs_batch_script_controller', config=True)
787 batch_file_name = Unicode(u'pbs_batch_script_controller', config=True)
789
788
790 def start(self, cluster_dir):
789 def start(self, cluster_dir):
791 """Start the controller by profile or cluster_dir."""
790 """Start the controller by profile or cluster_dir."""
792 # Here we save profile and cluster_dir in the context so they
791 # Here we save profile and cluster_dir in the context so they
793 # can be used in the batch script template as ${profile} and
792 # can be used in the batch script template as ${profile} and
794 # ${cluster_dir}
793 # ${cluster_dir}
795 self.context['cluster_dir'] = cluster_dir
794 self.context['cluster_dir'] = cluster_dir
796 self.cluster_dir = unicode(cluster_dir)
795 self.cluster_dir = unicode(cluster_dir)
797 log.msg("Starting PBSControllerLauncher: %r" % self.args)
796 log.msg("Starting PBSControllerLauncher: %r" % self.args)
798 return super(PBSControllerLauncher, self).start(1)
797 return super(PBSControllerLauncher, self).start(1)
799
798
800
799
801 class PBSEngineSetLauncher(PBSLauncher):
800 class PBSEngineSetLauncher(PBSLauncher):
802
801
803 batch_file_name = Unicode(u'pbs_batch_script_engines', config=True)
802 batch_file_name = Unicode(u'pbs_batch_script_engines', config=True)
804
803
805 def start(self, n, cluster_dir):
804 def start(self, n, cluster_dir):
806 """Start n engines by profile or cluster_dir."""
805 """Start n engines by profile or cluster_dir."""
807 self.program_args.extend(['--cluster-dir', cluster_dir])
806 self.program_args.extend(['--cluster-dir', cluster_dir])
808 self.cluster_dir = unicode(cluster_dir)
807 self.cluster_dir = unicode(cluster_dir)
809 log.msg('Starting PBSEngineSetLauncher: %r' % self.args)
808 log.msg('Starting PBSEngineSetLauncher: %r' % self.args)
810 return super(PBSEngineSetLauncher, self).start(n)
809 return super(PBSEngineSetLauncher, self).start(n)
811
810
812
811
813 #-----------------------------------------------------------------------------
812 #-----------------------------------------------------------------------------
814 # A launcher for ipcluster itself!
813 # A launcher for ipcluster itself!
815 #-----------------------------------------------------------------------------
814 #-----------------------------------------------------------------------------
816
815
817
816
818 class IPClusterLauncher(LocalProcessLauncher):
817 class IPClusterLauncher(LocalProcessLauncher):
819 """Launch the ipcluster program in an external process."""
818 """Launch the ipcluster program in an external process."""
820
819
821 ipcluster_cmd = List(ipcluster_cmd_argv, config=True)
820 ipcluster_cmd = List(ipcluster_cmd_argv, config=True)
822 # Command line arguments to pass to ipcluster.
821 # Command line arguments to pass to ipcluster.
823 ipcluster_args = List(
822 ipcluster_args = List(
824 ['--clean-logs', '--log-to-file', '--log-level', '40'], config=True)
823 ['--clean-logs', '--log-to-file', '--log-level', '40'], config=True)
825 ipcluster_subcommand = Str('start')
824 ipcluster_subcommand = Str('start')
826 ipcluster_n = Int(2)
825 ipcluster_n = Int(2)
827
826
828 def find_args(self):
827 def find_args(self):
829 return self.ipcluster_cmd + [self.ipcluster_subcommand] + \
828 return self.ipcluster_cmd + [self.ipcluster_subcommand] + \
830 ['-n', repr(self.ipcluster_n)] + self.ipcluster_args
829 ['-n', repr(self.ipcluster_n)] + self.ipcluster_args
831
830
832 def start(self):
831 def start(self):
833 log.msg("Starting ipcluster: %r" % self.args)
832 log.msg("Starting ipcluster: %r" % self.args)
834 return super(IPClusterLauncher, self).start()
833 return super(IPClusterLauncher, self).start()
835
834
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now