mirror of
https://codeberg.org/privacy1st/xournalpp-relative-background
synced 2024-12-03 22:15:02 +01:00
skip non .xopp files; recursively add files from selected directory
This commit is contained in:
parent
f1c15cad85
commit
abf2e31f83
@ -27,9 +27,8 @@ Here is an excerpt of a Xournal++ document `/home/user/notes/note1/paper.pdf.xop
|
||||
</xournal>
|
||||
```
|
||||
|
||||
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
|
||||
<?xml version="1.0" standalone="no"?>
|
||||
|
@ -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} <xopp-file-1> [<xopp-file-2>] [...]\n' \
|
||||
f'\tenv {NAUTILUS_SELECTION}=<newline-delimited-list-of-xopp-files> {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}=<newline delimited list of .xopp files or folders> {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
|
||||
|
Loading…
Reference in New Issue
Block a user