~egrajeda/arkiv

814d19125ccf9779e776410fb5dd6318babed073 — Eduardo Grajeda 6 months ago 9c8426f
feat: prompt user to renew token if expired
4 files changed, 32 insertions(+), 6 deletions(-)

M arkiv/bisync_thread.py
M arkiv/configuration.py
M arkiv/indicator.py
M arkiv/logs.py
M arkiv/bisync_thread.py => arkiv/bisync_thread.py +12 -2
@@ 10,7 10,8 @@ from gi.repository import GLib  # noqa: E402


class BisyncThread(threading.Thread):
    __COMMAND_BISYNC_WITH_DELETE = 1
    __COMMAND_BISYNC = 1
    __COMMAND_BISYNC_WITH_DELETE = 2

    def __init__(self, configuration: Configuration) -> None:
        super().__init__()


@@ 39,12 40,21 @@ class BisyncThread(threading.Thread):
            else:
                GLib.idle_add(self.__on_failure)

            delete = False
            try:
                input = self.__input_queue.get(timeout=self.__timeout)
                if input == BisyncThread.__COMMAND_BISYNC_WITH_DELETE:
                    delete = True
            except Empty:
                delete = False
                pass

    def renew_token(self):
        GLib.spawn_command_line_sync(
            f"rclone config reconnect {self.__configuration.remote}: --auto-confirm"
        )

    def bisync(self):
        self.__input_queue.put(BisyncThread.__COMMAND_BISYNC)

    def bisync_with_delete(self):
        self.__input_queue.put(BisyncThread.__COMMAND_BISYNC_WITH_DELETE)

M arkiv/configuration.py => arkiv/configuration.py +10 -4
@@ 25,7 25,10 @@ class Configuration:
    def get_logs_of_last_run(self) -> Logs:
        logs = ""
        with open(self.log_file, "r") as file:
            start_of_run_log = "bisync is EXPERIMENTAL. Don't use in production!"
            start_of_run_logs = [
                "bisync is EXPERIMENTAL. Don't use in production!",
                "Failed to create file system",
            ]
            buffer_size = 1024 * 4

            file.seek(0, 2)


@@ 36,9 39,12 @@ class Configuration:
                buffer = file.read(last_read_end_position - read_start_position)
                logs = buffer + logs

                start_of_run_index = logs.rfind(start_of_run_log)
                if start_of_run_index >= 0:
                    return Logs(logs[start_of_run_index + len(start_of_run_log) + 1 :])
                for start_of_run_log in start_of_run_logs:
                    start_of_run_index = logs.rfind(start_of_run_log)
                    if start_of_run_index >= 0:
                        return Logs(
                            logs[start_of_run_index + len(start_of_run_log) + 1 :]
                        )

                if read_start_position == 0:
                    break

M arkiv/indicator.py => arkiv/indicator.py +7 -0
@@ 66,6 66,13 @@ class Indicator:
            if dialog.run() == Gtk.ResponseType.YES:
                self.bisyncer_thread.bisync_with_delete()
            dialog.destroy()
        elif logs.is_failure_due_to_expired_token():
            self.__show_error_dialog(
                "Rclone failed to sync",
                "Your token has expired. You will now be prompted to renew it.",
            )
            self.bisyncer_thread.renew_token()
            self.bisyncer_thread.bisync()
        else:
            self.__show_error_dialog(
                "Rclone failed to sync", "See the logs for more information."

M arkiv/logs.py => arkiv/logs.py +3 -0
@@ 4,6 4,9 @@ class Logs:

    def is_failure_due_to_delete(self) -> bool:
        return self.logs.find("Failed to bisync: too many deletes") >= 0
    
    def is_failure_due_to_expired_token(self) -> bool:
        return self.logs.find("invalid_grant: maybe token expired") >= 0

    def __str__(self) -> str:
        return self.logs