FileLoader

class FileLoader(**properties: Any)

Superclasses: Object

Load a file into a GtkSourceBuffer.

A GtkSourceFileLoader object permits to load the contents of a File or a InputStream into a Buffer.

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

Running a GtkSourceFileLoader is an undoable action for the Buffer.

After a file loading, the buffer is reset to the contents provided by the File or InputStream, so the buffer is set as “unmodified”, that is, set_modified is called with False. If the contents isn’t saved somewhere (for example if you load from stdin), then you should probably call set_modified with True after calling load_finish.

Constructors

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

Creates a new GtkSourceFileLoader object. The contents is read from the File’s location.

If not already done, call set_location before calling this constructor. The previous location is anyway not needed, because as soon as the file loading begins, the buffer is emptied.

Parameters:
  • buffer – the Buffer to load the contents into.

  • file – the File.

classmethod new_from_stream(buffer: Buffer, file: File, stream: InputStream) FileLoader

Creates a new FileLoader object. The contents is read from stream.

Parameters:
  • buffer – the Buffer to load the contents into.

  • file – the File.

  • stream – the InputStream to load, e.g. stdin.

Methods

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

Loads asynchronously the file or input stream contents into the Buffer.

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.

load_finish(result: AsyncResult) bool

Finishes a file loading started with load_async.

If the contents has been loaded, the following File properties will be updated: the location, the encoding, the newline type and the compression type.

Parameters:

result – a AsyncResult.

set_candidate_encodings(candidate_encodings: list[Encoding]) None

Sets the candidate encodings for the file loading.

The encodings are tried in the same order as the list.

For convenience, candidate_encodings can contain duplicates. Only the first occurrence of a duplicated encoding is kept in the list.

By default the candidate encodings are (in that order in the list):

  1. If set, the File’s encoding as returned by get_encoding.

  2. The default candidates as returned by get_default_candidates.

Parameters:

candidate_encodings – a list of :obj:`~gi.repository.GtkSource.Encoding`<!– –>s.

Properties

class FileLoader
props.buffer: Buffer

The Buffer to load the contents into. The FileLoader object has a weak reference to the buffer.

props.file: File

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

props.input_stream: InputStream

The InputStream to load. Useful for reading stdin. If this property is set, the FileLoader:location property is ignored.

props.location: File

The File to load. If the FileLoader:input-stream is None, by default the location is taken from the File at construction time.