def __load_tests_from_directory(self, evt): folder = self.__get_safe_path_from_root_folder(RootFolder.TESTS_FOLDER) if folder: dialog = DirDialog(self, defaultPath=folder, style=DD_DIR_MUST_EXIST) if dialog.ShowModal() == ID_OK: self.__load_tests_to_tree(dir_path=dialog.GetPath()) else: show_dialog_path_doesnt_exist(self, folder)
def get_path(): app = App(None) style = FD_SAVE dialog = DirDialog(None, "Save to...", "", DD_DEFAULT_STYLE | DD_DIR_MUST_EXIST) if dialog.ShowModal() == ID_OK: path = dialog.GetPath() else: path = "" print("No directory selected, saving to current directory.") dialog.Destroy() return path
def _on_select_backup_dir(self, _): if not hasattr(self, "dlgBackupDir"): self.dlgBackupDir = DirDialog(self.master, "Choose directory for backup", "", DD_DEFAULT_STYLE | DD_DIR_MUST_EXIST ) cur = self.backupDir.GetValue() if cur: self.dlgBackupDir.SetPath(cur) if self.dlgBackupDir.ShowModal() == ID_OK: self.backupDir.SetValue(self.dlgBackupDir.GetPath())
def _on_select_save_dir(self, _): if not hasattr(self, "dlgSaveDir"): self.dlgSaveDir = DirDialog(self.master, "Choose directory of save data", "", DD_DEFAULT_STYLE | DD_DIR_MUST_EXIST ) cur = self.saveDir.GetValue() if cur: self.dlgSaveDir.SetPath(cur) if self.dlgSaveDir.ShowModal() == ID_OK: self.saveDir.SetValue(self.dlgSaveDir.GetPath())
def button_callback(self, event) -> None: """ Receive selection event after directory is selected, and pass to callback function :param event: Event contains directory path :return: None """ with DirDialog(parent=self, message=self._text_title, defaultPath="") as dialog: if dialog.ShowModal() == ID_CANCEL: return # The user changed their mind path = dialog.Path # Display path if self._reset: self._text_input.SetValue("") else: self._text_input.SetValue(path) self._callback(path)
def _askForDirectoryImport(self): """ Called by plugin to ask which directory must be imported """ aDirectory = DirDialog(self._umlFrame, "Choose a directory to import", defaultPath=self._ctrl.getCurrentDir()) # TODO : add this when supported...(cd) style=wx.DD_NEW_DIR_BUTTON) if aDirectory.ShowModal() == ID_CANCEL: aDirectory.Destroy() return "" else: directory = aDirectory.GetPath() self._ctrl.setCurrentDir(directory) aDirectory.Destroy() return directory
def _askForDirectoryExport(self, preferredDefaultPath: str = None): """ Called by plugin to ask for an output directory """ if preferredDefaultPath is None: defaultPath: str = self._ctrl.getCurrentDir() else: defaultPath = preferredDefaultPath dirDialog = DirDialog(self._umlFrame, "Choose a destination directory", defaultPath=defaultPath) if dirDialog.ShowModal() == ID_CANCEL: dirDialog.Destroy() return "" else: directory = dirDialog.GetPath() self._ctrl.setCurrentDir(directory) dirDialog.Destroy() return directory
def __set_root_folder(self, evt): dialog = DirDialog(self) if dialog.ShowModal() == ID_OK: path = dialog.GetPath() RootFolder.prepare_folder(path) self.__txt_root_path.SetValue(path)
class SaveSettings(object): def __init__(self, master, saveDirVal = None, backupDirVal = None): self.master = master saveDirSizer = BoxSizer(HORIZONTAL) self.saveDir = TextCtrl(master, size = (600, -1) ) if saveDirVal: self.saveDir.SetValue(saveDirVal) saveDirSizer.Add(StaticText(master, label = "Save directory"), 0, EXPAND ) saveDirSizer.Add(self.saveDir, 1, EXPAND) selectSaveDir = Button(master, -1, "Select") saveDirSizer.Add(selectSaveDir, 0, EXPAND) master.Bind(EVT_BUTTON, self._on_select_save_dir, selectSaveDir) openSave = Button(master, label = "Open") saveDirSizer.Add(openSave, 0, EXPAND) master.Bind(EVT_BUTTON, self._on_open_save_dir, openSave) hide = Button(master, label = "Hide") saveDirSizer.Add(hide, 0, EXPAND) master.Bind(EVT_BUTTON, self._on_hide, hide) backupDirSizer = BoxSizer(HORIZONTAL) self.backupDir = TextCtrl(master) if backupDirVal: self.backupDir.SetValue(backupDirVal) backupDirSizer.Add(StaticText(master, label = "Backup directory"), 0, EXPAND ) backupDirSizer.Add(self.backupDir, 1, EXPAND) switch = Button(master, label = "Switch") master.Bind(EVT_BUTTON, self._on_switch, switch) backupDirSizer.Add(switch, 0, EXPAND) override = Button(master, label = "Overwrite") master.Bind(EVT_BUTTON, self._on_overwrite, override) backupDirSizer.Add(override, 0, EXPAND) selectBackupDir = Button(master, -1, "Select") master.Bind(EVT_BUTTON, self._on_select_backup_dir, selectBackupDir) backupDirSizer.Add(selectBackupDir, 0, EXPAND) openBackup = Button(master, label = "Open") backupDirSizer.Add(openBackup, 0, EXPAND) master.Bind(EVT_BUTTON, self._on_open_backup_dir, openBackup) filterOutSizer = BoxSizer(HORIZONTAL) filterOutSizer.Add(StaticText(master, label = "Filter Out"), 0, EXPAND) self.filterOut = TextCtrl(master) filterOutSizer.Add(self.filterOut, 1, EXPAND) self.cbMonitor = CheckBox(master, label = "Monitor") master.Bind(EVT_CHECKBOX, self._on_monitor, self.cbMonitor) self.sizer = sizer = BoxSizer(VERTICAL) sizer.Add(saveDirSizer, 0, EXPAND) sizer.Add(backupDirSizer, 0, EXPAND) sizer.Add(filterOutSizer, 0, EXPAND) sizer.Add(self.cbMonitor, 0, EXPAND) self.settingsWidgets = [ selectSaveDir, self.saveDir, self.backupDir, switch, selectBackupDir, self.filterOut ] def _on_overwrite(self, _): self.ask_and_overwrite() def ask_and_overwrite(self): backupDir = self.backupDir.GetValue() savePath = self.saveDir.GetValue() if not (isdir(backupDir) and bool(savePath)): with MessageDialog(self.master, "Set paths up!", "Error") as dlg: dlg.ShowModal() return False repo = Repo(backupDir) if repo.is_dirty(): with MessageDialog(self.master, "Backup repository '%s' is dirty" % backupDir, "Error") as dlg: dlg.ShowModal() return False active_branch = repo.active_branch try: c = active_branch.commit except Exception as e: hint = "" try: if active_branch.name == "master": hint = "Is backup empty?" except: pass with MessageDialog(self.master, str(e) + "\n" + hint, "Error") as dlg: dlg.ShowModal() return False label = commit_time_str(c) + " | " + c.message dlg = MessageDialog(self.master, "Do you want to overwrite save data with current version?\n\n" + "SHA1: %s\n\n%s\n\n" % (c.hexsha, label) + "Files in save directory will be overwritten!", "Confirmation is required", YES_NO ) switch = dlg.ShowModal() == ID_YES dlg.Destroy() if not switch: return False self._switch_to(c) return True def _on_switch(self, _): backupDir = self.backupDir.GetValue() if not isdir(backupDir): return with BackupSelector(self.master, backupDir) as dlg: res = dlg.ShowModal() if res != ID_OK: return target = dlg.target try: self._switch_to(target) except BaseException as e: with MessageDialog(self.master, str(e), "Error") as dlg: dlg.ShowModal() def _switch_to(self, target): repo = Repo(self.backupDir.GetValue()) if repo.is_dirty(): raise RuntimeError("Backup repository is dirty") active = repo.active_branch cur = active.commit if cur.hexsha != target.hexsha: # select name for backup branch backups = [] need_head = True for h in repo.heads: mi = backup_re.match(h.name) if mi: backups.append(int(mi.group(1), base = 10)) if h.commit.hexsha == cur.hexsha: need_head = False if backups: n = max(backups) + 1 else: n = 0 # TODO: do not set branch if commits are reachable (other # branch exists) # setup backup branch and checkout new version if need_head: back_head = repo.create_head("backup_%u" % n, cur) try: active.commit = target try: active.checkout(True) except: active.commit = cur raise except: if need_head: repo.delete_head(back_head) raise save_path = self.saveDir.GetValue() # remove files of current stack = [cur.tree] while stack: node = stack.pop() for b in node.blobs: b_path = join(save_path, b.path) if exists(b_path): remove(b_path) stack.extend(node.trees) # copy files from target stack = [target.tree] while stack: node = stack.pop() for b in node.blobs: b_path = join(save_path, b.path) makedirs(dirname(b_path), exist_ok = True) with open(b_path, "wb+") as f: b.stream_data(f) stack.extend(node.trees) def _open_dir(self, path): if exists(path): open_directory_in_explorer(path) def _on_open_save_dir(self, _): self._open_dir(self.saveDir.GetValue()) def _on_open_backup_dir(self, _): self._open_dir(self.backupDir.GetValue()) def _on_select_save_dir(self, _): if not hasattr(self, "dlgSaveDir"): self.dlgSaveDir = DirDialog(self.master, "Choose directory of save data", "", DD_DEFAULT_STYLE | DD_DIR_MUST_EXIST ) cur = self.saveDir.GetValue() if cur: self.dlgSaveDir.SetPath(cur) if self.dlgSaveDir.ShowModal() == ID_OK: self.saveDir.SetValue(self.dlgSaveDir.GetPath()) def _on_select_backup_dir(self, _): if not hasattr(self, "dlgBackupDir"): self.dlgBackupDir = DirDialog(self.master, "Choose directory for backup", "", DD_DEFAULT_STYLE | DD_DIR_MUST_EXIST ) cur = self.backupDir.GetValue() if cur: self.dlgBackupDir.SetPath(cur) if self.dlgBackupDir.ShowModal() == ID_OK: self.backupDir.SetValue(self.dlgBackupDir.GetPath()) def _enable_settings(self): for w in self.settingsWidgets: w.Enable(True) def _disable_settings(self): for w in self.settingsWidgets: w.Enable(False) def _on_monitor(self, _): root = self.saveDir.GetValue() backup = self.backupDir.GetValue() root2threads = self.master.root2threads # See: http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html if self.cbMonitor.IsChecked(): self._disable_settings() if not root: dlg = MessageDialog(self.master, "Pleas select save directory", "Error" ) dlg.ShowModal() dlg.Destroy() self.cbMonitor.SetValue(False) self._enable_settings() return if not exists(root): dlg = MessageDialog(self.master, "No such directory '%s'" % root, "Error" ) dlg.ShowModal() dlg.Destroy() self.cbMonitor.SetValue(False) self._enable_settings() return if not backup: dlg = MessageDialog(self.master, "Pleas select backup directory", "Error" ) dlg.ShowModal() dlg.Destroy() self.cbMonitor.SetValue(False) self._enable_settings() return if root in root2threads: return # already monitored if not exists(backup): dlg = MessageDialog(self.master, "Directory '%s' does not exist. Create?" % backup, "Create backup directory", YES_NO ) res = dlg.ShowModal() dlg.Destroy() if not res: self.cbMonitor.SetValue(False) self._enable_settings() return makedirs(backup) filterOutRe = None filterOut = self.filterOut.GetValue() if filterOut: try: filterOutRe = compile(filterOut) except: if filterOut: dlg = MessageDialog(self.master, "Incorrect filter expression" " (use Python's re syntax)\n" + format_exc() + "\nContinue without filter?", "Filter Out Error", YES_NO ) res = dlg.ShowModal() dlg.Destroy() if res == ID_NO: self.cbMonitor.SetValue(False) self._enable_settings() return mt = MonitorThread(root, lambda : root2threads.pop(root)) bt = BackUpThread(root, backup, mt.changes, filterOutRe) root2threads[root] = (mt, bt) mt.start() bt.start() else: self._enable_settings() if root in root2threads: for t in root2threads[root]: t.exit_request = True def _on_hide(self, __): self.master._hide_save_settings(self) @property def saveData(self): return ( self.saveDir.GetValue(), self.backupDir.GetValue(), self.filterOut.GetValue(), )
def choose_dir(): """图形方式选择路径""" app = PySimpleApp() dialog = DirDialog(None, "选择安装目录", style=DD_DEFAULT_STYLE | DD_NEW_DIR_BUTTON) if dialog.ShowModal() == ID_OK: return dialog.GetPath()
def doAction(self, umlObjects: List[OglClass], selectedObjects: List[OglClass], umlFrame: UmlFrame): """ Args: umlObjects: list of the uml objects of the diagram selectedObjects: list of the selected objects umlFrame: The diagram frame """ if umlFrame is None: self.displayNoUmlFrame() return ctrl = getMediator() project = ctrl.getFileHandling().getProjectFromFrame(umlFrame) if project.getCodePath() == "": dlg = DirDialog(None, _("Choose the root directory for the code"), getcwd()) if dlg.ShowModal() == ID_OK: codeDir = dlg.GetPath() self.logger.info(f"Chosen directory is {codeDir}") umlFrame.setCodePath(codeDir) dlg.Destroy() else: return oglClasses = [x for x in selectedObjects if isinstance(x, OglClass)] if len(oglClasses) == 0: self.logger.info("Nothing selected") return oldDir = getcwd() chdir(project.getCodePath()) sysPath.append(getcwd()) # plug = IoPython(None, None) plug = IoPython(cast(OglObject, None), cast(UmlFrame, None)) normalDir = getcwd() for oglClass in oglClasses: pyutClass = oglClass.getPyutObject() filename = pyutClass.getFilename() if filename == "": dlg = FileDialog(None, _("Choose the file for this UML class"), project.getCodePath(), "", "*.py", FD_OPEN) if dlg.ShowModal() != ID_OK: dlg.Destroy() continue filename = dlg.GetPaths()[0] self.logger.info(f"Chosen filename is {filename}") pyutClass.setFilename(filename) dlg.Destroy() modulename = filename[:-3] # remove ".py" try: # normalDir = getcwd() path, name = osPath.split(osPath.abspath(modulename)) chdir(path) module = __import__(name) importlib.reload(module) chdir(normalDir) except ImportError as ie: self.logger.error( f"Error while trying to import module '{str(modulename)}' --- '{ie}'" ) chdir(normalDir) continue orgClass = module.__dict__[pyutClass.getName()] plug.getPyutClass(orgClass, filename, pyutClass) oglClass.autoResize() chdir(oldDir)