FileSaver

class FileSaver(**properties: Any)

Superclasses: Object

Save a Buffer into a file.

A GtkSourceFileSaver object permits to save a Buffer into a File.

A file saver should be used only for one save operation, including errors handling. If an error occurs, you can reconfigure the saver and relaunch the operation with save_async.

Constructors

class FileSaver
classmethod new(buffer: Buffer, file: File) FileSaver

Creates a new FileSaver object. The buffer will be saved to the File’s location.

This constructor is suitable for a simple “save” operation, when the file already contains a non-None location.

Parameters:
  • buffer – the Buffer to save.

  • file – the File.

classmethod new_with_target(buffer: Buffer, file: File, target_location: File) FileSaver

Creates a new FileSaver object with a target location.

When the file saving is finished successfully, target_location is set to the file’s location property. If an error occurs, the previous valid location is still available in File.

This constructor is suitable for a “save as” operation, or for saving a new buffer for the first time.

Parameters:
  • buffer – the Buffer to save.

  • file – the File.

  • target_location – the File where to save the buffer to.

Methods

class FileSaver
get_buffer() Buffer
get_compression_type() CompressionType
get_encoding() Encoding
get_file() File
get_flags() FileSaverFlags
get_location() File
get_newline_type() NewlineType
save_async(io_priority: int, cancellable: Cancellable | None = None, progress_callback: Callable[[...], None] | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

Saves asynchronously the buffer into the file.

See the AsyncResult documentation to know how to use this function.

Parameters:
  • io_priority – the I/O priority of the request. E.g. %G_PRIORITY_LOW, %G_PRIORITY_DEFAULT or %G_PRIORITY_HIGH.

  • cancellable – optional Cancellable object, None to ignore.

  • progress_callback – function to call back with progress information, or None if progress information is not needed.

  • callback – a AsyncReadyCallback to call when the request is satisfied.

  • user_data – user data to pass to callback.

save_finish(result: AsyncResult) bool

Finishes a file saving started with save_async.

If the file has been saved successfully, the following File properties will be updated: the location, the encoding, the newline type and the compression type.

Since the 3.20 version, set_modified is called with False if the file has been saved successfully.

Parameters:

result – a AsyncResult.

set_compression_type(compression_type: CompressionType) None

Sets the compression type. By default the compression type is taken from the File.

Parameters:

compression_type – the new compression type.

set_encoding(encoding: Encoding | None = None) None

Sets the encoding. If encoding is None, the UTF-8 encoding will be set.

By default the encoding is taken from the File.

Parameters:

encoding – the new encoding, or None for UTF-8.

set_flags(flags: FileSaverFlags) None
Parameters:

flags – the new flags.

set_newline_type(newline_type: NewlineType) None

Sets the newline type. By default the newline type is taken from the File.

Parameters:

newline_type – the new newline type.

Properties

class FileSaver
props.buffer: Buffer

The Buffer to save. The FileSaver object has a weak reference to the buffer.

props.compression_type: CompressionType

The compression type.

props.encoding: Encoding

The file’s encoding.

props.file: File

The File. The FileSaver object has a weak reference to the file.

props.flags: FileSaverFlags

File saving flags.

props.location: File

The File where to save the buffer. By default the location is taken from the File at construction time.

props.newline_type: NewlineType

The newline type.