##// END OF EJS Templates
ipapi decorators ashook, asmagic; ipapi.options() for __IP.rc access
vivainio -
r110:d6556633
parent child
Show More
@@ -1,14 +1,28
1 ''' IPython customization API
1 ''' IPython customization API
2
2
3 Your one-stop module for configuring ipython
3 Your one-stop module for configuring & extending ipython
4
4
5 This is experimental, use at your own risk.
5 The API will probably break when ipython 1.0 is released, but so
6 will the other configuration method (rc files).
6
7
7 All names prefixed by underscores are for internal use, not part
8 All names prefixed by underscores are for internal use, not part
8 of the public api.
9 of the public api.
9
10
10 No formal doc yet, here's an example that you can just put
11 Below is an example that you can just put to a module and import from ipython.
11 to a module and import from ipython.
12
13 A good practice is to install the config script below as e.g.
14
15 ~/.ipython/my_private_conf.py
16
17 And do
18
19 import_mod my_private_conf
20
21 in ~/.ipython/ipythonrc
22
23 That way the module is imported at startup and you can have all your
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
25 stuff) in there.
12
26
13 -----------------------------------------------
27 -----------------------------------------------
14 import IPython.ipapi as ip
28 import IPython.ipapi as ip
@@ -38,13 +52,14 def jed_editor(self,filename, linenum=None):
38 print "exiting jed"
52 print "exiting jed"
39
53
40 ip.set_hook('editor',jed_editor)
54 ip.set_hook('editor',jed_editor)
55
56 o = ip.options()
57 o.autocall = 2 # FULL autocall mode
58
41 print "done!"
59 print "done!"
42
60
43 '''
61 '''
44
62
45
46
47
48 def _init_with_shell(ip):
63 def _init_with_shell(ip):
49 global magic
64 global magic
50 magic = ip.ipmagic
65 magic = ip.ipmagic
@@ -52,9 +67,14 def _init_with_shell(ip):
52 system = ip.ipsystem
67 system = ip.ipsystem
53 global set_hook
68 global set_hook
54 set_hook = ip.set_hook
69 set_hook = ip.set_hook
70
55 global __IP
71 global __IP
56 __IP = ip
72 __IP = ip
57
73
74 def options():
75 """ All configurable variables """
76 return __IP.rc
77
58 def user_ns():
78 def user_ns():
59 return __IP.user_ns
79 return __IP.user_ns
60
80
@@ -72,7 +92,43 def expose_magic(magicname, func):
72 from IPython import Magic
92 from IPython import Magic
73
93
74 setattr(Magic.Magic, "magic_" + magicname, func)
94 setattr(Magic.Magic, "magic_" + magicname, func)
95
96 class asmagic:
97 """ Decorator for exposing magics in a friendly 2.4 decorator form
98
99 @ip.asmagic("foo")
100 def f(self,arg):
101 pring "arg given:",arg
102
103 After this, %foo is a magic function.
104 """
105
106 def __init__(self,magicname):
107 self.name = magicname
108
109 def __call__(self,f):
110 expose_magic(self.name, f)
111 return f
112
113 class ashook:
114 """ Decorator for exposing magics in a friendly 2.4 decorator form
115
116 @ip.ashook("editor")
117 def jed_editor(self,filename, linenum=None):
118 import os
119 if linenum is None: linenum = 0
120 os.system('jed +%d %s' % (linenum, filename))
121
122 """
75
123
124 def __init__(self,name):
125 self.name = name
126
127 def __call__(self,f):
128 set_hook(self.name, f)
129 return f
130
131
76 def ex(cmd):
132 def ex(cmd):
77 """ Execute a normal python statement """
133 """ Execute a normal python statement """
78 exec cmd in user_ns() No newline at end of file
134 exec cmd in user_ns()
@@ -6,7 +6,7 Requires Python 2.3 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 1016 2006-01-14 00:54:23Z vivainio $
9 $Id: iplib.py 1017 2006-01-14 09:46:45Z vivainio $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -192,7 +192,7 class InteractiveShell(object,Magic):
192 custom_exceptions=((),None),embedded=False):
192 custom_exceptions=((),None),embedded=False):
193
193
194 # first thing: introduce ourselves to IPython.ipapi which is uncallable
194 # first thing: introduce ourselves to IPython.ipapi which is uncallable
195 # before it knows an InteractiveShell object. Uninitialized state is ok
195 # before it knows an InteractiveShell object.
196 IPython.ipapi._init_with_shell(self)
196 IPython.ipapi._init_with_shell(self)
197
197
198 # some minimal strict typechecks. For some core data structures, I
198 # some minimal strict typechecks. For some core data structures, I
@@ -6,7 +6,7 Requires Python 2.1 or better.
6
6
7 This file contains the main make_IPython() starter function.
7 This file contains the main make_IPython() starter function.
8
8
9 $Id: ipmaker.py 1005 2006-01-12 08:39:26Z fperez $"""
9 $Id: ipmaker.py 1017 2006-01-14 09:46:45Z vivainio $"""
10
10
11 #*****************************************************************************
11 #*****************************************************************************
12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -415,6 +415,7 object? -> Details about 'object'. ?object also works, ?? prints more.
415 warn('Profile configuration file %s not found. Ignoring request.'
415 warn('Profile configuration file %s not found. Ignoring request.'
416 % (opts_all.profile) )
416 % (opts_all.profile) )
417
417
418
418 # load the config file
419 # load the config file
419 rcfiledata = None
420 rcfiledata = None
420 if opts_all.quick:
421 if opts_all.quick:
@@ -553,7 +554,7 object? -> Details about 'object'. ?object also works, ?? prints more.
553 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
554 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
554
555
555 # Now run through the different sections of the users's config
556 # Now run through the different sections of the users's config
556 if IP_rc.debug:
557 if IP_rc.debug:
557 print 'Trying to execute the following configuration structure:'
558 print 'Trying to execute the following configuration structure:'
558 print '(Things listed first are deeper in the inclusion tree and get'
559 print '(Things listed first are deeper in the inclusion tree and get'
559 print 'loaded first).\n'
560 print 'loaded first).\n'
@@ -1,3 +1,10
1 2006-01-14 Ville Vainio <vivainio@gmail.com>
2
3 * IPython/ipapi.py (ashook, asmagic, options): Added convenience
4 ipapi decorators for python 2.4 users, options() provides access to rc
5 data.
6
7
1 2006-01-13 Ville Vainio <vivainio@gmail.com>
8 2006-01-13 Ville Vainio <vivainio@gmail.com>
2
9
3 * IPython/platutils*.py: platform specific utility functions,
10 * IPython/platutils*.py: platform specific utility functions,
General Comments 0
You need to be logged in to leave comments. Login now