##// END OF EJS Templates
Add sys.argv support for demos.
fperez -
Show More
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 583 2005-05-13 21:20:33Z fperez $"""
4 $Id: Magic.py 897 2005-09-22 09:32:46Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -21,7 +21,7 b' __license__ = Release.license'
21 21
22 22 # Python standard modules
23 23 import __builtin__
24 import os,sys,inspect,pydoc,re,tempfile,shlex,pdb,bdb,time
24 import os,sys,inspect,pydoc,re,tempfile,pdb,bdb,time
25 25 try:
26 26 import profile,pstats
27 27 except ImportError:
@@ -82,42 +82,6 b' def get_py_filename(name):'
82 82 else:
83 83 raise IOError,'File `%s` not found.' % name
84 84
85 # Try to use shlex.split for converting an input string into a sys.argv-type
86 # list. This appeared in Python 2.3, so here's a quick backport for 2.2.
87 try:
88 shlex_split = shlex.split
89 except AttributeError:
90 _quotesre = re.compile(r'[\'"](.*)[\'"]')
91 _wordchars = ('abcdfeghijklmnopqrstuvwxyz'
92 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?'
93 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
94 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ%s'
95 % os.sep)
96
97 def shlex_split(s):
98 """Simplified backport to Python 2.2 of shlex.split().
99
100 This is a quick and dirty hack, since the shlex module under 2.2 lacks
101 several of the features needed to really match the functionality of
102 shlex.split() in 2.3."""
103
104 lex = shlex.shlex(StringIO(s))
105 # Try to get options, extensions and path separators as characters
106 lex.wordchars = _wordchars
107 lex.commenters = ''
108 # Make a list out of the lexer by hand, since in 2.2 it's not an
109 # iterator.
110 lout = []
111 while 1:
112 token = lex.get_token()
113 if token == '':
114 break
115 # Try to handle quoted tokens correctly
116 quotes = _quotesre.match(token)
117 if quotes:
118 token = quotes.group(1)
119 lout.append(token)
120 return lout
121 85
122 86 #****************************************************************************
123 87 # Utility classes
@@ -10,19 +10,40 b" Sorry, but this uses Python 2.3 features, so it won't work in 2.2 environments."
10 10 #
11 11 #*****************************************************************************
12 12
13 import sys
13 14 import exceptions
14 15 import re
15 16
16 17 from IPython.PyColorize import Parser
17 from IPython.genutils import marquee
18 from IPython.genutils import marquee, shlex_split
18 19
19 20 class DemoError(exceptions.Exception): pass
20 21
21 22 class Demo:
22 def __init__(self,fname,mark_pause='# pause',mark_silent='# silent',
23 auto=False):
24 """The marks are turned into regexps which match them as standalone in
25 a line, with all leading/trailing whitespace ignored."""
23 def __init__(self,fname,arg_str='',mark_pause='# pause',
24 mark_silent='# silent',auto=False):
25 """Make a new demo object. To run the demo, simply call the object.
26
27 Inputs:
28
29 - fname = filename.
30
31 Optional inputs:
32
33 - arg_str(''): a string of arguments, internally converted to a list
34 just like sys.argv, so the demo script can see a similar
35 environment.
36
37 - mark_pause ('# pause'), mark_silent('# silent'): marks for pausing
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 confirmation. Note that silent blocks are always automatically
44 executed. This flag is an attribute of the object, and can be
45 changed at runtime simply by reassigning it.
46 """
26 47
27 48 self.fname = fname
28 49 self.mark_pause = mark_pause
@@ -30,6 +51,7 b' class Demo:'
30 51 self.mark_silent = mark_silent
31 52 self.re_silent = re.compile(r'^\s*%s\s*$' % mark_silent,re.MULTILINE)
32 53 self.auto = auto
54 self.sys_argv = shlex_split(arg_str)
33 55
34 56 # get a few things from ipython. While it's a bit ugly design-wise,
35 57 # it ensures that things like color scheme and the like are always in
@@ -134,8 +156,12 b' class Demo:'
134 156 if ans:
135 157 print marquee('Block NOT executed')
136 158 return
137
138 exec next_block in self.user_ns
159 try:
160 save_argv = sys.argv
161 sys.argv = self.sys_argv
162 exec next_block in self.user_ns
163 finally:
164 sys.argv = save_argv
139 165
140 166 except:
141 167 self.ip_showtraceback(filename=self.fname)
@@ -5,7 +5,7 b' General purpose utilities.'
5 5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 6 these things are also convenient when working at the command line.
7 7
8 $Id: genutils.py 894 2005-09-22 07:16:18Z fperez $"""
8 $Id: genutils.py 897 2005-09-22 09:32:46Z fperez $"""
9 9
10 10 #*****************************************************************************
11 11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -24,6 +24,7 b' __license__ = Release.license'
24 24 # required modules
25 25 import __main__
26 26 import types,commands,time,sys,os,re,shutil
27 import shlex
27 28 import tempfile
28 29 from IPython.Itpl import Itpl,itpl,printpl
29 30 from IPython import DPyGetOpt
@@ -51,6 +52,43 b' except NameError:'
51 52 __builtin__.False = False
52 53 __builtin__.enumerate = enumerate
53 54
55 # Try to use shlex.split for converting an input string into a sys.argv-type
56 # list. This appeared in Python 2.3, so here's a quick backport for 2.2.
57 try:
58 shlex_split = shlex.split
59 except AttributeError:
60 _quotesre = re.compile(r'[\'"](.*)[\'"]')
61 _wordchars = ('abcdfeghijklmnopqrstuvwxyz'
62 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?'
63 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
64 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ%s'
65 % os.sep)
66
67 def shlex_split(s):
68 """Simplified backport to Python 2.2 of shlex.split().
69
70 This is a quick and dirty hack, since the shlex module under 2.2 lacks
71 several of the features needed to really match the functionality of
72 shlex.split() in 2.3."""
73
74 lex = shlex.shlex(StringIO(s))
75 # Try to get options, extensions and path separators as characters
76 lex.wordchars = _wordchars
77 lex.commenters = ''
78 # Make a list out of the lexer by hand, since in 2.2 it's not an
79 # iterator.
80 lout = []
81 while 1:
82 token = lex.get_token()
83 if token == '':
84 break
85 # Try to handle quoted tokens correctly
86 quotes = _quotesre.match(token)
87 if quotes:
88 token = quotes.group(1)
89 lout.append(token)
90 return lout
91
54 92 #****************************************************************************
55 93 # Exceptions
56 94 class Error(Exception):
@@ -1,7 +1,11 b''
1 1 2005-09-22 Fernando Perez <Fernando.Perez@colorado.edu>
2 2
3 * IPython/genutils.py (shlex_split): moved from Magic to here,
4 where all 2.2 compatibility stuff lives. I needed it for demo.py.
5
3 6 * IPython/demo.py (Demo.__init__): added support for silent
4 7 blocks, improved marks as regexps, docstrings written.
8 (Demo.__init__): better docstring, added support for sys.argv.
5 9
6 10 * IPython/genutils.py (marquee): little utility used by the demo
7 11 code, handy in general.
General Comments 0
You need to be logged in to leave comments. Login now