diff --git a/pdf-sign b/pdf-sign index b83a954..64be231 100755 --- a/pdf-sign +++ b/pdf-sign @@ -4,8 +4,6 @@ import argparse, os, queue, re, subprocess, sys, tempfile, time -signatureDir=os.path.expanduser(os.environ['PDF_SIGNATURE_DIR'] if 'PDF_SIGNATURE_DIR' in os.environ else "~/.pdf_signatures") - # Inspired by https://unix.stackexchange.com/a/141496 def main(args): filePath=args.input @@ -35,7 +33,10 @@ def main(args): # The chosen signature if not args.signature and args.batch: die('In batch mode, signature must be specified.') + signatureDir=getSignatureDir() signatures=[*filter(lambda x: m("^.*\.pdf$", x), os.listdir(signatureDir))] if not args.signature else [None] + if not signatures: + die(f'No .pdf files found in {signatureDir}') signatureIndex=Cell(0) signaturePath=Cell(lambda: args.signature if args.signature else os.path.join(signatureDir, signatures[signatureIndex()])) signatureSize=Cell(lambda: pdfGetSize(signaturePath())) @@ -281,6 +282,22 @@ def main(args): else: print(f'Aborted') +def getSignatureDir(): + if 'PDF_SIGNATURE_DIR' in os.environ: + sd=os.environ['PDF_SIGNATURE_DIR'] + elif 'XDG_CONFIG_HOME' in os.environ: + sd=os.path.join(os.environ['XDG_CONFIG_HOME'], 'pdf_signatures') + elif os.path.exists(os.path.expanduser("~/.config/pdf_signatures")): + sd="~/.config/pdf_signatures" + else: + sd="~/.pdf_signatures" + sd=os.path.expanduser(sd) + if not os.path.exists(sd): + raise Exception(f'Signature directory {sd} does not exist') + if not os.path.isdir(sd): + raise Exception(f'Signature directory {sd} is not a directory') + return sd + # Simple dependency tracking. # Init with a value or function to calculate the value. # Update by calling with one argument (as in init). @@ -384,7 +401,7 @@ if not 'BooleanOptionalAction' in dir(argparse): 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)') -parser.add_argument('-s', '--signature', type=str, help='Path to file used as signature. Required in batch mode. In GUI mode, the user can choose among files in $PDF_SIGNATURE_DIR or ~/.pdf_signatures.') +parser.add_argument('-s', '--signature', type=str, help='Path to file used as signature. Required in batch mode. In GUI mode, the user can choose among files in $PDF_SIGNATURE_DIR, $XDG_CONFIG_HOME/pdf_signatures (defaulting to ~/.config/pdf_signatures), or ~/.pdf_signatures.') parser.add_argument('-x', '--x-coordinate', type=float, default=0.5, help='Horizontal coordinate of signature center, in page width units. (default: 0.5)') parser.add_argument('-y', '--y-coordinate', type=float, default=0.75, help='Vertical coordinate of signature center, in page height units. (default: 0.75)') parser.add_argument('-W', '--width', type=float, default=0.28, help='Width of box to fit signature to, in page width units. (default: 0.28)')