def expand_path(cls, file_path: str, cwd: str = None) -> List[str]: """Expands path with globs and colons into a list of absolute paths""" cwd = cwd or os.getcwd() pathComponents = [PathComponents(path) for path in splitPath(file_path)] expanded_paths = [] missing_files = [] for components in pathComponents: if os.path.isabs(components.externalPath): externalPath = components.externalPath else: externalPath = os.path.join(cwd, components.externalPath) expanded_path = os.path.expanduser(externalPath) unglobbed_paths = glob.glob(expanded_path) if not unglobbed_paths: missing_files.append(expanded_path) continue for ext_path in unglobbed_paths: if not cls.fileHasInternalPaths(ext_path) or not components.internalPath: expanded_paths.append(ext_path) continue internal_paths = cls.globInternalPaths(ext_path, components.internalPath) expanded_paths.extend([os.path.join(ext_path, int_path) for int_path in internal_paths]) if missing_files: raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), os.path.pathsep.join(missing_files)) return sorted(p.replace("\\", "/") for p in expanded_paths)
def testSplitPath(self): pathsep = os.path.pathsep multipath1 = f"/some/file.txt{pathsep}http://example.com:5000{pathsep}/some/other/file" expected1 = [ "/some/file.txt", "http://example.com:5000", "/some/other/file" ] assert splitPath(multipath1) == expected1 multipath2 = ( f"http://example1.com:5000/some/endpoint{pathsep}http://example2/another/endpoint{pathsep}my/file.txt" ) expected2 = [ "http://example1.com:5000/some/endpoint", "http://example2/another/endpoint", "my/file.txt" ] assert splitPath(multipath2) == expected2
def setupOutputs(self): """ Inspect the file name and instantiate and connect an internal operator of the appropriate type. TODO: Handle datasets of non-standard (non-5d) dimensions. """ path_components = splitPath(self.FilePath.value) cwd = self.WorkingDirectory.value if self.WorkingDirectory.ready() else None abs_paths = [] for path in path_components: if isRelative(path): if cwd is None: return # FIXME: this mirrors old logic but I'm not sure if it's safe abs_paths.append(os.path.normpath(os.path.join(cwd, path)).replace("\\", "/")) else: abs_paths.append(path) filePath = os.path.pathsep.join(abs_paths) # Clean up before reconfiguring if self.internalOperators: self.Output.disconnect() self.opInjector.cleanUp() for op in self.internalOperators[::-1]: op.cleanUp() self.internalOperators = [] self.internalOutput = None if self._file is not None: self._file.close() openFuncs = [ self._attemptOpenAsKlb, self._attemptOpenAsUfmf, self._attemptOpenAsMmf, self._attemptOpenAsRESTfulPrecomputedChunkedVolume, self._attemptOpenAsDvidVolume, self._attemptOpenAsH5N5Stack, self._attemptOpenAsTiffStack, self._attemptOpenAsStack, self._attemptOpenAsH5N5, self._attemptOpenAsNpy, self._attemptOpenAsRawBinary, self._attemptOpenAsTiledVolume, self._attemptOpenAsH5BlockStore, self._attemptOpenAsBlockwiseFileset, self._attemptOpenAsRESTfulBlockwiseFileset, self._attemptOpenAsBigTiff, self._attemptOpenAsTiff, self._attemptOpenWithVigraImpex, ] # Try every method of opening the file until one works. iterFunc = openFuncs.__iter__() while not self.internalOperators: try: openFunc = next(iterFunc) except StopIteration: break self.internalOperators, self.internalOutput = openFunc(filePath) if self.internalOutput is None: raise RuntimeError("Can't read " + filePath + " because it has an unrecognized format.") # If we've got a ROI, append a subregion operator. if self.SubVolumeRoi.ready(): self._opSubRegion = OpSubRegion(parent=self) self._opSubRegion.Roi.setValue(self.SubVolumeRoi.value) self._opSubRegion.Input.connect(self.internalOutput) self.internalOutput = self._opSubRegion.Output self.opInjector = OpMetadataInjector(parent=self) self.opInjector.Input.connect(self.internalOutput) # Add metadata for estimated RAM usage if the internal operator didn't already provide it. if self.internalOutput.meta.ram_usage_per_requested_pixel is None: ram_per_pixel = self.internalOutput.meta.dtype().nbytes if "c" in self.internalOutput.meta.getTaggedShape(): ram_per_pixel *= self.internalOutput.meta.getTaggedShape()["c"] self.opInjector.Metadata.setValue({"ram_usage_per_requested_pixel": ram_per_pixel}) else: # Nothing to add self.opInjector.Metadata.setValue({}) # Directly connect our own output to the internal output self.Output.connect(self.opInjector.Output)
def is_stack(self) -> bool: return len(splitPath(self.effective_path)) > 1