Ejemplo n.º 1
0
def edit_function_call(func_def):

    if func_def.code is None:
        msg = "Perhaps your python path is not set correctly or\n" \
              "there is an error in the file (or one it imports).\n"
        error(None, msg, "Error loading function")

        return None

    # If it is a user-defined function, one would want to edit it
    # else it can remain un-editable.
    path_dir = os.path.dirname(func_def.filename)
    usr_path = os.path.join(ETSConfig.user_data, USER_MODULE_NAME)

    if path_dir == usr_path:
        # This is a user function

        is_ok = edit_user_function(func_def)

        if is_ok:
            if func_def.dirty:
                func_def.write(overwrite = True)

            return func_def
    else:
        is_ok = edit_sys_function(func_def)

        if is_ok:
            if func_def.dirty:
                func_def.write(overwrite = False)
            return func_def
Ejemplo n.º 2
0
    def save_as(self, info):
        """ Handles saving the current model to file.
        """
        if not info.initialized:
            return

#        retval = self.edit_traits(parent=info.ui.control, view="file_view")

        dlg = FileDialog( action = "save as",
            wildcard = "Graphviz Files (*.dot, *.xdot, *.txt)|" \
                "*.dot;*.xdot;*.txt|Dot Files (*.dot)|*.dot|" \
                "All Files (*.*)|*.*|")

        if dlg.open() == OK:
            fd = None
            try:
                fd = open(dlg.path, "wb")
                dot_code = str(self.model)
                fd.write(dot_code)

                self.save_file = dlg.path

            except:
                error(parent=info.ui.control, title="Save Error",
                      message="An error was encountered when saving\nto %s"
                      % self.file)

            finally:
                if fd is not None:
                    fd.close()

        del dlg
Ejemplo n.º 3
0
    def perform(self, event):

        plot_component = self.container.component

        filter = 'PNG file (*.png)|*.png|\nTIFF file (*.tiff)|*.tiff|'
        dialog = FileDialog(action='save as', wildcard=filter)

        if dialog.open() != OK:
            return

        # Remove the toolbar before saving the plot, so the output doesn't
        # include the toolbar.
        plot_component.remove_toolbar()

        filename = dialog.path

        width, height = plot_component.outer_bounds

        gc = PlotGraphicsContext((width, height), dpi=72)
        gc.render_component(plot_component)
        try:
            gc.save(filename)
        except KeyError, e:
            errmsg = "The filename must have an extension that matches a graphics"
            errmsg = errmsg + " format, such as '.png' or '.tiff'."
            if str(e.message) != '':
                errmsg = ("Unknown filename extension: '%s'\n" % str(e.message)) + errmsg

            error(None, errmsg, title="Invalid Filename Extension")
Ejemplo n.º 4
0
    def _us_error(e):
        """Display a message to the user after a UserStorageError exception has
        been raised.  If the message is empty then we assume the user has
        already been informed."""

        msg = str(e)
        if msg:
            error(None, msg)
Ejemplo n.º 5
0
    def _us_error(e):
        """Display a message to the user after a UserStorageError exception has
        been raised.  If the message is empty then we assume the user has
        already been informed."""

        msg = str(e)
        if msg:
            error(None, msg)
 def _save_project_graphically(self, parent, project, dirname):
     """ Saves the given project to a name **dirname**.  Gives the user
     graphical feedback.
     """
     # FIXME: pop up a progress bar
     try:
         project.save(dirname)
     except Exception, e:
         pyface.error(parent=parent, message='Error while saving project:\n%s' % str(e))
         raise
Ejemplo n.º 7
0
def role_assignment():
    """Implement the role assignment for the current policy manager."""

    # Create a dictionary of roles keyed by the role name.
    all_roles = {}

    try:
        roles = get_permissions_manager().policy_manager.policy_storage.all_roles()
    except PolicyStorageError, e:
        error(None, str(e))
        return
Ejemplo n.º 8
0
    def _on_exit(self):
        """ Called when the exit action is invoked. """

        parent = self.control

        information(parent, 'Going...')
        warning(parent, 'Going......')
        error(parent, 'Gone!')

        if confirm(parent, 'Should I exit?') == YES:
            self.close()

        return
 def _on_open_project(self, info):
     """ Open and load a project
     """
     app = info.object
     project_dir = self._get_current_project_dir(app.project)
     dir_dialog = DirectoryDialog(action='open', default_path=project_dir,
                                  size=(500,400))
     dir_dialog.open()
     if dir_dialog.path != '':
         try:
             app.load_project(dir_dialog.path)
         except IOError, e:
             pyface.error(parent=info.ui.control, message=e)
Ejemplo n.º 10
0
    def _on_exit(self):
        """ Called when the exit action is invoked. """

        parent = self.control

        information(parent, 'Going...')
        warning(parent, 'Going......')
        error(parent, 'Gone!')

        if confirm(parent, 'Should I exit?') == YES:
            self.close()

        return
Ejemplo n.º 11
0
    def rst2pdf(self, info):
        rest_filepath = info.object.selected_view.model.filepath

        if rest_filepath == '':
            error(info.ui.control,
                  'File must be saved before converting it to pdf.')
            return

        pdf_filepath = None

        index = rest_filepath.rfind('.')
        if index != -1:
            pdf_filepath = rest_filepath[:index] + '.pdf'

        rest_to_pdf(rest_filepath, pdf_filepath)
Ejemplo n.º 12
0
    def load_policy(self, user):
        """Load the policy for the given user."""

        self.user_permissions = []

        # See if the policy is to be unloaded.
        if user is None:
            return

        # Get the user's policy.
        try:
            user_name, perm_ids = self.policy_storage.get_policy(user.name)
        except PolicyStorageError, e:
            error(None, str(e))
            return
Ejemplo n.º 13
0
    def load_policy(self, user):
        """Load the policy for the given user."""

        self.user_permissions = []

        # See if the policy is to be unloaded.
        if user is None:
            return

        # Get the user's policy.
        try:
            user_name, perm_ids = self.policy_storage.get_policy(user.name)
        except PolicyStorageError, e:
            error(None, str(e))
            return
Ejemplo n.º 14
0
def exception(msg='Exception', parent=None):
    """This function handles any exception derived from Exception and
    prints out an error.  The optional `parent` argument is passed
    along to the dialog box.  The optional `msg` is printed and sent
    to the logger.  So you could send extra information here.
    """
    try:
        type, value, tb = sys.exc_info()
        info = traceback.extract_tb(tb)
        filename, lineno, function, text = info[-1] # last line only
        exc_msg = "%s\nIn %s:%d\n%s: %s (in %s)" %\
                  (msg, filename, lineno, type.__name__, str(value),
                   function)
        # Log and display the message.
        logger.exception(msg)
        if pyface is not None:
            pyface.error(parent, exc_msg, title='Exception')
    finally:
        type = value = tb = None # clean up
Ejemplo n.º 15
0
def exception(msg='Exception', parent=None):
    """This function handles any exception derived from Exception and
    prints out an error.  The optional `parent` argument is passed
    along to the dialog box.  The optional `msg` is printed and sent
    to the logger.  So you could send extra information here.
    """
    try:
        type, value, tb = sys.exc_info()
        info = traceback.extract_tb(tb)
        filename, lineno, function, text = info[-1]  # last line only
        exc_msg = "%s\nIn %s:%d\n%s: %s (in %s)" %\
                  (msg, filename, lineno, type.__name__, str(value),
                   function)
        # Log and display the message.
        logger.exception(msg)
        if pyface is not None:
            pyface.error(parent, exc_msg, title='Exception')
    finally:
        type = value = tb = None  # clean up
Ejemplo n.º 16
0
def _validate_password(password, confirmation):
    """Validate a password and return True if it is valid."""

    MIN_PASSWORD_LEN = 6

    if password != confirmation:
        error(None, "The passwords do not match.")
        return False

    if not password:
        error(None, "A password must be given.")
        return False

    if len(password) < MIN_PASSWORD_LEN:
        error(None, "The password must be at least %d characters long." % MIN_PASSWORD_LEN)
        return False

    return True
Ejemplo n.º 17
0
def _validate_password(password, confirmation):
    """Validate a password and return True if it is valid."""

    MIN_PASSWORD_LEN = 6

    if password != confirmation:
        error(None, "The passwords do not match.")
        return False

    if not password:
        error(None, "A password must be given.")
        return False

    if len(password) < MIN_PASSWORD_LEN:
        error(
            None, "The password must be at least %d characters long." %
            MIN_PASSWORD_LEN)
        return False

    return True
Ejemplo n.º 18
0
    def _error(msg):
        """Display an error message to the user."""

        error(None, msg)
Ejemplo n.º 19
0
def activate_function_call(func_def):
    """ Given a function and its path (optionally), open a function-call.

        If the function is a user-defined/edited/saved function then make it
        editable. If the user edits the code of the user-function, overwrite the
        function and prompt the user to add the function. If the user does not
        edit the code of the function, just add it as other functions would be
        added.

        Parameters:
        -----------
        func_def: FunctionDefinition
            Contains the function, and its python path.

        Returns:
        --------
        edited_function: function_object
            The function that is edited.
        activated_function: function_object
            The function that must be activated.
        python_string: List[Str]
            includes import and function call to be added to a script.

    """

    if func_def.code is None:
        msg = "Perhaps your python path is not set correctly or\n" \
              "there is an error in the file (or one it imports).\n"
        error(None, msg, "Error loading function")

        return None

    # If it is a user-defined function, one would want to edit it
    # else it can remain un-editable.
    path_dir = os.path.dirname(func_def.filename)
    usr_path = os.path.join(ETSConfig.user_data, USER_MODULE_NAME)

    if path_dir == usr_path:
        # This is a user function

        # We no longer edit the function before dropping it on the canvas
        #is_ok = edit_user_function(func_def)

        # Note: If the user edits the code, one should save the
        #        code and ask the user if (s)he wants to continue
        #        adding the function to the block.
        #        If the user does not edit the code, it should be
        #        the same as it is for a regular function, that is,
        #        the user automatically adds the code to the block.
        if func_def.dirty:
            func_def.write(overwrite = True)

            # Ask for confirmation if the user has to add it to
            # block now or quit without adding.
            msg= 'The user function \'%s' % func_def.name+ \
                 '\' has been edited and overwritten at \n' + \
                 '%s \n\n'% func_def.filename + \
                 'Do you wish to add the function to the block?'

            if confirm(None, msg) == YES:
                return func_def
            else:
                return None

        else:
            return func_def

    else:
        # This is a sys function which we are trying to override as a new user func
        # We no longer edit the function before dropping it on the canvas
        #is_ok = edit_sys_function(func_def)

        if func_def.dirty:
            func_def.write(overwrite = False)

            # Ask for confirmation if the user has to add it to
            # block now or quit without adding.
            msg= 'The user function \'%s' % func_def.name+ \
                 '\' has been written at \n' + \
                 '%s \n\n'% func_def.filename + \
                 'Do you wish to add the function to the block?'

            if confirm(None, msg) == YES:
                return func_def
            else:
                return None

        else:
            return func_def

    return None
Ejemplo n.º 20
0
def error(msg, parent=None):
    """Handle an error message.
    """
    logger.error(msg)
    if pyface is not None:
        pyface.error(parent, msg)
Ejemplo n.º 21
0
    # Private interface.
    ###########################################################################

    def _select_user(self, name):
        """Select a user returning the data as a tuple of name and description.
        """

        # Get all users that satisfy the criteria.
        try:
            users = self.user_storage.matching_users(name)
        except UserStorageError, e:
            self._us_error(e)
            return '', ''

        if len(users) == 0:
            error(None, "There is no user that matches \"%s\"." % name)
            return '', ''

        return select_user(users)

    @staticmethod
    def _us_error(e):
        """Display a message to the user after a UserStorageError exception has
        been raised.  If the message is empty then we assume the user has
        already been informed."""

        msg = str(e)
        if msg:
            error(None, msg)

Ejemplo n.º 22
0
    def _ps_error(e):
        """Display a message to the user after a PolicyStorageError exception
        has been raised."""

        error(None, str(e))
Ejemplo n.º 23
0
def error(msg, parent=None):
    """Handle an error message.
    """
    logger.error(msg)
    if pyface is not None:
        pyface.error(parent, msg)
Ejemplo n.º 24
0
    # Private interface.
    ###########################################################################

    def _select_user(self, name):
        """Select a user returning the data as a tuple of name and description.
        """

        # Get all users that satisfy the criteria.
        try:
            users = self.user_storage.matching_users(name)
        except UserStorageError, e:
            self._us_error(e)
            return '', ''

        if len(users) == 0:
            error(None, "There is no user that matches \"%s\"." % name)
            return '', ''

        return select_user(users)

    @staticmethod
    def _us_error(e):
        """Display a message to the user after a UserStorageError exception has
        been raised.  If the message is empty then we assume the user has
        already been informed."""

        msg = str(e)
        if msg:
            error(None, msg)

Ejemplo n.º 25
0
 def _error_changed(self, value):
     if value != '':
         error(None, value, 'Error')