From 24eaefc87eb9455bab371e9efcb831a8a1f93ee7 Mon Sep 17 00:00:00 2001 From: Tobi208 Date: Wed, 8 May 2024 12:46:20 +0200 Subject: [PATCH 1/3] remove deprecated pkg_resources.get_distribution --- src/pypdf_cli.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/pypdf_cli.py b/src/pypdf_cli.py index 3d51d20..ef3040c 100644 --- a/src/pypdf_cli.py +++ b/src/pypdf_cli.py @@ -4,7 +4,6 @@ import click from pypdf import PdfReader, PdfWriter from pypdf.errors import PdfReadError -from pkg_resources import get_distribution # auxiliary functions @@ -99,8 +98,8 @@ def write(writer, output): # click functions -@click.group() -@click.version_option(get_distribution('pypdf-cli').version) +@click.group(invoke_without_command=True, no_args_is_help=True) +@click.version_option() def cli(): pass From afecafe5e77dadf445dfcd016b19d1c686080b9f Mon Sep 17 00:00:00 2001 From: Tobi208 Date: Wed, 8 May 2024 14:15:57 +0200 Subject: [PATCH 2/3] updated versions --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 239962b..a6c8658 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi" [project] name = "pypdf-cli" -version = "1.0.0" +version = "1.0.1" authors = [ { name = "Tobias Lass", email = "tobi208@github.com" } ] description = "A Python-based CLI that allows for comfortable every-day PDF manipulation with pypdf." readme = "README.md" @@ -16,7 +16,7 @@ classifiers = [ requires-python = ">=3.6" dependencies = [ "click>=8.0.3", - "pypdf>=3.0.0", + "pypdf>=3.1.0", "setuptools>=68.2.2", ] license = { file = "LICENSE" } From dc73bea0028bcc9fa6aef0abf0fb6e7a8b1ebc33 Mon Sep 17 00:00:00 2001 From: Tobi208 Date: Wed, 8 May 2024 14:16:34 +0200 Subject: [PATCH 3/3] allow to sort files by DATE or NAME when merging all files in dir --- README.md | 9 ++++++--- src/pypdf_cli.py | 29 ++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e1cdf9c..27f6fc6 100644 --- a/README.md +++ b/README.md @@ -158,9 +158,12 @@ Usage: pypdf-cli merge [OPTIONS] [INPUT_FILES]... INPUT_FILES are the locations of at least two pdf files to be merged. Options: - -o, --output PATH Optional location of the output pdf file. WARNING: - overwrites existing files. - --help Show this message and exit. + -o, --output PATH Optional location of the output pdf file. WARNING: + overwrites existing files. + -a, --all Select every index. + -s, --sort SORT BY Sort input files by NAME or DATE (last modified) when + selecting all files. + --help Show this message and exit. ``` diff --git a/src/pypdf_cli.py b/src/pypdf_cli.py index ef3040c..d283349 100644 --- a/src/pypdf_cli.py +++ b/src/pypdf_cli.py @@ -1,4 +1,4 @@ -from os import makedirs, listdir, getcwd +from os import makedirs, scandir, getcwd from os.path import basename, dirname import click @@ -127,12 +127,26 @@ def convert(self, value, param, ctx): return selection + +class SortByType(click.ParamType): + + name = "SORT BY" + + def convert(self, value, param, ctx): + + if value.upper() not in ['NAME', 'DATE']: + self.fail(f'{value!r} is not a valid metric to sort the input files by. Use NAME or DATE.', param, ctx) + else: + return value.upper() + INT_PAGES = PagesType() +SORT_BY = SortByType() OUTPUT_HELP = 'Optional location of the output pdf file. WARNING: overwrites existing files.' PAGES_MULTI_HELP = 'Selection of pages. Enter list of integers and ranges without spaces or wrap in quotation marks. E.g. 1,3-5,7.' PAGES_SINGLE_HELP = 'Selection of page. Enter integer. E.g. 2.' ALL_HELP = 'Select every index.' +SORT_BY_HELP = 'Sort input files by NAME or DATE (last modified) when selecting all files.' def common_options(f): @@ -252,7 +266,8 @@ def insert(input_files, output, select_pages): @click.argument('input-files', nargs=-1, type=click.Path(exists=True), required=False) @click.option('--output', '-o', type=click.Path(), help=OUTPUT_HELP) @click.option('--all', '-a', is_flag=True, default=False, help=ALL_HELP) -def merge(input_files, output, all): +@click.option('--sort', '-s', type=SORT_BY, default='NAME', help=SORT_BY_HELP) +def merge(input_files, output, all, sort): """ Merge two or more pdf files. Files are appended in the order they are entered. @@ -264,7 +279,15 @@ def merge(input_files, output, all): # if no input files are specified or 'all' flag is set, # take all pdf files in current directory if len(input_files) == 0 or all: - input_files = [f for f in listdir(getcwd()) if f.endswith('.pdf')] + with scandir(getcwd()) as scanner: + input_files = [f for f in scanner if f.name.endswith('.pdf')] + + if sort == 'DATE': + input_files.sort(key=lambda f: float(f.stat().st_mtime)) + else: # default NAME + input_files.sort(key=lambda f: f.name) + + input_files = [f.name for f in input_files] # specific verification if len(input_files) < 2: