Show More
@@ -1,7 +1,7 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | |
|
4 |
$Id: Magic.py 90 |
|
|
4 | $Id: Magic.py 907 2005-09-24 00:59:56Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
@@ -2463,6 +2463,7 b' Defaulting color scheme to \'NoColor\'"""' | |||
|
2463 | 2463 | filename = get_py_filename(parameter_s) |
|
2464 | 2464 | except IndexError: |
|
2465 | 2465 | warn('you must provide at least a filename.') |
|
2466 | return | |
|
2466 | 2467 | fobj=open(filename,'r') |
|
2467 | 2468 | source = fobj.read() |
|
2468 | 2469 | fobj.close() |
@@ -21,7 +21,7 b' class DemoError(exceptions.Exception): pass' | |||
|
21 | 21 | |
|
22 | 22 | class Demo: |
|
23 | 23 | def __init__(self,fname,arg_str='',mark_pause='# pause', |
|
24 | mark_silent='# silent',auto=False): | |
|
24 | mark_silent='# silent',mark_auto='# auto',auto=False): | |
|
25 | 25 | """Make a new demo object. To run the demo, simply call the object. |
|
26 | 26 | |
|
27 | 27 | Inputs: |
@@ -34,48 +34,70 b' class Demo:' | |||
|
34 | 34 | just like sys.argv, so the demo script can see a similar |
|
35 | 35 | environment. |
|
36 | 36 | |
|
37 |
- mark_pause ('# pause') |
|
|
38 | (block boundaries) and to tag blocks as silent. The marks are | |
|
39 | turned into regexps which match them as standalone in a line, with | |
|
40 | all leading/trailing whitespace ignored. | |
|
41 | ||
|
42 | - auto(False): flag to run each block automatically without | |
|
43 |
|
|
|
44 | executed. This flag is an attribute of the object, and can be | |
|
45 | changed at runtime simply by reassigning it. | |
|
37 | - mark_pause ('# pause'): marks for pausing (block boundaries). The | |
|
38 | marks are turned into regexps which match them as standalone in a | |
|
39 | line, with all leading/trailing whitespace ignored. | |
|
40 | ||
|
41 | - mark_silent('# silent'): mark blocks as silent, which means that | |
|
42 | they are executed without printing their content to screen. Silent | |
|
43 | blocks are always automatically executed. | |
|
44 | ||
|
45 | - mark_auto ('# auto'): mark individual blocks as automatically | |
|
46 | executed (without asking for confirmation). | |
|
47 | ||
|
48 | - auto(False): global flag to run all blocks automatically without | |
|
49 | confirmation. This attribute overrides the block-level tags and | |
|
50 | applies to the whole demo. It is an attribute of the object, and | |
|
51 | can be changed at runtime simply by reassigning it to a boolean | |
|
52 | value. | |
|
46 | 53 | """ |
|
47 | 54 | |
|
48 | self.fname = fname | |
|
49 | self.mark_pause = mark_pause | |
|
50 | self.re_pause = re.compile(r'^\s*%s\s*$' % mark_pause,re.MULTILINE) | |
|
55 | self.fname = fname | |
|
56 | self.sys_argv = [fname] + shlex_split(arg_str) | |
|
57 | self.mark_pause = mark_pause | |
|
51 | 58 | self.mark_silent = mark_silent |
|
52 |
self.re_ |
|
|
53 | self.auto = auto | |
|
54 | self.sys_argv = [fname]+shlex_split(arg_str) | |
|
59 | self.re_pause = re.compile(r'^\s*%s\s*$' % mark_pause,re.MULTILINE) | |
|
60 | self.re_silent = re.compile(r'^\s*%s\s*$' % mark_silent,re.MULTILINE) | |
|
61 | self.re_auto = re.compile(r'^\s*%s\s*$' % mark_auto,re.MULTILINE) | |
|
62 | self.auto = auto | |
|
55 | 63 | |
|
56 | 64 | # get a few things from ipython. While it's a bit ugly design-wise, |
|
57 | 65 | # it ensures that things like color scheme and the like are always in |
|
58 | 66 | # sync with the ipython mode being used. This class is only meant to |
|
59 | 67 | # be used inside ipython anyways, so it's OK. |
|
60 |
self.ip_showt |
|
|
61 | self.ip_ns = __IPYTHON__.user_ns | |
|
68 | self.ip_showtb = __IPYTHON__.showtraceback | |
|
69 | self.ip_ns = __IPYTHON__.user_ns | |
|
62 | 70 | self.ip_colors = __IPYTHON__.rc['colors'] |
|
71 | self.colorize = Parser().format | |
|
72 | ||
|
73 | # load user data and initialize data structures | |
|
74 | self.reload() | |
|
63 | 75 | |
|
76 | def reload(self): | |
|
77 | """Reload source from disk and initialize state.""" | |
|
64 | 78 | # read data and parse into blocks |
|
65 | fobj = file(fname,'r') | |
|
79 | fobj = file(self.fname,'r') | |
|
66 | 80 | self.src = fobj.read() |
|
67 | 81 | fobj.close() |
|
68 |
|
|
|
69 |
self.silent = [bool(self.re_silent.findall(b)) for b in |
|
|
70 | self.nblocks = len(self.src_blocks) | |
|
82 | src_blocks = [b.strip() for b in self.re_pause.split(self.src) if b] | |
|
83 | self._silent = [bool(self.re_silent.findall(b)) for b in src_blocks] | |
|
84 | self._auto = [bool(self.re_auto.findall(b)) for b in src_blocks] | |
|
85 | # strip out the 'auto' markers | |
|
86 | src_b = [] | |
|
87 | auto_strip = lambda s: self.re_auto.sub('',s) | |
|
88 | for i,b in enumerate(src_blocks): | |
|
89 | if self._auto[i]: | |
|
90 | src_b.append(auto_strip(b)) | |
|
91 | else: | |
|
92 | src_b.append(b) | |
|
93 | self.nblocks = len(src_b) | |
|
94 | self.src_blocks = src_b | |
|
71 | 95 | |
|
72 | 96 | # try to colorize blocks |
|
73 | colorize = Parser().format | |
|
74 | 97 | col_scheme = self.ip_colors |
|
75 | self.src_blocks_colored = [colorize(s_blk,'str',col_scheme) | |
|
98 | self.src_blocks_colored = [self.colorize(s_blk,'str',col_scheme) | |
|
76 | 99 | for s_blk in self.src_blocks] |
|
77 | ||
|
78 | # finish initialization | |
|
100 | # ensure clean namespace and seek offset | |
|
79 | 101 | self.reset() |
|
80 | 102 | |
|
81 | 103 | def reset(self): |
@@ -97,10 +119,10 b' class Demo:' | |||
|
97 | 119 | def seek(self,index): |
|
98 | 120 | """Move the current seek pointer to the given block""" |
|
99 | 121 | self._validate_index(index) |
|
100 |
self.block_index = index |
|
|
122 | self.block_index = index | |
|
101 | 123 | self.finished = False |
|
102 | 124 | |
|
103 |
def show |
|
|
125 | def show(self,index=None): | |
|
104 | 126 | """Show a single block on screen""" |
|
105 | 127 | if index is None: |
|
106 | 128 | if self.finished: |
@@ -113,12 +135,12 b' class Demo:' | |||
|
113 | 135 | (self.fname,index,self.nblocks-index-1)) |
|
114 | 136 | print self.src_blocks_colored[index], |
|
115 | 137 | |
|
116 | def show(self): | |
|
138 | def show_all(self): | |
|
117 | 139 | """Show entire demo on screen, block by block""" |
|
118 | 140 | |
|
119 | 141 | fname = self.fname |
|
120 | 142 | nblocks = self.nblocks |
|
121 | silent = self.silent | |
|
143 | silent = self._silent | |
|
122 | 144 | for index,block in enumerate(self.src_blocks_colored): |
|
123 | 145 | if silent[index]: |
|
124 | 146 | print marquee('<%s> SILENT block # %s (%s remaining)' % |
@@ -146,12 +168,12 b' class Demo:' | |||
|
146 | 168 | try: |
|
147 | 169 | next_block = self.src_blocks[index] |
|
148 | 170 | self.block_index += 1 |
|
149 | if self.silent[index]: | |
|
171 | if self._silent[index]: | |
|
150 | 172 | print marquee('Executing silent block # %s (%s remaining)' % |
|
151 | 173 | (index,self.nblocks-index-1)) |
|
152 | 174 | else: |
|
153 |
self.show |
|
|
154 | if self.auto: | |
|
175 | self.show(index) | |
|
176 | if self.auto or self._auto[index]: | |
|
155 | 177 | print marquee('output') |
|
156 | 178 | else: |
|
157 | 179 | print marquee('Press <q> to quit, <Enter> to execute...'), |
@@ -167,7 +189,7 b' class Demo:' | |||
|
167 | 189 | sys.argv = save_argv |
|
168 | 190 | |
|
169 | 191 | except: |
|
170 |
self.ip_showt |
|
|
192 | self.ip_showtb(filename=self.fname) | |
|
171 | 193 | else: |
|
172 | 194 | self.ip_ns.update(self.user_ns) |
|
173 | 195 | |
@@ -176,3 +198,4 b' class Demo:' | |||
|
176 | 198 | print marquee(' END OF DEMO ') |
|
177 | 199 | print marquee('Use reset() if you want to rerun it.') |
|
178 | 200 | self.finished = True |
|
201 |
@@ -1,5 +1,8 b'' | |||
|
1 | 1 | 2005-09-23 Fernando Perez <Fernando.Perez@colorado.edu> |
|
2 | 2 | |
|
3 | * IPython/demo.py (Demo.__init__): added support for individually | |
|
4 | tagging blocks for automatic execution. | |
|
5 | ||
|
3 | 6 | * IPython/Magic.py (magic_pycat): new %pycat magic for showing |
|
4 | 7 | syntax-highlighted python sources, requested by John. |
|
5 | 8 |
General Comments 0
You need to be logged in to leave comments.
Login now