diff --git a/IPython/Extensions/ipy_kitcfg.py b/IPython/Extensions/ipy_kitcfg.py index a638b29..cc53c87 100644 --- a/IPython/Extensions/ipy_kitcfg.py +++ b/IPython/Extensions/ipy_kitcfg.py @@ -1,31 +1,54 @@ - import os,sys -def selflaunch(line): - """ Launch python script with 'this' interpreter +import ipy_rehashdir,glob +from ipy_rehashdir import selflaunch, PyLauncher + +def pylaunchers(): + """Create launchers for python scripts in cwd and store them in alias table - e.g. d:\foo\ipython.exe a.py + This is useful if you want to invoke .py scripts from ipykit session, + just adding .py files in PATH does not work without file association. """ - cmd = sys.executable + ' ' + line.split(None,1)[1] - print ">",cmd - os.system(cmd) + fs = glob.glob('*.py*') + for f in fs: + l = PyLauncher(f) + n = os.path.splitext(f)[0] + ip.defalias(n, l) + ip.magic('store '+n) + + +def exta_imports(): + # add some modules that you'd want to be bundled in the ipykit + # library zip file here. Do this if you get ImportErrors from scripts you + # try to launch with 'py' or pylaunchers. In theory you could include + # the whole stdlib here for full script coverage + # note that this is never run, it's just here for py2exe + import distutils.dir_util + def main(): - import IPython.ipapi - ip = IPython.ipapi.get() - root = os.environ.get('IPYKITROOT', None) if not root: print "Can't configure ipykit, IPYKITROOT should be set." return os.environ["PATH"] = os.environ["PATH"] + ";" + root + "\\bin;" + ip.to_user_ns("pylaunchers") - ip.defalias('py',selflaunch) - ip.defalias('ls','ls -F') +def ipython_firstrun(ip): + print "First run of ipykit - configuring" + ip.defalias('py',selflaunch) + ip.defalias('d','ls -F') + ip.defalias('ls','ls') + ip.magic('store py') + ip.magic('store d') + ip.magic('store ls') -main() +def init_ipython(ipy): + global ip + ip = ipy + main() diff --git a/IPython/Extensions/ipy_rehashdir.py b/IPython/Extensions/ipy_rehashdir.py index 8aaef22..02c2a77 100644 --- a/IPython/Extensions/ipy_rehashdir.py +++ b/IPython/Extensions/ipy_rehashdir.py @@ -19,8 +19,31 @@ import IPython.ipapi ip = IPython.ipapi.get() -import os,re,fnmatch +import os,re,fnmatch,sys +def selflaunch(line): + """ Launch python script with 'this' interpreter + + e.g. d:\foo\ipython.exe a.py + + """ + cmd = sys.executable + ' ' + line.split(None,1)[1] + print ">",cmd + os.system(cmd) + +class PyLauncher: + """ Invoke selflanucher on the specified script + + This is mostly useful for associating with scripts using:: + _ip.defalias('foo',PyLauncher('foo_script.py')) + + """ + def __init__(self,script): + self.script = os.path.abspath(script) + def __call__(self, line): + selflaunch("py " + self.script + ' ' + line) + def __repr__(self): + return 'PyLauncher("%s")' % self.script def rehashdir_f(self,arg): """ Add executables in all specified dirs to alias table @@ -95,4 +118,5 @@ def rehashdir_f(self,arg): finally: os.chdir(savedir) return created + ip.expose_magic("rehashdir",rehashdir_f) diff --git a/IPython/Magic.py b/IPython/Magic.py index 3a320b6..44ed8f8 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 2663 2007-08-22 21:46:52Z vivainio $""" +$Id: Magic.py 2668 2007-08-24 17:10:46Z vivainio $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -2298,7 +2298,7 @@ Defaulting color scheme to 'NoColor'""" special = False try: tgt = atab[alias][1] - except TypeError: + except (TypeError, AttributeError): # unsubscriptable? probably a callable tgt = atab[alias] special = True diff --git a/IPython/Release.py b/IPython/Release.py index d352c78..b6bb753 100644 --- a/IPython/Release.py +++ b/IPython/Release.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Release data for the IPython project. -$Id: Release.py 2664 2007-08-22 21:56:12Z vivainio $""" +$Id: Release.py 2668 2007-08-24 17:10:46Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez @@ -22,7 +22,7 @@ name = 'ipython' # because bdist_rpm does not accept dashes (an RPM) convention, and # bdist_deb does not accept underscores (a Debian convention). -revision = '2663M' +revision = '2665' version = '0.8.2.svn.r' + revision.rstrip('M') diff --git a/IPython/ipapi.py b/IPython/ipapi.py index 51b46c3..b2e51ca 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -433,6 +433,16 @@ class IPApi: self.IP.rl_next_input = s def load(self, mod): + """ Load an extension. + + Some modules should (or must) be 'load()':ed, rather than just imported. + + Loading will do: + + - run init_ipython(ip) + - run ipython_firstrun(ip) + + """ if mod in self.extensions: # just to make sure we don't init it twice # note that if you 'load' a module that has already been @@ -443,6 +453,14 @@ class IPApi: m = sys.modules[mod] if hasattr(m,'init_ipython'): m.init_ipython(self) + + if hasattr(m,'ipython_firstrun'): + already_loaded = self.db.get('firstrun_done', set()) + if mod not in already_loaded: + m.ipython_firstrun(self) + already_loaded.add(mod) + self.db['firstrun_done'] = already_loaded + self.extensions[mod] = m return m diff --git a/exesetup.py b/exesetup.py index 1c675ac..71bca84 100644 --- a/exesetup.py +++ b/exesetup.py @@ -85,10 +85,18 @@ setup(name = name, **egg_extra_kwds ) +minimal_conf = """ +import IPython.ipapi +ip = IPython.ipapi.get() +import ipy_profile_sh +ip.load('ipy_kitcfg') + +""" + if not os.path.isdir("dist/_ipython"): print "Creating simple _ipython dir" os.mkdir("dist/_ipython") open("dist/_ipython/ipythonrc.ini","w").write("# intentionally blank\n") - open("dist/_ipython/ipy_user_conf.py","w").write("import ipy_kitcfg\nimport ipy_profile_sh\n") + open("dist/_ipython/ipy_user_conf.py","w").write(minimal_conf) if os.path.isdir('bin'): dir_util.copy_tree('bin','dist/_ipython/bin')