File#

ignis.utils.Utils.read_file(path: str | None = None, uri: str | None = None, gfile: Gio.File | None = None, decode: bool = True) str | bytes#

Read the contents of a file.

Parameters:
  • path (default: None) -- The path to the file.

  • uri (default: None) -- The URI of the file.

  • gfile (default: None) -- An instance of Gio.File.

  • decode (default: True) -- Whether to decode the file's contents. If True, this function will return str, otherwise bytes.

Returns:

The contents of the file.

At least one of the arguments, path, uri, or gfile, must be provided.

Example usage:

from ignis.utils import Utils

# regular file
contents = Utils.read_file(path="/path/to/file", decode=True)
print(contents)

# URI
Utils.read_file(uri="file:///path/to/file", decode=True)
# Web also supported
Utils.read_file(uri="https://SOME_SITE.org/example_content", decode=False)
ignis.utils.Utils.read_file_async(path: str | None = None, uri: str | None = None, gfile: Gio.File | None = None, decode: bool = True, callback: Callable | None = None, *user_data) None#

Asynchronously read the contents of a file.

Parameters:
  • path (default: None) -- The path to the file.

  • uri (default: None) -- The URI of the file.

  • gfile (default: None) -- An instance of Gio.File.

  • decode (default: True) -- Whether to decode the file's contents. If True, the callback will receive str, otherwise bytes.

  • callback (default: None) -- A function to call when the operation is complete. It will receive the file's contents (bytes or str).

  • *user_data -- User data to pass to callback.

At least one of the arguments, path, uri, or gfile, must be provided.

Example usage:

from ignis.utils import Utils

def some_callback(contents: bytes | str) -> None:
    print(contents)

# regular file
Utils.read_file_async(path="/path/to/file", decode=True, callback=some_callback)

# URI
Utils.read_file_async(uri="file:///path/to/file", decode=True, callback=some_callback)
# Web also supported
Utils.read_file_async(uri="https://SOME_SITE.org/example_content", decode=False, callback=some_callback)
ignis.utils.Utils.write_file(path: str | None = None, uri: str | None = None, gfile: Gio.File | None = None, contents: bytes | None = None, string: str | None = None) None#

Write contents to a file.

Parameters:
  • path (default: None) -- The path to the file.

  • uri (default: None) -- The URI of the file.

  • gfile (default: None) -- An instance of Gio.File.

  • contents (default: None) -- The bytes to write to the file.

  • string (default: None) -- The string to write to the file.

At least one of the arguments, path, uri, or gfile, must be provided. Either contents or string must be provided.

Example usage:

from ignis.utils import Utils

# write string
Utils.write_file(path="/path/to/file", string="some_string")
# or bytes
Utils.write_file(path="/path/to/file", bytes=b"some bytes")
ignis.utils.Utils.write_file_async(path: str | None = None, uri: str | None = None, gfile: Gio.File | None = None, contents: bytes | None = None, string: str | None = None, callback: Callable | None = None, *user_data) None#

Asynchronously write contents to a file.

Parameters:
  • path (default: None) -- The path to the file.

  • uri (default: None) -- The URI of the file.

  • gfile (default: None) -- An instance of Gio.File.

  • contents (default: None) -- The bytes to write to the file.

  • string (default: None) -- The string to write to the file.

  • callback (default: None) -- A function to call when the operation is complete.

  • *user_data -- User data to pass to callback.

At least one of the arguments, path, uri, or gfile, must be provided. Either contents or string must be provided.

Example usage:

from ignis.utils import Utils

def some_callback() -> None:
    print("Operation is complete")

# write string
Utils.write_file_async(path="/path/to/file", string="some_string", callback=some_callback)
# or bytes
Utils.write_file_async(path="/path/to/file", bytes=b"some bytes", callback=some_callback)