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