From 7b696f6f9fb959d8972fde42c6bb23ce295fede6 2006-01-14 00:54:23 From: vivainio Date: 2006-01-14 00:54:23 Subject: [PATCH] Added ipapi, the extension api for ipython. Fixed oinspect for obj.__doc__ == None (magics) --- diff --git a/IPython/OInspect.py b/IPython/OInspect.py index 7c975a9..aff32da 100644 --- a/IPython/OInspect.py +++ b/IPython/OInspect.py @@ -6,7 +6,7 @@ Uses syntax highlighting for presenting the various information elements. Similar in spirit to the inspect module, but all calls take a name argument to reference the name under which an object is being read. -$Id: OInspect.py 958 2005-12-27 23:17:51Z fperez $ +$Id: OInspect.py 1016 2006-01-14 00:54:23Z vivainio $ """ #***************************************************************************** @@ -268,6 +268,8 @@ class Inspector: ds = "Alias to the system command:\n %s" % obj[1] else: ds = getdoc(obj) + if ds is None: + ds = '' if formatter is not None: ds = formatter(ds) diff --git a/IPython/__init__.py b/IPython/__init__.py index 6988403..ea46063 100644 --- a/IPython/__init__.py +++ b/IPython/__init__.py @@ -27,7 +27,7 @@ IPython tries to: IPython requires Python 2.2 or newer. -$Id: __init__.py 1014 2006-01-13 19:16:41Z vivainio $""" +$Id: __init__.py 1016 2006-01-14 00:54:23Z vivainio $""" #***************************************************************************** # Copyright (C) 2001-2004 Fernando Perez. @@ -44,7 +44,8 @@ if sys.version[0:3] < '2.3': # Define what gets imported with a 'from IPython import *' __all__ = ['deep_reload','genutils','ipstruct','ultraTB','DPyGetOpt', 'Itpl','hooks','ConfigLoader','OutputTrap','Release','Shell', - 'platutils','platutils_win32','platutils_posix','platutils_dummy'] + 'platutils','platutils_win32','platutils_posix','platutils_dummy', + 'ipapi'] # Load __all__ in IPython namespace so that a simple 'import IPython' gives # access to them via IPython. diff --git a/IPython/ipapi.py b/IPython/ipapi.py new file mode 100644 index 0000000..d181d10 --- /dev/null +++ b/IPython/ipapi.py @@ -0,0 +1,78 @@ +''' IPython customization API + +Your one-stop module for configuring ipython + +This is experimental, use at your own risk. + +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. + +----------------------------------------------- +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) +print "done!" + +''' + + + + +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 user_ns(): + return __IP.user_ns + +def expose_magic(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) + ''' + + from IPython import Magic + + setattr(Magic.Magic, "magic_" + magicname, func) + +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 3f71534..3202e33 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -2,11 +2,11 @@ """ IPython -- An enhanced Interactive Python -Requires Python 2.1 or newer. +Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 1015 2006-01-13 22:47:06Z vivainio $ +$Id: iplib.py 1016 2006-01-14 00:54:23Z vivainio $ """ #***************************************************************************** @@ -74,6 +74,7 @@ from IPython.ipstruct import Struct from IPython.background_jobs import BackgroundJobManager from IPython.usage import cmd_line_usage,interactive_usage from IPython.genutils import * +import IPython.ipapi # Globals @@ -190,6 +191,10 @@ class InteractiveShell(object,Magic): user_ns = None,user_global_ns=None,banner2='', 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 + IPython.ipapi._init_with_shell(self) + # some minimal strict typechecks. For some core data structures, I # want actual basic python types, not just anything that looks like # one. This is especially true for namespaces. @@ -821,8 +826,8 @@ class InteractiveShell(object,Magic): args = arg_s.split(' ',1) magic_name = args[0] - if magic_name.startswith(self.ESC_MAGIC): - magic_name = magic_name[1:] + magic_name = magic_name.lstrip(self.ESC_MAGIC) + try: magic_args = args[1] except IndexError: diff --git a/doc/ChangeLog b/doc/ChangeLog index 56a7de7..e452f26 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,16 +1,20 @@ 2006-01-13 Ville Vainio * IPython/platutils*.py: platform specific utility functions, - so far only set_term_title is implemented (change terminal - label in windowing systems). %cd now changes the title to - current dir. + so far only set_term_title is implemented (change terminal + label in windowing systems). %cd now changes the title to + current dir. - * IPython/Release.py: Added myself to "authors" list, + * IPython/Release.py: Added myself to "authors" list, had to create new files. * IPython/iplib.py (handle_shell_escape): fixed logical flaw in shell escape; not a known bug but had potential to be one in the future. + + * IPython/ipapi.py (added),OInspect.py,iplib.py: "Public" + extension API for IPython! See the module for usage example. Fix + OInspect for docstring-less magic functions. 2006-01-13 Fernando Perez