Show More
@@ -1,86 +1,86 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | IPython: tools for interactive and parallel computing in Python. |
|
3 | IPython: tools for interactive and parallel computing in Python. | |
4 |
|
4 | |||
5 | http://ipython.org |
|
5 | http://ipython.org | |
6 | """ |
|
6 | """ | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (c) 2008-2011, IPython Development Team. |
|
8 | # Copyright (c) 2008-2011, IPython Development Team. | |
9 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> |
|
9 | # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu> | |
10 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> |
|
10 | # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> | |
11 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> |
|
11 | # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu> | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 | # |
|
14 | # | |
15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | # The full license is in the file COPYING.txt, distributed with this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | from __future__ import absolute_import |
|
21 | from __future__ import absolute_import | |
22 |
|
22 | |||
23 | import os |
|
23 | import os | |
24 | import sys |
|
24 | import sys | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Setup everything |
|
27 | # Setup everything | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | # Don't forget to also update setup.py when this changes! |
|
30 | # Don't forget to also update setup.py when this changes! | |
31 | if sys.version[0:3] < '2.6': |
|
31 | if sys.version[0:3] < '2.6': | |
32 | raise ImportError('Python Version 2.6 or above is required for IPython.') |
|
32 | raise ImportError('Python Version 2.6 or above is required for IPython.') | |
33 |
|
33 | |||
34 | # Make it easy to import extensions - they are always directly on pythonpath. |
|
34 | # Make it easy to import extensions - they are always directly on pythonpath. | |
35 | # Therefore, non-IPython modules can be added to extensions directory. |
|
35 | # Therefore, non-IPython modules can be added to extensions directory. | |
36 | # This should probably be in ipapp.py. |
|
36 | # This should probably be in ipapp.py. | |
37 | sys.path.append(os.path.join(os.path.dirname(__file__), "extensions")) |
|
37 | sys.path.append(os.path.join(os.path.dirname(__file__), "extensions")) | |
38 |
|
38 | |||
39 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
40 | # Setup the top level names |
|
40 | # Setup the top level names | |
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 |
|
42 | |||
43 | from .config.loader import Config |
|
43 | from .config.loader import Config | |
44 | from .core.getipython import get_ipython |
|
44 | from .core.getipython import get_ipython | |
45 | from .core import release |
|
45 | from .core import release | |
46 | from .core.application import Application |
|
46 | from .core.application import Application | |
47 |
from |
|
47 | from .terminal.embed import embed | |
48 |
|
48 | |||
49 | from .core.error import TryNext |
|
49 | from .core.error import TryNext | |
50 | from .core.interactiveshell import InteractiveShell |
|
50 | from .core.interactiveshell import InteractiveShell | |
51 | from .testing import test |
|
51 | from .testing import test | |
52 | from .utils.sysinfo import sys_info |
|
52 | from .utils.sysinfo import sys_info | |
53 | from .utils.frame import extract_module_locals |
|
53 | from .utils.frame import extract_module_locals | |
54 |
|
54 | |||
55 | # Release data |
|
55 | # Release data | |
56 | __author__ = '%s <%s>' % (release.author, release.author_email) |
|
56 | __author__ = '%s <%s>' % (release.author, release.author_email) | |
57 | __license__ = release.license |
|
57 | __license__ = release.license | |
58 | __version__ = release.version |
|
58 | __version__ = release.version | |
59 | version_info = release.version_info |
|
59 | version_info = release.version_info | |
60 |
|
60 | |||
61 | def embed_kernel(module=None, local_ns=None, **kwargs): |
|
61 | def embed_kernel(module=None, local_ns=None, **kwargs): | |
62 | """Embed and start an IPython kernel in a given scope. |
|
62 | """Embed and start an IPython kernel in a given scope. | |
63 |
|
63 | |||
64 | Parameters |
|
64 | Parameters | |
65 | ---------- |
|
65 | ---------- | |
66 | module : ModuleType, optional |
|
66 | module : ModuleType, optional | |
67 | The module to load into IPython globals (default: caller) |
|
67 | The module to load into IPython globals (default: caller) | |
68 | local_ns : dict, optional |
|
68 | local_ns : dict, optional | |
69 | The namespace to load into IPython user namespace (default: caller) |
|
69 | The namespace to load into IPython user namespace (default: caller) | |
70 |
|
70 | |||
71 | kwargs : various, optional |
|
71 | kwargs : various, optional | |
72 | Further keyword args are relayed to the IPKernelApp constructor, |
|
72 | Further keyword args are relayed to the IPKernelApp constructor, | |
73 | allowing configuration of the Kernel. Will only have an effect |
|
73 | allowing configuration of the Kernel. Will only have an effect | |
74 | on the first embed_kernel call for a given process. |
|
74 | on the first embed_kernel call for a given process. | |
75 |
|
75 | |||
76 | """ |
|
76 | """ | |
77 |
|
77 | |||
78 | (caller_module, caller_locals) = extract_module_locals(1) |
|
78 | (caller_module, caller_locals) = extract_module_locals(1) | |
79 | if module is None: |
|
79 | if module is None: | |
80 | module = caller_module |
|
80 | module = caller_module | |
81 | if local_ns is None: |
|
81 | if local_ns is None: | |
82 | local_ns = caller_locals |
|
82 | local_ns = caller_locals | |
83 |
|
83 | |||
84 | # Only import .zmq when we really need it |
|
84 | # Only import .zmq when we really need it | |
85 | from IPython.kernel.zmq.embed import embed_kernel as real_embed_kernel |
|
85 | from IPython.kernel.zmq.embed import embed_kernel as real_embed_kernel | |
86 | real_embed_kernel(module=module, local_ns=local_ns, **kwargs) |
|
86 | real_embed_kernel(module=module, local_ns=local_ns, **kwargs) |
@@ -1,77 +1,72 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Shim to maintain backwards compatibility with old frontend imports. |
|
2 | Shim to maintain backwards compatibility with old frontend imports. | |
3 |
|
3 | |||
4 | We have moved all contents of the old `frontend` subpackage into top-level |
|
4 | We have moved all contents of the old `frontend` subpackage into top-level | |
5 | subpackages (`html`, `qt` and `terminal`). This will let code that was making |
|
5 | subpackages (`html`, `qt` and `terminal`). This will let code that was making | |
6 | `from IPython.frontend...` calls continue working, though a warning will be |
|
6 | `from IPython.frontend...` calls continue working, though a warning will be | |
7 | printed. |
|
7 | printed. | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (c) 2013, IPython Development Team. |
|
11 | # Copyright (c) 2013, IPython Development Team. | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 | # |
|
14 | # | |
15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | # The full license is in the file COPYING.txt, distributed with this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | from __future__ import print_function |
|
21 | from __future__ import print_function | |
22 |
|
22 | |||
23 | # Stdlib |
|
23 | # Stdlib | |
24 | import sys |
|
24 | import sys | |
25 | import types |
|
25 | import types | |
|
26 | from warnings import warn | |||
26 |
|
27 | |||
27 | m = """\ |
|
28 | warn("The top-level `frontend` package has been deprecated. " | |
28 | *** WARNING*** : The top-level `frontend` package has been deprecated. |
|
29 | "All its subpackages have been moved to the top `IPython` level.") | |
29 | All its subpackages have been moved to the top `IPython` level.""" |
|
|||
30 |
|
||||
31 | print(m, file=sys.stderr) |
|
|||
32 |
|
||||
33 | # FIXME: turn this into a Warning once we've fixed all our own imports. |
|
|||
34 | #raise DeprecationWarning(m) |
|
|||
35 |
|
30 | |||
36 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
37 | # Class declarations |
|
32 | # Class declarations | |
38 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
39 |
|
34 | |||
40 | class ShimModule(types.ModuleType): |
|
35 | class ShimModule(types.ModuleType): | |
41 |
|
36 | |||
42 | def __getattribute__(self, key): |
|
37 | def __getattribute__(self, key): | |
43 | # Use the equivalent of import_item(name), see below |
|
38 | # Use the equivalent of import_item(name), see below | |
44 | name = 'IPython.' + key |
|
39 | name = 'IPython.' + key | |
45 |
|
40 | |||
46 | # NOTE: the code below is copied *verbatim* from |
|
41 | # NOTE: the code below is copied *verbatim* from | |
47 | # importstring.import_item. For some very strange reason that makes no |
|
42 | # importstring.import_item. For some very strange reason that makes no | |
48 | # sense to me, if we call it *as a function*, it doesn't work. This |
|
43 | # sense to me, if we call it *as a function*, it doesn't work. This | |
49 | # has something to do with the deep bowels of the import machinery and |
|
44 | # has something to do with the deep bowels of the import machinery and | |
50 | # I couldn't find a way to make the code work as a standard function |
|
45 | # I couldn't find a way to make the code work as a standard function | |
51 | # call. But at least since it's an unmodified copy of import_item, |
|
46 | # call. But at least since it's an unmodified copy of import_item, | |
52 | # which is used extensively and has a test suite, we can be reasonably |
|
47 | # which is used extensively and has a test suite, we can be reasonably | |
53 | # confident this is OK. If anyone finds how to call the function, all |
|
48 | # confident this is OK. If anyone finds how to call the function, all | |
54 | # the below could be replaced simply with: |
|
49 | # the below could be replaced simply with: | |
55 | # |
|
50 | # | |
56 | # from IPython.utils.importstring import import_item |
|
51 | # from IPython.utils.importstring import import_item | |
57 | # return import_item('IPython.' + key) |
|
52 | # return import_item('IPython.' + key) | |
58 |
|
53 | |||
59 | parts = name.rsplit('.', 1) |
|
54 | parts = name.rsplit('.', 1) | |
60 | if len(parts) == 2: |
|
55 | if len(parts) == 2: | |
61 | # called with 'foo.bar....' |
|
56 | # called with 'foo.bar....' | |
62 | package, obj = parts |
|
57 | package, obj = parts | |
63 | module = __import__(package, fromlist=[obj]) |
|
58 | module = __import__(package, fromlist=[obj]) | |
64 | try: |
|
59 | try: | |
65 | pak = module.__dict__[obj] |
|
60 | pak = module.__dict__[obj] | |
66 | except KeyError: |
|
61 | except KeyError: | |
67 | raise ImportError('No module named %s' % obj) |
|
62 | raise ImportError('No module named %s' % obj) | |
68 | return pak |
|
63 | return pak | |
69 | else: |
|
64 | else: | |
70 | # called with un-dotted string |
|
65 | # called with un-dotted string | |
71 | return __import__(parts[0]) |
|
66 | return __import__(parts[0]) | |
72 |
|
67 | |||
73 |
|
68 | |||
74 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
69 | # Unconditionally insert the shim into sys.modules so that further import calls | |
75 | # trigger the custom attribute access above |
|
70 | # trigger the custom attribute access above | |
76 |
|
71 | |||
77 | sys.modules['IPython.frontend'] = ShimModule('frontend') |
|
72 | sys.modules['IPython.frontend'] = ShimModule('frontend') |
@@ -1,7 +1,7 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | """Terminal-based IPython entry point. |
|
2 | """Terminal-based IPython entry point. | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 |
from IPython. |
|
5 | from IPython.terminal.ipapp import launch_new_instance | |
6 |
|
6 | |||
7 | launch_new_instance() |
|
7 | launch_new_instance() |
@@ -1,296 +1,296 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | An embedded IPython shell. |
|
3 | An embedded IPython shell. | |
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Brian Granger |
|
7 | * Brian Granger | |
8 | * Fernando Perez |
|
8 | * Fernando Perez | |
9 |
|
9 | |||
10 | Notes |
|
10 | Notes | |
11 | ----- |
|
11 | ----- | |
12 | """ |
|
12 | """ | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Copyright (C) 2008-2011 The IPython Development Team |
|
15 | # Copyright (C) 2008-2011 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 with_statement |
|
25 | from __future__ import with_statement | |
26 |
|
26 | |||
27 | import sys |
|
27 | import sys | |
28 | import warnings |
|
28 | import warnings | |
29 |
|
29 | |||
30 | # We need to use nested to support python 2.6, once we move to >=2.7, we can |
|
30 | # We need to use nested to support python 2.6, once we move to >=2.7, we can | |
31 | # use the with keyword's new builtin support for nested managers |
|
31 | # use the with keyword's new builtin support for nested managers | |
32 | try: |
|
32 | try: | |
33 | from contextlib import nested |
|
33 | from contextlib import nested | |
34 | except: |
|
34 | except: | |
35 | from IPython.utils.nested_context import nested |
|
35 | from IPython.utils.nested_context import nested | |
36 |
|
36 | |||
37 | from IPython.core import ultratb, compilerop |
|
37 | from IPython.core import ultratb, compilerop | |
38 | from IPython.core.magic import Magics, magics_class, line_magic |
|
38 | from IPython.core.magic import Magics, magics_class, line_magic | |
39 |
from IPython |
|
39 | from IPython.terminal.interactiveshell import TerminalInteractiveShell | |
40 |
from IPython |
|
40 | from IPython.terminal.ipapp import load_default_config | |
41 |
|
41 | |||
42 | from IPython.utils.traitlets import Bool, CBool, Unicode |
|
42 | from IPython.utils.traitlets import Bool, CBool, Unicode | |
43 | from IPython.utils.io import ask_yes_no |
|
43 | from IPython.utils.io import ask_yes_no | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | #----------------------------------------------------------------------------- |
|
46 | #----------------------------------------------------------------------------- | |
47 | # Classes and functions |
|
47 | # Classes and functions | |
48 | #----------------------------------------------------------------------------- |
|
48 | #----------------------------------------------------------------------------- | |
49 |
|
49 | |||
50 | # This is an additional magic that is exposed in embedded shells. |
|
50 | # This is an additional magic that is exposed in embedded shells. | |
51 | @magics_class |
|
51 | @magics_class | |
52 | class EmbeddedMagics(Magics): |
|
52 | class EmbeddedMagics(Magics): | |
53 |
|
53 | |||
54 | @line_magic |
|
54 | @line_magic | |
55 | def kill_embedded(self, parameter_s=''): |
|
55 | def kill_embedded(self, parameter_s=''): | |
56 | """%kill_embedded : deactivate for good the current embedded IPython. |
|
56 | """%kill_embedded : deactivate for good the current embedded IPython. | |
57 |
|
57 | |||
58 | This function (after asking for confirmation) sets an internal flag so |
|
58 | This function (after asking for confirmation) sets an internal flag so | |
59 | that an embedded IPython will never activate again. This is useful to |
|
59 | that an embedded IPython will never activate again. This is useful to | |
60 | permanently disable a shell that is being called inside a loop: once |
|
60 | permanently disable a shell that is being called inside a loop: once | |
61 | you've figured out what you needed from it, you may then kill it and |
|
61 | you've figured out what you needed from it, you may then kill it and | |
62 | the program will then continue to run without the interactive shell |
|
62 | the program will then continue to run without the interactive shell | |
63 | interfering again. |
|
63 | interfering again. | |
64 | """ |
|
64 | """ | |
65 |
|
65 | |||
66 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " |
|
66 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " | |
67 | "(y/n)? [y/N] ",'n') |
|
67 | "(y/n)? [y/N] ",'n') | |
68 | if kill: |
|
68 | if kill: | |
69 | self.shell.embedded_active = False |
|
69 | self.shell.embedded_active = False | |
70 | print ("This embedded IPython will not reactivate anymore " |
|
70 | print ("This embedded IPython will not reactivate anymore " | |
71 | "once you exit.") |
|
71 | "once you exit.") | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | class InteractiveShellEmbed(TerminalInteractiveShell): |
|
74 | class InteractiveShellEmbed(TerminalInteractiveShell): | |
75 |
|
75 | |||
76 | dummy_mode = Bool(False) |
|
76 | dummy_mode = Bool(False) | |
77 | exit_msg = Unicode('') |
|
77 | exit_msg = Unicode('') | |
78 | embedded = CBool(True) |
|
78 | embedded = CBool(True) | |
79 | embedded_active = CBool(True) |
|
79 | embedded_active = CBool(True) | |
80 | # Like the base class display_banner is not configurable, but here it |
|
80 | # Like the base class display_banner is not configurable, but here it | |
81 | # is True by default. |
|
81 | # is True by default. | |
82 | display_banner = CBool(True) |
|
82 | display_banner = CBool(True) | |
83 |
|
83 | |||
84 | def __init__(self, config=None, ipython_dir=None, user_ns=None, |
|
84 | def __init__(self, config=None, ipython_dir=None, user_ns=None, | |
85 | user_module=None, custom_exceptions=((),None), |
|
85 | user_module=None, custom_exceptions=((),None), | |
86 | usage=None, banner1=None, banner2=None, |
|
86 | usage=None, banner1=None, banner2=None, | |
87 | display_banner=None, exit_msg=u'', user_global_ns=None): |
|
87 | display_banner=None, exit_msg=u'', user_global_ns=None): | |
88 |
|
88 | |||
89 | if user_global_ns is not None: |
|
89 | if user_global_ns is not None: | |
90 | warnings.warn("user_global_ns has been replaced by user_module. The\ |
|
90 | warnings.warn("user_global_ns has been replaced by user_module. The\ | |
91 | parameter will be ignored.", DeprecationWarning) |
|
91 | parameter will be ignored.", DeprecationWarning) | |
92 |
|
92 | |||
93 | super(InteractiveShellEmbed,self).__init__( |
|
93 | super(InteractiveShellEmbed,self).__init__( | |
94 | config=config, ipython_dir=ipython_dir, user_ns=user_ns, |
|
94 | config=config, ipython_dir=ipython_dir, user_ns=user_ns, | |
95 | user_module=user_module, custom_exceptions=custom_exceptions, |
|
95 | user_module=user_module, custom_exceptions=custom_exceptions, | |
96 | usage=usage, banner1=banner1, banner2=banner2, |
|
96 | usage=usage, banner1=banner1, banner2=banner2, | |
97 | display_banner=display_banner |
|
97 | display_banner=display_banner | |
98 | ) |
|
98 | ) | |
99 |
|
99 | |||
100 | self.exit_msg = exit_msg |
|
100 | self.exit_msg = exit_msg | |
101 |
|
101 | |||
102 | # don't use the ipython crash handler so that user exceptions aren't |
|
102 | # don't use the ipython crash handler so that user exceptions aren't | |
103 | # trapped |
|
103 | # trapped | |
104 | sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors, |
|
104 | sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors, | |
105 | mode=self.xmode, |
|
105 | mode=self.xmode, | |
106 | call_pdb=self.pdb) |
|
106 | call_pdb=self.pdb) | |
107 |
|
107 | |||
108 | def init_sys_modules(self): |
|
108 | def init_sys_modules(self): | |
109 | pass |
|
109 | pass | |
110 |
|
110 | |||
111 | def init_magics(self): |
|
111 | def init_magics(self): | |
112 | super(InteractiveShellEmbed, self).init_magics() |
|
112 | super(InteractiveShellEmbed, self).init_magics() | |
113 | self.register_magics(EmbeddedMagics) |
|
113 | self.register_magics(EmbeddedMagics) | |
114 |
|
114 | |||
115 | def __call__(self, header='', local_ns=None, module=None, dummy=None, |
|
115 | def __call__(self, header='', local_ns=None, module=None, dummy=None, | |
116 | stack_depth=1, global_ns=None, compile_flags=None): |
|
116 | stack_depth=1, global_ns=None, compile_flags=None): | |
117 | """Activate the interactive interpreter. |
|
117 | """Activate the interactive interpreter. | |
118 |
|
118 | |||
119 | __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start |
|
119 | __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start | |
120 | the interpreter shell with the given local and global namespaces, and |
|
120 | the interpreter shell with the given local and global namespaces, and | |
121 | optionally print a header string at startup. |
|
121 | optionally print a header string at startup. | |
122 |
|
122 | |||
123 | The shell can be globally activated/deactivated using the |
|
123 | The shell can be globally activated/deactivated using the | |
124 | dummy_mode attribute. This allows you to turn off a shell used |
|
124 | dummy_mode attribute. This allows you to turn off a shell used | |
125 | for debugging globally. |
|
125 | for debugging globally. | |
126 |
|
126 | |||
127 | However, *each* time you call the shell you can override the current |
|
127 | However, *each* time you call the shell you can override the current | |
128 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
128 | state of dummy_mode with the optional keyword parameter 'dummy'. For | |
129 | example, if you set dummy mode on with IPShell.dummy_mode = True, you |
|
129 | example, if you set dummy mode on with IPShell.dummy_mode = True, you | |
130 | can still have a specific call work by making it as IPShell(dummy=False). |
|
130 | can still have a specific call work by making it as IPShell(dummy=False). | |
131 | """ |
|
131 | """ | |
132 |
|
132 | |||
133 | # If the user has turned it off, go away |
|
133 | # If the user has turned it off, go away | |
134 | if not self.embedded_active: |
|
134 | if not self.embedded_active: | |
135 | return |
|
135 | return | |
136 |
|
136 | |||
137 | # Normal exits from interactive mode set this flag, so the shell can't |
|
137 | # Normal exits from interactive mode set this flag, so the shell can't | |
138 | # re-enter (it checks this variable at the start of interactive mode). |
|
138 | # re-enter (it checks this variable at the start of interactive mode). | |
139 | self.exit_now = False |
|
139 | self.exit_now = False | |
140 |
|
140 | |||
141 | # Allow the dummy parameter to override the global __dummy_mode |
|
141 | # Allow the dummy parameter to override the global __dummy_mode | |
142 | if dummy or (dummy != 0 and self.dummy_mode): |
|
142 | if dummy or (dummy != 0 and self.dummy_mode): | |
143 | return |
|
143 | return | |
144 |
|
144 | |||
145 | if self.has_readline: |
|
145 | if self.has_readline: | |
146 | self.set_readline_completer() |
|
146 | self.set_readline_completer() | |
147 |
|
147 | |||
148 | # self.banner is auto computed |
|
148 | # self.banner is auto computed | |
149 | if header: |
|
149 | if header: | |
150 | self.old_banner2 = self.banner2 |
|
150 | self.old_banner2 = self.banner2 | |
151 | self.banner2 = self.banner2 + '\n' + header + '\n' |
|
151 | self.banner2 = self.banner2 + '\n' + header + '\n' | |
152 | else: |
|
152 | else: | |
153 | self.old_banner2 = '' |
|
153 | self.old_banner2 = '' | |
154 |
|
154 | |||
155 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
155 | # Call the embedding code with a stack depth of 1 so it can skip over | |
156 | # our call and get the original caller's namespaces. |
|
156 | # our call and get the original caller's namespaces. | |
157 | self.mainloop(local_ns, module, stack_depth=stack_depth, |
|
157 | self.mainloop(local_ns, module, stack_depth=stack_depth, | |
158 | global_ns=global_ns, compile_flags=compile_flags) |
|
158 | global_ns=global_ns, compile_flags=compile_flags) | |
159 |
|
159 | |||
160 | self.banner2 = self.old_banner2 |
|
160 | self.banner2 = self.old_banner2 | |
161 |
|
161 | |||
162 | if self.exit_msg is not None: |
|
162 | if self.exit_msg is not None: | |
163 | print self.exit_msg |
|
163 | print self.exit_msg | |
164 |
|
164 | |||
165 | def mainloop(self, local_ns=None, module=None, stack_depth=0, |
|
165 | def mainloop(self, local_ns=None, module=None, stack_depth=0, | |
166 | display_banner=None, global_ns=None, compile_flags=None): |
|
166 | display_banner=None, global_ns=None, compile_flags=None): | |
167 | """Embeds IPython into a running python program. |
|
167 | """Embeds IPython into a running python program. | |
168 |
|
168 | |||
169 | Input: |
|
169 | Input: | |
170 |
|
170 | |||
171 | - header: An optional header message can be specified. |
|
171 | - header: An optional header message can be specified. | |
172 |
|
172 | |||
173 | - local_ns, module: working local namespace (a dict) and module (a |
|
173 | - local_ns, module: working local namespace (a dict) and module (a | |
174 | module or similar object). If given as None, they are automatically |
|
174 | module or similar object). If given as None, they are automatically | |
175 | taken from the scope where the shell was called, so that |
|
175 | taken from the scope where the shell was called, so that | |
176 | program variables become visible. |
|
176 | program variables become visible. | |
177 |
|
177 | |||
178 | - stack_depth: specifies how many levels in the stack to go to |
|
178 | - stack_depth: specifies how many levels in the stack to go to | |
179 | looking for namespaces (when local_ns or module is None). This |
|
179 | looking for namespaces (when local_ns or module is None). This | |
180 | allows an intermediate caller to make sure that this function gets |
|
180 | allows an intermediate caller to make sure that this function gets | |
181 | the namespace from the intended level in the stack. By default (0) |
|
181 | the namespace from the intended level in the stack. By default (0) | |
182 | it will get its locals and globals from the immediate caller. |
|
182 | it will get its locals and globals from the immediate caller. | |
183 |
|
183 | |||
184 | - compile_flags: A bit field identifying the __future__ features |
|
184 | - compile_flags: A bit field identifying the __future__ features | |
185 | that are enabled, as passed to the builtin `compile` function. If |
|
185 | that are enabled, as passed to the builtin `compile` function. If | |
186 | given as None, they are automatically taken from the scope where the |
|
186 | given as None, they are automatically taken from the scope where the | |
187 | shell was called. |
|
187 | shell was called. | |
188 |
|
188 | |||
189 | Warning: it's possible to use this in a program which is being run by |
|
189 | Warning: it's possible to use this in a program which is being run by | |
190 | IPython itself (via %run), but some funny things will happen (a few |
|
190 | IPython itself (via %run), but some funny things will happen (a few | |
191 | globals get overwritten). In the future this will be cleaned up, as |
|
191 | globals get overwritten). In the future this will be cleaned up, as | |
192 | there is no fundamental reason why it can't work perfectly.""" |
|
192 | there is no fundamental reason why it can't work perfectly.""" | |
193 |
|
193 | |||
194 | if (global_ns is not None) and (module is None): |
|
194 | if (global_ns is not None) and (module is None): | |
195 | class DummyMod(object): |
|
195 | class DummyMod(object): | |
196 | """A dummy module object for embedded IPython.""" |
|
196 | """A dummy module object for embedded IPython.""" | |
197 | pass |
|
197 | pass | |
198 | warnings.warn("global_ns is deprecated, use module instead.", DeprecationWarning) |
|
198 | warnings.warn("global_ns is deprecated, use module instead.", DeprecationWarning) | |
199 | module = DummyMod() |
|
199 | module = DummyMod() | |
200 | module.__dict__ = global_ns |
|
200 | module.__dict__ = global_ns | |
201 |
|
201 | |||
202 | # Get locals and globals from caller |
|
202 | # Get locals and globals from caller | |
203 | if ((local_ns is None or module is None or compile_flags is None) |
|
203 | if ((local_ns is None or module is None or compile_flags is None) | |
204 | and self.default_user_namespaces): |
|
204 | and self.default_user_namespaces): | |
205 | call_frame = sys._getframe(stack_depth).f_back |
|
205 | call_frame = sys._getframe(stack_depth).f_back | |
206 |
|
206 | |||
207 | if local_ns is None: |
|
207 | if local_ns is None: | |
208 | local_ns = call_frame.f_locals |
|
208 | local_ns = call_frame.f_locals | |
209 | if module is None: |
|
209 | if module is None: | |
210 | global_ns = call_frame.f_globals |
|
210 | global_ns = call_frame.f_globals | |
211 | module = sys.modules[global_ns['__name__']] |
|
211 | module = sys.modules[global_ns['__name__']] | |
212 | if compile_flags is None: |
|
212 | if compile_flags is None: | |
213 | compile_flags = (call_frame.f_code.co_flags & |
|
213 | compile_flags = (call_frame.f_code.co_flags & | |
214 | compilerop.PyCF_MASK) |
|
214 | compilerop.PyCF_MASK) | |
215 |
|
215 | |||
216 | # Save original namespace and module so we can restore them after |
|
216 | # Save original namespace and module so we can restore them after | |
217 | # embedding; otherwise the shell doesn't shut down correctly. |
|
217 | # embedding; otherwise the shell doesn't shut down correctly. | |
218 | orig_user_module = self.user_module |
|
218 | orig_user_module = self.user_module | |
219 | orig_user_ns = self.user_ns |
|
219 | orig_user_ns = self.user_ns | |
220 | orig_compile_flags = self.compile.flags |
|
220 | orig_compile_flags = self.compile.flags | |
221 |
|
221 | |||
222 | # Update namespaces and fire up interpreter |
|
222 | # Update namespaces and fire up interpreter | |
223 |
|
223 | |||
224 | # The global one is easy, we can just throw it in |
|
224 | # The global one is easy, we can just throw it in | |
225 | if module is not None: |
|
225 | if module is not None: | |
226 | self.user_module = module |
|
226 | self.user_module = module | |
227 |
|
227 | |||
228 | # But the user/local one is tricky: ipython needs it to store internal |
|
228 | # But the user/local one is tricky: ipython needs it to store internal | |
229 | # data, but we also need the locals. We'll throw our hidden variables |
|
229 | # data, but we also need the locals. We'll throw our hidden variables | |
230 | # like _ih and get_ipython() into the local namespace, but delete them |
|
230 | # like _ih and get_ipython() into the local namespace, but delete them | |
231 | # later. |
|
231 | # later. | |
232 | if local_ns is not None: |
|
232 | if local_ns is not None: | |
233 | self.user_ns = local_ns |
|
233 | self.user_ns = local_ns | |
234 | self.init_user_ns() |
|
234 | self.init_user_ns() | |
235 |
|
235 | |||
236 | # Compiler flags |
|
236 | # Compiler flags | |
237 | if compile_flags is not None: |
|
237 | if compile_flags is not None: | |
238 | self.compile.flags = compile_flags |
|
238 | self.compile.flags = compile_flags | |
239 |
|
239 | |||
240 | # Patch for global embedding to make sure that things don't overwrite |
|
240 | # Patch for global embedding to make sure that things don't overwrite | |
241 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
241 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> | |
242 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
242 | # FIXME. Test this a bit more carefully (the if.. is new) | |
243 | # N.B. This can't now ever be called. Not sure what it was for. |
|
243 | # N.B. This can't now ever be called. Not sure what it was for. | |
244 | # And now, since it wasn't called in the previous version, I'm |
|
244 | # And now, since it wasn't called in the previous version, I'm | |
245 | # commenting out these lines so they can't be called with my new changes |
|
245 | # commenting out these lines so they can't be called with my new changes | |
246 | # --TK, 2011-12-10 |
|
246 | # --TK, 2011-12-10 | |
247 | #if local_ns is None and module is None: |
|
247 | #if local_ns is None and module is None: | |
248 | # self.user_global_ns.update(__main__.__dict__) |
|
248 | # self.user_global_ns.update(__main__.__dict__) | |
249 |
|
249 | |||
250 | # make sure the tab-completer has the correct frame information, so it |
|
250 | # make sure the tab-completer has the correct frame information, so it | |
251 | # actually completes using the frame's locals/globals |
|
251 | # actually completes using the frame's locals/globals | |
252 | self.set_completer_frame() |
|
252 | self.set_completer_frame() | |
253 |
|
253 | |||
254 | with nested(self.builtin_trap, self.display_trap): |
|
254 | with nested(self.builtin_trap, self.display_trap): | |
255 | self.interact(display_banner=display_banner) |
|
255 | self.interact(display_banner=display_banner) | |
256 |
|
256 | |||
257 | # now, purge out the local namespace of IPython's hidden variables. |
|
257 | # now, purge out the local namespace of IPython's hidden variables. | |
258 | if local_ns is not None: |
|
258 | if local_ns is not None: | |
259 | for name in self.user_ns_hidden: |
|
259 | for name in self.user_ns_hidden: | |
260 | local_ns.pop(name, None) |
|
260 | local_ns.pop(name, None) | |
261 |
|
261 | |||
262 | # Restore original namespace so shell can shut down when we exit. |
|
262 | # Restore original namespace so shell can shut down when we exit. | |
263 | self.user_module = orig_user_module |
|
263 | self.user_module = orig_user_module | |
264 | self.user_ns = orig_user_ns |
|
264 | self.user_ns = orig_user_ns | |
265 | self.compile.flags = orig_compile_flags |
|
265 | self.compile.flags = orig_compile_flags | |
266 |
|
266 | |||
267 |
|
267 | |||
268 | def embed(**kwargs): |
|
268 | def embed(**kwargs): | |
269 | """Call this to embed IPython at the current point in your program. |
|
269 | """Call this to embed IPython at the current point in your program. | |
270 |
|
270 | |||
271 | The first invocation of this will create an :class:`InteractiveShellEmbed` |
|
271 | The first invocation of this will create an :class:`InteractiveShellEmbed` | |
272 | instance and then call it. Consecutive calls just call the already |
|
272 | instance and then call it. Consecutive calls just call the already | |
273 | created instance. |
|
273 | created instance. | |
274 |
|
274 | |||
275 | Here is a simple example:: |
|
275 | Here is a simple example:: | |
276 |
|
276 | |||
277 | from IPython import embed |
|
277 | from IPython import embed | |
278 | a = 10 |
|
278 | a = 10 | |
279 | b = 20 |
|
279 | b = 20 | |
280 | embed('First time') |
|
280 | embed('First time') | |
281 | c = 30 |
|
281 | c = 30 | |
282 | d = 40 |
|
282 | d = 40 | |
283 | embed |
|
283 | embed | |
284 |
|
284 | |||
285 | Full customization can be done by passing a :class:`Config` in as the |
|
285 | Full customization can be done by passing a :class:`Config` in as the | |
286 | config argument. |
|
286 | config argument. | |
287 | """ |
|
287 | """ | |
288 | config = kwargs.get('config') |
|
288 | config = kwargs.get('config') | |
289 | header = kwargs.pop('header', u'') |
|
289 | header = kwargs.pop('header', u'') | |
290 | compile_flags = kwargs.pop('compile_flags', None) |
|
290 | compile_flags = kwargs.pop('compile_flags', None) | |
291 | if config is None: |
|
291 | if config is None: | |
292 | config = load_default_config() |
|
292 | config = load_default_config() | |
293 | config.InteractiveShellEmbed = config.TerminalInteractiveShell |
|
293 | config.InteractiveShellEmbed = config.TerminalInteractiveShell | |
294 | kwargs['config'] = config |
|
294 | kwargs['config'] = config | |
295 | shell = InteractiveShellEmbed.instance(**kwargs) |
|
295 | shell = InteractiveShellEmbed.instance(**kwargs) | |
296 | shell(header=header, stack_depth=2, compile_flags=compile_flags) |
|
296 | shell(header=header, stack_depth=2, compile_flags=compile_flags) |
@@ -1,395 +1,395 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 | * Min Ragan-Kelley |
|
12 | * Min Ragan-Kelley | |
13 | """ |
|
13 | """ | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Copyright (C) 2008-2011 The IPython Development Team |
|
16 | # Copyright (C) 2008-2011 The IPython Development Team | |
17 | # |
|
17 | # | |
18 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | # Distributed under the terms of the BSD License. The full license is in | |
19 | # the file COPYING, distributed as part of this software. |
|
19 | # the file COPYING, distributed as part of this software. | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Imports |
|
23 | # Imports | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | from __future__ import absolute_import |
|
26 | from __future__ import absolute_import | |
27 |
|
27 | |||
28 | import logging |
|
28 | import logging | |
29 | import os |
|
29 | import os | |
30 | import sys |
|
30 | import sys | |
31 |
|
31 | |||
32 | from IPython.config.loader import ( |
|
32 | from IPython.config.loader import ( | |
33 | Config, PyFileConfigLoader, ConfigFileNotFound |
|
33 | Config, PyFileConfigLoader, ConfigFileNotFound | |
34 | ) |
|
34 | ) | |
35 | from IPython.config.application import boolean_flag, catch_config_error |
|
35 | from IPython.config.application import boolean_flag, catch_config_error | |
36 | from IPython.core import release |
|
36 | from IPython.core import release | |
37 | from IPython.core import usage |
|
37 | from IPython.core import usage | |
38 | from IPython.core.completer import IPCompleter |
|
38 | from IPython.core.completer import IPCompleter | |
39 | from IPython.core.crashhandler import CrashHandler |
|
39 | from IPython.core.crashhandler import CrashHandler | |
40 | from IPython.core.formatters import PlainTextFormatter |
|
40 | from IPython.core.formatters import PlainTextFormatter | |
41 | from IPython.core.history import HistoryManager |
|
41 | from IPython.core.history import HistoryManager | |
42 | from IPython.core.prompts import PromptManager |
|
42 | from IPython.core.prompts import PromptManager | |
43 | from IPython.core.application import ( |
|
43 | from IPython.core.application import ( | |
44 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases |
|
44 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases | |
45 | ) |
|
45 | ) | |
46 | from IPython.core.magics import ScriptMagics |
|
46 | from IPython.core.magics import ScriptMagics | |
47 | from IPython.core.shellapp import ( |
|
47 | from IPython.core.shellapp import ( | |
48 | InteractiveShellApp, shell_flags, shell_aliases |
|
48 | InteractiveShellApp, shell_flags, shell_aliases | |
49 | ) |
|
49 | ) | |
50 |
from IPython |
|
50 | from IPython.terminal.interactiveshell import TerminalInteractiveShell | |
51 | from IPython.utils import warn |
|
51 | from IPython.utils import warn | |
52 | from IPython.utils.path import get_ipython_dir, check_for_old_config |
|
52 | from IPython.utils.path import get_ipython_dir, check_for_old_config | |
53 | from IPython.utils.traitlets import ( |
|
53 | from IPython.utils.traitlets import ( | |
54 | Bool, List, Dict, CaselessStrEnum |
|
54 | Bool, List, Dict, CaselessStrEnum | |
55 | ) |
|
55 | ) | |
56 |
|
56 | |||
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 | # Globals, utilities and helpers |
|
58 | # Globals, utilities and helpers | |
59 | #----------------------------------------------------------------------------- |
|
59 | #----------------------------------------------------------------------------- | |
60 |
|
60 | |||
61 | #: The default config file name for this application. |
|
61 | #: The default config file name for this application. | |
62 | default_config_file_name = u'ipython_config.py' |
|
62 | default_config_file_name = u'ipython_config.py' | |
63 |
|
63 | |||
64 | _examples = """ |
|
64 | _examples = """ | |
65 | ipython --pylab # start in pylab mode |
|
65 | ipython --pylab # start in pylab mode | |
66 | ipython --pylab=qt # start in pylab mode with the qt4 backend |
|
66 | ipython --pylab=qt # start in pylab mode with the qt4 backend | |
67 | ipython --log-level=DEBUG # set logging to DEBUG |
|
67 | ipython --log-level=DEBUG # set logging to DEBUG | |
68 | ipython --profile=foo # start with profile foo |
|
68 | ipython --profile=foo # start with profile foo | |
69 |
|
69 | |||
70 | ipython qtconsole # start the qtconsole GUI application |
|
70 | ipython qtconsole # start the qtconsole GUI application | |
71 | ipython help qtconsole # show the help for the qtconsole subcmd |
|
71 | ipython help qtconsole # show the help for the qtconsole subcmd | |
72 |
|
72 | |||
73 | ipython console # start the terminal-based console application |
|
73 | ipython console # start the terminal-based console application | |
74 | ipython help console # show the help for the console subcmd |
|
74 | ipython help console # show the help for the console subcmd | |
75 |
|
75 | |||
76 | ipython notebook # start the IPython notebook |
|
76 | ipython notebook # start the IPython notebook | |
77 | ipython help notebook # show the help for the notebook subcmd |
|
77 | ipython help notebook # show the help for the notebook subcmd | |
78 |
|
78 | |||
79 | ipython profile create foo # create profile foo w/ default config files |
|
79 | ipython profile create foo # create profile foo w/ default config files | |
80 | ipython help profile # show the help for the profile subcmd |
|
80 | ipython help profile # show the help for the profile subcmd | |
81 |
|
81 | |||
82 | ipython locate # print the path to the IPython directory |
|
82 | ipython locate # print the path to the IPython directory | |
83 | ipython locate profile foo # print the path to the directory for profile `foo` |
|
83 | ipython locate profile foo # print the path to the directory for profile `foo` | |
84 | """ |
|
84 | """ | |
85 |
|
85 | |||
86 | #----------------------------------------------------------------------------- |
|
86 | #----------------------------------------------------------------------------- | |
87 | # Crash handler for this application |
|
87 | # Crash handler for this application | |
88 | #----------------------------------------------------------------------------- |
|
88 | #----------------------------------------------------------------------------- | |
89 |
|
89 | |||
90 | class IPAppCrashHandler(CrashHandler): |
|
90 | class IPAppCrashHandler(CrashHandler): | |
91 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
91 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" | |
92 |
|
92 | |||
93 | def __init__(self, app): |
|
93 | def __init__(self, app): | |
94 | contact_name = release.author |
|
94 | contact_name = release.author | |
95 | contact_email = release.author_email |
|
95 | contact_email = release.author_email | |
96 | bug_tracker = 'https://github.com/ipython/ipython/issues' |
|
96 | bug_tracker = 'https://github.com/ipython/ipython/issues' | |
97 | super(IPAppCrashHandler,self).__init__( |
|
97 | super(IPAppCrashHandler,self).__init__( | |
98 | app, contact_name, contact_email, bug_tracker |
|
98 | app, contact_name, contact_email, bug_tracker | |
99 | ) |
|
99 | ) | |
100 |
|
100 | |||
101 | def make_report(self,traceback): |
|
101 | def make_report(self,traceback): | |
102 | """Return a string containing a crash report.""" |
|
102 | """Return a string containing a crash report.""" | |
103 |
|
103 | |||
104 | sec_sep = self.section_sep |
|
104 | sec_sep = self.section_sep | |
105 | # Start with parent report |
|
105 | # Start with parent report | |
106 | report = [super(IPAppCrashHandler, self).make_report(traceback)] |
|
106 | report = [super(IPAppCrashHandler, self).make_report(traceback)] | |
107 | # Add interactive-specific info we may have |
|
107 | # Add interactive-specific info we may have | |
108 | rpt_add = report.append |
|
108 | rpt_add = report.append | |
109 | try: |
|
109 | try: | |
110 | rpt_add(sec_sep+"History of session input:") |
|
110 | rpt_add(sec_sep+"History of session input:") | |
111 | for line in self.app.shell.user_ns['_ih']: |
|
111 | for line in self.app.shell.user_ns['_ih']: | |
112 | rpt_add(line) |
|
112 | rpt_add(line) | |
113 | rpt_add('\n*** Last line of input (may not be in above history):\n') |
|
113 | rpt_add('\n*** Last line of input (may not be in above history):\n') | |
114 | rpt_add(self.app.shell._last_input_line+'\n') |
|
114 | rpt_add(self.app.shell._last_input_line+'\n') | |
115 | except: |
|
115 | except: | |
116 | pass |
|
116 | pass | |
117 |
|
117 | |||
118 | return ''.join(report) |
|
118 | return ''.join(report) | |
119 |
|
119 | |||
120 | #----------------------------------------------------------------------------- |
|
120 | #----------------------------------------------------------------------------- | |
121 | # Aliases and Flags |
|
121 | # Aliases and Flags | |
122 | #----------------------------------------------------------------------------- |
|
122 | #----------------------------------------------------------------------------- | |
123 | flags = dict(base_flags) |
|
123 | flags = dict(base_flags) | |
124 | flags.update(shell_flags) |
|
124 | flags.update(shell_flags) | |
125 | frontend_flags = {} |
|
125 | frontend_flags = {} | |
126 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) |
|
126 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) | |
127 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', |
|
127 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', | |
128 | 'Turn on auto editing of files with syntax errors.', |
|
128 | 'Turn on auto editing of files with syntax errors.', | |
129 | 'Turn off auto editing of files with syntax errors.' |
|
129 | 'Turn off auto editing of files with syntax errors.' | |
130 | ) |
|
130 | ) | |
131 | addflag('banner', 'TerminalIPythonApp.display_banner', |
|
131 | addflag('banner', 'TerminalIPythonApp.display_banner', | |
132 | "Display a banner upon starting IPython.", |
|
132 | "Display a banner upon starting IPython.", | |
133 | "Don't display a banner upon starting IPython." |
|
133 | "Don't display a banner upon starting IPython." | |
134 | ) |
|
134 | ) | |
135 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', |
|
135 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', | |
136 | """Set to confirm when you try to exit IPython with an EOF (Control-D |
|
136 | """Set to confirm when you try to exit IPython with an EOF (Control-D | |
137 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
137 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', | |
138 | you can force a direct exit without any confirmation.""", |
|
138 | you can force a direct exit without any confirmation.""", | |
139 | "Don't prompt the user when exiting." |
|
139 | "Don't prompt the user when exiting." | |
140 | ) |
|
140 | ) | |
141 | addflag('term-title', 'TerminalInteractiveShell.term_title', |
|
141 | addflag('term-title', 'TerminalInteractiveShell.term_title', | |
142 | "Enable auto setting the terminal title.", |
|
142 | "Enable auto setting the terminal title.", | |
143 | "Disable auto setting the terminal title." |
|
143 | "Disable auto setting the terminal title." | |
144 | ) |
|
144 | ) | |
145 | classic_config = Config() |
|
145 | classic_config = Config() | |
146 | classic_config.InteractiveShell.cache_size = 0 |
|
146 | classic_config.InteractiveShell.cache_size = 0 | |
147 | classic_config.PlainTextFormatter.pprint = False |
|
147 | classic_config.PlainTextFormatter.pprint = False | |
148 | classic_config.PromptManager.in_template = '>>> ' |
|
148 | classic_config.PromptManager.in_template = '>>> ' | |
149 | classic_config.PromptManager.in2_template = '... ' |
|
149 | classic_config.PromptManager.in2_template = '... ' | |
150 | classic_config.PromptManager.out_template = '' |
|
150 | classic_config.PromptManager.out_template = '' | |
151 | classic_config.InteractiveShell.separate_in = '' |
|
151 | classic_config.InteractiveShell.separate_in = '' | |
152 | classic_config.InteractiveShell.separate_out = '' |
|
152 | classic_config.InteractiveShell.separate_out = '' | |
153 | classic_config.InteractiveShell.separate_out2 = '' |
|
153 | classic_config.InteractiveShell.separate_out2 = '' | |
154 | classic_config.InteractiveShell.colors = 'NoColor' |
|
154 | classic_config.InteractiveShell.colors = 'NoColor' | |
155 | classic_config.InteractiveShell.xmode = 'Plain' |
|
155 | classic_config.InteractiveShell.xmode = 'Plain' | |
156 |
|
156 | |||
157 | frontend_flags['classic']=( |
|
157 | frontend_flags['classic']=( | |
158 | classic_config, |
|
158 | classic_config, | |
159 | "Gives IPython a similar feel to the classic Python prompt." |
|
159 | "Gives IPython a similar feel to the classic Python prompt." | |
160 | ) |
|
160 | ) | |
161 | # # log doesn't make so much sense this way anymore |
|
161 | # # log doesn't make so much sense this way anymore | |
162 | # paa('--log','-l', |
|
162 | # paa('--log','-l', | |
163 | # action='store_true', dest='InteractiveShell.logstart', |
|
163 | # action='store_true', dest='InteractiveShell.logstart', | |
164 | # help="Start logging to the default log file (./ipython_log.py).") |
|
164 | # help="Start logging to the default log file (./ipython_log.py).") | |
165 | # |
|
165 | # | |
166 | # # quick is harder to implement |
|
166 | # # quick is harder to implement | |
167 | frontend_flags['quick']=( |
|
167 | frontend_flags['quick']=( | |
168 | {'TerminalIPythonApp' : {'quick' : True}}, |
|
168 | {'TerminalIPythonApp' : {'quick' : True}}, | |
169 | "Enable quick startup with no config files." |
|
169 | "Enable quick startup with no config files." | |
170 | ) |
|
170 | ) | |
171 |
|
171 | |||
172 | frontend_flags['i'] = ( |
|
172 | frontend_flags['i'] = ( | |
173 | {'TerminalIPythonApp' : {'force_interact' : True}}, |
|
173 | {'TerminalIPythonApp' : {'force_interact' : True}}, | |
174 | """If running code from the command line, become interactive afterwards. |
|
174 | """If running code from the command line, become interactive afterwards. | |
175 | Note: can also be given simply as '-i.'""" |
|
175 | Note: can also be given simply as '-i.'""" | |
176 | ) |
|
176 | ) | |
177 | flags.update(frontend_flags) |
|
177 | flags.update(frontend_flags) | |
178 |
|
178 | |||
179 | aliases = dict(base_aliases) |
|
179 | aliases = dict(base_aliases) | |
180 | aliases.update(shell_aliases) |
|
180 | aliases.update(shell_aliases) | |
181 |
|
181 | |||
182 | #----------------------------------------------------------------------------- |
|
182 | #----------------------------------------------------------------------------- | |
183 | # Main classes and functions |
|
183 | # Main classes and functions | |
184 | #----------------------------------------------------------------------------- |
|
184 | #----------------------------------------------------------------------------- | |
185 |
|
185 | |||
186 |
|
186 | |||
187 | class LocateIPythonApp(BaseIPythonApplication): |
|
187 | class LocateIPythonApp(BaseIPythonApplication): | |
188 | description = """print the path to the IPython dir""" |
|
188 | description = """print the path to the IPython dir""" | |
189 | subcommands = Dict(dict( |
|
189 | subcommands = Dict(dict( | |
190 | profile=('IPython.core.profileapp.ProfileLocate', |
|
190 | profile=('IPython.core.profileapp.ProfileLocate', | |
191 | "print the path to an IPython profile directory", |
|
191 | "print the path to an IPython profile directory", | |
192 | ), |
|
192 | ), | |
193 | )) |
|
193 | )) | |
194 | def start(self): |
|
194 | def start(self): | |
195 | if self.subapp is not None: |
|
195 | if self.subapp is not None: | |
196 | return self.subapp.start() |
|
196 | return self.subapp.start() | |
197 | else: |
|
197 | else: | |
198 | print self.ipython_dir |
|
198 | print self.ipython_dir | |
199 |
|
199 | |||
200 |
|
200 | |||
201 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): |
|
201 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): | |
202 | name = u'ipython' |
|
202 | name = u'ipython' | |
203 | description = usage.cl_usage |
|
203 | description = usage.cl_usage | |
204 | default_config_file_name = default_config_file_name |
|
204 | default_config_file_name = default_config_file_name | |
205 | crash_handler_class = IPAppCrashHandler |
|
205 | crash_handler_class = IPAppCrashHandler | |
206 | examples = _examples |
|
206 | examples = _examples | |
207 |
|
207 | |||
208 | flags = Dict(flags) |
|
208 | flags = Dict(flags) | |
209 | aliases = Dict(aliases) |
|
209 | aliases = Dict(aliases) | |
210 | classes = List() |
|
210 | classes = List() | |
211 | def _classes_default(self): |
|
211 | def _classes_default(self): | |
212 | """This has to be in a method, for TerminalIPythonApp to be available.""" |
|
212 | """This has to be in a method, for TerminalIPythonApp to be available.""" | |
213 | return [ |
|
213 | return [ | |
214 | InteractiveShellApp, # ShellApp comes before TerminalApp, because |
|
214 | InteractiveShellApp, # ShellApp comes before TerminalApp, because | |
215 | self.__class__, # it will also affect subclasses (e.g. QtConsole) |
|
215 | self.__class__, # it will also affect subclasses (e.g. QtConsole) | |
216 | TerminalInteractiveShell, |
|
216 | TerminalInteractiveShell, | |
217 | PromptManager, |
|
217 | PromptManager, | |
218 | HistoryManager, |
|
218 | HistoryManager, | |
219 | ProfileDir, |
|
219 | ProfileDir, | |
220 | PlainTextFormatter, |
|
220 | PlainTextFormatter, | |
221 | IPCompleter, |
|
221 | IPCompleter, | |
222 | ScriptMagics, |
|
222 | ScriptMagics, | |
223 | ] |
|
223 | ] | |
224 |
|
224 | |||
225 | subcommands = Dict(dict( |
|
225 | subcommands = Dict(dict( | |
226 |
qtconsole=('IPython. |
|
226 | qtconsole=('IPython.qt.console.qtconsoleapp.IPythonQtConsoleApp', | |
227 | """Launch the IPython Qt Console.""" |
|
227 | """Launch the IPython Qt Console.""" | |
228 | ), |
|
228 | ), | |
229 |
notebook=('IPython. |
|
229 | notebook=('IPython.html.notebook.notebookapp.NotebookApp', | |
230 | """Launch the IPython HTML Notebook Server.""" |
|
230 | """Launch the IPython HTML Notebook Server.""" | |
231 | ), |
|
231 | ), | |
232 | profile = ("IPython.core.profileapp.ProfileApp", |
|
232 | profile = ("IPython.core.profileapp.ProfileApp", | |
233 | "Create and manage IPython profiles." |
|
233 | "Create and manage IPython profiles." | |
234 | ), |
|
234 | ), | |
235 | kernel = ("IPython.kernel.zmq.kernelapp.IPKernelApp", |
|
235 | kernel = ("IPython.kernel.zmq.kernelapp.IPKernelApp", | |
236 | "Start a kernel without an attached frontend." |
|
236 | "Start a kernel without an attached frontend." | |
237 | ), |
|
237 | ), | |
238 |
console=('IPython. |
|
238 | console=('IPython.terminal.console.app.ZMQTerminalIPythonApp', | |
239 | """Launch the IPython terminal-based Console.""" |
|
239 | """Launch the IPython terminal-based Console.""" | |
240 | ), |
|
240 | ), | |
241 |
locate=('IPython. |
|
241 | locate=('IPython.terminal.ipapp.LocateIPythonApp', | |
242 | LocateIPythonApp.description |
|
242 | LocateIPythonApp.description | |
243 | ), |
|
243 | ), | |
244 | history=('IPython.core.historyapp.HistoryApp', |
|
244 | history=('IPython.core.historyapp.HistoryApp', | |
245 | "Manage the IPython history database." |
|
245 | "Manage the IPython history database." | |
246 | ), |
|
246 | ), | |
247 | )) |
|
247 | )) | |
248 |
|
248 | |||
249 | # *do* autocreate requested profile, but don't create the config file. |
|
249 | # *do* autocreate requested profile, but don't create the config file. | |
250 | auto_create=Bool(True) |
|
250 | auto_create=Bool(True) | |
251 | # configurables |
|
251 | # configurables | |
252 | ignore_old_config=Bool(False, config=True, |
|
252 | ignore_old_config=Bool(False, config=True, | |
253 | help="Suppress warning messages about legacy config files" |
|
253 | help="Suppress warning messages about legacy config files" | |
254 | ) |
|
254 | ) | |
255 | quick = Bool(False, config=True, |
|
255 | quick = Bool(False, config=True, | |
256 | help="""Start IPython quickly by skipping the loading of config files.""" |
|
256 | help="""Start IPython quickly by skipping the loading of config files.""" | |
257 | ) |
|
257 | ) | |
258 | def _quick_changed(self, name, old, new): |
|
258 | def _quick_changed(self, name, old, new): | |
259 | if new: |
|
259 | if new: | |
260 | self.load_config_file = lambda *a, **kw: None |
|
260 | self.load_config_file = lambda *a, **kw: None | |
261 | self.ignore_old_config=True |
|
261 | self.ignore_old_config=True | |
262 |
|
262 | |||
263 | display_banner = Bool(True, config=True, |
|
263 | display_banner = Bool(True, config=True, | |
264 | help="Whether to display a banner upon starting IPython." |
|
264 | help="Whether to display a banner upon starting IPython." | |
265 | ) |
|
265 | ) | |
266 |
|
266 | |||
267 | # if there is code of files to run from the cmd line, don't interact |
|
267 | # if there is code of files to run from the cmd line, don't interact | |
268 | # unless the --i flag (App.force_interact) is true. |
|
268 | # unless the --i flag (App.force_interact) is true. | |
269 | force_interact = Bool(False, config=True, |
|
269 | force_interact = Bool(False, config=True, | |
270 | help="""If a command or file is given via the command-line, |
|
270 | help="""If a command or file is given via the command-line, | |
271 | e.g. 'ipython foo.py""" |
|
271 | e.g. 'ipython foo.py""" | |
272 | ) |
|
272 | ) | |
273 | def _force_interact_changed(self, name, old, new): |
|
273 | def _force_interact_changed(self, name, old, new): | |
274 | if new: |
|
274 | if new: | |
275 | self.interact = True |
|
275 | self.interact = True | |
276 |
|
276 | |||
277 | def _file_to_run_changed(self, name, old, new): |
|
277 | def _file_to_run_changed(self, name, old, new): | |
278 | if new: |
|
278 | if new: | |
279 | self.something_to_run = True |
|
279 | self.something_to_run = True | |
280 | if new and not self.force_interact: |
|
280 | if new and not self.force_interact: | |
281 | self.interact = False |
|
281 | self.interact = False | |
282 | _code_to_run_changed = _file_to_run_changed |
|
282 | _code_to_run_changed = _file_to_run_changed | |
283 | _module_to_run_changed = _file_to_run_changed |
|
283 | _module_to_run_changed = _file_to_run_changed | |
284 |
|
284 | |||
285 | # internal, not-configurable |
|
285 | # internal, not-configurable | |
286 | interact=Bool(True) |
|
286 | interact=Bool(True) | |
287 | something_to_run=Bool(False) |
|
287 | something_to_run=Bool(False) | |
288 |
|
288 | |||
289 | def parse_command_line(self, argv=None): |
|
289 | def parse_command_line(self, argv=None): | |
290 | """override to allow old '-pylab' flag with deprecation warning""" |
|
290 | """override to allow old '-pylab' flag with deprecation warning""" | |
291 |
|
291 | |||
292 | argv = sys.argv[1:] if argv is None else argv |
|
292 | argv = sys.argv[1:] if argv is None else argv | |
293 |
|
293 | |||
294 | if '-pylab' in argv: |
|
294 | if '-pylab' in argv: | |
295 | # deprecated `-pylab` given, |
|
295 | # deprecated `-pylab` given, | |
296 | # warn and transform into current syntax |
|
296 | # warn and transform into current syntax | |
297 | argv = argv[:] # copy, don't clobber |
|
297 | argv = argv[:] # copy, don't clobber | |
298 | idx = argv.index('-pylab') |
|
298 | idx = argv.index('-pylab') | |
299 | warn.warn("`-pylab` flag has been deprecated.\n" |
|
299 | warn.warn("`-pylab` flag has been deprecated.\n" | |
300 | " Use `--pylab` instead, or `--pylab=foo` to specify a backend.") |
|
300 | " Use `--pylab` instead, or `--pylab=foo` to specify a backend.") | |
301 | sub = '--pylab' |
|
301 | sub = '--pylab' | |
302 | if len(argv) > idx+1: |
|
302 | if len(argv) > idx+1: | |
303 | # check for gui arg, as in '-pylab qt' |
|
303 | # check for gui arg, as in '-pylab qt' | |
304 | gui = argv[idx+1] |
|
304 | gui = argv[idx+1] | |
305 | if gui in ('wx', 'qt', 'qt4', 'gtk', 'auto'): |
|
305 | if gui in ('wx', 'qt', 'qt4', 'gtk', 'auto'): | |
306 | sub = '--pylab='+gui |
|
306 | sub = '--pylab='+gui | |
307 | argv.pop(idx+1) |
|
307 | argv.pop(idx+1) | |
308 | argv[idx] = sub |
|
308 | argv[idx] = sub | |
309 |
|
309 | |||
310 | return super(TerminalIPythonApp, self).parse_command_line(argv) |
|
310 | return super(TerminalIPythonApp, self).parse_command_line(argv) | |
311 |
|
311 | |||
312 | @catch_config_error |
|
312 | @catch_config_error | |
313 | def initialize(self, argv=None): |
|
313 | def initialize(self, argv=None): | |
314 | """Do actions after construct, but before starting the app.""" |
|
314 | """Do actions after construct, but before starting the app.""" | |
315 | super(TerminalIPythonApp, self).initialize(argv) |
|
315 | super(TerminalIPythonApp, self).initialize(argv) | |
316 | if self.subapp is not None: |
|
316 | if self.subapp is not None: | |
317 | # don't bother initializing further, starting subapp |
|
317 | # don't bother initializing further, starting subapp | |
318 | return |
|
318 | return | |
319 | if not self.ignore_old_config: |
|
319 | if not self.ignore_old_config: | |
320 | check_for_old_config(self.ipython_dir) |
|
320 | check_for_old_config(self.ipython_dir) | |
321 | # print self.extra_args |
|
321 | # print self.extra_args | |
322 | if self.extra_args and not self.something_to_run: |
|
322 | if self.extra_args and not self.something_to_run: | |
323 | self.file_to_run = self.extra_args[0] |
|
323 | self.file_to_run = self.extra_args[0] | |
324 | self.init_path() |
|
324 | self.init_path() | |
325 | # create the shell |
|
325 | # create the shell | |
326 | self.init_shell() |
|
326 | self.init_shell() | |
327 | # and draw the banner |
|
327 | # and draw the banner | |
328 | self.init_banner() |
|
328 | self.init_banner() | |
329 | # Now a variety of things that happen after the banner is printed. |
|
329 | # Now a variety of things that happen after the banner is printed. | |
330 | self.init_gui_pylab() |
|
330 | self.init_gui_pylab() | |
331 | self.init_extensions() |
|
331 | self.init_extensions() | |
332 | self.init_code() |
|
332 | self.init_code() | |
333 |
|
333 | |||
334 | def init_shell(self): |
|
334 | def init_shell(self): | |
335 | """initialize the InteractiveShell instance""" |
|
335 | """initialize the InteractiveShell instance""" | |
336 | # Create an InteractiveShell instance. |
|
336 | # Create an InteractiveShell instance. | |
337 | # shell.display_banner should always be False for the terminal |
|
337 | # shell.display_banner should always be False for the terminal | |
338 | # based app, because we call shell.show_banner() by hand below |
|
338 | # based app, because we call shell.show_banner() by hand below | |
339 | # so the banner shows *before* all extension loading stuff. |
|
339 | # so the banner shows *before* all extension loading stuff. | |
340 | self.shell = TerminalInteractiveShell.instance(config=self.config, |
|
340 | self.shell = TerminalInteractiveShell.instance(config=self.config, | |
341 | display_banner=False, profile_dir=self.profile_dir, |
|
341 | display_banner=False, profile_dir=self.profile_dir, | |
342 | ipython_dir=self.ipython_dir) |
|
342 | ipython_dir=self.ipython_dir) | |
343 | self.shell.configurables.append(self) |
|
343 | self.shell.configurables.append(self) | |
344 |
|
344 | |||
345 | def init_banner(self): |
|
345 | def init_banner(self): | |
346 | """optionally display the banner""" |
|
346 | """optionally display the banner""" | |
347 | if self.display_banner and self.interact: |
|
347 | if self.display_banner and self.interact: | |
348 | self.shell.show_banner() |
|
348 | self.shell.show_banner() | |
349 | # Make sure there is a space below the banner. |
|
349 | # Make sure there is a space below the banner. | |
350 | if self.log_level <= logging.INFO: print |
|
350 | if self.log_level <= logging.INFO: print | |
351 |
|
351 | |||
352 | def _pylab_changed(self, name, old, new): |
|
352 | def _pylab_changed(self, name, old, new): | |
353 | """Replace --pylab='inline' with --pylab='auto'""" |
|
353 | """Replace --pylab='inline' with --pylab='auto'""" | |
354 | if new == 'inline': |
|
354 | if new == 'inline': | |
355 | warn.warn("'inline' not available as pylab backend, " |
|
355 | warn.warn("'inline' not available as pylab backend, " | |
356 | "using 'auto' instead.") |
|
356 | "using 'auto' instead.") | |
357 | self.pylab = 'auto' |
|
357 | self.pylab = 'auto' | |
358 |
|
358 | |||
359 | def start(self): |
|
359 | def start(self): | |
360 | if self.subapp is not None: |
|
360 | if self.subapp is not None: | |
361 | return self.subapp.start() |
|
361 | return self.subapp.start() | |
362 | # perform any prexec steps: |
|
362 | # perform any prexec steps: | |
363 | if self.interact: |
|
363 | if self.interact: | |
364 | self.log.debug("Starting IPython's mainloop...") |
|
364 | self.log.debug("Starting IPython's mainloop...") | |
365 | self.shell.mainloop() |
|
365 | self.shell.mainloop() | |
366 | else: |
|
366 | else: | |
367 | self.log.debug("IPython not interactive...") |
|
367 | self.log.debug("IPython not interactive...") | |
368 |
|
368 | |||
369 |
|
369 | |||
370 | def load_default_config(ipython_dir=None): |
|
370 | def load_default_config(ipython_dir=None): | |
371 | """Load the default config file from the default ipython_dir. |
|
371 | """Load the default config file from the default ipython_dir. | |
372 |
|
372 | |||
373 | This is useful for embedded shells. |
|
373 | This is useful for embedded shells. | |
374 | """ |
|
374 | """ | |
375 | if ipython_dir is None: |
|
375 | if ipython_dir is None: | |
376 | ipython_dir = get_ipython_dir() |
|
376 | ipython_dir = get_ipython_dir() | |
377 | profile_dir = os.path.join(ipython_dir, 'profile_default') |
|
377 | profile_dir = os.path.join(ipython_dir, 'profile_default') | |
378 | cl = PyFileConfigLoader(default_config_file_name, profile_dir) |
|
378 | cl = PyFileConfigLoader(default_config_file_name, profile_dir) | |
379 | try: |
|
379 | try: | |
380 | config = cl.load_config() |
|
380 | config = cl.load_config() | |
381 | except ConfigFileNotFound: |
|
381 | except ConfigFileNotFound: | |
382 | # no config found |
|
382 | # no config found | |
383 | config = Config() |
|
383 | config = Config() | |
384 | return config |
|
384 | return config | |
385 |
|
385 | |||
386 |
|
386 | |||
387 | def launch_new_instance(): |
|
387 | def launch_new_instance(): | |
388 | """Create and run a full blown IPython instance""" |
|
388 | """Create and run a full blown IPython instance""" | |
389 | app = TerminalIPythonApp.instance() |
|
389 | app = TerminalIPythonApp.instance() | |
390 | app.initialize() |
|
390 | app.initialize() | |
391 | app.start() |
|
391 | app.start() | |
392 |
|
392 | |||
393 |
|
393 | |||
394 | if __name__ == '__main__': |
|
394 | if __name__ == '__main__': | |
395 | launch_new_instance() |
|
395 | launch_new_instance() |
General Comments 0
You need to be logged in to leave comments.
Login now