;ò
ÎÑAHc           @   s7  d  Z  d k Z d k Z d k Z d k Z d k Z d k l Z d k l	 Z	 l
 Z
 l Z d d d d d g Z d e i f d	 „  ƒ  YZ d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e e f d „  ƒ  YZ d S(   sŸ  Module for interactive demos using IPython.

This module implements a few classes for running Python scripts interactively
in IPython for demonstrations.  With very simple markup (a few tags in
comments), you can control points where the script stops executing and returns
control to IPython.


Provided classes
================

The classes are (see their docstrings for further details):

 - Demo: pure python demos

 - IPythonDemo: demos with input to be processed by IPython as if it had been
 typed interactively (so magics work, as well as any other special syntax you
 may have added via input prefilters).

 - LineDemo: single-line version of the Demo class.  These demos are executed
 one line at a time, and require no markup.

 - IPythonLineDemo: IPython version of the LineDemo class (the demo is
 executed a line at a time, but processed via IPython).

 - ClearMixin: mixin to make Demo classes with less visual clutter.  It
   declares an empty marquee and a pre_cmd that clears the screen before each
   block (see Subclassing below).

 - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
   classes.


Subclassing
===========

The classes here all include a few methods meant to make customization by
subclassing more convenient.  Their docstrings below have some more details:

  - marquee(): generates a marquee to provide visible on-screen markers at each
    block start and end.

  - pre_cmd(): run right before the execution of each block.

  - post_cmd(): run right after the execution of each block.  If the block
    raises an exception, this is NOT called.
    

Operation
=========

The file is run in its own empty namespace (though you can pass it a string of
arguments as if in a command line environment, and it will see those as
sys.argv).  But at each stop, the global IPython namespace is updated with the
current internal demo namespace, so you can work interactively with the data
accumulated so far.

By default, each block of code is printed (with syntax highlighting) before
executing it and you have to confirm execution.  This is intended to show the
code to an audience first so you can discuss it, and only proceed with
execution once you agree.  There are a few tags which allow you to modify this
behavior.

The supported tags are:

# <demo> stop

  Defines block boundaries, the points where IPython stops execution of the
  file and returns to the interactive prompt.

  You can optionally mark the stop tag with extra dashes before and after the
  word 'stop', to help visually distinguish the blocks in a text editor:

  # <demo> --- stop ---
  

# <demo> silent

  Make a block execute silently (and hence automatically).  Typically used in
  cases where you have some boilerplate or initialization code which you need
  executed but do not want to be seen in the demo.
  
# <demo> auto

  Make a block execute automatically, but still being printed.  Useful for
  simple code which does not warrant discussion, since it avoids the extra
  manual confirmation.

# <demo> auto_all

  This tag can _only_ be in the first block, and if given it overrides the
  individual auto tags to make the whole demo fully automatic (no block asks
  for confirmation).  It can also be given at creation time (or the attribute
  set later) to override what's in the file.

While _any_ python file can be run as a Demo instance, if there are no stop
tags the whole file will run in a single block (no different that calling
first %pycat and then %run).  The minimal markup to make this useful is to
place a set of stop tags; the other tags are only there to let you fine-tune
the execution.

This is probably best explained with the simple example file below.  You can
copy this into a file named ex_demo.py, and try running it via:

from IPython.demo import Demo
d = Demo('ex_demo.py')
d()  <--- Call the d object (omit the parens if you have autocall set to 2).

Each time you call the demo object, it runs the next block.  The demo object
has a few useful methods for navigation, like again(), edit(), jump(), seek()
and back().  It can be reset for a new run via reset() or reloaded from disk
(in case you've edited the source) via reload().  See their docstrings below.


Example
=======

The following is a very simple example of a valid demo file.

#################### EXAMPLE DEMO <ex_demo.py> ###############################
'''A simple interactive demo to illustrate the use of IPython's Demo class.'''

print 'Hello, welcome to an interactive IPython demo.'

# The mark below defines a block boundary, which is a point where IPython will
# stop execution and return to the interactive prompt. The dashes are actually
# optional and used only as a visual aid to clearly separate blocks while
editing the demo code.
# <demo> stop

x = 1
y = 2

# <demo> stop

# the mark below makes this block as silent
# <demo> silent

print 'This is a silent block, which gets executed but not printed.'

# <demo> stop
# <demo> auto
print 'This is an automatic block.'
print 'It is executed without asking for confirmation, but printed.'
z = x+y

print 'z=',x

# <demo> stop
# This is just another normal block.
print 'z is now:', z

print 'bye!'
################### END EXAMPLE DEMO <ex_demo.py> ############################
N(   s   Parser(   s   marquees	   file_reads   file_readliness   Demos   IPythonDemos   LineDemos   IPythonLineDemos	   DemoErrorc           B   s   t  Z RS(   N(   s   __name__s
   __module__(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys	   DemoError°   s    c         C   s   t  i d |  t  i ƒ Sd  S(   Ns   ^\s*#\s+<demo>\s+%s\s*$(   s   res   compiles   marks	   MULTILINE(   s   mark(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   re_mark²   s    c           B   sï   t  Z e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z d e d „ Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d d „ Z d d „ Z d „  Z e d „ Z e d „ Z d „  Z d „  Z e d „ Z d d d d „ Z d „  Z d „  Z RS(   Ns   -?\s?stop\s?-?s   silents   autos   auto_alls    c         C   sr   | |  _  | g t i | ƒ |  _ | |  _ t i |  _	 t i
 |  _ t i |  _ t i |  _ t |  _ |  i ƒ  d S(   s  Make a new demo object.  To run the demo, simply call the object.

        See the module docstring for full details and an example (you can use
        IPython.Demo? in IPython to see it).

        Inputs:
        
          - fname = filename.

        Optional inputs:

          - arg_str(''): a string of arguments, internally converted to a list
          just like sys.argv, so the demo script can see a similar
          environment.

          - auto_all(None): global flag to run all blocks automatically without
          confirmation.  This attribute overrides the block-level tags and
          applies to the whole demo.  It is an attribute of the object, and
          can be changed at runtime simply by reassigning it to a boolean
          value.
          N(   s   fnames   selfs   shlexs   splits   arg_strs   sys_argvs   auto_alls   __IPYTHON__s   user_nss   ip_nss
   pycolorizes   ip_colorizes   showtracebacks	   ip_showtbs   runliness   ip_runliness   shells   reload(   s   selfs   fnames   arg_strs   auto_all(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   __init__¼   s     			c            sÒ  t  ˆ  i ƒ ˆ  _ g  i } ˆ  i i ˆ  i ƒ D]! } | o | | i	 ƒ  ƒ q/ q/ ~ } g  i } | D]" } | t ˆ  i i | ƒ ƒ ƒ qg ~ ˆ  _ g  i } | D]" } | t ˆ  i i | ƒ ƒ ƒ q£ ~ ˆ  _ ˆ  i t j o# t ˆ  i i | d ƒ ƒ ˆ  _ n t ˆ  i ƒ ˆ  _ g  } ‡  d †  } xL t | ƒ D]> \ } } ˆ  i | o | i | | ƒ ƒ q5| i | ƒ q5Wˆ  i i d | d ƒ | d <t | ƒ ˆ  _ | ˆ  _ t ˆ  i ˆ  i ƒ ˆ  _ ˆ  i ƒ  d S(   s-   Reload source from disk and initialize state.i    c            s   ˆ  i i d |  ƒ S(   Ns    (   s   selfs   re_autos   subs   s(   s   s(   s   self(    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   <lambda>õ   s    s    N(   s	   file_reads   selfs   fnames   srcs   appends   _[1]s   re_stops   splits   bs   strips   src_bs   bools	   re_silents   findalls   _silents   re_autos   _autos   auto_alls   Nones   re_auto_alls
   src_blockss
   auto_strips	   enumerates   is   subs   lens   nblockss   maps   ip_colorizes   src_blocks_coloreds   reset(   s   selfs
   auto_strips   bs   is   src_bs
   src_blockss   _[1](    (   s   selfs=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   reloadä   s(     G<<# 	c         C   s   h  |  _ t |  _ d |  _ d S(   s8   Reset the namespace and seek pointer to restart the demoi    N(   s   selfs   user_nss   Falses   finisheds   block_index(   s   self(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   reset  s     		c         C   s5   | d j  p | |  i j o t d | ƒ ‚ n d  S(   Ni    s   invalid block index %s(   s   indexs   selfs   nblockss
   ValueError(   s   selfs   index(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   _validate_index  s    c         C   sF   | t j o$ |  i o d GHt Sn |  i } n |  i | ƒ | Sd S(   sj   Get the current block index, validating and checking status.

        Returns None if the demo is finisheds4   Demo finished.  Use reset() if you want to rerun it.N(   s   indexs   Nones   selfs   finisheds   block_indexs   _validate_index(   s   selfs   index(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys
   _get_index  s     
c         C   sA   | d j  o |  i | } n |  i | ƒ | |  _ t |  _ d S(   s¨   Move the current seek pointer to the given block.

        You can use negative indices to seek from the end, with identical
        semantics to those of Python lists.i    N(   s   indexs   selfs   nblockss   _validate_indexs   block_indexs   Falses   finished(   s   selfs   index(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   seek  s     	i   c         C   s   |  i |  i | ƒ d S(   s5   Move the seek pointer back num blocks (default is 1).N(   s   selfs   seeks   block_indexs   num(   s   selfs   num(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   back*  s     c         C   s   |  i |  i | ƒ d S(   sz   Jump a given number of blocks relative to the current one.

        The offset can be positive or negative, defaults to 1.N(   s   selfs   seeks   block_indexs   num(   s   selfs   num(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   jump.  s     c         C   s   |  i d ƒ |  ƒ  d S(   s4   Move the seek pointer back one block and re-execute.i   N(   s   selfs   back(   s   self(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   again4  s     c         C   s±   |  i | ƒ } | t j o d Sn | d j o | d 8} n |  i i |  i | ƒ } |  i i i	 | d ƒ t
 | ƒ } | |  i | <|  i | ƒ |  i | <| |  _ |  ƒ  d S(   sÂ  Edit a block.

        If no number is given, use the last block executed.

        This edits the in-memory copy of the demo, it does NOT modify the
        original source file.  If you want to do that, simply open the file in
        an editor and use reload() when you make changes to the file.  This
        method is meant to let you change a block during a demonstration for
        explanatory purposes, without damaging your original script.Ni    i   (   s   selfs
   _get_indexs   indexs   Nones   shells
   mktempfiles
   src_blockss   filenames   hookss   editors	   file_reads	   new_blocks   ip_colorizes   src_blocks_coloreds   block_index(   s   selfs   indexs   filenames	   new_block(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   edit9  s    	 	c         C   su   |  i | ƒ } | t j o d Sn |  i d |  i | |  i | d f ƒ GHt i i	 |  i
 | ƒ t i i ƒ  d S(   s   Show a single block on screenNs   <%s> block # %s (%s remaining)i   (   s   selfs
   _get_indexs   indexs   Nones   marquees   fnames   nblockss   syss   stdouts   writes   src_blocks_coloreds   flush(   s   selfs   index(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   showV  s     )c         C   s¨   |  i } |  i } |  i } |  i } xp t |  i ƒ D]_ \ } } | | o$ | d | | | | d f ƒ GHn! | d | | | | d f ƒ GH| Gq4 Wt
 i i ƒ  d S(   s*   Show entire demo on screen, block by blocks%   <%s> SILENT block # %s (%s remaining)i   s   <%s> block # %s (%s remaining)N(   s   selfs   fnames   nblockss   _silents   silents   marquees	   enumerates   src_blocks_coloreds   indexs   blocks   syss   stdouts   flush(   s   selfs   indexs   silents   marquees   fnames   nblockss   block(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   show_allb  s     				 $ c         B   s   | |  i Ud S(   s/   Execute a string with one or more lines of codeN(   s   sources   selfs   user_ns(   s   selfs   source(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   runliness  s     c         C   s¯  |  i | ƒ } | t j o d Sn y	|  i } |  i | } |  i d 7_ |  i | o$ | d | |  i	 | d f ƒ GHnr |  i
 ƒ  |  i | ƒ |  i p |  i | o | d ƒ GHn4 | d ƒ Gt ƒ  i ƒ  } | o | d ƒ GHd Sn z0 t i } |  i t _ |  i | ƒ |  i ƒ  Wd | t _ XWn |  i d |  i ƒ n X|  i i |  i ƒ |  i |  i	 j o; |  i d ƒ } | o H| GH|  i d	 ƒ GHn t |  _ n d S(
   sb  run a block of the demo.

        If index is given, it should be an integer >=1 and <= nblocks.  This
        means that the calling convention is one off from typical Python
        lists.  The reason for the inconsistency is that the demo always
        prints 'Block n/N, and N is the total, so it would be very odd to use
        zero-indexing here.Ni   s*   Executing silent block # %s (%s remaining)s   output:s(   Press <q> to quit, <Enter> to execute...s   Block NOT executeds   filenames   END OF DEMOs$   Use reset() if you want to rerun it.(   s   selfs
   _get_indexs   indexs   Nones   marquees
   src_blockss
   next_blocks   block_indexs   _silents   nblockss   pre_cmds   shows   auto_alls   _autos	   raw_inputs   strips   anss   syss   argvs	   save_argvs   sys_argvs   runliness   post_cmds	   ip_showtbs   fnames   ip_nss   updates   user_nss   mq1s   Trues   finished(   s   selfs   indexs   marquees	   save_argvs   mq1s   anss
   next_block(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   __call__x  sH     	$

	 iN   s   *c         C   s   t  | | | ƒ Sd S(   s0   Return the input string centered in a 'marquee'.N(   s   marquees   txts   widths   mark(   s   selfs   txts   widths   mark(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   marquee®  s     c         C   s   d S(   s*   Method called before executing each block.N(    (   s   self(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   pre_cmd²  s     c         C   s   d S(   s)   Method called after executing each block.N(    (   s   self(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   post_cmd¶  s     (   s   __name__s
   __module__s   re_marks   re_stops	   re_silents   re_autos   re_auto_alls   Nones   __init__s   reloads   resets   _validate_indexs
   _get_indexs   seeks   backs   jumps   agains   edits   shows   show_alls   runliness   __call__s   marquees   pre_cmds   post_cmd(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   Demoµ   s*   (	#							6	c           B   s   t  Z d  Z d „  Z RS(   sD  Class for interactive demos with IPython's input processing applied.

    This subclasses Demo, but instead of executing each block by the Python
    interpreter (via exec), it actually calls IPython on it, so that any input
    filters which may be in place are applied to the input block.

    If you have an interactive environment which exposes special input
    processing, you can use this class instead to write demo scripts which
    operate exactly as if you had typed them interactively.  The default Demo
    class requires the input to be valid, pure Python code.
    c         C   s   |  i i | ƒ d S(   s/   Execute a string with one or more lines of codeN(   s   selfs   shells   runliness   source(   s   selfs   source(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   runlinesÈ  s     (   s   __name__s
   __module__s   __doc__s   runlines(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   IPythonDemo»  s    c           B   s   t  Z d  Z d „  Z RS(   ss  Demo where each line is executed as a separate block.

    The input script should be valid Python code.

    This class doesn't require any markup at all, and it's meant for simple
    scripts (with no nesting or any kind of indentation) which consist of
    multiple lines of input to be executed, one at a time, as if they had been
    typed in the interactive prompt.c         C   sÌ   g  i  } t |  i ƒ D]! } | i ƒ  o | | ƒ q q ~ } t | ƒ } t
 i i t |  i ƒ ƒ |  _ t g | |  _ t g | |  _ t |  _ | |  _	 | |  _ t |  i |  i ƒ |  _ |  i ƒ  d S(   s-   Reload source from disk and initialize state.N(   s   appends   _[1]s   file_readliness   selfs   fnames   ls   strips   src_bs   lens   nblockss   oss   lineseps   joins   srcs   Falses   _silents   Trues   _autos   auto_alls
   src_blockss   maps   ip_colorizes   src_blocks_coloreds   reset(   s   selfs   src_bs   ls   _[1]s   nblocks(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   reload×  s     A			(   s   __name__s
   __module__s   __doc__s   reload(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   LineDemoÍ  s    c           B   s   t  Z d  Z RS(   sB   Variant of the LineDemo class whose input is processed by IPython.(   s   __name__s
   __module__s   __doc__(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   IPythonLineDemoê  s    s
   ClearMixinc           B   s)   t  Z d  Z d d d d „ Z d „  Z RS(   sÐ  Use this mixin to make Demo classes with less visual clutter.

    Demos using this mixin will clear the screen before every block and use
    blank marquees.

    Note that in order for the methods defined here to actually override those
    of the classes it's mixed with, it must go /first/ in the inheritance
    tree.  For example:

        class ClearIPDemo(ClearMixin,IPythonDemo): pass

    will provide an IPythonDemo class with the mixin's features.
    s    iN   s   *c         C   s   d Sd S(   s7   Blank marquee that returns '' no matter what the input.s    N(    (   s   selfs   txts   widths   mark(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   marqueeþ  s     c         C   s   t  i d ƒ d S(   sV   Method called before executing each block.

        This one simply clears the screen.s   clearN(   s   oss   system(   s   self(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   pre_cmd  s     (   s   __name__s
   __module__s   __doc__s   marquees   pre_cmd(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys
   ClearMixinï  s    s	   ClearDemoc           B   s   t  Z RS(   N(   s   __name__s
   __module__(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys	   ClearDemo	  s   s   ClearIPDemoc           B   s   t  Z RS(   N(   s   __name__s
   __module__(    (    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   ClearIPDemo  s   (   s   __doc__s
   exceptionss   oss   res   shlexs   syss   IPython.PyColorizes   Parsers   IPython.genutilss   marquees	   file_reads   file_readliness   __all__s	   Exceptions	   DemoErrors   re_marks   objects   Demos   IPythonDemos   LineDemos   IPythonLineDemos
   ClearMixins	   ClearDemos   ClearIPDemo(   s   file_readliness	   ClearDemos
   ClearMixins   __all__s   marquees   IPythonDemos	   file_reads   Parsers   syss   res   ClearIPDemos   IPythonLineDemos	   DemoErrors   shlexs   Demos
   exceptionss   LineDemos   oss   re_mark(    (    s=   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/demo.pys   ?›   s$   
						ÿ 