##// END OF EJS Templates
Made 'old' readline work again (GetOutpuFile was not exported ...
Made 'old' readline work again (GetOutpuFile was not exported with 'from readline import *', need to access via real module)

File last commit:

r151:a65f5f12
r164:c66a3f62
Show More
ipapi.py
176 lines | 4.4 KiB | text/x-python | PythonLexer
vivainio
Added ipapi, the extension api for ipython....
r109 ''' IPython customization API
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110 Your one-stop module for configuring & extending ipython
vivainio
Added ipapi, the extension api for ipython....
r109
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110 The API will probably break when ipython 1.0 is released, but so
will the other configuration method (rc files).
vivainio
Added ipapi, the extension api for ipython....
r109
All names prefixed by underscores are for internal use, not part
of the public api.
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110 Below is an example that you can just put to a module and import from ipython.
A good practice is to install the config script below as e.g.
~/.ipython/my_private_conf.py
And do
import_mod my_private_conf
in ~/.ipython/ipythonrc
That way the module is imported at startup and you can have all your
personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
stuff) in there.
vivainio
Added ipapi, the extension api for ipython....
r109
-----------------------------------------------
import IPython.ipapi as ip
def ankka_f(self, arg):
print "Ankka",self,"says uppercase:",arg.upper()
ip.expose_magic("ankka",ankka_f)
ip.magic('alias sayhi echo "Testing, hi ok"')
ip.magic('alias helloworld echo "Hello world"')
ip.system('pwd')
ip.ex('import re')
ip.ex("""
def funcci(a,b):
print a+b
print funcci(3,4)
""")
ip.ex("funcci(348,9)")
def jed_editor(self,filename, linenum=None):
print "Calling my own editor, jed ... via hook!"
import os
if linenum is None: linenum = 0
os.system('jed +%d %s' % (linenum, filename))
print "exiting jed"
ip.set_hook('editor',jed_editor)
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
o = ip.options()
o.autocall = 2 # FULL autocall mode
vivainio
Added ipapi, the extension api for ipython....
r109 print "done!"
'''
vivainio
result_display can return value. ipapi.is_ipython_session(). %paste -> %cpaste.
r144
class TryNext(Exception):
""" Try next hook exception.
Raise this in your hook function to indicate that the next
hook handler should be used to handle the operation.
"""
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 # contains the most recently instantiated IPApi
_recent = None
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 def get():
""" Get an IPApi object, or None if not running under ipython
vivainio
Added ipapi, the extension api for ipython....
r109
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 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.
vivainio
Added ipapi, the extension api for ipython....
r109
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 """
vivainio
Added ipapi, the extension api for ipython....
r109
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 return _recent
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146
class IPApi:
""" The actual API class for configuring IPython
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 You should do all of the IPython configuration by getting
an IPApi object with IPython.ipapi.get() and using the provided
methods.
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
"""
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 def __init__(self,ip):
self.magic = ip.ipmagic
self.system = ip.ipsystem
self.set_hook = ip.set_hook
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 self.IP = ip
global _recent
_recent = self
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 def options(self):
""" All configurable variables """
return self.IP.rc
vivainio
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
r110
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 def user_ns(self):
return self.IP.user_ns
vivainio
Added ipapi, the extension api for ipython....
r109
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 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()
vivainio
config changes
r130
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 def ev(self,expr):
""" Evaluate python expression expr in user namespace
Returns the result of evaluation"""
return eval(expr,self.user_ns())
vivainio
a = !ls, a = %alias now work (captures output or gets ret val for aliases)...
r151
def meta(self):
""" Get a session-specific data store
Object returned by this method can be used to store
data that should persist through the ipython session.
"""
return self.IP.meta
vivainio
Separate eggsetup.py that handles scripts installation in the egg...
r138
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 def launch_new_instance(user_ns = None):
vivainio
Merged 1071-1076 from banches/0.7.1
r145 """ Create and start a new ipython instance.
vivainio
Separate eggsetup.py that handles scripts installation in the egg...
r138
This can be called even without having an already initialized
ipython session running.
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 This is also used as the egg entry point for the 'ipython' script.
vivainio
Separate eggsetup.py that handles scripts installation in the egg...
r138 """
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 ses = create_session(user_ns)
ses.mainloop()
vivainio
Separate eggsetup.py that handles scripts installation in the egg...
r138
vivainio
result_display can return value. ipapi.is_ipython_session(). %paste -> %cpaste.
r144
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 def create_session(user_ns = None):
""" Creates, but does not launch an IPython session.
vivainio
Separate eggsetup.py that handles scripts installation in the egg...
r138
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 Later on you can call obj.mainloop() on the returned object.
vivainio
Separate eggsetup.py that handles scripts installation in the egg...
r138
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 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)