def magic_ganga(self, parameter_s=''): """ IPython magic function for executing Python scripts in Ganga namespace Usage: ganga <script> <arguments> <script> - Python script in Ganga search path <arguments> - Arguments to be passed to <script> """ # Obtain list of arguments from input parameter string argList = parameter_s.split() # Determine path to script, using Ganga search rules if argList: path = getSearchPath() script = getScriptPath(argList[0], path) # Use mechanism based on that used in magic_run function of IPython # for executing script if script: save_argv = sys.argv sys.argv = [script] + argList[1:] prog_ns = self.shell.user_ns runner = self.shell.safe_execfile runner(script, prog_ns, prog_ns) sys.argv = save_argv else: logger.warning("Script '%s' not found in search path '%s'" % (argList[0], path)) else: logger.info(magic_ganga.__doc__) return None
def load(filename="", returnList=True): """Function to load previously exported Ganga objects Arguments: filename - String [default ''] giving path to a file containing definitions of Ganga objects, such as produced with the 'export' function => The path can be absolute, relative, or relative to a directory in the search path LOAD_SCRIPTS, defined in [Configuration] section of Ganga configuration returnList - Switch [default True] defining the return type: => True : List of loaded objects is returned => False : None returned - job and template objects stored in job repository Return value: List of Ganga objects or None, as determined by value of argument returnList """ #logger.info("Loading: %s" % str(filename)) if returnList: returnValue = [] else: returnValue = None if not filename: logger.info("Usage:") logger.info("load( '<filename>', [ <returnList> ] )") logger.info("See also: 'help( load )'") return returnValue searchPath = getSearchPath("LOAD_PATH") filepath = getScriptPath(filename, searchPath) try: with open(filepath) as inFile: lineList = [] for line in inFile: if (line.strip().startswith("#Ganga#")): lineList.append("#Ganga#") else: lineList.append(line) itemList = ("".join(lineList)).split("#Ganga#") except IOError: logger.error("Unable to open file %s" % (str(filename))) logger.error("No objects loaded") return returnValue objectList = [] for item in itemList: item = item.strip() if item: try: this_object = eval(str(item), Ganga.GPI.__dict__) objectList.append(this_object) except NameError as x: logger.exception(x) logger.warning("Unable to load object with definition %s" % item) logger.warning("Required plug-ins may not be available") if not objectList: logger.warning("Unable to load any objects from file %s" % filename) if returnList: returnValue = objectList return returnValue