From d65566334c04c96439aa2440e7eca79a2df9ff87 2006-01-14 09:46:45 From: vivainio Date: 2006-01-14 09:46:45 Subject: [PATCH] ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access --- diff --git a/IPython/ipapi.py b/IPython/ipapi.py index d181d10..3e87744 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -1,14 +1,28 @@ ''' IPython customization API -Your one-stop module for configuring ipython +Your one-stop module for configuring & extending ipython -This is experimental, use at your own risk. +The API will probably break when ipython 1.0 is released, but so +will the other configuration method (rc files). All names prefixed by underscores are for internal use, not part of the public api. -No formal doc yet, here's an example that you can just put -to a module and import from ipython. +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. ----------------------------------------------- import IPython.ipapi as ip @@ -38,13 +52,14 @@ def jed_editor(self,filename, linenum=None): print "exiting jed" ip.set_hook('editor',jed_editor) + +o = ip.options() +o.autocall = 2 # FULL autocall mode + print "done!" ''' - - - def _init_with_shell(ip): global magic magic = ip.ipmagic @@ -52,9 +67,14 @@ def _init_with_shell(ip): system = ip.ipsystem global set_hook set_hook = ip.set_hook + global __IP __IP = ip +def options(): + """ All configurable variables """ + return __IP.rc + def user_ns(): return __IP.user_ns @@ -72,7 +92,43 @@ def expose_magic(magicname, func): from IPython import Magic setattr(Magic.Magic, "magic_" + magicname, func) + +class asmagic: + """ Decorator for exposing magics in a friendly 2.4 decorator form + + @ip.asmagic("foo") + def f(self,arg): + pring "arg given:",arg + + After this, %foo is a magic function. + """ + + def __init__(self,magicname): + self.name = magicname + + def __call__(self,f): + expose_magic(self.name, f) + return f + +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 __init__(self,name): + self.name = name + + def __call__(self,f): + set_hook(self.name, f) + return f + + def ex(cmd): """ Execute a normal python statement """ exec cmd in user_ns() \ No newline at end of file diff --git a/IPython/iplib.py b/IPython/iplib.py index 3202e33..15e1d2d 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 1016 2006-01-14 00:54:23Z vivainio $ +$Id: iplib.py 1017 2006-01-14 09:46:45Z vivainio $ """ #***************************************************************************** @@ -192,7 +192,7 @@ class InteractiveShell(object,Magic): custom_exceptions=((),None),embedded=False): # first thing: introduce ourselves to IPython.ipapi which is uncallable - # before it knows an InteractiveShell object. Uninitialized state is ok + # before it knows an InteractiveShell object. IPython.ipapi._init_with_shell(self) # some minimal strict typechecks. For some core data structures, I diff --git a/IPython/ipmaker.py b/IPython/ipmaker.py index 42a7c81..a03ab72 100644 --- a/IPython/ipmaker.py +++ b/IPython/ipmaker.py @@ -6,7 +6,7 @@ Requires Python 2.1 or better. This file contains the main make_IPython() starter function. -$Id: ipmaker.py 1005 2006-01-12 08:39:26Z fperez $""" +$Id: ipmaker.py 1017 2006-01-14 09:46:45Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -415,6 +415,7 @@ object? -> Details about 'object'. ?object also works, ?? prints more. warn('Profile configuration file %s not found. Ignoring request.' % (opts_all.profile) ) + # load the config file rcfiledata = None if opts_all.quick: @@ -553,7 +554,7 @@ object? -> Details about 'object'. ?object also works, ?? prints more. #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who # Now run through the different sections of the users's config - if IP_rc.debug: + if IP_rc.debug: print 'Trying to execute the following configuration structure:' print '(Things listed first are deeper in the inclusion tree and get' print 'loaded first).\n' diff --git a/doc/ChangeLog b/doc/ChangeLog index e452f26..97ad74f 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2006-01-14 Ville Vainio + + * IPython/ipapi.py (ashook, asmagic, options): Added convenience + ipapi decorators for python 2.4 users, options() provides access to rc + data. + + 2006-01-13 Ville Vainio * IPython/platutils*.py: platform specific utility functions,