def edit_directory(astergui, directory): """ Edit Input or Output directory Arguments: astergui (AsterGui): AsterGui instance. directory (Directory): *Directory* object. """ if directory is None: return try: case = directory.case dir_type = directory.dir_type path = directory.directory is_in_dir = dir_type == Directory.InDir operation_name = translate("AsterStudy", "Set-up directories") ctr = Controller(operation_name, astergui) if ctr.controllerStart(): path = get_directory(astergui.mainWindow(), path, is_in_dir) if path: if is_in_dir: out_dir = case.out_dir if out_dir is not None and same_path(path, out_dir): message = translate( "DirsPanel", "Input and output " "directories cannot be the same") raise ValueError(message) if is_subpath(path, out_dir) or is_subpath(out_dir, path): message = translate( "DirsPanel", "Input and output " "directories cannot be sub-path " "of each other") raise ValueError(message) if not os.path.exists(path): message = translate( "DirsPanel", "Input directory '{}'" " does not exist").format(path) raise ValueError(message) else: in_dir = case.in_dir if in_dir is not None and same_path(path, in_dir): message = translate( "DirsPanel", "Input and output " "directories cannot be the same") raise ValueError(message) if is_subpath(path, in_dir) or is_subpath(in_dir, path): message = translate( "DirsPanel", "Input and output " "directories cannot be sub-path " "of each other") raise ValueError(message) directory.directory = path ctr.controllerCommit() astergui.study().commit(operation_name) astergui.update() else: ctr.controllerAbort()
def out_dir(self, out_dir): if out_dir is not None: out_dir = osp.realpath(out_dir) if out_dir is not None and self._in_dir is not None: if same_path(out_dir, self._in_dir): raise ValueError, "input and output dirs can't be the same" elif is_subpath(out_dir, self._in_dir): raise ValueError, "output dir can't be sub-path of input dir" elif is_subpath(self._in_dir, out_dir): raise ValueError, "output dir can't be parent of input dir" self._out_dir = out_dir
def in_dir(self, in_dir): if in_dir is not None: in_dir = osp.realpath(in_dir) if in_dir is not None and self._out_dir is not None: if same_path(in_dir, self._out_dir): raise ValueError, "input and output dirs can't be the same" elif is_subpath(in_dir, self._out_dir): raise ValueError, "input dir can't be sub-path of output dir" elif is_subpath(self._out_dir, in_dir): raise ValueError, "input dir can't be parent of output dir" if in_dir is not None and not osp.exists(in_dir): raise ValueError, "non-existent directory: '{}'".format(in_dir) self._in_dir = in_dir
def delete(self, delete_files=False): """ Remove directory from study. Arguments: delete_files (Optional[bool]): Forces removal of related data files from study. Defaults to *False*. """ directory = self.directory if directory is None: return with auto_dupl_on(self.case): if self.dir_type == Directory.InDir: self.case.in_dir = None else: self.case.out_dir = None if delete_files: for stage in self.case.stages: if stage.is_graphical_mode(): continue units = stage.handle2info.keys() for unit in units: file_obj = File(stage, unit) if is_subpath(file_obj.filename, directory): file_obj.delete()
def _add_file_item(stage, unit, stage_item, indir_item, outdir_item): """Add file item to the view model.""" filename = stage.handle2file(unit) file_item = HandleItem(File(stage, unit)) stage_item.appendChild(file_item) ref_item = None for dir_item in indir_item, outdir_item: directory = dir_item.dir if is_subpath(filename, directory): if behavior().join_similar_files: for i in range(dir_item.childCount()): item = dir_item.child(i) if item.filename == filename and item.unit == unit: ref_item = item break if ref_item is not None: ref_item.itemData().add_entry(stage, unit) else: file_unit = File(stage, unit) file_unit.forced_attr = FileAttr.In \ if dir_item is indir_item else FileAttr.Out ref_item = HandleItem(file_unit) dir_item.appendChild(ref_item) break return file_item, ref_item
def visible(self): """Redefined from TreeItem.""" root = self.root() hidden = False if root is not None: case = root.case # pragma pylint: disable=no-member in_dir = case.in_dir out_dir = case.out_dir hidden = isinstance(self.parent(), StageItem) and \ is_subpath(self.filename, (in_dir, out_dir))
def accept(self): """Redefined from *EditionWidget* class.""" in_dir = self.in_dir.text().strip() out_dir = self.out_dir.text().strip() if in_dir and out_dir: if same_path(in_dir, out_dir): message = translate( "DirsPanel", "Input and output " "directories cannot be the same") Q.QMessageBox.critical(self, "AsterStudy", message) return False if is_subpath(in_dir, out_dir) or is_subpath(out_dir, in_dir): message = translate( "DirsPanel", "Input and output directories" " cannot be sub-path of each other") Q.QMessageBox.critical(self, "AsterStudy", message) return False if not os.path.exists(in_dir): message = translate("DirsPanel", "Input directory '{}' " "does not exist").format(in_dir) Q.QMessageBox.critical(self, "AsterStudy", message) return False return True