Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a version option (Issue #81) #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ John Heidemann <johnh at isi dot edu>
Lumír Balhar <lbalhar at redhat dot com>
@csingley [Github]
@corollari [Github]
@muddi900 [Github]
37 changes: 28 additions & 9 deletions staplelib/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import print_function
import math
import os
import subprocess

try:
from PyPDF2 import PdfFileWriter, PdfFileReader
Expand All @@ -11,13 +12,15 @@
from . import CommandError, iohelper
import staplelib


def _write_output(output, outputfilename):
if os.path.isabs(outputfilename):
iohelper.write_pdf(output, outputfilename)
else:
iohelper.write_pdf(output, os.path.normpath(staplelib.OPTIONS.destdir +
os.sep + outputfilename))


def select(args, inverse=False):
"""
Concatenate files / select pages from files.
Expand Down Expand Up @@ -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 = []
Expand All @@ -160,14 +164,15 @@ 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']))
filestozip.append(pagestozip)

return filestozip


def background(args):
"""Combine 2 files with corresponding pages merged."""

Expand Down Expand Up @@ -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])
Expand All @@ -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. """

Expand All @@ -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])
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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)
3 changes: 3 additions & 0 deletions staplelib/stapler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down