def apply_clicked(self, button):
        """Triggered when the Apply button in the source editor is clicked.

        """
        if isinstance(self.model.state, LibraryState):
            logger.warning("It is not allowed to modify libraries.")
            self.view.set_text("")
            return

        # Ugly workaround to give user at least some feedback about the parser
        # Without the loop, this function would block the GTK main loop and the log message would appear after the
        # function has finished
        # TODO: run parser in separate thread
        while Gtk.events_pending():
            Gtk.main_iteration_do(False)

        # get script
        current_text = self.view.get_text()

        # Directly apply script if linter was deactivated
        if not self.view['pylint_check_button'].get_active():
            self.set_script_text(current_text)
            return

        logger.debug("Parsing execute script...")
        with open(self.tmp_file, "w") as text_file:
            text_file.write(current_text)

        # clear astroid module cache, see http://stackoverflow.com/questions/22241435/pylint-discard-cached-file-state
        MANAGER.astroid_cache.clear()
        lint_config_file = resource_filename(rafcon.__name__, "pylintrc")
        args = ["--rcfile={}".format(lint_config_file)]  # put your own here
        with contextlib.closing(StringIO()) as dummy_buffer:
            json_report = JSONReporter(dummy_buffer.getvalue())
            try:
                lint.Run([self.tmp_file] + args, reporter=json_report, exit=False)
            except:
                logger.exception("Could not run linter to check script")
        os.remove(self.tmp_file)

        if json_report.messages:
            def on_message_dialog_response_signal(widget, response_id):
                if response_id == 1:
                    self.set_script_text(current_text)
                else:
                    logger.debug("The script was not saved")
                widget.destroy()

            message_string = "Are you sure that you want to save this file?\n\nThe following errors were found:"

            line = None
            for message in json_report.messages:
                (error_string, line) = self.format_error_string(message)
                message_string += "\n\n" + error_string

            # focus line of error
            if line:
                tbuffer = self.view.get_buffer()
                start_iter = tbuffer.get_start_iter()
                start_iter.set_line(int(line)-1)
                tbuffer.place_cursor(start_iter)
                message_string += "\n\nThe line was focused in the source editor."
                self.view.scroll_to_cursor_onscreen()

            # select state to show source editor
            sm_m = state_machine_manager_model.get_state_machine_model(self.model)
            if sm_m.selection.get_selected_state() is not self.model:
                sm_m.selection.set(self.model)

            dialog = RAFCONButtonDialog(message_string, ["Save with errors", "Do not save"],
                               on_message_dialog_response_signal,
                               message_type=Gtk.MessageType.WARNING, parent=self.get_root_window())
            result = dialog.run()
        else:
            self.set_script_text(current_text)
def apply_clicked ( self , button ) : if isinstance ( self . model . state , LibraryState ) : logger . warning ( "It is not allowed to modify libraries." ) self . view . set_text ( "" ) return while Gtk . events_pending ( ) : Gtk . main_iteration_do ( False ) current_text = self . view . get_text ( ) if not self . view [ 'pylint_check_button' ] . get_active ( ) : self . set_script_text ( current_text ) return logger . debug ( "Parsing execute script..." ) with open ( self . tmp_file , "w" ) as text_file : text_file . write ( current_text ) MANAGER . astroid_cache . clear ( ) lint_config_file = resource_filename ( rafcon . __name__ , "pylintrc" ) args = [ "--rcfile={}" . format ( lint_config_file ) ] with contextlib . closing ( StringIO ( ) ) as dummy_buffer : json_report = JSONReporter ( dummy_buffer . getvalue ( ) ) try : lint . Run ( [ self . tmp_file ] + args , reporter = json_report , exit = False ) except : logger . exception ( "Could not run linter to check script" ) os . remove ( self . tmp_file ) if json_report . messages : def on_message_dialog_response_signal ( widget , response_id ) : if response_id == 1 : self . set_script_text ( current_text ) else : logger . debug ( "The script was not saved" ) widget . destroy ( ) message_string = "Are you sure that you want to save this file?\n\nThe following errors were found:" line = None for message in json_report . messages : ( error_string , line ) = self . format_error_string ( message ) message_string += "\n\n" + error_string if line : tbuffer = self . view . get_buffer ( ) start_iter = tbuffer . get_start_iter ( ) start_iter . set_line ( int ( line ) - 1 ) tbuffer . place_cursor ( start_iter ) message_string += "\n\nThe line was focused in the source editor." self . view . scroll_to_cursor_onscreen ( ) sm_m = state_machine_manager_model . get_state_machine_model ( self . model ) if sm_m . selection . get_selected_state ( ) is not self . model : sm_m . selection . set ( self . model ) dialog = RAFCONButtonDialog ( message_string , [ "Save with errors" , "Do not save" ] , on_message_dialog_response_signal , message_type = Gtk . MessageType . WARNING , parent = self . get_root_window ( ) ) result = dialog . run ( ) else : self . set_script_text ( current_text )
Triggered when the Apply button in the source editor is clicked.