Example #1
0
 def _save_set ( self ):
     """ Handles the 'save' facet being changed.
     """
     file_name = join( self.path, self.file_name )
     if write_file( file_name, self.code ):
         exported_tools = self.facet_db_get( ExportedTools, {} )
         exported_tools[ self.title ] = ( file_name, self.class_name )
         self.facet_db_set( ExportedTools, exported_tools )
         self.facet_db_set( 'path', self.path )
         self.status = '%s saved on %s' % ( file_name, time_stamp() )
     else:
         self.status = 'Error occurred trying to write %s' % file_name
Example #2
0
 def save_file(self):
     """ Saves the text contents of the file to its assigned file name.
     """
     if self.file_name == "":
         self.save_as_file()
     else:
         self.ignore_reload = True
         if write_file(self.file_name, self.file_text) is None:
             self.status = "Error occurred saving file"
             self.ignore_reload = False
         else:
             self.status = "File saved"
             self.modified = False
Example #3
0
    def save_file ( self ):
        """ Save the contents of the item to a file.

            The [[s]] key saves the current textual contents of a shell item,
            not including any textual annotations such as items ids or line
            numbers, to a file.

            A file dialog appears prompting you for the name and location of
            where to save the file. The file dialog defaults to saving the file
            in the shell's current working directory with a name of the form:
            vip_shell_<<item_id>>.txt, where <<item_id>> is the id associated
            with the shell item.
        """
        from file_item        import FileItem
        from python_file_item import PythonFileItem

        fd = FileDialog( default_path = self.file_name, action = 'save as' )
        if fd.open() == OK:
            self.file_name = file_name = fd.path
            ext            = splitext( file_name )[1]
            write_file( file_name, remove_color( self.str( self.item ) ) )
            self.shell.add_history_item_for(
                ( FileItem, PythonFileItem )[ ext == '.py' ], file_name
            )
Example #4
0
    def save(self):
        """ Saves the contents of the image volume using the current contents
            of the **ImageVolume**.
        """
        path = self.path

        if not self.is_zip_file:
            # Make sure the directory is writable:
            if not access(path, R_OK | W_OK | X_OK):
                return False

        # Make sure the directory and zip file are writable:
        elif (not access(dirname(path), R_OK | W_OK | X_OK)) or (exists(path) and (not access(path, W_OK))):
            return False

        # Pre-compute all of the file contents that need to be generated so that
        # we can check them against the existing files (if any) to see if an
        # update really needs to be made:
        image_volume_code = self.image_volume_code
        images_code = self.images_code
        license_text = self.license_text

        if not self.is_zip_file:
            # Get the file paths for all files that need to be updated:
            image_volume_py = join(path, "image_volume.py")
            image_info_py = join(path, "image_info.py")
            license_txt = join(path, "license.txt")
            modified = False

            # Write the image info source code to a file if it changed:
            if images_code != read_file(image_info_py):
                write_file(image_info_py, images_code)
                modified = True

            # Write a separate license file for human consumption if it changed:
            if license_text != read_file(license_txt):
                write_file(license_txt, license_text)
                modified = True

            # Write the volume manifest source code to a file if anything
            # changed:
            if modified or (image_volume_code != read_file(image_volume_py)):
                write_file(image_volume_py, image_volume_code)

            return True

        # Check to see if we really have to update the current zip file by
        # checking to see if any changes have actually occurred to any of the
        # files we need to update:
        try:
            cur_zf = self.zip_file
            if (
                (images_code == cur_zf.read("image_info.py"))
                and (license_text == cur_zf.read("license.txt"))
                and (image_volume_code == cur_zf.read("image_volume.py"))
            ):
                return True
        except:
            pass

        # Create a temporary name for the new .zip file:
        file_name = path + ".###"

        # Create the new zip file:
        try:
            new_zf = ZipFile(file_name, "w", ZIP_DEFLATED)
        except IOError:
            # We catch this error because there are some cases under Windows
            # where it passes the preceding os.access tests incorrectly. Once
            # the bug in os.access has been fixed, this try block can be
            # removed...
            return False

        try:
            # Copy all of the image files from the current zip file to the new
            # zip file:
            for name in cur_zf.namelist():
                if name not in dont_copy_list:
                    new_zf.writestr(name, cur_zf.read(name))

            # Temporarily close the current zip file while we replace it with
            # the new version:
            cur_zf.close()

            # Write the volume manifest source code to the zip file:
            new_zf.writestr("image_volume.py", image_volume_code)

            # Write the image info source code to the zip file:
            new_zf.writestr("image_info.py", images_code)

            # Write a separate license file for human consumption:
            new_zf.writestr("license.txt", license_text)

            # Done creating the new zip file:
            new_zf.close()
            new_zf = None

            # Rename the original file to a temporary name, so we can give the
            # new file the original name. Note that unlocking the original zip
            # file after the previous close sometimes seems to take a while,
            # which is why we repeatedly try the rename until it either succeeds
            # or takes so long that it must have failed for another reason:
            temp_name = path + ".$$$"
            for i in range(50):
                try:
                    rename(path, temp_name)

                    break
                except:
                    sleep(0.1)

            try:
                rename(file_name, path)
                file_name = temp_name
            except:
                rename(temp_name, path)

                raise
        finally:
            if new_zf is not None:
                new_zf.close()

            remove(file_name)

        return True
Example #5
0
 def save ( self ):
     """ Saves the current 'data' contents of the file back to the file
         system.
     """
     if write_file( self.absolute_path, self.data ):
         self.modified = False