From aaa4ef61d5548e9a86bf89ee21b4188def43a93f Mon Sep 17 00:00:00 2001 From: furtest Date: Thu, 24 Jul 2025 15:59:42 +0200 Subject: [PATCH] Adds beginning of paths management The paths file will be used for everything related to the paths to synchronise. Adds the user_select_files functions that allows the user to select paths --- src/unisync/paths.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/unisync/paths.py diff --git a/src/unisync/paths.py b/src/unisync/paths.py new file mode 100644 index 0000000..5285446 --- /dev/null +++ b/src/unisync/paths.py @@ -0,0 +1,40 @@ +# Copyright (C) 2025 Paul Retourné +# SPDX-License-Identifier: GPL-3.0-or-later + +import subprocess + +def user_select_files(local_dir:str, choice_timeout:int=120) -> list[str]: + """ + Make the user select files in the top directory. + Currently uses nnn for the selection, + the goal is to replace it in order to avoid using external programs. + Returns the list of paths selected. + """ + command = [ + "/usr/bin/nnn", + "-H", + "-p", "-", + local_dir + ] + nnn_process = subprocess.Popen(command, stdout=subprocess.PIPE) + try: + ret_code = nnn_process.wait(timeout=choice_timeout) + except subprocess.TimeoutExpired as e: + print("Choice timeout expired", file=sys.stderr) + raise e + + if ret_code != 0: + print("File selection failed", file=sys.stderr) + raise subprocess.CalledProcessError("File selection failed") + + paths_list:list[str] = [] + while (next_path := nnn_process.stdout.readline()) != b'': + next_path = next_path.decode().strip() + # Make the path relative to the top directory + next_path = next_path[len(local_dir):].lstrip("/") + paths_list.append(next_path) + + return paths_list + + +