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.
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user