1 """
2 Support functions for parsing command-line arguments and providing
3 the Topographica command prompt. Typically called from the
4 './topographica' script, but can be called directly if using
5 Topographica files within a separate Python.
6
7 $Id: commandline.py 9177 2008-09-10 15:51:38Z jbednar $
8 """
9 __version__='$Revision: 9177 $'
10
11
12 from optparse import OptionParser
13
14 import sys, __main__, math, os, re
15
16 import topo
17
18 matplotlib_imported=False
19 try:
20
21 import matplotlib
22 matplotlib_imported=True
23 from matplotlib import rcParams
24 rcParams['backend']='Agg'
25 except ImportError:
26 print "Warning: Could not import matplotlib; pylab plots will not work."
27
28
29 BANNER = """
30 Welcome to Topographica!
31
32 Type help() for interactive help with python, help(topo) for general
33 information about Topographica, help(commandname) for info on a
34 specific command, or topo.about() for info on this release, including
35 licensing information.
36 """
43 """
44 Allows control over IPython's dynamic command prompts.
45 """
46 _format = ''
47 _prompt = ''
48
49 @classmethod
60
61 @classmethod
67
70 """
71 Control over input prompt.
72
73 Several predefined formats are provided, and any of these (or any
74 arbitrary string) can be used by calling set_format() with their
75 values.
76
77 See the IPython manual for details:
78 http://ipython.scipy.org/doc/manual/html/config/index.html
79
80 Examples:
81 # Use one of the predefined formats:
82 CommandPrompt.set_format(CommandPrompt.basic_format)
83 # Just print the command number:
84 CommandPrompt.set_format('\# ')
85 # Print the command number but don't use color:
86 CommandPrompt.set_format('\N ')
87 # Print the value of my_var at each prompt:
88 CommandPrompt.set_format('${my_var}>>> ')
89 """
90 _prompt = 'prompt1'
91
92
93 basic_format = 'Topographica>>> '
94 simtime_format = 'topo_t${topo.sim.timestr()}>>> '
95 simtimecmd_format = 'topo_t${topo.sim.timestr()}_c\\#>>> '
96
97 _format = simtimecmd_format
98
109
120
121
122
123
124
125
126 global_constants = {'pi':math.pi}
127
128
129 usage = "usage: topographica ([<option>]:[<filename>])*\n\
130 where any combination of options and Python script filenames will be\n\
131 processed in order left to right."
132 topo_parser = OptionParser(usage=usage)
136 """
137 Set the simulation title from the given filename, if none has been
138 set already.
139 """
140 if topo.sim.name is None:
141 topo.sim.name=re.sub('.ty$','',os.path.basename(filename))
142
145 """Callback function for boolean-valued options that apply to the entire run."""
146
147 setattr(parser.values,option.dest,True)
148
151 os.environ['PYTHONINSPECT']='1'
152
153
154
155 -def i_action(option,opt_str,value,parser):
159
160 topo_parser.add_option("-i","--interactive",action="callback",callback=i_action,
161 dest="interactive",default=False,
162 help="provide an interactive prompt even if stdin does not appear to be a terminal.")
163
164
165 -def v_action(option,opt_str,value,parser):
170
171 topo_parser.add_option("-v","--verbose",action="callback",callback=v_action,dest="verbose",default=False,help="""\
172 enable verbose messaging output.""")
173
174
175 -def d_action(option,opt_str,value,parser):
180
181 topo_parser.add_option("-d","--debug",action="callback",callback=d_action,dest="debug",default=False,help="""\
182 enable debugging message output (implies --verbose).""")
183
184
185
186 -def l_action(option,opt_str,value,parser):
192
193 topo_parser.add_option("-l","--legacy",action="callback",callback=l_action,dest="legacy",default=False,help="""\
194 launch Topographica with legacy support enabled.""")
195
196
197 -def gui(start=True):
206
216
222
223
224
225
226
227 -def g_action(option,opt_str,value,parser):
232
233
234 topo_parser.add_option("-g","--gui",action="callback",callback=g_action,dest="gui",default=False,help="""\
235 launch an interactive graphical user interface; \
236 equivalent to -c 'from topo.misc.commandline import gui ; gui()'. \
237 Implies -a.""")
238
239
240
241 -def G_action(option,opt_str,value,parser):
246
247
248 topo_parser.add_option("-G","--more_gui",action="callback",callback=G_action,dest="more_gui",default=False,help="""\
249 EXPERIMENTAL: launch a more interactive graphical user interface...; \
250 Implies -a.""")
263
264 topo_parser.add_option("-c","--command",action = "callback",callback=c_action,type="string",
265 default=[],dest="commands",metavar="\"<command>\"",
266 help="string of arbitrary Python code to be executed in the main namespace.")
270 """Import the contents of all files in the topo/command/ directory."""
271 import re,os
272 from filepath import application_path
273 import __main__
274
275 for f in os.listdir(os.path.join(application_path,"topo/command")):
276 if re.match('^[^_].*\.py$',f):
277 modulename = re.sub('\.py$','',f)
278 exec "from topo.command."+modulename+" import *" in __main__.__dict__
279
280 -def a_action(option,opt_str,value,parser):
283
284 topo_parser.add_option("-a","--auto-import-commands",action="callback",callback=a_action,help="""\
285 import everything from commands/*.py into the main namespace, for convenience; \
286 equivalent to -c 'from topo.misc.commandline import auto_import_commands ; auto_import_commands()'.""")
291 """
292 Execute startup files, looking at appropriate locations for many different platforms.
293 """
294 home = os.path.expanduser("~")
295 appdata = os.path.expandvars("$APPDATA")
296 appsupport = os.path.join(home,"Library","Application Support")
297
298 rcpath = os.path.join(home,'.topographicarc')
299 inipath = os.path.join(appdata,'Topographica','topographica.ini')
300 configpath = os.path.join(appsupport,'Topographica','topographica.config')
301
302 for startup_file in (rcpath,configpath,inipath):
303 if os.path.exists(startup_file):
304 print "Executing user startup file %s" % (startup_file)
305 execfile(startup_file,__main__.__dict__)
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337 -def process_argv(argv):