##// END OF EJS Templates
Added notes with ideas about new design for %magics.
Fernando Perez -
Show More
@@ -0,0 +1,103 b''
1 ==============================
2 The magic commands subsystem
3 ==============================
4
5 .. warning::
6
7 These are *preliminary* notes and thoughts on the magic system, kept here
8 for reference so we can come up with a good design now that the major core
9 refactoring has made so much progress. Do not consider yet any part of this
10 document final.
11
12 Two entry points:
13
14 - m.line_eval(self,parameter_s): like today
15 - m.block_eval(self,code_block): for whole-block evaluation.
16
17 This would allow us to have magics that take input, and whose single line form
18 can even take input and call block_eval later (like %cpaste does, but with a
19 generalized interface).
20
21 Constructor
22 ===========
23
24 Suggested syntax::
25
26 class MyMagic(BaseMagic):
27 requires_shell = True/False
28 def __init__(self,shell=None):
29
30
31 Registering magics
32 ==================
33
34 Today, ipapi provides an *expose_magic()* function for making simple magics.
35 We will probably extend this (in a backwards-compatible manner if possible) to
36 allow the simplest cases to work as today, while letting users register more
37 complex ones.
38
39 Use cases::
40
41 def func(arg): pass # note signature, no 'self'
42 ip.expose_magic('name',func)
43
44 def func_line(arg): pass
45 def func_block(arg):pass
46 ip.expose_magic('name',func_line,func_block)
47
48 class mymagic(BaseMagic):
49 """Magic docstring, used in help messages.
50 """
51 def line_eval(self,arg): pass
52 def block_eval(self,arg): pass
53
54 ip.register_magic(mymagic)
55
56
57 The BaseMagic class will offer common functionality to all, including things
58 like options handling (via argparse).
59
60
61 Call forms: line and block
62 ==========================
63
64 Block-oriented environments will call line_eval() for the first line of input
65 (the call line starting with '%') and will then feed the rest of the block to
66 block_eval() if the magic in question has a block mode.
67
68 In line environments, by default %foo -> foo.line_eval(), but no block call is
69 made. Specific implementations of line_eval can decide to then call block_eval
70 if they want to provide for whole-block input in line-oriented environments.
71
72 The api might be adapted for this decision to be made automatically by the
73 frontend...
74
75
76 Precompiled magics for rapid loading
77 ====================================
78
79 For IPython itself, we'll have a module of 'core' magic functions that do not
80 require run-time registration. These will be the ones contained today in
81 Magic.py, plus any others we deem worthy of being available by default. This
82 is a trick to enable faster startup, since once we move to a model where each
83 magic can in principle be registered at runtime, creating a lot of them can
84 easily swamp startup time.
85
86 The trick is to make a module with a top-level class object that contains
87 explicit references to all the 'core' magics in its dict. This way, the magic
88 table can be quickly updated at interpreter startup with a single call, by
89 doing something along the lines of::
90
91 self.magic_table.update(static_magics.__dict__)
92
93 The point will be to be able to bypass the explicit calling of whatever
94 register_magic() API we end up making for users to declare their own magics.
95 So ultimately one should be able to do either::
96
97 ip.register_magic(mymagic) # for one function
98
99 or::
100
101 ip.load_magics(static_magics) # for a bunch of them
102
103 I still need to clarify exactly how this should work though.
@@ -1,19 +1,20 b''
1 1 .. _developer_guide:
2 2
3 3 =========================
4 4 IPython developer's guide
5 5 =========================
6 6
7 7 .. toctree::
8 8 :maxdepth: 1
9 9
10 10 contributing.txt
11 11 coding_guide.txt
12 12 doc_guide.txt
13 13 testing.txt
14 14 release.txt
15 15 roadmap.txt
16 16 reorg.txt
17 magic_blueprint.txt
17 18 notification_blueprint.txt
18 19 ipgraph.txt
19 20
General Comments 0
You need to be logged in to leave comments. Login now