Saturday, July 3, 2010

iPython in Plone 4

[edit (2010-07-30): add link to full iPython extension script which doesn't require login]

The Plone 3 Products Development Cookbook shows a way to make your Plone 3 buildout create a Zope-aware iPython script. With a slight modification, the script can be used for Plone 4 as well.

buildout.cfg:
parts +=
    ipzope

[ipzope]
# an IPython Shell for interactive use with zope running.
# You also need to put
# https://svn.plone.org/svn/collective/dotipython/branches/plone4/
# ipy_profile_zope.py
# in your $HOME/.ipython directory for the following to work.
# You may have to remove an existing ipy_profile_zope.py: if you 
# get errors when running ./bin/ipzope, check the stack trace for that 
# filename to make sure the correct version is used. 
recipe = zc.recipe.egg
eggs =
    ipython
    ${instance:eggs}
initialization =
    import sys, os
    os.environ["INSTANCE_HOME"] = "${instance:location}"
    sys.argv[1:1] = "-p zope".split()
scripts = ipython=ipzope


The modification makes the script check if a SOFTWARE_HOME environment variable existst. If not, it assumes we have an egg-based Zope (2.12 or higher). The full script is available from SVN.

The script still works with older Zope versions (Plone 3), so i think the branch can be merged. Note that in a Plone 3 buildout, you'd also have to define extra-paths = ${zope2:location}/lib/python in the [ipzope] part.

10 comments:

Davidjb said...

Looks good. I haven't tried it on Plone 4 yet, but I'll certainly put it to use.

I did try it with Plone 3, however, and ipython uses the Zope profile within its egg (eg ipython-0.10-py2.4.egg/IPython/Extensions/ipy_profile_zope.py) rather than the one I've got in my .ipython directory in my home folder.

I'm guessing that's what you're getting at in your comments in the buildout.cfg snippet, but it's helpful to know that this existing profile needs to either be replaced by the patched one or else removed (and then ipython falls back to the user's directory).

TimCook said...

The modification link requires a login at dev.zope.org

Is the above code supposed to be the corrected code for Plone 4 buildout.cfg ?

Thanks,
Tim

Kees Hink said...

@Tim, thank you for your reply. I've added a link to the SVN repository which doesn't require login, see above. (By the way, the login required is for dev.plone.org.)

Márcio Mazza said...

@Davidjb Another possibility to solve the conflict between the different versions of ipy_profile_zope.py would be to rename it for your $HOME/.ipython and use the corresponding name in the "ipzope" script configuration.

One could download the profile as "ipy_profile_zopePlone.py":

curl https://svn.plone.org/svn/collective/dotipython/branches/plone4/ipy_profile_zope.py > ~/.ipython/ipy_profile_zopePlone.py

And replace the line in [ipzope]:
sys.argv[1:1] = "-p zope".split()
with
sys.argv[1:1] = "-p zopePlone".split()

Julian Co said...

Just note that in ipython version 0.11 and up the -p option is not working anymore.
For this reason, the following line:

sys.argv[1:1] = "-p zope".split()
should be changed to:

sys.argv[1:1] = "--profile=zope".split()

Julian Co said...

Just note that in ipython version 0.11 and up the -p option is not working anymore.
For this reason, the following line:

sys.argv[1:1] = "-p zope".split()

should be changed to:

sys.argv[1:1] = "--profile=zope".split()

me-kell said...

as of ipython version 0.11 (http://ipython.org/ipython-doc/dev/whatsnew/version0.11.html) some modifications in the profile script are necessary:

IPython.ipapi has been moved to IPython.core.ipapi and IPython.Release to IPython.core.release

change imports in lines 20,21 as follows:

from IPython.core import ipapi
from IPython.core import release

TerminalInteractiveShell has no attributes "options" nor "banner".
Remove line 286 (#o = ip.options)
and change line 304 to something like:

my_banner = "ZOPE Py %s IPy %s\n" % (sys.version.split('\n')[0], release.version)
ip.init_banner(my_banner,'b2','b3')

please note the lower case in release!

Thanks,
me-kell

me-kell said...

here the patch (unified diff) of my comments before
best regards,
me-kell
###################### BEGIN ##############################
--- ipy_profile_zope-rev120186.py 2012-01-03 18:46:52.000000000 -0000
+++ ipy_profile_zope-working-copy.py 2012-01-03 18:38:04.000000000 -0000
@@ -14,14 +14,14 @@
# The License may be obtained under .

__author__ = """Stefan Eletzhofer """
__docformat__ = 'plaintext'
__revision__ = "$Revision$"

-from IPython import ipapi
-from IPython import Release
+from IPython.core import ipapi
+from IPython.core import release
from types import StringType
import sys
import os
import textwrap

# The import below effectively obsoletes your old-style ipythonrc[.ini],
@@ -280,13 +280,13 @@
def ipy_set_trace():
import IPython; IPython.Debugger.Pdb().set_trace()

def main():
global zope_debug
ip = ipapi.get()
- o = ip.options
+ #o = ip.options
# autocall to "full" mode (smart mode is default, I like full mode)

SOFTWARE_HOME = os.environ.get( "SOFTWARE_HOME" )
if SOFTWARE_HOME:
sys.path.append( SOFTWARE_HOME )
print "SOFTWARE_HOME=%s\n" % SOFTWARE_HOME
@@ -298,13 +298,14 @@
#
import pdb;
pdb.set_trace = ipy_set_trace
#

# I like my banner minimal.
- o.banner = "ZOPE Py %s IPy %s\n" % (sys.version.split('\n')[0],Release.version)
+ banner = "ZOPE Py %s IPy %s\n" % (sys.version.split('\n')[0], release.version)
+ ip.init_banner(banner,'b2','b3')

print textwrap.dedent("""\
ZOPE mode iPython shell.

Bound names:
app
###################### END ##############################

claytron said...

I merged down the Plone 4 branch and made it compatible with 0.12 a few days ago.

https://svn.plone.org/svn/collective/dotipython/trunk/

Jean said...

The IPython profile now needs to be in a subdirectory of `~/.ipython`.
Updated documentation at https://github.com/collective/dotipython