Show More
@@ -61,6 +61,11 b' def on_off(tag):' | |||||
61 |
|
61 | |||
62 |
|
62 | |||
63 | def compress_dhist(dh): |
|
63 | def compress_dhist(dh): | |
|
64 | """Compress a directory history into a new one with at most 20 entries. | |||
|
65 | ||||
|
66 | Return a new list made from the first and last 10 elements of dhist after | |||
|
67 | removal of duplicates. | |||
|
68 | """ | |||
64 | head, tail = dh[:-10], dh[-10:] |
|
69 | head, tail = dh[:-10], dh[-10:] | |
65 |
|
70 | |||
66 | newhead = [] |
|
71 | newhead = [] | |
@@ -84,6 +89,23 b' def needs_local_scope(func):' | |||||
84 | #----------------------------------------------------------------------------- |
|
89 | #----------------------------------------------------------------------------- | |
85 |
|
90 | |||
86 | def magics_class(cls): |
|
91 | def magics_class(cls): | |
|
92 | """Class decorator for all subclasses of the main Magics class. | |||
|
93 | ||||
|
94 | Any class that subclasses Magics *must* also apply this decorator, to | |||
|
95 | ensure that all the methods that have been decorated as line/cell magics | |||
|
96 | get correctly registered in the class instance. This is necessary because | |||
|
97 | when method decorators run, the class does not exist yet, so they | |||
|
98 | temporarily store their information into a module global. Application of | |||
|
99 | this class decorator copies that global data to the class instance and | |||
|
100 | clears the global. | |||
|
101 | ||||
|
102 | Obviously, this mechanism is not thread-safe, which means that the | |||
|
103 | *creation* of subclasses of Magic should only be done in a single-thread | |||
|
104 | context. Instantiation of the classes has no restrictions. Given that | |||
|
105 | these classes are typically created at IPython startup time and before user | |||
|
106 | application code becomes active, in practice this should not pose any | |||
|
107 | problems. | |||
|
108 | """ | |||
87 | cls.registered = True |
|
109 | cls.registered = True | |
88 | cls.magics = dict(line = magics['line'], |
|
110 | cls.magics = dict(line = magics['line'], | |
89 | cell = magics['cell']) |
|
111 | cell = magics['cell']) | |
@@ -92,14 +114,35 b' def magics_class(cls):' | |||||
92 | return cls |
|
114 | return cls | |
93 |
|
115 | |||
94 |
|
116 | |||
95 |
def record_magic(dct, m |
|
117 | def record_magic(dct, magic_kind, magic_name, func): | |
96 | if mtype == 'line_cell': |
|
118 | """Utility function to store a function as a magic of a specific kind. | |
97 | dct['line'][mname] = dct['cell'][mname] = func |
|
119 | ||
|
120 | Parameters | |||
|
121 | ---------- | |||
|
122 | dct : dict | |||
|
123 | A dictionary with 'line' and 'cell' subdicts. | |||
|
124 | ||||
|
125 | magic_kind : str | |||
|
126 | Kind of magic to be stored. | |||
|
127 | ||||
|
128 | magic_name : str | |||
|
129 | Key to store the magic as. | |||
|
130 | ||||
|
131 | func : function | |||
|
132 | Callable object to store. | |||
|
133 | """ | |||
|
134 | if magic_kind == 'line_cell': | |||
|
135 | dct['line'][magic_name] = dct['cell'][magic_name] = func | |||
98 | else: |
|
136 | else: | |
99 |
dct[m |
|
137 | dct[magic_kind][magic_name] = func | |
100 |
|
138 | |||
101 |
|
139 | |||
102 | def validate_type(magic_kind): |
|
140 | def validate_type(magic_kind): | |
|
141 | """Ensure that the given magic_kind is valid. | |||
|
142 | ||||
|
143 | Check that the given magic_kind is one of the accepted spec types (stored | |||
|
144 | in the global `magic_spec`), raise ValueError otherwise. | |||
|
145 | """ | |||
103 | if magic_kind not in magic_spec: |
|
146 | if magic_kind not in magic_spec: | |
104 | raise ValueError('magic_kind must be one of %s, %s given' % |
|
147 | raise ValueError('magic_kind must be one of %s, %s given' % | |
105 | magic_kinds, magic_kind) |
|
148 | magic_kinds, magic_kind) |
@@ -9,9 +9,22 b' To automatically restore stored variables at startup, add this to your' | |||||
9 |
|
9 | |||
10 | c.StoreMagic.autorestore = True |
|
10 | c.StoreMagic.autorestore = True | |
11 | """ |
|
11 | """ | |
12 |
|
12 | #----------------------------------------------------------------------------- | ||
|
13 | # Copyright (c) 2012, The IPython Development Team. | |||
|
14 | # | |||
|
15 | # Distributed under the terms of the Modified BSD License. | |||
|
16 | # | |||
|
17 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Imports | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | # Stdlib | |||
13 | import inspect, os, sys, textwrap |
|
25 | import inspect, os, sys, textwrap | |
14 |
|
26 | |||
|
27 | # Our own | |||
15 | from IPython.core.error import UsageError |
|
28 | from IPython.core.error import UsageError | |
16 | from IPython.core.fakemodule import FakeModule |
|
29 | from IPython.core.fakemodule import FakeModule | |
17 | from IPython.core.magic import Magics, magics_class, line_magic |
|
30 | from IPython.core.magic import Magics, magics_class, line_magic | |
@@ -19,6 +32,9 b' from IPython.core.plugin import Plugin' | |||||
19 | from IPython.testing.skipdoctest import skip_doctest |
|
32 | from IPython.testing.skipdoctest import skip_doctest | |
20 | from IPython.utils.traitlets import Bool, Instance |
|
33 | from IPython.utils.traitlets import Bool, Instance | |
21 |
|
34 | |||
|
35 | #----------------------------------------------------------------------------- | |||
|
36 | # Functions and classes | |||
|
37 | #----------------------------------------------------------------------------- | |||
22 |
|
38 | |||
23 | def restore_aliases(ip): |
|
39 | def restore_aliases(ip): | |
24 | staliases = ip.db.get('stored_aliases', {}) |
|
40 | staliases = ip.db.get('stored_aliases', {}) |
General Comments 0
You need to be logged in to leave comments.
Login now