Beispiel #1
0
def save_file ( file_name, data = None ):
    """ Saves the specified *data* to the specified *file_name* after prompting
        the user for a new name. *data* can either be a string or a callable
        which will return the data to be saved. If *data* is omitted (or None),
        no data will have been written upon return. In this case, the result
        can be used to complete the operation.

        Returns the name of the file the data was successfully saved to, or None
        if the user cancelled the operation.

        Raises a FacetError if an error occurs writing the data to the file.
    """
    from facets.ui.pyface.api import FileDialog, OK
    from facet_errors         import FacetError

    fd = FileDialog( default_path = file_name, action = 'save as' )
    if fd.open() == OK:
        file_name = fd.path
        if data is None:
            return file_name

        if callable( data ):
            try:
                data = data( file_name )
            except:
                data = data()

        if write_file( file_name, data ):
            return file_name

        raise FacetError( "Error occurred writing '%s'" % file_name )

    return None
    def click ( self ):
        """ Handles the user left clicking on the feature image.
        """
        # Create the file dialog:
        fd = FileDialog()

        # Set up the default path based on the current value (if any):
        default_path = getattr( self.dock_control.object, self.name, None )
        if default_path is not None:
            fd.default_path = default_path

        # Set up the appropriate extension filters (if any):
        if len( self.extensions ) > 0:
            fd.wildcard = '\n'.join( [
                FileDialog.create_wildcard( '', '*' + ext )
                for ext in self.extensions
            ] )

        # Display the file dialog, and if successful, set the new file name:
        if fd.open() == OK:
            self.drop( fd.path )
Beispiel #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
            )