Commit e638b523 authored by Philipp Hörist's avatar Philipp Hörist

Add async read/write file helpers

parent d4b9b823
......@@ -1738,3 +1738,51 @@ class Observable:
self._callbacks[signal_name].remove(func)
continue
func()(self, signal_name, *args, **kwargs)
def write_file_async(path, data, callback, user_data=None):
file = Gio.File.new_for_path(str(path))
file.create_async(Gio.FileCreateFlags.PRIVATE,
GLib.PRIORITY_DEFAULT,
None,
_on_file_created,
(callback, data, user_data))
def _on_file_created(file, result, user_data):
callback, data, user_data = user_data
try:
outputstream = file.create_finish(result)
except GLib.Error as error:
callback(False, error, user_data)
return
outputstream.write_all_async(data,
GLib.PRIORITY_DEFAULT,
None,
_on_write_finished,
(callback, user_data))
def _on_write_finished(outputstream, result, user_data):
callback, user_data = user_data
try:
successful, _bytes_written = outputstream.write_all_finish(result)
except GLib.Error as error:
callback(False, error, user_data)
else:
callback(successful, None, user_data)
def load_file_async(path, callback, user_data=None):
file = Gio.File.new_for_path(str(path))
file.load_contents_async(None,
_on_load_finished,
(callback, user_data))
def _on_load_finished(file, result, user_data):
callback, user_data = user_data
try:
_successful, contents, _etag = file.load_contents_finish(result)
except GLib.Error as error:
callback(None, error, user_data)
else:
callback(contents, None, user_data)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment