def TryLoadFile(self, path): try: yield self.LoadFile(path) self.EmptyUndoBuffer() yield True except Exception as e: dialogs.error(self.dialog_parent, "Error opening file:\n\n%s" % e) yield False
def NewFolder(self, node, name): path = os.path.join(node.path, name) try: yield async_call(os.mkdir, path) if self.IsExpanded(node.item): self.SelectLater(node.item, name) except OSError, e: dialogs.error(self, str(e))
def OnReplace(self, evt): details = self.GetFindDetails(True) if details: try: if not details.Replace(self.editor): dialogs.info(self, "Pattern not found: '%s'" % details.find, "Replace") except re.error as e: dialogs.error(self, "Error: %s." % str(e).capitalize())
def shell_copy(srcpath, dstpath, parent=None): if destination_is_same(srcpath, dstpath): return if ask_copy_file(parent, srcpath, dstpath): try: yield async_call(shutil.copy2, srcpath, dstpath) except Exception as e: dialogs.error(parent, "Error copying file:\n\n%s" % e)
def OnExport(self, evt): path = dialogs.get_file_to_save(self, wildcard=commands_wildcard, context="commands") if path: if not path.endswith(commands_ext) and "." not in path: path = path + commands_ext try: commands = clean_commands(self.GetCommands()) write_settings(path, commands) except Exception as e: dialogs.error(self, "Error exporting commands file:\n\n%s" % e)
def Save(self): if self.path: try: yield self.WriteFile(self.path) self.env.add_monitor_path(self.path) yield True except Exception as e: dialogs.error(self.dialog_parent, "Error saving file '%s'\n\n%s" % (self.path, e)) raise else: yield (yield self.SaveAsInSameTab())
def RenameNode(self, node, name): newpath = os.path.join(os.path.dirname(node.path), name) if newpath != node.path: try: if (yield async_call(os.path.exists, newpath)): if not dialogs.ask_overwrite(self, newpath): return yield async_call(fileutil.rename, node.path, newpath) self.SelectLater(self.GetItemParent(node.item), name) except OSError, e: dialogs.error(self, str(e))
def OnOK(self, evt): for field_name, getter_func in self._fields: ctrl = getattr(self, "field_" + field_name) try: value = getter_func(ctrl) except Exception as e: ctrl.SetFocus() dialogs.error(self, "Error: %s" % e) return self.command[field_name] = value evt.Skip()
def Startup(self, args): from dialogs import dialogs try: import async_wx from app_instance import AppListener, get_app_instance from fileutil import get_user_config_dir, mkpath from log_file import get_log_file async_wx.set_wx_scheduler() config_dir = get_user_config_dir("devo") try: mkpath(config_dir) except OSError as e: message = "Failed to create Devo configuration directory:\n\n" + str(e) dialogs.error(None, message, "Initialization Error") return False if not args.new_instance: instance = get_app_instance("devo") if instance: try: if instance.call("process_args", args.raw_args, os.getcwd()): return False except Exception: pass self.listener = AppListener("devo", DevoAppHandler(self)) if hasattr(sys, "frozen"): log_filename = os.path.join(config_dir, "errors.log") self.log_file = get_log_file(log_filename) sys.stdout, self.stdout = self.log_file, sys.stdout sys.stderr, self.stderr = self.log_file, sys.stderr from mainframe import MainFrame self.mainframe = MainFrame.__new__(MainFrame, args) self.mainframe.__init__(args) self.SetTopWindow(self.mainframe) self.Bind(wx.EVT_END_SESSION, self.OnEndSession) return True except Exception: message = "Devo failed to initialize due to the following error:\n\n" + traceback.format_exc() if self.mainframe: try: self.mainframe.Destroy() except Exception: pass dialogs.error(None, message, "Initialization Error") return False
def _ReplaceSelected(self, editor): text = editor.GetSelectedText() if text: if self.regexp: try: repl = self.rx_find.sub(self.replace, editor.GetSelectedText(), 1) editor.ReplaceSelection(repl) except re.error, e: dialogs.error(editor, "Replace error:\n\n" + str(e).capitalize()) return False else: editor.ReplaceSelection(self.replace)
def SaveAsInNewTab(self): path = self.env.get_file_to_save(path=os.path.dirname(self.path)) if path: path = os.path.realpath(path) editor = self.env.new_editor(path) editor.SetText(self.GetText()) try: yield editor.WriteFile(path) except Exception as e: dialogs.error(self.dialog_parent, "Error saving file '%s'\n\n%s" % (path, e)) raise else: editor.SetPath(path)
def SaveAsInSameTab(self): path = self.env.get_file_to_save(path=os.path.dirname(self.path)) if path: path = os.path.realpath(path) try: yield self.WriteFile(path) except Exception as e: dialogs.error(self.dialog_parent, "Error saving file '%s'\n\n%s" % (path, e)) raise else: self.SetPath(path) yield True yield False
def OnImport(self, evt): path = dialogs.get_file_to_open(self, wildcard=commands_wildcard, context="commands") if path: try: commands = read_settings(path) if isinstance(commands, dict): commands = commands.get("commands", []) if isinstance(commands, list): for command in commands: if isinstance(command, dict) and "name" in command: self.cmdlist.Append(command["name"], command) except Exception as e: dialogs.error(self, "Error importing commands file:\n\n%s" % e)
def GetFindDetails(self, show_error=False): try: return FindReplaceDetails( find = self.combo_find.GetValue(), find_history = get_combo_history(self.combo_find), replace = self.combo_replace.GetValue(), replace_history = get_combo_history(self.combo_replace), case = self.check_case.GetValue(), reverse = self.check_reverse.GetValue(), regexp = self.check_regexp.GetValue()) except re.error as e: if show_error: dialogs.error(self, "Invalid regular expression: %s." % str(e).capitalize())
def OnReplaceAll(self, evt): details = self.GetFindDetails(True) if not details: return try: count = details.ReplaceAll(self.editor) except re.error as e: dialogs.error(self, "Error: %s." % str(e).capitalize()) else: if count > 0: dialogs.info(self, "Replaced %d instances of '%s'" % (count, details.find), "Replace All") else: dialogs.info(self, "Pattern not found: '%s'" % details.find, "Replace All")
def shell_move(srcpath, dstpath, parent=None): if destination_is_same(srcpath, dstpath): return if ask_move_file(parent, srcpath, dstpath): dstfile = os.path.join(dstpath, os.path.basename(srcpath)) if os.path.exists(dstfile): if not ask_overwrite_file(parent, dstfile): return try: os.remove(dstfile) except Exception as e: dialogs.error(parent, "Error overwriting file file:\n\n%s" % e) return try: yield async_call(shutil.move, srcpath, dstpath) except Exception as e: dialogs.error(parent, "Error moving file:\n\n%s" % e)
def shell_open(self, path): try: self.cm.add(async_call(fileutil.shell_open, path, workdir=os.path.dirname(path))) except OSError as e: dialogs.error(self, str(e))
def shell_remove(path, parent=None): if ask_delete_file(parent, path): try: yield async_call(remove, path) except Exception as e: dialogs.error(parent, "Error deleting file:\n\n%s" % e)