diff --git a/IPython/Extensions/ext_rehashdir.py b/IPython/Extensions/ext_rehashdir.py index 109392d..699d220 100644 --- a/IPython/Extensions/ext_rehashdir.py +++ b/IPython/Extensions/ext_rehashdir.py @@ -24,12 +24,12 @@ prompt. $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $ """ -import IPython.ipapi as ip +import IPython.ipapi +ip = IPython.ipapi.get() import os,re,fnmatch -@ip.asmagic("rehashdir") def rehashdir_f(self,arg): """ Add executables in all specified dirs to alias table @@ -99,3 +99,4 @@ def rehashdir_f(self,arg): self.shell.init_auto_alias() finally: os.chdir(savedir) +ip.expose_magic("rehashdir",rehashdir_f) diff --git a/IPython/Shell.py b/IPython/Shell.py index 1f3a779..3a62089 100644 --- a/IPython/Shell.py +++ b/IPython/Shell.py @@ -4,7 +4,7 @@ All the matplotlib support code was co-developed with John Hunter, matplotlib's author. -$Id: Shell.py 1058 2006-01-22 14:30:01Z vivainio $""" +$Id: Shell.py 1079 2006-01-24 21:52:31Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez @@ -919,7 +919,7 @@ def _matplotlib_shell_class(): return sh_class # This is the one which should be called by external code. -def start(): +def start(user_ns = None): """Return a running shell instance, dealing with threading options. This is a factory function which will instantiate the proper IPython shell @@ -947,7 +947,7 @@ def start(): shell = IPShell else: shell = IPShell - return shell() + return shell(user_ns = user_ns) # Some aliases for backwards compatibility IPythonShell = IPShell diff --git a/IPython/UserConfig/ipy_user_conf.py b/IPython/UserConfig/ipy_user_conf.py index 2b319eb..cc8cb68 100644 --- a/IPython/UserConfig/ipy_user_conf.py +++ b/IPython/UserConfig/ipy_user_conf.py @@ -14,8 +14,8 @@ empty. # Most of your config files and extensions will probably start with this import -import IPython.ipapi as ip - +from IPython import ipapi +ip = ipapi.get() import os o = ip.options() diff --git a/IPython/ipapi.py b/IPython/ipapi.py index f9d1ac8..b697d35 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -69,106 +69,100 @@ class TryNext(Exception): """ - -__IP = None - -def _init_with_shell(ip): - global magic - magic = ip.ipmagic - global system - system = ip.ipsystem - global set_hook - set_hook = ip.set_hook - - global __IP - __IP = ip - -def options(): - """ All configurable variables """ - return __IP.rc +# contains the most recently instantiated IPApi +_recent = None -def user_ns(): - return __IP.user_ns +def get(): + """ Get an IPApi object, or None if not running under ipython -def expose_magic(magicname, func): - ''' Expose own function as magic function for ipython + Running this should be the first thing you do when writing + extensions that can be imported as normal modules. You can then + direct all the configuration operations against the returned + object. - def foo_impl(self,parameter_s=''): - """My very own magic!. (Use docstrings, IPython reads them).""" - print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>' - print 'The self object is:',self + """ - ipapi.expose_magic("foo",foo_impl) - ''' - - from IPython import Magic - import new - im = new.instancemethod(func,__IP, __IP.__class__) - setattr(__IP, "magic_" + magicname, im) + return _recent -class asmagic: - """ Decorator for exposing magics in a friendly 2.4 decorator form + + +class IPApi: + """ The actual API class for configuring IPython - @ip.asmagic("foo") - def f(self,arg): - pring "arg given:",arg + You should do all of the IPython configuration by getting + an IPApi object with IPython.ipapi.get() and using the provided + methods. - After this, %foo is a magic function. """ - - def __init__(self,magicname): - self.name = magicname + def __init__(self,ip): + + self.magic = ip.ipmagic + + self.system = ip.ipsystem + + self.set_hook = ip.set_hook - def __call__(self,f): - expose_magic(self.name, f) - return f + self.IP = ip + global _recent + _recent = self -class ashook: - """ Decorator for exposing magics in a friendly 2.4 decorator form + - @ip.ashook("editor") - def jed_editor(self,filename, linenum=None): - import os - if linenum is None: linenum = 0 - os.system('jed +%d %s' % (linenum, filename)) + def options(self): + """ All configurable variables """ + return self.IP.rc - """ + def user_ns(self): + return self.IP.user_ns - def __init__(self,name,priority=50): - self.name = name - self.prio = priority - - def __call__(self,f): - set_hook(self.name, f, self.prio) - return f - - -def ex(cmd): - """ Execute a normal python statement in user namespace """ - exec cmd in user_ns() - -def ev(expr): - """ Evaluate python expression expr in user namespace + def expose_magic(self,magicname, func): + ''' Expose own function as magic function for ipython + + def foo_impl(self,parameter_s=''): + """My very own magic!. (Use docstrings, IPython reads them).""" + print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>' + print 'The self object is:',self + + ipapi.expose_magic("foo",foo_impl) + ''' + + import new + im = new.instancemethod(func,self.IP, self.IP.__class__) + setattr(self.IP, "magic_" + magicname, im) + + + def ex(self,cmd): + """ Execute a normal python statement in user namespace """ + exec cmd in self.user_ns() - Returns the result """ - return eval(expr,user_ns()) + def ev(self,expr): + """ Evaluate python expression expr in user namespace + + Returns the result of evaluation""" + return eval(expr,self.user_ns()) -def launch_new_instance(): +def launch_new_instance(user_ns = None): """ Create and start a new ipython instance. This can be called even without having an already initialized ipython session running. + This is also used as the egg entry point for the 'ipython' script. + """ - import IPython + ses = create_session(user_ns) + ses.mainloop() - IPython.Shell.start().mainloop() -def is_ipython_session(): - """ Return a true value if running inside IPython. +def create_session(user_ns = None): + """ Creates, but does not launch an IPython session. - """ + Later on you can call obj.mainloop() on the returned object. - # Yes, this is the shell object or None - however, it's an implementation - # detail and should not be relied on, only truth value matters. - return __IP + This should *not* be run when a session exists already. + + """ + if user_ns is not None: + user_ns["__name__"] = user_ns.get("__name__",'ipy_session') + import IPython + return IPython.Shell.start(user_ns = user_ns) \ No newline at end of file diff --git a/IPython/iplib.py b/IPython/iplib.py index 84f0188..d02cd12 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 1077 2006-01-24 18:15:27Z vivainio $ +$Id: iplib.py 1079 2006-01-24 21:52:31Z vivainio $ """ #***************************************************************************** @@ -194,9 +194,10 @@ class InteractiveShell(object,Magic): # log system self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') - # introduce ourselves to IPython.ipapi which is uncallable - # before it knows an InteractiveShell object. - IPython.ipapi._init_with_shell(self) + # Produce a public API instance + + self.api = IPython.ipapi.IPApi(self) + # some minimal strict typechecks. For some core data structures, I # want actual basic python types, not just anything that looks like diff --git a/doc/ChangeLog b/doc/ChangeLog index b83ccf3..fe771b5 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -3,12 +3,13 @@ * iplib.py, hooks.py: 'result_display' hook can return a non-None value to manipulate resulting history entry. - * ipapi.py: Moved TryNext here from hooks.py, added - is_ipython_session() to determine whether we are running - inside an ipython session. + * ipapi.py: Moved TryNext here from hooks.py. Moved functions + to instance methods of IPApi class, to make extending an embedded + IPython feasible. See ext_rehashdir.py for example usage. * Merged 1071-1076 from banches/0.7.1 + 2006-01-23 Fernando Perez * tools/release (daystamp): Fix build tools to use the new