def process_argv(argv): """ Process command-line arguments (minus argv[0]!), rearrange and execute. """ # Initial preparation import __main__ for (k, v) in global_constants.items(): exec '%s = %s' % (k, v) in __main__.__dict__ # Allow param.normalize_path.prefix to be overridden in the # startup files, but otherwise force it to exist before doing # anything else param.normalize_path.prefix = default_output_path() exec_startup_files() set_output_path(param.normalize_path.prefix) # Tell the user how many cores are in use, if available openmp_main = Parameterized( name="OpenMP") # Dummy object just for messages try: import os, multiprocessing total_cores = multiprocessing.cpu_count() num_threads = int(os.environ.get('OMP_NUM_THREADS', total_cores)) openmp_main.verbose( "Using %d threads on a machine with %d detected CPUs", num_threads, total_cores) except: pass # Repeatedly process options, if any, followed by filenames, if any, until nothing is left topo_parser.disable_interspersed_args() args = argv option = None global something_executed while True: # Process options up until the first filename (option, args) = topo_parser.parse_args(args, option) # Handle filename if args: filename = args.pop(0) #print "Executing %s" % (filename) filedir = os.path.dirname(os.path.abspath(filename)) sys.path.insert( 0, filedir) # Allow imports relative to this file's path sim_name_from_filename(filename) # Default value of topo.sim.name execfile(filename, __main__.__dict__) something_executed = True if not args: break global_params.check_for_unused_names() # If no scripts and no commands were given, pretend -i was given. if not something_executed: interactive() if option.gui: topo.guimain.title(topo.sim.name) ## INTERACTIVE SESSION BEGINS HERE (i.e. can't have anything but ## some kind of cleanup code afterwards) if os.environ.get('PYTHONINSPECT'): print "Output path: %s" % param.normalize_path.prefix print BANNER # CBALERT: should probably allow a way for users to pass # things to IPython? Or at least set up some kind of # topographica ipython config file. Right now, a topo_parser # option has to be added for every ipython option we want to # support (e.g. see --pdb) if ipython_shell_interface == "IPython.Shell": # IPython 0.10 and earlier # Stop IPython namespace hack? # http://www.nabble.com/__main__-vs-__main__-td14606612.html __main__.__name__ = "__mynamespace__" ipython_args = [ '-noconfirm_exit', '-nobanner', '-pi1', CommandPrompt.get_format(), '-pi2', CommandPrompt2.get_format(), '-po', OutputPrompt.get_format() ] if option.pdb: ipython_args.append('-pdb') ipshell = IPShell(ipython_args, user_ns=__main__.__dict__) ipshell.mainloop(sys_exit=1) elif ipython_shell_interface == "InteractiveShellEmbed": # IPython 0.11 and later config = Config() if ipython_prompt_interface == "PromptManager": config.PromptManager.in_template = CommandPrompt.get_format() config.PromptManager.in2_template = CommandPrompt2.get_format() config.PromptManager.out_template = OutputPrompt.get_format() else: config.InteractiveShell.prompt_in1 = CommandPrompt.get_format() config.InteractiveShell.prompt_in2 = CommandPrompt2.get_format( ) config.InteractiveShell.prompt_out = OutputPrompt.get_format() config.InteractiveShell.confirm_exit = False ipshell = IPShell(config=config, user_ns=__main__.__dict__, banner1="", exit_msg="") if option.pdb: ipshell.call_pdb = True # Load Topographica IPython extension in embedded shell try: ipshell.extension_manager.load_extension('topo.misc.ipython') except: cmdline_main.warning( "Could not load IPython extension 'topo.misc.ipython'; ignored error was:\n%s" % traceback.format_exc()) ipshell() global return_code if return_code != 0: cmdline_main.warning( "Errors encountered; exiting with return code %d" % return_code) sys.exit(return_code)
def __setstate__(self,state): """ Execute the startup commands and set class attributes. """ self.startup_commands = state['startup_commands'] for cmd in self.startup_commands: exec cmd in __main__.__dict__ to_restore = {} ########## pre-processing (renames, moves, etc) for class_path,state in state['class_attributes'].items(): # from e.g. "topo.base.parameter.Parameter", we want "topo.base.parameter" if class_path in self.do_not_restore: #print "Did not restore:",class_path break for p_name,p_obj in state.items(): if p_name in self.param_moves.get(class_path,{}): assert p_name not in self.param_name_changes.get(class_path,{}) new_class_path,new_p_name = self.param_moves[class_path][p_name] if new_class_path not in to_restore: to_restore[new_class_path] = {} Parameterized().message("%s.%s has been moved to %s.%s"%(class_path,p_name,new_class_path,new_p_name)) assert new_p_name not in to_restore[new_class_path] to_restore[new_class_path][new_p_name]=p_obj elif p_name in self.param_name_changes.get(class_path,{}): new_p_name = self.param_name_changes[class_path][p_name] if class_path not in to_restore: to_restore[class_path] = {} Parameterized().message("%s's %s parameter has been renamed to %s."%(class_path,p_name,new_p_name)) to_restore[class_path][new_p_name] = p_obj else: if class_path not in to_restore: to_restore[class_path] = {} to_restore[class_path][p_name]= p_obj ########## restoring for class_path in to_restore: module_path = class_path[0:class_path.rindex('.')] class_name = class_path[class_path.rindex('.')+1::] try: module = __import__(module_path,fromlist=[module_path]) except: Parameterized().warning("Could not find module '%s' to restore parameter values of '%s' (module might have been moved or renamed; if you are using this module, please file a support request via topographica.org"%(module_path,class_path)) break try: class_=getattr(module,class_name) except: Parameterized().warning("Could not find class '%s' to restore its parameter values (class might have been removed or renamed; if you are using this class, please file a support request via topographica.org)."%class_path) break for p_name,p_obj in to_restore[class_path].items(): if p_name not in class_.params(): # CEBALERT: GlobalParams's source code never has # parameters. If we move Parameter saving and # restoring to Parameterized, could allow # individual classes to customize Parameter # restoration. if class_.__name__!='GlobalParams': Parameterized(name='load_snapshot').warning("%s.%s found in snapshot, but '%s' is no longer defined as a Parameter by the current version of %s. If you are using this class, please file a support request via topographica.org." % (class_.__name__, p_name,p_name,class_.__name__)) else: setattr(class_,p_name,p_obj)
from param import parameterized from param.parameterized import Parameterized from topo.base.simulation import OptionalSingleton try: # By default, use a non-GUI backend for matplotlib. from matplotlib import pyplot as plt plt.switch_backend('agg') matplotlib_imported = True except ImportError: matplotlib_imported = False # Dummy object just for messages cmdline_main = Parameterized(name="CommandLine") ipython_shell_interface = None ipython_prompt_interface = None try: try: from IPython.terminal.embed import InteractiveShellEmbed as IPShell except ImportError: # Prior to IPython 1.0, InteractiveShellEmbed was found in the frontend package from IPython.frontend.terminal.embed import InteractiveShellEmbed as IPShell # pyflakes:ignore (try/except import) from IPython.config.loader import Config ipython_shell_interface = "InteractiveShellEmbed" try: from IPython.core.prompts import PromptManager # pyflakes:ignore (try/except import) ipython_prompt_interface = "PromptManager" except ImportError: pass
launch an interactive graphical user interface; \ equivalent to -c 'from topo.misc.commandline import gui ; gui()'. \ Implies -a.""") topo_parser.add_option("--pdb", action="store_true", dest="pdb", help="""\ Automatically call the pdb debugger after every uncaught \ exception. See IPython documentation for further details.""") # Keeps track of whether something has been performed, when deciding whether to assume -i something_executed = False # Dummy object used for user messages about OpenMP openmp_main = Parameterized(name="OpenMP") def c_action(option, opt_str, value, parser): """Callback function for the -c option.""" #print "Processing %s '%s'" % (opt_str,value) exec value in __main__.__dict__ global something_executed something_executed = True openmp_settings_names = [ 'openmp_threads', 'openmp_min_threads', 'openmp_max_threads' ] openmp_present = [ True for k in openmp_settings_names if (k in __main__.__dict__) ] if openmp_present and parser.values.gui: