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. |
General Comments 0
You need to be logged in to leave comments.
Login now