From b27e50f5009ea8c22d56c8ae256b44eb58efa82e Mon Sep 17 00:00:00 2001 From: Mudassir Chapra <37051110+muddi900@users.noreply.github.com> Date: Mon, 28 Nov 2022 09:14:04 +0000 Subject: [PATCH] added a version option (Issue #81) --- CONTRIBUTORS | 1 + staplelib/commands.py | 37 ++++++++++++++++++++++++++++--------- staplelib/stapler.py | 3 +++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a7f8e29..1a38dbc 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -7,3 +7,4 @@ John Heidemann Lumír Balhar @csingley [Github] @corollari [Github] +@muddi900 [Github] \ No newline at end of file diff --git a/staplelib/commands.py b/staplelib/commands.py index d37bfa7..3ae4275 100644 --- a/staplelib/commands.py +++ b/staplelib/commands.py @@ -2,6 +2,7 @@ from __future__ import print_function import math import os +import subprocess try: from PyPDF2 import PdfFileWriter, PdfFileReader @@ -11,6 +12,7 @@ from . import CommandError, iohelper import staplelib + def _write_output(output, outputfilename): if os.path.isabs(outputfilename): iohelper.write_pdf(output, outputfilename) @@ -18,6 +20,7 @@ def _write_output(output, outputfilename): iohelper.write_pdf(output, os.path.normpath(staplelib.OPTIONS.destdir + os.sep + outputfilename)) + def select(args, inverse=False): """ Concatenate files / select pages from files. @@ -139,6 +142,7 @@ def info(args): print(" (No metadata found.)") print() + def zip_pdf_pages(filesandranges, verbose): # Make [[file1_p1, file1_p2], [file2_p1, file2_p2], ...]. filestozip = [] @@ -160,7 +164,7 @@ def zip_pdf_pages(filesandranges, verbose): pageno, rotate)) pagestozip.append(pdf.getPage(pageno-1) - .rotateClockwise(rotate)) + .rotateClockwise(rotate)) else: raise CommandError("Page {} not found in {}.".format( pageno, input['name'])) @@ -168,6 +172,7 @@ def zip_pdf_pages(filesandranges, verbose): return filestozip + def background(args): """Combine 2 files with corresponding pages merged.""" @@ -201,6 +206,7 @@ def background(args): _write_output(output, outputfilename) + def zip(args): """Combine 2 files with interleaved pages.""" filesandranges = iohelper.parse_ranges(args[:-1]) @@ -226,12 +232,14 @@ def int_to_page_alpha(pageno, base): """return uppercase alphabetic page numbers for PAGENO starting at BASE (a or A). Adobe defines them as A to Z, then AA to ZZ, and so on. Yes, that is somewhat wacky.""" - (div, mod) = divmod( pageno-1, 26) + (div, mod) = divmod(pageno-1, 26) c = chr(mod + ord(base)) return c * (div+1) # next text is from Paul M. Winkler # via https://www.oreilly.com/library/view/python-cookbook/0596001673/ch03s24.html + + def int_to_roman(input): """ Convert an integer to a Roman numeral. """ @@ -240,7 +248,8 @@ def int_to_roman(input): if not 0 < input < 4000: raise ValueError("Argument must be between 1 and 3999") ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) - nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I') + nums = ('M', 'CM', 'D', 'CD', 'C', 'XC', + 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I') result = [] for i in range(len(ints)): count = int(input / ints[i]) @@ -249,7 +258,6 @@ def int_to_roman(input): return ''.join(result) - # # pdf_page_enumeration is # inspired by https://stackoverflow.com/questions/12360999/retrieve-page-numbers-from-document-with-pypdf @@ -261,21 +269,22 @@ def int_to_roman(input): # def pdf_page_enumeration(pdf): """Generate a list of pages, using /PageLabels (if it exists). Returns a list of labels.""" - + try: pagelabels = pdf.trailer["/Root"]["/PageLabels"] except: # ("No /Root/PageLabels object"), so infer the list. return range(1, pdf.getNumPages() + 1) - + # """Select the item that is most likely to contain the information you desire; e.g. # {'/Nums': [0, IndirectObject(42, 0)]} # here, we only have "/Num". """ - + try: pagelabels_nums = pdf.trailer["/Root"]["/PageLabels"]["/Nums"] except: - raise CommandError("Malformed PDF, /Root/PageLabels but no .../Nums object") + raise CommandError( + "Malformed PDF, /Root/PageLabels but no .../Nums object") # # At this point we have either the object or the list. @@ -311,7 +320,8 @@ def pdf_page_enumeration(pdf): elif style == '/r': pageno_str = int_to_roman(next_pageno).lower() else: - raise CommandError("Malformded PDF: unkown page numbering style " + style) + raise CommandError( + "Malformded PDF: unkown page numbering style " + style) labels.append(prefix + pageno_str) next_pageno += 1 @@ -338,3 +348,12 @@ def list_logical_pages(args): except Exception as e: raise CommandError(e) + + +def version(): + ''' + Prints out the current version of the staple module + ''' + ver = str(subprocess.run(['poetry', 'version'], + capture_output=True, check=True)) + print(ver) diff --git a/staplelib/stapler.py b/staplelib/stapler.py index 465247a..adc1eb4 100644 --- a/staplelib/stapler.py +++ b/staplelib/stapler.py @@ -74,6 +74,8 @@ argparser.add_argument('mode', action='store', help="requested stapler mode") +argparser.add_argument('-V', '--version', action='version', + help="outputs the current version") def main(arguments=None): @@ -105,6 +107,7 @@ def main(arguments=None): "background": commands.background, "list-log": commands.list_logical_pages, "list-logical": commands.list_logical_pages, + "version": commands.version } mode = staplelib.OPTIONS.mode