def getOutput(self, name=None, block="AUTO", file_link=False): """Return a link to a zip file of the working directory. Parameters ---------- name : str The name of the zip file. block : bool Whether to block until the process has finished running. file_link : bool Whether to return a FileLink when working in Jupyter. Returns ------- ouput : str, IPython.display.FileLink A path, or file link, to an archive of the process output. """ if name is None: name = self._name else: if type(name) is not str: raise TypeError("'name' must be of type 'str'") # Wait for the process to finish. if block is True: self.wait() elif block == "AUTO" and self._is_blocked: self.wait() # Generate the zip file name. zipname = "%s.zip" % name # Glob all of the output files. output = _glob.glob("%s/*" % self._work_dir) with _zipfile.ZipFile(zipname, "w") as zip: # Loop over all of the file outputs. for file in output: zip.write(file, arcname=_os.path.basename(file)) # Return a link to the archive. if _is_notebook(): if file_link: return _FileLink(zipname) else: return zipname # Return the path to the archive. else: return zipname
def getOutput(self, filename=None, file_link=False): """Return a zip file containing the output from the parameterisation process. Parameters ---------- filename : str The name to write the output to. file_link : bool Whether to return a FileLink when working in Jupyter. Returns ------- file_link : str, IPython.lib.display.FileLink The name of, or link to, a zipfile containing the output. """ if self._zipfile is None or filename is not None: if filename is not None: zipname = "%s.zip" % filename # If the user has passed a directory, make sure that is exists. if _os.path.basename(filename) != filename: dirname = _os.path.dirname(filename) # Create the directory if it doesn't already exist. if not _os.path.isdir(dirname): _os.makedirs(dirname, exist_ok=True) else: zipname = "%s.zip" % self._hash # Append the files to the archive. with _zipfile.ZipFile(zipname, "w") as zip: # Loop over all of the output files. for file in _glob.glob("%s/*" % self._work_dir): zip.write(file, arcname=_os.path.basename(file)) # Store the location of the zip file. Only do so if the # parameterisation has finished. if not self._is_finished: self._zipfile = zipname # Return a link to the archive. if _is_notebook: if file_link: return _FileLink(self._zipfile) else: return self._zipfile else: return self._zipfile
def getInput(self, name=None, file_link=False): """Return a link to a zip file containing the input files used by the process. Parameters ---------- name : str The name of the zip file. file_link : bool Whether to return a FileLink when working in Jupyter. Returns ------- ouput : str, IPython.display.FileLink A path, or file link, to an archive of the process input. """ if name is None: name = self._name + "_input" else: if type(name) is not str: raise TypeError("'name' must be of type 'str'") # Generate the zip file name. zipname = "%s.zip" % name with _zipfile.ZipFile(zipname, "w") as zip: # Loop over all of the file outputs. for file in self.inputFiles(): zip.write(file, arcname=_os.path.basename(file)) # Return a link to the archive. if _is_notebook: if file_link: return _FileLink(zipname) else: return zipname # Return the path to the archive. else: return zipname
def validate(self, file_prefix="output"): """Whether the output requirements are satisfied. Parameters ---------- file_prefix : str The prefix of the output file name. Returns ------- output : IPython.lib.display.FileLink, str If running interatvely: A link to a zipfile containing the validated output, else the name of a YAML file containing the node output. """ if type(file_prefix) is not str: raise TypeError("The 'file_prefix' keyword must be of type 'str'.") # Flag that we have validated output. self._is_output_validated = True # A list of File and FileSet outputs. file_outputs = [] # Check no outputs are None. for name, output in self._outputs.items(): if output.getValue() is None: self._errors.append("Missing output for requirement '%s'" % name) else: if type(output) is _File or type(output) is _FileSet: file_outputs.append(output) # Node failed. if len(self._errors) > 0: for error in self._errors: print("%s" % error, file=_sys.stderr) if self._name is not None: raise SystemExit("Node '%s' failed!" % self._name) else: raise SystemExit("Node failed!") # Create a compressed archive containing all file output for the node. if self._is_notebook: # There are files. if len(file_outputs) > 0: # Create the archive name. zipname = "%s.zip" % file_prefix # Append the files to the archive. with _zipfile.ZipFile(zipname, "w") as zip: # Loop over all of the file outputs. for output in file_outputs: if type(output) is _File: file = output.getValue() zip.write(file, arcname=_os.path.basename(file)) else: for file in output.getValue(): zip.write(file, arcname=_os.path.basename(file)) # Return a link to the archive. return _FileLink(zipname) else: # Initialise an empty dictionary to store the output data. data = {} # Create the YAML file name. yamlname = "%s.yaml" % file_prefix # Populate the dictionary. for name, output in self._outputs.items(): data[name] = output.getValue() # Write the outputs to a YAML file. with open(yamlname, "w") as file: _yaml.dump(data, file, default_flow_style=False) return yamlname