Show More
@@ -24,12 +24,12 b' prompt.' | |||
|
24 | 24 | $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ |
|
25 | 25 | """ |
|
26 | 26 | |
|
27 |
import IPython.ipapi |
|
|
27 | import IPython.ipapi | |
|
28 | ip = IPython.ipapi.get() | |
|
28 | 29 | |
|
29 | 30 | |
|
30 | 31 | import os,re,fnmatch |
|
31 | 32 | |
|
32 | @ip.asmagic("rehashdir") | |
|
33 | 33 | def rehashdir_f(self,arg): |
|
34 | 34 | """ Add executables in all specified dirs to alias table |
|
35 | 35 | |
@@ -99,3 +99,4 b' def rehashdir_f(self,arg):' | |||
|
99 | 99 | self.shell.init_auto_alias() |
|
100 | 100 | finally: |
|
101 | 101 | os.chdir(savedir) |
|
102 | ip.expose_magic("rehashdir",rehashdir_f) |
@@ -4,7 +4,7 b'' | |||
|
4 | 4 | All the matplotlib support code was co-developed with John Hunter, |
|
5 | 5 | matplotlib's author. |
|
6 | 6 | |
|
7 |
$Id: Shell.py 10 |
|
|
7 | $Id: Shell.py 1079 2006-01-24 21:52:31Z vivainio $""" | |
|
8 | 8 | |
|
9 | 9 | #***************************************************************************** |
|
10 | 10 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
@@ -919,7 +919,7 b' def _matplotlib_shell_class():' | |||
|
919 | 919 | return sh_class |
|
920 | 920 | |
|
921 | 921 | # This is the one which should be called by external code. |
|
922 | def start(): | |
|
922 | def start(user_ns = None): | |
|
923 | 923 | """Return a running shell instance, dealing with threading options. |
|
924 | 924 | |
|
925 | 925 | This is a factory function which will instantiate the proper IPython shell |
@@ -947,7 +947,7 b' def start():' | |||
|
947 | 947 | shell = IPShell |
|
948 | 948 | else: |
|
949 | 949 | shell = IPShell |
|
950 | return shell() | |
|
950 | return shell(user_ns = user_ns) | |
|
951 | 951 | |
|
952 | 952 | # Some aliases for backwards compatibility |
|
953 | 953 | IPythonShell = IPShell |
@@ -14,8 +14,8 b' empty.' | |||
|
14 | 14 | |
|
15 | 15 | # Most of your config files and extensions will probably start with this import |
|
16 | 16 | |
|
17 | import IPython.ipapi as ip | |
|
18 | ||
|
17 | from IPython import ipapi | |
|
18 | ip = ipapi.get() | |
|
19 | 19 | import os |
|
20 | 20 | |
|
21 | 21 | o = ip.options() |
@@ -69,106 +69,100 b' class TryNext(Exception):' | |||
|
69 | 69 | """ |
|
70 | 70 | |
|
71 | 71 | |
|
72 | ||
|
73 |
_ |
|
|
74 | ||
|
75 | def _init_with_shell(ip): | |
|
76 | global magic | |
|
77 | magic = ip.ipmagic | |
|
78 | global system | |
|
79 | system = ip.ipsystem | |
|
80 | global set_hook | |
|
81 | set_hook = ip.set_hook | |
|
82 | ||
|
83 | global __IP | |
|
84 | __IP = ip | |
|
85 | ||
|
86 | def options(): | |
|
87 | """ All configurable variables """ | |
|
88 | return __IP.rc | |
|
72 | # contains the most recently instantiated IPApi | |
|
73 | _recent = None | |
|
89 | 74 | |
|
90 |
def |
|
|
91 | return __IP.user_ns | |
|
75 | def get(): | |
|
76 | """ Get an IPApi object, or None if not running under ipython | |
|
92 | 77 |
|
|
93 | def expose_magic(magicname, func): | |
|
94 | ''' Expose own function as magic function for ipython | |
|
78 | Running this should be the first thing you do when writing | |
|
79 | extensions that can be imported as normal modules. You can then | |
|
80 | direct all the configuration operations against the returned | |
|
81 | object. | |
|
95 | 82 | |
|
96 | def foo_impl(self,parameter_s=''): | |
|
97 | """My very own magic!. (Use docstrings, IPython reads them).""" | |
|
98 | print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>' | |
|
99 | print 'The self object is:',self | |
|
83 | """ | |
|
100 | 84 | |
|
101 | ipapi.expose_magic("foo",foo_impl) | |
|
102 | ''' | |
|
103 | ||
|
104 | from IPython import Magic | |
|
105 | import new | |
|
106 | im = new.instancemethod(func,__IP, __IP.__class__) | |
|
107 | setattr(__IP, "magic_" + magicname, im) | |
|
85 | return _recent | |
|
108 | 86 | |
|
109 | class asmagic: | |
|
110 | """ Decorator for exposing magics in a friendly 2.4 decorator form | |
|
87 | ||
|
88 | ||
|
89 | class IPApi: | |
|
90 | """ The actual API class for configuring IPython | |
|
111 | 91 | |
|
112 | @ip.asmagic("foo") | |
|
113 | def f(self,arg): | |
|
114 | pring "arg given:",arg | |
|
92 | You should do all of the IPython configuration by getting | |
|
93 | an IPApi object with IPython.ipapi.get() and using the provided | |
|
94 | methods. | |
|
115 | 95 | |
|
116 | After this, %foo is a magic function. | |
|
117 | 96 | """ |
|
118 | ||
|
119 | def __init__(self,magicname): | |
|
120 |
self. |
|
|
97 | def __init__(self,ip): | |
|
98 | ||
|
99 | self.magic = ip.ipmagic | |
|
100 | ||
|
101 | self.system = ip.ipsystem | |
|
102 | ||
|
103 | self.set_hook = ip.set_hook | |
|
121 | 104 | |
|
122 | def __call__(self,f): | |
|
123 | expose_magic(self.name, f) | |
|
124 |
ret |
|
|
105 | self.IP = ip | |
|
106 | global _recent | |
|
107 | _recent = self | |
|
125 | 108 | |
|
126 | class ashook: | |
|
127 | """ Decorator for exposing magics in a friendly 2.4 decorator form | |
|
109 | ||
|
128 | 110 | |
|
129 | @ip.ashook("editor") | |
|
130 | def jed_editor(self,filename, linenum=None): | |
|
131 | import os | |
|
132 | if linenum is None: linenum = 0 | |
|
133 | os.system('jed +%d %s' % (linenum, filename)) | |
|
111 | def options(self): | |
|
112 | """ All configurable variables """ | |
|
113 | return self.IP.rc | |
|
134 | 114 | |
|
135 | """ | |
|
115 | def user_ns(self): | |
|
116 | return self.IP.user_ns | |
|
136 | 117 | |
|
137 | def __init__(self,name,priority=50): | |
|
138 | self.name = name | |
|
139 | self.prio = priority | |
|
140 | ||
|
141 | def __call__(self,f): | |
|
142 | set_hook(self.name, f, self.prio) | |
|
143 | return f | |
|
144 | ||
|
145 | ||
|
146 | def ex(cmd): | |
|
147 | """ Execute a normal python statement in user namespace """ | |
|
148 | exec cmd in user_ns() | |
|
149 | ||
|
150 | def ev(expr): | |
|
151 | """ Evaluate python expression expr in user namespace | |
|
118 | def expose_magic(self,magicname, func): | |
|
119 | ''' Expose own function as magic function for ipython | |
|
120 | ||
|
121 | def foo_impl(self,parameter_s=''): | |
|
122 | """My very own magic!. (Use docstrings, IPython reads them).""" | |
|
123 | print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>' | |
|
124 | print 'The self object is:',self | |
|
125 | ||
|
126 | ipapi.expose_magic("foo",foo_impl) | |
|
127 | ''' | |
|
128 | ||
|
129 | import new | |
|
130 | im = new.instancemethod(func,self.IP, self.IP.__class__) | |
|
131 | setattr(self.IP, "magic_" + magicname, im) | |
|
132 | ||
|
133 | ||
|
134 | def ex(self,cmd): | |
|
135 | """ Execute a normal python statement in user namespace """ | |
|
136 | exec cmd in self.user_ns() | |
|
152 | 137 | |
|
153 | Returns the result """ | |
|
154 | return eval(expr,user_ns()) | |
|
138 | def ev(self,expr): | |
|
139 | """ Evaluate python expression expr in user namespace | |
|
140 | ||
|
141 | Returns the result of evaluation""" | |
|
142 | return eval(expr,self.user_ns()) | |
|
155 | 143 | |
|
156 | def launch_new_instance(): | |
|
144 | def launch_new_instance(user_ns = None): | |
|
157 | 145 | """ Create and start a new ipython instance. |
|
158 | 146 | |
|
159 | 147 | This can be called even without having an already initialized |
|
160 | 148 | ipython session running. |
|
161 | 149 | |
|
150 | This is also used as the egg entry point for the 'ipython' script. | |
|
151 | ||
|
162 | 152 | """ |
|
163 | import IPython | |
|
153 | ses = create_session(user_ns) | |
|
154 | ses.mainloop() | |
|
164 | 155 | |
|
165 | IPython.Shell.start().mainloop() | |
|
166 | 156 | |
|
167 | def is_ipython_session(): | |
|
168 | """ Return a true value if running inside IPython. | |
|
157 | def create_session(user_ns = None): | |
|
158 | """ Creates, but does not launch an IPython session. | |
|
169 | 159 | |
|
170 | """ | |
|
160 | Later on you can call obj.mainloop() on the returned object. | |
|
171 | 161 |
|
|
172 | # Yes, this is the shell object or None - however, it's an implementation | |
|
173 | # detail and should not be relied on, only truth value matters. | |
|
174 | return __IP | |
|
162 | This should *not* be run when a session exists already. | |
|
163 | ||
|
164 | """ | |
|
165 | if user_ns is not None: | |
|
166 | user_ns["__name__"] = user_ns.get("__name__",'ipy_session') | |
|
167 | import IPython | |
|
168 | return IPython.Shell.start(user_ns = user_ns) No newline at end of file |
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.' | |||
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 107 |
|
|
9 | $Id: iplib.py 1079 2006-01-24 21:52:31Z vivainio $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
@@ -194,9 +194,10 b' class InteractiveShell(object,Magic):' | |||
|
194 | 194 | # log system |
|
195 | 195 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
196 | 196 | |
|
197 | # introduce ourselves to IPython.ipapi which is uncallable | |
|
198 | # before it knows an InteractiveShell object. | |
|
199 |
IPython.ipapi. |
|
|
197 | # Produce a public API instance | |
|
198 | ||
|
199 | self.api = IPython.ipapi.IPApi(self) | |
|
200 | ||
|
200 | 201 | |
|
201 | 202 | # some minimal strict typechecks. For some core data structures, I |
|
202 | 203 | # want actual basic python types, not just anything that looks like |
@@ -3,12 +3,13 b'' | |||
|
3 | 3 | * iplib.py, hooks.py: 'result_display' hook can return a non-None |
|
4 | 4 | value to manipulate resulting history entry. |
|
5 | 5 | |
|
6 |
* ipapi.py: Moved TryNext here from hooks.py |
|
|
7 | is_ipython_session() to determine whether we are running | |
|
8 | inside an ipython session. | |
|
6 | * ipapi.py: Moved TryNext here from hooks.py. Moved functions | |
|
7 | to instance methods of IPApi class, to make extending an embedded | |
|
8 | IPython feasible. See ext_rehashdir.py for example usage. | |
|
9 | 9 | |
|
10 | 10 | * Merged 1071-1076 from banches/0.7.1 |
|
11 | 11 | |
|
12 | ||
|
12 | 13 | 2006-01-23 Fernando Perez <Fernando.Perez@colorado.edu> |
|
13 | 14 | |
|
14 | 15 | * tools/release (daystamp): Fix build tools to use the new |
General Comments 0
You need to be logged in to leave comments.
Login now