Show More
@@ -0,0 +1,65 b'' | |||
|
1 | from IPython.demo import Demo,IPythonDemo,LineDemo,IPythonLineDemo,ClearDemo,ClearIPDemo | |
|
2 | import tempfile, os, StringIO, shutil | |
|
3 | ||
|
4 | example1 = """ | |
|
5 | '''A simple interactive demo to illustrate the use of IPython's Demo class.''' | |
|
6 | ||
|
7 | print 'Hello, welcome to an interactive IPython demo.' | |
|
8 | ||
|
9 | # The mark below defines a block boundary, which is a point where IPython will | |
|
10 | # stop execution and return to the interactive prompt. The dashes are actually | |
|
11 | # optional and used only as a visual aid to clearly separate blocks while | |
|
12 | # editing the demo code. | |
|
13 | # <demo> stop | |
|
14 | ||
|
15 | x = 1 | |
|
16 | y = 2 | |
|
17 | ||
|
18 | # <demo> stop | |
|
19 | ||
|
20 | # the mark below makes this block as silent | |
|
21 | # <demo> silent | |
|
22 | ||
|
23 | print 'This is a silent block, which gets executed but not printed.' | |
|
24 | ||
|
25 | # <demo> stop | |
|
26 | # <demo> auto | |
|
27 | print 'This is an automatic block.' | |
|
28 | print 'It is executed without asking for confirmation, but printed.' | |
|
29 | z = x+y | |
|
30 | ||
|
31 | print 'z=',x | |
|
32 | ||
|
33 | # <demo> stop | |
|
34 | # This is just another normal block. | |
|
35 | print 'z is now:', z | |
|
36 | ||
|
37 | print 'bye!' | |
|
38 | """ | |
|
39 | fp = tempfile.mkdtemp(prefix = 'DemoTmp') | |
|
40 | fd, filename = tempfile.mkstemp(prefix = 'demoExample1File', suffix = '.py', dir = fp) | |
|
41 | f = os.fdopen(fd, 'wt') | |
|
42 | ||
|
43 | f.write(example1) | |
|
44 | f.close() | |
|
45 | ||
|
46 | my_d = Demo(filename) | |
|
47 | my_cd = ClearDemo(filename) | |
|
48 | ||
|
49 | fobj = StringIO.StringIO(example1) | |
|
50 | str_d = Demo(fobj, title='via stringio') | |
|
51 | #~ def tmpcleanup(): | |
|
52 | #~ global my_d, my_cd, fp | |
|
53 | #~ del my_d | |
|
54 | #~ del my_cd | |
|
55 | #~ shutil.rmtree(fp, False) | |
|
56 | ||
|
57 | print ''' | |
|
58 | The following demos should now be available to use: | |
|
59 | my_d -- created from a file | |
|
60 | my_cd -- created from a file, a ClearDemo | |
|
61 | str_d -- same as above, but created via a stringi\o object | |
|
62 | ||
|
63 | ''' | |
|
64 | # call tmpcleanup to delete the temporary files created. -not implemented | |
|
65 |
@@ -125,7 +125,7 b" print 'Hello, welcome to an interactive IPython demo.'" | |||
|
125 | 125 | # The mark below defines a block boundary, which is a point where IPython will |
|
126 | 126 | # stop execution and return to the interactive prompt. The dashes are actually |
|
127 | 127 | # optional and used only as a visual aid to clearly separate blocks while |
|
128 | editing the demo code. | |
|
128 | # editing the demo code. | |
|
129 | 129 | # <demo> stop |
|
130 | 130 | |
|
131 | 131 | x = 1 |
@@ -170,6 +170,7 b' import sys' | |||
|
170 | 170 | |
|
171 | 171 | from IPython.PyColorize import Parser |
|
172 | 172 | from IPython.genutils import marquee, file_read, file_readlines |
|
173 | from genutils import Term | |
|
173 | 174 | |
|
174 | 175 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] |
|
175 | 176 | |
@@ -185,7 +186,7 b' class Demo(object):' | |||
|
185 | 186 | re_auto = re_mark('auto') |
|
186 | 187 | re_auto_all = re_mark('auto_all') |
|
187 | 188 | |
|
188 |
def __init__(self, |
|
|
189 | def __init__(self,src,title='',arg_str='',auto_all=None): | |
|
189 | 190 | """Make a new demo object. To run the demo, simply call the object. |
|
190 | 191 | |
|
191 | 192 | See the module docstring for full details and an example (you can use |
@@ -193,9 +194,14 b' class Demo(object):' | |||
|
193 | 194 | |
|
194 | 195 | Inputs: |
|
195 | 196 | |
|
196 | - fname = filename. | |
|
197 | - src is either a file, or file-like object, or a | |
|
198 | string that can be resolved to a filename. | |
|
197 | 199 | |
|
198 | 200 | Optional inputs: |
|
201 | ||
|
202 | - title: a string to use as the demo name. Of most use when the demo | |
|
203 | you are making comes from an object that has no filename, or if you | |
|
204 | want an alternate denotation distinct from the filename. | |
|
199 | 205 | |
|
200 | 206 | - arg_str(''): a string of arguments, internally converted to a list |
|
201 | 207 | just like sys.argv, so the demo script can see a similar |
@@ -207,9 +213,24 b' class Demo(object):' | |||
|
207 | 213 | can be changed at runtime simply by reassigning it to a boolean |
|
208 | 214 | value. |
|
209 | 215 | """ |
|
210 | ||
|
211 | self.fname = fname | |
|
212 | self.sys_argv = [fname] + shlex.split(arg_str) | |
|
216 | if hasattr(src, "read"): | |
|
217 | # It seems to be a file or a file-like object | |
|
218 | self.fobj = src | |
|
219 | self.fname = "from a file-like object" | |
|
220 | if title == '': | |
|
221 | self.title = "from a file-like object" | |
|
222 | else: | |
|
223 | self.title = title | |
|
224 | else: | |
|
225 | # Assume it's a string or something that can be converted to one | |
|
226 | self.fobj = open(src) | |
|
227 | self.fname = src | |
|
228 | if title == '': | |
|
229 | (filepath, filename) = os.path.split(src) | |
|
230 | self.title = filename | |
|
231 | else: | |
|
232 | self.title = title | |
|
233 | self.sys_argv = [src] + shlex.split(arg_str) | |
|
213 | 234 | self.auto_all = auto_all |
|
214 | 235 | |
|
215 | 236 | # get a few things from ipython. While it's a bit ugly design-wise, |
@@ -228,7 +249,7 b' class Demo(object):' | |||
|
228 | 249 | def reload(self): |
|
229 | 250 | """Reload source from disk and initialize state.""" |
|
230 | 251 | # read data and parse into blocks |
|
231 |
self.src = |
|
|
252 | self.src = self.fobj.read() | |
|
232 | 253 | src_b = [b.strip() for b in self.re_stop.split(self.src) if b] |
|
233 | 254 | self._silent = [bool(self.re_silent.findall(b)) for b in src_b] |
|
234 | 255 | self._auto = [bool(self.re_auto.findall(b)) for b in src_b] |
@@ -277,7 +298,7 b' class Demo(object):' | |||
|
277 | 298 | |
|
278 | 299 | if index is None: |
|
279 | 300 | if self.finished: |
|
280 | print 'Demo finished. Use reset() if you want to rerun it.' | |
|
301 | print >>Term.cout, 'Demo finished. Use <demo_name>.reset() if you want to rerun it.' | |
|
281 | 302 | return None |
|
282 | 303 | index = self.block_index |
|
283 | 304 | else: |
@@ -346,26 +367,27 b' class Demo(object):' | |||
|
346 | 367 | if index is None: |
|
347 | 368 | return |
|
348 | 369 | |
|
349 | print self.marquee('<%s> block # %s (%s remaining)' % | |
|
350 |
(self. |
|
|
351 |
|
|
|
370 | print >>Term.cout, self.marquee('<%s> block # %s (%s remaining)' % | |
|
371 | (self.title,index,self.nblocks-index-1)) | |
|
372 | print >>Term.cout,(self.src_blocks_colored[index]) | |
|
352 | 373 | sys.stdout.flush() |
|
353 | 374 | |
|
354 | 375 | def show_all(self): |
|
355 | 376 | """Show entire demo on screen, block by block""" |
|
356 | 377 | |
|
357 |
fname = self. |
|
|
378 | fname = self.title | |
|
379 | title = self.title | |
|
358 | 380 | nblocks = self.nblocks |
|
359 | 381 | silent = self._silent |
|
360 | 382 | marquee = self.marquee |
|
361 | 383 | for index,block in enumerate(self.src_blocks_colored): |
|
362 | 384 | if silent[index]: |
|
363 | print marquee('<%s> SILENT block # %s (%s remaining)' % | |
|
364 |
( |
|
|
385 | print >>Term.cout, marquee('<%s> SILENT block # %s (%s remaining)' % | |
|
386 | (title,index,nblocks-index-1)) | |
|
365 | 387 | else: |
|
366 | print marquee('<%s> block # %s (%s remaining)' % | |
|
367 |
( |
|
|
368 | print block, | |
|
388 | print >>Term.cout, marquee('<%s> block # %s (%s remaining)' % | |
|
389 | (title,index,nblocks-index-1)) | |
|
390 | print >>Term.cout, block, | |
|
369 | 391 | sys.stdout.flush() |
|
370 | 392 | |
|
371 | 393 | def runlines(self,source): |
@@ -390,18 +412,18 b' class Demo(object):' | |||
|
390 | 412 | next_block = self.src_blocks[index] |
|
391 | 413 | self.block_index += 1 |
|
392 | 414 | if self._silent[index]: |
|
393 | print marquee('Executing silent block # %s (%s remaining)' % | |
|
415 | print >>Term.cout, marquee('Executing silent block # %s (%s remaining)' % | |
|
394 | 416 | (index,self.nblocks-index-1)) |
|
395 | 417 | else: |
|
396 | 418 | self.pre_cmd() |
|
397 | 419 | self.show(index) |
|
398 | 420 | if self.auto_all or self._auto[index]: |
|
399 | print marquee('output:') | |
|
421 | print >>Term.cout, marquee('output:') | |
|
400 | 422 | else: |
|
401 | print marquee('Press <q> to quit, <Enter> to execute...'), | |
|
423 | print >>Term.cout, marquee('Press <q> to quit, <Enter> to execute...'), | |
|
402 | 424 | ans = raw_input().strip() |
|
403 | 425 | if ans: |
|
404 | print marquee('Block NOT executed') | |
|
426 | print >>Term.cout, marquee('Block NOT executed') | |
|
405 | 427 | return |
|
406 | 428 | try: |
|
407 | 429 | save_argv = sys.argv |
@@ -419,10 +441,10 b' class Demo(object):' | |||
|
419 | 441 | if self.block_index == self.nblocks: |
|
420 | 442 | mq1 = self.marquee('END OF DEMO') |
|
421 | 443 | if mq1: |
|
422 | # avoid spurious prints if empty marquees are used | |
|
423 | ||
|
424 | print mq1 | |
|
425 | print self.marquee('Use reset() if you want to rerun it.') | |
|
444 | # avoid spurious print >>Term.cout,s if empty marquees are used | |
|
445 | print >>Term.cout | |
|
446 | print >>Term.cout, mq1 | |
|
447 | print >>Term.cout, self.marquee('Use <demo_name>.reset() if you want to rerun it.') | |
|
426 | 448 | self.finished = True |
|
427 | 449 | |
|
428 | 450 | # These methods are meant to be overridden by subclasses who may wish to |
@@ -471,9 +493,9 b' class LineDemo(Demo):' | |||
|
471 | 493 | def reload(self): |
|
472 | 494 | """Reload source from disk and initialize state.""" |
|
473 | 495 | # read data and parse into blocks |
|
474 |
src_b = [l for l in |
|
|
496 | src_b = [l for l in self.fobj.readline() if l.strip()] | |
|
475 | 497 | nblocks = len(src_b) |
|
476 |
self.src = os.linesep.join( |
|
|
498 | self.src = os.linesep.join(self.fobj.readlines()) | |
|
477 | 499 | self._silent = [False]*nblocks |
|
478 | 500 | self._auto = [True]*nblocks |
|
479 | 501 | self.auto_all = True |
@@ -494,28 +516,32 b' class IPythonLineDemo(IPythonDemo,LineDemo):' | |||
|
494 | 516 | |
|
495 | 517 | class ClearMixin(object): |
|
496 | 518 | """Use this mixin to make Demo classes with less visual clutter. |
|
497 | ||
|
519 | ||
|
498 | 520 | Demos using this mixin will clear the screen before every block and use |
|
499 | 521 | blank marquees. |
|
500 | ||
|
522 | ||
|
501 | 523 | Note that in order for the methods defined here to actually override those |
|
502 | 524 | of the classes it's mixed with, it must go /first/ in the inheritance |
|
503 | 525 | tree. For example: |
|
504 | ||
|
526 | ||
|
505 | 527 | class ClearIPDemo(ClearMixin,IPythonDemo): pass |
|
506 | ||
|
528 | ||
|
507 | 529 | will provide an IPythonDemo class with the mixin's features. |
|
508 | 530 | """ |
|
509 | 531 | |
|
510 | 532 | def marquee(self,txt='',width=78,mark='*'): |
|
511 | 533 | """Blank marquee that returns '' no matter what the input.""" |
|
512 | 534 | return '' |
|
513 | ||
|
535 | ||
|
514 | 536 | def pre_cmd(self): |
|
515 | 537 | """Method called before executing each block. |
|
516 | ||
|
538 | ||
|
517 | 539 | This one simply clears the screen.""" |
|
518 | os.system('clear') | |
|
540 | if sys.platform == "win32": | |
|
541 | os.system('cls') | |
|
542 | else: | |
|
543 | os.system('clear') | |
|
544 | ||
|
519 | 545 | |
|
520 | 546 | |
|
521 | 547 | class ClearDemo(ClearMixin,Demo): |
General Comments 0
You need to be logged in to leave comments.
Login now