def edit_script(self): item = self.scriptList.currentItem() if item is not None: script = item.toolTip() if self.use_custom_editor: try: (path, app) = os.path.split(self.custom_editor) (base_name, ext) = os.path.splitext(app) if ext == '.app' and platform.system() == 'Darwin': # open it using open -a syntax subprocess.Popen(['open', '-a', app, script]) else: if platform.system() == 'Windows': # use the short name editor = win32api.GetShortPathName( str(self.custom_editor)) else: editor = str(self.custom_editor) # use subprocess to call custom editor subprocess.Popen([str(self.custom_editor), str(script)]) except: QMessageBox.critical( None, "Error Opening Editor", """Atempting to open %s using %s failed.\n Check the path to your custom editor.""" % (script, self.custom_editor)) tb = TracebackDialog() tb.ui.teTraceback.setTextColor(QColor(Qt.red)) self.last_traceback = traceback.format_exc() tb.ui.teTraceback.setText(traceback.format_exc()) tb.show() tb.exec_() else: # Open the script with the system default QDesktopServices.openUrl(QUrl("file://%s" % script))
def run_script(self, user_args): """ Run the currently selected script. """ # get the selected item from the list item = self.scriptList.currentItem() #settrace() if item is not None: script = item.toolTip() self.main_window.statusbar.showMessage( "Running script: %s" % script) # get the path and add it to sys.path (script_dir, script_name) = os.path.split(str(script)) if script_dir not in sys.path: sys.path.append(script_dir) (user_module, ext) = os.path.splitext(script_name) user_script = __import__(user_module) abnormal_exit = False self.last_traceback = '' try: # grab stdout self.old_stdout = sys.stdout sys.stdout = self.stdout if self.clear_console: self.stdout.setPlainText('') print "----------%s----------" % datetime.datetime.now() print "Running %s in: %s" % (script_name, script_dir) #print user_args # for debug #print type(user_args) if type(user_args) is not dict: user_script.run_script(self.iface) else: func = "user_script.run_script(self.iface, " #func += ",' ".join(user_args['args']) for ar in user_args['args']: func += "%s, " % ar func = func[:-2] if user_args['keywords'] is not None: kwlist = "dict(%s)" % user_args['keywords'] kwargs = eval(kwlist) func += ", **kwargs)" else: func += ")" #print "func is %s" % func exec(func) except: # show traceback tb = TracebackDialog() tb.ui.teTraceback.setTextColor(QColor(Qt.red)) self.last_traceback = traceback.format_exc() tb.ui.teTraceback.setText(traceback.format_exc()) tb.show() tb.exec_() abnormal_exit = True print "\n%s\nAbnormal termination" % self.last_traceback #QMessageBox.information(None, "Error", traceback.format_exc()) finally: print "Completed script: %s" % script_name sys.stdout = self.old_stdout if self.log_output: self.log_file.flush() self.main_window.statusbar.showMessage( "Completed script: %s" % script_name)