##// END OF EJS Templates
Add sys.argv support for demos.
fperez -
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 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 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -21,7 +21,7 b' __license__ = Release.license'
21
21
22 # Python standard modules
22 # Python standard modules
23 import __builtin__
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 try:
25 try:
26 import profile,pstats
26 import profile,pstats
27 except ImportError:
27 except ImportError:
@@ -82,42 +82,6 b' def get_py_filename(name):'
82 else:
82 else:
83 raise IOError,'File `%s` not found.' % name
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 # Utility classes
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 import exceptions
14 import exceptions
14 import re
15 import re
15
16
16 from IPython.PyColorize import Parser
17 from IPython.PyColorize import Parser
17 from IPython.genutils import marquee
18 from IPython.genutils import marquee, shlex_split
18
19
19 class DemoError(exceptions.Exception): pass
20 class DemoError(exceptions.Exception): pass
20
21
21 class Demo:
22 class Demo:
22 def __init__(self,fname,mark_pause='# pause',mark_silent='# silent',
23 def __init__(self,fname,arg_str='',mark_pause='# pause',
23 auto=False):
24 mark_silent='# silent',auto=False):
24 """The marks are turned into regexps which match them as standalone in
25 """Make a new demo object. To run the demo, simply call the object.
25 a line, with all leading/trailing whitespace ignored."""
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 self.fname = fname
48 self.fname = fname
28 self.mark_pause = mark_pause
49 self.mark_pause = mark_pause
@@ -30,6 +51,7 b' class Demo:'
30 self.mark_silent = mark_silent
51 self.mark_silent = mark_silent
31 self.re_silent = re.compile(r'^\s*%s\s*$' % mark_silent,re.MULTILINE)
52 self.re_silent = re.compile(r'^\s*%s\s*$' % mark_silent,re.MULTILINE)
32 self.auto = auto
53 self.auto = auto
54 self.sys_argv = shlex_split(arg_str)
33
55
34 # get a few things from ipython. While it's a bit ugly design-wise,
56 # get a few things from ipython. While it's a bit ugly design-wise,
35 # it ensures that things like color scheme and the like are always in
57 # it ensures that things like color scheme and the like are always in
@@ -134,8 +156,12 b' class Demo:'
134 if ans:
156 if ans:
135 print marquee('Block NOT executed')
157 print marquee('Block NOT executed')
136 return
158 return
137
159 try:
138 exec next_block in self.user_ns
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 except:
166 except:
141 self.ip_showtraceback(filename=self.fname)
167 self.ip_showtraceback(filename=self.fname)
@@ -5,7 +5,7 b' General purpose utilities.'
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
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 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
@@ -24,6 +24,7 b' __license__ = Release.license'
24 # required modules
24 # required modules
25 import __main__
25 import __main__
26 import types,commands,time,sys,os,re,shutil
26 import types,commands,time,sys,os,re,shutil
27 import shlex
27 import tempfile
28 import tempfile
28 from IPython.Itpl import Itpl,itpl,printpl
29 from IPython.Itpl import Itpl,itpl,printpl
29 from IPython import DPyGetOpt
30 from IPython import DPyGetOpt
@@ -51,6 +52,43 b' except NameError:'
51 __builtin__.False = False
52 __builtin__.False = False
52 __builtin__.enumerate = enumerate
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 # Exceptions
93 # Exceptions
56 class Error(Exception):
94 class Error(Exception):
@@ -1,7 +1,11 b''
1 2005-09-22 Fernando Perez <Fernando.Perez@colorado.edu>
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 * IPython/demo.py (Demo.__init__): added support for silent
6 * IPython/demo.py (Demo.__init__): added support for silent
4 blocks, improved marks as regexps, docstrings written.
7 blocks, improved marks as regexps, docstrings written.
8 (Demo.__init__): better docstring, added support for sys.argv.
5
9
6 * IPython/genutils.py (marquee): little utility used by the demo
10 * IPython/genutils.py (marquee): little utility used by the demo
7 code, handy in general.
11 code, handy in general.
General Comments 0
You need to be logged in to leave comments. Login now