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