Esempio n. 1
0
    def save(self, location=None, overwrite=False):
        """
        Save this project.

        The project is saved to its current location, identified by the value
        of the *location* trait, unless a new location is explicitly provided.
        The specification of a new location is used to do a 'save as'
        operation.

        If a new location is provided, and a file or directory already exists
        at that location, then an *AssertionError* exception is raised unless
        the overwrite flag is True.  This ensures that users won't accidentally
        overwrite existing data.

        This method requires the overwrite flag because prompting the user to
        confirm the overwrite requires GUI interaction and thus should not be
        done at the model level.

        Note that, because we can rely on the location of a loaded project
        always being set to the location it was loaded from, there is no reason
        to try to force the *location* trait within a saved project to the
        location we are trying to save to.  Instead, we update the value in
        the in-memory version of the project only if the save completed
        successfully.

        The dirty flag is always cleared upon a succesfull save of the project.

        An exception will be raised to indicate a failure.

        """

        # Ensure saving (or save as) is allowed at this time.
        if location is None or location == self.location:
            if not self.is_save_allowed:
                raise AssertionError("Saving is currently not allowed.")
        elif location != self.location:
            if not self.is_save_as_allowed:
                raise AssertionError("Save as is currently not allowed.")

        # Use the internally-specified location unless a new location was
        # explicitly provided.  The new location can not contain any starting
        # or trailing whitespace and it cannot overwrite an existing file or
        # directory unless that was explicitly allowed.
        loc = self.location
        if location is not None:
            location = location.strip()
            if len(location) > 0 and location != self.location:

                # Ensure we never overwrite existing files / directories just
                # because someone specified a new location.  (Confirmation or
                # correction of overwriting requires prompting of the user and
                # is thus not part of the project model.)
                if os.path.exists(location) and overwrite is False:
                    raise AssertionError("Can not overwrite existing " +
                                         "location [%s]" % location)

                # The requested location is valid so let's use it.
                loc = location

        # Ensure all necessary directories exist.  If we're saving a file, then
        # these are the path upto the file name.  If we're saving to a directory
        # then the path is the complete location.
        if self.PROJECTS_ARE_FILES:
            path, filename = os.path.split(loc)
        else:
            path = loc
        if len(path) > 0:
            f = File(path)
            if f.is_file:
                f.delete()
            if not f.exists:
                f.create_folders()

        # Save this project in a manner that derived classes can modify.
        self._save(loc)

        # If the save succeeds (no exceptions were raised), then update the
        # location of the project and clear the dirty flag.
        self.location = loc
        self.dirty = False

        return
Esempio n. 2
0
    def save(self, location=None, overwrite=False):
        """
        Save this project.

        The project is saved to its current location, identified by the value
        of the *location* trait, unless a new location is explicitly provided.
        The specification of a new location is used to do a 'save as'
        operation.

        If a new location is provided, and a file or directory already exists
        at that location, then an *AssertionError* exception is raised unless
        the overwrite flag is True.  This ensures that users won't accidentally
        overwrite existing data.

        This method requires the overwrite flag because prompting the user to
        confirm the overwrite requires GUI interaction and thus should not be
        done at the model level.

        Note that, because we can rely on the location of a loaded project
        always being set to the location it was loaded from, there is no reason
        to try to force the *location* trait within a saved project to the
        location we are trying to save to.  Instead, we update the value in
        the in-memory version of the project only if the save completed
        successfully.

        The dirty flag is always cleared upon a succesfull save of the project.

        An exception will be raised to indicate a failure.

        """

        # Ensure saving (or save as) is allowed at this time.
        if location is None or location == self.location:
            if not self.is_save_allowed:
                raise AssertionError('Saving is currently not allowed.')
        elif location != self.location:
            if not self.is_save_as_allowed:
                raise AssertionError('Save as is currently not allowed.')

        # Use the internally-specified location unless a new location was
        # explicitly provided.  The new location can not contain any starting
        # or trailing whitespace and it cannot overwrite an existing file or
        # directory unless that was explicitly allowed.
        loc = self.location
        if location is not None:
            location = location.strip()
            if len(location) > 0 and location != self.location:

                # Ensure we never overwrite existing files / directories just
                # because someone specified a new location.  (Confirmation or
                # correction of overwriting requires prompting of the user and
                # is thus not part of the project model.)
                if os.path.exists(location) and overwrite is False:
                    raise AssertionError('Can not overwrite existing ' + \
                        'location [%s]' % location)

                # The requested location is valid so let's use it.
                loc = location

        # Ensure all necessary directories exist.  If we're saving a file, then
        # these are the path upto the file name.  If we're saving to a directory
        # then the path is the complete location.
        if self.PROJECTS_ARE_FILES:
            path, filename = os.path.split(loc)
        else:
            path = loc
        if len(path) > 0:
            f = File(path)
            if f.is_file:
                f.delete()
            if not f.exists:
                f.create_folders()

        # Save this project in a manner that derived classes can modify.
        self._save(loc)

        # If the save succeeds (no exceptions were raised), then update the
        # location of the project and clear the dirty flag.
        self.location = loc
        self.dirty = False

        return