Copies/to_tablette.py

73 lines
2.4 KiB
Python

import sys
import os
from pathlib import Path
def process_directory(dir_arg):
# Résolution des chemins
base_dir = Path(dir_arg)
bgnot_dir = base_dir / "BGnot"
sync_dir = Path.home() / "SyncCopies" / "À Annoter" / dir_arg
# Création du dossier de destination s'il n'existe pas
sync_dir.mkdir(parents=True, exist_ok=True)
if not bgnot_dir.is_dir():
print(f"Erreur : le sous-dossier {bgnot_dir} n'existe pas.")
return
# Récupération de tous les sous-dossiers
subdirs = [d for d in bgnot_dir.iterdir() if d.is_dir()]
if not subdirs:
return
# Application des règles de sélection
all_start_with_copie = all(d.name.startswith("Copie") for d in subdirs)
if all_start_with_copie:
chosen_dirs = subdirs
else:
chosen_dirs = [d for d in subdirs if not d.name.startswith("Copie")]
# Traitement des dossiers sélectionnés
for subdir in chosen_dirs:
concat_file = subdir / "Concat.pdf"
if not concat_file.is_file():
print(f"Attention : le fichier {concat_file} est introuvable.")
continue
symlink_path = sync_dir / f"{subdir.name}.pdf"
# Supprime le lien précédent s'il existe pour éviter une erreur
if symlink_path.is_symlink() or symlink_path.exists():
symlink_path.unlink()
# Création du lien symbolique (pointe vers le chemin absolu pour éviter les problèmes)
os.link(concat_file.absolute(), symlink_path)
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Move to tablette folder.")
parser.add_argument("dir", help="The directory to process")
parser.add_argument("--refaire", action="store_true", help="Process only copies/labels defined in refaire.json")
args = parser.parse_args()
root_dir = args.dir
if args.refaire:
base_dir = Path(root_dir)
brnot_dir = base_dir / "BRnot"
sync_dir = Path.home() / "SyncCopies" / "À Annoter" / root_dir
sync_dir.mkdir(parents=True, exist_ok=True)
for f in brnot_dir.iterdir():
concat_file = f / "Concat.pdf"
if f.is_dir() and concat_file.is_file():
symlink_path = sync_dir / f"{f.name}.pdf"
if symlink_path.exists():
symlink_path.unlink()
os.link(concat_file.absolute(), symlink_path)
sys.exit(0)
else:
process_directory(root_dir)