Ejemplo n.º 1
0
def protected_exec(fn, *args, **kargs):
    """Apply the given function, catching all RelaxErrors.

    All args and keyword args supplied will be directly applied to the given function.

    @param fn:      The function to apply.
    @type fn:       func
    @return:        The status of execution.
    @rtype:         bool
    """

    # Apply the function.
    try:
        fn(*args, **kargs)

    # Catch RelaxErrors.
    except AllRelaxErrors:
        instance = sys.exc_info()[1]

        # Raise the error in debugging mode.
        if status.debug:
            raise

        # Display a dialog with the error.
        gui_raise(instance, raise_flag=False)

        # Failure.
        return False

    # Success.
    return True
Ejemplo n.º 2
0
def protected_exec(fn, *args, **kargs):
    """Apply the given function, catching all RelaxErrors.

    All args and keyword args supplied will be directly applied to the given function.

    @param fn:      The function to apply.
    @type fn:       func
    @return:        The status of execution.
    @rtype:         bool
    """

    # Apply the function.
    try:
        fn(*args, **kargs)

    # Catch RelaxErrors.
    except AllRelaxErrors:
        instance = sys.exc_info()[1]

        # Raise the error in debugging mode.
        if status.debug:
            raise

        # Display a dialog with the error.
        gui_raise(instance, raise_flag=False)

        # Failure.
        return False

    # Success.
    return True
Ejemplo n.º 3
0
    def update_args(self):
        """Update all the argument ComboBox choices.

        @return:    The status of the update - False if a RelaxError occurs, True otherwise.
        @rtype:     bool
        """

        # Loop over the arguments.
        for i in range(len(self.uf_data.kargs)):
            # The argument name.
            name = self.uf_data.kargs[i]['name']

            # No iterator method for updating the list.
            iterator = self.uf_data.kargs[i]['wiz_combo_iter']
            if iterator == None:
                continue

            # Get the new choices and data (in a safe way).
            try:
                choices = []
                data = []
                for vals in iterator():
                    if lib.arg_check.is_tuple(vals, size=2, raise_error=False) or lib.arg_check.is_list(vals, size=2, raise_error=False):
                        choices.append(vals[0])
                        data.append(vals[1])
                    else:
                        choices.append(vals)
                        data.append(vals)

            # Catch all RelaxErrors.
            except AllRelaxErrors:
                instance = sys.exc_info()[1]

                # Signal the failure to the wizard.
                self.setup_fail = True

                # Display a dialog with the error.
                gui_raise(instance)

                # Return as a failure.
                return False

            # Get the current value, for setting as the default.
            val = self.uf_args[name].GetValue()

            # Update the GUI element.
            self.UpdateChoices(name, combo_choices=choices, combo_data=data, combo_default=val)

        # Successful update.
        return True
Ejemplo n.º 4
0
    def update_args(self):
        """Update all the argument ComboBox choices.

        @return:    The status of the update - False if a RelaxError occurs, True otherwise.
        @rtype:     bool
        """

        # Loop over the arguments.
        for i in range(len(self.uf_data.kargs)):
            # The argument name.
            name = self.uf_data.kargs[i]['name']

            # No iterator method for updating the list.
            iterator = self.uf_data.kargs[i]['wiz_combo_iter']
            if iterator == None:
                continue

            # Get the new choices and data (in a safe way).
            try:
                choices = []
                data = []
                for vals in iterator():
                    if lib.arg_check.is_tuple(vals, size=2, raise_error=False) or lib.arg_check.is_list(vals, size=2, raise_error=False):
                        choices.append(vals[0])
                        data.append(vals[1])
                    else:
                        choices.append(vals)
                        data.append(vals)

            # Catch all RelaxErrors.
            except AllRelaxErrors:
                instance = sys.exc_info()[1]

                # Signal the failure to the wizard.
                self.setup_fail = True

                # Display a dialog with the error.
                gui_raise(instance)

                # Return as a failure.
                return False

            # Get the current value, for setting as the default.
            val = self.uf_args[name].GetValue()

            # Update the GUI element.
            self.UpdateChoices(name, combo_choices=choices, combo_data=data, combo_default=val)

        # Successful update.
        return True
Ejemplo n.º 5
0
    def apply(self, uf, *args, **kwds):
        """Apply a user function for synchronous execution.

        @param uf:      The user function as a string.
        @type uf:       str
        @param args:    The user function arguments.
        @type args:     any arguments
        @param kwds:    The user function keyword arguments.
        @type kwds:     any keyword arguments
        @return:        Whether the user function was successfully applied or not.
        @rtype:         bool
        """

        # Debugging.
        if status.debug:
            sys.stdout.write(
                "debug> GUI interpreter:  Applying the %s user function for synchronous execution.\n"
                % uf)

        # Get the user function backend.
        fn = self._get_backend(uf)

        # Execute the user function.
        try:
            fn(*args, **kwds)

        # Catch all RelaxErrors.
        except AllRelaxErrors:
            instance = sys.exc_info()[1]

            # Display a dialog with the error.
            gui_raise(instance, raise_flag=False)

            # Return as a failure.
            return False

        # Catch any other errors.
        except:
            # Print the exception.
            print_exc()
            sys.stderr.flush()

            # Return as a failure.
            return False

        # Notify all observers that a user function has completed.
        status.observers.gui_uf.notify()

        # Return success.
        return True
Ejemplo n.º 6
0
    def GetValue(self):
        """Special method for returning the value of the GUI element.

        @return:    The string list value.
        @rtype:     list of str
        """

        # Convert and return the value from a TextCtrl.
        if self.element_type == 'text':
            # The value.
            value = self._field.GetValue()

            # Convert.
            try:
                value = self.convert_from_gui(value)

            # Raise a clear error for user feedback.
            except:
                gui_raise(
                    RelaxError("The value '%s' is not of the Python %s type." %
                               (value, self.type_string)))
                return None

            return value

        # Return the integer value from a SpinCtrl.
        if self.element_type == 'spin':
            # The value.
            return self._field.GetValue()

        # Convert and return the value from a ComboBox.
        if self.element_type == 'combo':
            # An element selected from the list.
            sel_index = self._field.GetSelection()
            if sel_index == wx.NOT_FOUND:
                value = None
            else:
                value = self.convert_from_gui(
                    self._field.GetClientData(sel_index))

            # A non-list value.
            if value == None:
                value = self.convert_from_gui(self._field.GetValue())

            # Return the value.
            return value
Ejemplo n.º 7
0
    def GetValue(self):
        """Special method for returning the value of the GUI element.

        @return:    The string list value.
        @rtype:     list of str
        """

        # Convert and return the value from a TextCtrl.
        if self.element_type == 'text':
            # The value.
            value = self._field.GetValue()

            # Convert.
            try:
                value = self.convert_from_gui(value)

            # Raise a clear error for user feedback.
            except:
                gui_raise(RelaxError("The value '%s' is not of the Python %s type." % (value, self.type_string)))
                return None

            return value

        # Return the integer value from a SpinCtrl.
        if self.element_type == 'spin':
            # The value.
            return self._field.GetValue()

        # Convert and return the value from a ComboBox.
        if self.element_type == 'combo':
            # An element selected from the list.
            sel_index = self._field.GetSelection()
            if sel_index == wx.NOT_FOUND:
                value = None
            else:
                value = self.convert_from_gui(self._field.GetClientData(sel_index))

            # A non-list value.
            if value == None:
                value = self.convert_from_gui(self._field.GetValue())

            # Return the value.
            return value
Ejemplo n.º 8
0
    def apply(self, uf, *args, **kwds):
        """Apply a user function for synchronous execution.

        @param uf:      The user function as a string.
        @type uf:       str
        @param args:    The user function arguments.
        @type args:     any arguments
        @param kwds:    The user function keyword arguments.
        @type kwds:     any keyword arguments
        @return:        Whether the user function was successfully applied or not.
        @rtype:         bool
        """

        # Debugging.
        if status.debug:
            sys.stdout.write("debug> GUI interpreter:  Applying the %s user function for synchronous execution.\n" % uf)

        # Get the user function backend.
        fn = self._get_backend(uf)

        # Execute the user function.
        try:
            fn(*args, **kwds)

        # Catch all RelaxErrors.
        except AllRelaxErrors:
            instance = sys.exc_info()[1]

            # Display a dialog with the error.
            gui_raise(instance, raise_flag=False)

            # Return as a failure.
            return False

        # Notify all observers that a user function has completed.
        status.observers.gui_uf.notify()

        # Return success.
        return True
Ejemplo n.º 9
0
    def on_execute(self, force_exec=False):
        """Execute the user function.

        @keyword force_exec:    A flag which if True will cause the execution flag to be ignored and the user function to be executed.
        @type force_exec:       bool
        """

        # Don't execute.
        if not force_exec and not self.execute_flag:
            return

        # Get the argument values.
        kargs = {}
        for i in range(len(self.uf_data.kargs)):
            # The argument name.
            name = self.uf_data.kargs[i]['name']

            # Store the value.
            kargs[name] = self.GetValue(name)

            # Skip execution when a Combo_list does not have enough elements.
            if self.uf_data.kargs[i]['wiz_combo_list_min'] != None and kargs[name] == None:
                return True

        # Handle the free file format args.
        if 'free_file_format' in self.uf_args:
            kargs.update(self.uf_args['free_file_format'].GetValue())

        # Display the relax controller, if asked.
        if self.uf_data.display:
            # Get the App.
            app = wx.GetApp()

            # First show the controller.
            app.gui.show_controller(None)

            # Go to the last line.
            app.gui.controller.log_panel.on_goto_end(None)

        # The user function intro text.
        if status.uf_intro:
            # Convert the keys and values.
            keys = []
            values = []
            for i in range(len(self.uf_data.kargs)):
                keys.append(self.uf_data.kargs[i]['name'])
                values.append(kargs[self.uf_data.kargs[i]['name']])

            # The printout.
            print(self._intro_text(keys, values))

        # User function argument validation.
        for i in range(len(self.uf_data.kargs)):
            arg = self.uf_data.kargs[i]
            try:
                lib.arg_check.validate_arg(kargs[arg['name']], arg['desc_short'], dim=arg['dim'], basic_types=arg['basic_types'], container_types=arg['container_types'], can_be_none=arg['can_be_none'], can_be_empty=arg['can_be_empty'], none_elements=arg['none_elements'])
            except AllRelaxErrors:
                # Display a dialog with the error.
                gui_raise(sys.exc_info()[1])

                # Return as a failure.
                return False

        # Execute the user function.
        return_status = self.execute(self.name, **kargs)

        # Bring the controller to the front.
        if status.show_gui and self.uf_data.display:
            wx.CallAfter(app.gui.controller.Raise)

        # Return the status.
        return return_status