From 8caba75060493a0a871a8886bf9ef0f0e63f3df4 Mon Sep 17 00:00:00 2001 From: furtest Date: Sun, 10 Aug 2025 18:21:10 +0200 Subject: [PATCH] Adds paths adding functionnality Adds functions that allows adding new paths to the synchronisation. When writing the new paths to the file if a parent directory is synchronised all the childrens are removed. --- src/unisync/paths.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/unisync/paths.py b/src/unisync/paths.py index d550e77..c06a15f 100644 --- a/src/unisync/paths.py +++ b/src/unisync/paths.py @@ -1,6 +1,8 @@ # Copyright (C) 2025 Paul Retourné # SPDX-License-Identifier: GPL-3.0-or-later + +import os.path import subprocess from pathlib import Path, PosixPath @@ -35,7 +37,10 @@ class PathsManager: Args: choice_timeout: Time given to make choices in nnn Returns: - list[str]: The list of paths that was selected + list[str]: The list of paths that was selected relative to the top directory + Raise: + TimeoutExpired: User took too long to choose + CalledProcessError: An unknown error occured during the selection """ command = [ "/usr/bin/nnn", @@ -62,6 +67,16 @@ class PathsManager: paths_list.append(next_path) return paths_list + def add_files_to_sync(self): + while true: + try: + paths = self.user_select_files() + break + except subprocess.TimeoutExpired: + if input("Timeout expired do you want to retry (y/n): ") != "y": + raise + self.write_new_paths(paths) + def get_paths_to_sync(self) -> list[str]: """ Return the paths to synchronise as list. @@ -71,4 +86,27 @@ class PathsManager: paths.pop() return paths + def write_new_paths(self, paths:list[str]): + """ + Writes a list of new paths to the file + """ + current_paths = self.get_paths_to_sync() + paths_to_add = list() + # Check if one of the parent is already being synchronised + # If so there is no need to add the child path + for new_path in paths: + is_contained = False + for existing in current_paths: + common = os.path.commonpath([new_path, existing]) + if common == existing: + is_contained = True + break + + if not is_contained and new_path not in paths_to_add: + paths_to_add.append(new_path) + + with self.paths_file.open("w") as f: + for p in paths_to_add: + f.write(p + "\n") +