From abf2e31f83602bc7ee4cc7c2e7a28fa6a241589b Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 18 Dec 2021 21:26:07 +0100 Subject: [PATCH] skip non .xopp files; recursively add files from selected directory --- README.md | 5 ++-- relative-xopp-background | 64 ++++++++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index ecc7376..648c298 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,8 @@ Here is an excerpt of a Xournal++ document `/home/user/notes/note1/paper.pdf.xop ``` -If the background PDF file `/home/user/Documents/paper.pdf` does not exist -but `/home/user/notes/note1/paper.pdf` is accessible (which is in the same folder as the Xournal++ file), -then the absolute path will be replaced with a path relative one: +If `/home/user/notes/note1/paper.pdf` is accessible (in the same folder as the Xournal++ file), +then the absolute path will be replaced with a path **relative** one: ```xml diff --git a/relative-xopp-background b/relative-xopp-background index 9b22c3c..a2cbd20 100755 --- a/relative-xopp-background +++ b/relative-xopp-background @@ -51,24 +51,56 @@ def main(): notify(summary='Success!', message=f'Modified {modified_files} out of {len(xopp_files)} files.') -def parse_args() -> List[Path]: - NAUTILUS_SELECTION = 'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS' - SCRIPT_NAME = 'relative-xopp-background' +NAUTILUS_SELECTION = 'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS' +SCRIPT_NAME = 'relative-xopp-background' + +def parse_args() -> List[Path]: + """ + :return: List of absolute paths to .xopp files + """ + if len(argv) < 2: + usage() + + # get list of selected paths + selected_paths: List[Path] = [] selected_files_str = getenv(NAUTILUS_SELECTION) if selected_files_str: selected_files = selected_files_str.split('\n') + # if last filename is empty, remove it if len(selected_files[-1]) < 1: selected_files = selected_files[:-1] - return [Path(file) for file in selected_files] + selected_paths = [Path(file) for file in selected_files] + else: + selected_paths = [Path(file) for file in argv[1:]] - if len(argv) < 2: - usage_str = f'Usage:\n' \ - f'\t{SCRIPT_NAME} [] [...]\n' \ - f'\tenv {NAUTILUS_SELECTION}= {SCRIPT_NAME}' - print(usage_str, file=stderr) - raise Exception(usage_str) - return [Path(file) for file in argv[1:]] + # for each selected path + # if it is a file, check if it ends with .xopp + # if it is a directory, recursively add all .xopp files + xopp_files: List[Path] = [] + path: Path + for path in selected_paths: + if not path.exists(): + notify(summary='Note', message=f'Skipped non existing path {path}') + continue + elif path.is_dir(): + for child in path.glob(r'**/*.xopp'): + if child.is_file(): + xopp_files.append(child.absolute()) + else: + if path.suffix == '.xopp': + xopp_files.append(path.absolute()) + + return xopp_files + + +def usage(): + usage_str = f'Usage:\n' \ + f'\t{SCRIPT_NAME} <.xopp file or folder> [<.xopp file or folder>] [...]\n' \ + f'\tenv {NAUTILUS_SELECTION}= {SCRIPT_NAME}' + + notify(summary='Usage', message=usage_str) + exit(1) def save_relative_paths(xopp_file: Path) -> int: @@ -122,8 +154,8 @@ def save_relative_paths(xopp_file: Path) -> int: if not abs_pdf.is_absolute(): continue # No action required as the path is already relative - if abs_pdf.exists(): - continue # No action required as the background pdf will be found by Xournal++ + # if abs_pdf.exists(): + # continue # No action required as the background pdf will be found by Xournal++ if len(abs_pdf.name) < 1: raise Exception(f'Expected the PDF file name to be non empty: {abs_pdf}') @@ -142,11 +174,11 @@ def save_relative_paths(xopp_file: Path) -> int: print('The xml was modified!') - # Current date as string including milliseconds - date_str = datetime.today().strftime('%Y-%m-%d_%H-%M-%S_%f')[:-3] - xopp_file: Path + # Optional: Backup old .xopp file + # Current date as string including milliseconds + # date_str = datetime.today().strftime('%Y-%m-%d_%H-%M-%S_%f')[:-3] # xopp_file.rename(xopp_file.parent.joinpath(xopp_file.name + '.' + date_str + '.backup')) # Open in binary mode