From 216d9c266867d45921627d4986366fa50e495114 Mon Sep 17 00:00:00 2001 From: Axel Svensson Date: Wed, 27 Jul 2022 23:29:57 +0200 Subject: [PATCH] Make compatible with Python 3.7 Fixes #1. --- README.md | 2 +- pdf-sign | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 00104cc..09f0d7f 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ You can now use the `pdf-sign` tool interactively (or non-interactively) to sign Run `pdf-sign -h` or `pdf-create-empty -h` for details. Installation: -* Install dependencies: `gs`, `mv`, `pdfinfo`, `pdftk`, `python3` and python3 module `tkinter`. +* Install dependencies: `python3.7` or later with module `tkinter`, `gs` (Ghostscript), `pdftk` and `pdfinfo`. * Copy one or both tools to a directory in your `$PATH`. Installation on Debian: diff --git a/pdf-sign b/pdf-sign index a3e59d1..7f3c5af 100755 --- a/pdf-sign +++ b/pdf-sign @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -#Dependencies: python3, pdftk, gs, mv, pdfinfo +#Dependencies: python3.7 or later with module tkinter, gs (Ghostscript), pdftk and pdfinfo. import argparse, os, queue, re, subprocess, sys, tempfile, time @@ -359,6 +359,26 @@ def die(reason): print(reason, file=sys.stderr) sys.exit(2) +# Monkey-patch argparse if necessary +if not 'BooleanOptionalAction' in dir(argparse): + class BooleanOptionalAction(argparse.Action): + def __init__(self, option_strings, dest, default=None, type=None, choices=None, required=False, help=None, metavar=None): + _option_strings = [] + for option_string in option_strings: + _option_strings.append(option_string) + if option_string.startswith('--'): + option_string = '--no-' + option_string[2:] + _option_strings.append(option_string) + if help is not None and default is not None: + help += f" (default: {default})" + super().__init__(option_strings=_option_strings, dest=dest, nargs=0, default=default, type=type, choices=choices, required=required, help=help, metavar=metavar) + def __call__(self, _parser, namespace, values, option_string=None): + if option_string in self.option_strings: + setattr(namespace, self.dest, not option_string.startswith('--no-')) + def format_usage(self): + return ' | '.join(self.option_strings) + argparse.BooleanOptionalAction = BooleanOptionalAction + parser = argparse.ArgumentParser(description='Sign a PDF file.') parser.add_argument('input', metavar='input.pdf', type=str, help='Input PDF file.') parser.add_argument('-p', '--page', type=int, default=-1, help='The page to sign, negative for counting from the end. (default: -1)')