Merge branch 'agroszer-py2' into 'master'

fixing a slew of python 2 issues

See merge request !4
pull/110/head
josch 5 years ago
commit 4cebd9f15d

@ -1514,26 +1514,42 @@ def parse_borderarg(string):
def input_images(path): def input_images(path):
if path == '-': if path == '-':
# we slurp in all data from stdin because we need to seek in it later # we slurp in all data from stdin because we need to seek in it later
result = sys.stdin.buffer.read() if PY3:
result = sys.stdin.buffer.read()
else:
result = sys.stdin.read()
if len(result) == 0: if len(result) == 0:
raise argparse.ArgumentTypeError("\"%s\" is empty" % path) raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
else: else:
try: if PY3:
if os.path.getsize(path) == 0: try:
raise argparse.ArgumentTypeError("\"%s\" is empty" % path) if os.path.getsize(path) == 0:
# test-read a byte from it so that we can abort early in case raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
# we cannot read data from the file # test-read a byte from it so that we can abort early in case
with open(path, "rb") as im: # we cannot read data from the file
im.read(1) with open(path, "rb") as im:
except IsADirectoryError: im.read(1)
raise argparse.ArgumentTypeError( except IsADirectoryError:
"\"%s\" is a directory" % path) raise argparse.ArgumentTypeError(
except PermissionError: "\"%s\" is a directory" % path)
raise argparse.ArgumentTypeError( except PermissionError:
"\"%s\" permission denied" % path) raise argparse.ArgumentTypeError(
except FileNotFoundError: "\"%s\" permission denied" % path)
raise argparse.ArgumentTypeError( except FileNotFoundError:
"\"%s\" does not exist" % path) raise argparse.ArgumentTypeError(
"\"%s\" does not exist" % path)
else:
try:
if os.path.getsize(path) == 0:
raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
# test-read a byte from it so that we can abort early in case
# we cannot read data from the file
with open(path, "rb") as im:
im.read(1)
except IOError as err:
raise argparse.ArgumentTypeError(str(err))
except OSError as err:
raise argparse.ArgumentTypeError(str(err))
result = path result = path
return result return result
@ -1616,7 +1632,7 @@ def valid_date(string):
raise argparse.ArgumentTypeError("cannot parse date: %s" % string) raise argparse.ArgumentTypeError("cannot parse date: %s" % string)
def main(): def main(argv=sys.argv):
rendered_papersizes = "" rendered_papersizes = ""
for k, v in sorted(papersizes.items()): for k, v in sorted(papersizes.items()):
rendered_papersizes += " %-8s %s\n" % (papernames[k], v) rendered_papersizes += " %-8s %s\n" % (papernames[k], v)
@ -1769,7 +1785,7 @@ Report bugs at https://gitlab.mister-muffin.de/josch/img2pdf/issues
outargs.add_argument( outargs.add_argument(
'-o', '--output', metavar='out', type=argparse.FileType('wb'), '-o', '--output', metavar='out', type=argparse.FileType('wb'),
default=sys.stdout.buffer, default=sys.stdout.buffer if PY3 else sys.stdout,
help='Makes the program output to a file instead of standard output.') help='Makes the program output to a file instead of standard output.')
outargs.add_argument( outargs.add_argument(
'-C', '--colorspace', metavar='colorspace', type=parse_colorspacearg, '-C', '--colorspace', metavar='colorspace', type=parse_colorspacearg,
@ -1982,7 +1998,7 @@ values set via the --border option.
'--viewer-fullscreen', action="store_true", '--viewer-fullscreen', action="store_true",
help='Instruct the PDF viewer to open the PDF in fullscreen mode') help='Instruct the PDF viewer to open the PDF in fullscreen mode')
args = parser.parse_args() args = parser.parse_args(argv[1:])
if args.verbose: if args.verbose:
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
@ -1998,7 +2014,10 @@ values set via the --border option.
if len(args.images) == 0: if len(args.images) == 0:
logging.info("reading image from standard input") logging.info("reading image from standard input")
try: try:
args.images = [sys.stdin.buffer.read()] if PY3:
args.images = [sys.stdin.buffer.read()]
else:
args.images = [sys.stdin.read()]
except KeyboardInterrupt: except KeyboardInterrupt:
exit(0) exit(0)

@ -6,7 +6,7 @@ import struct
import sys import sys
import zlib import zlib
from PIL import Image from PIL import Image
from io import StringIO, BytesIO from io import StringIO, BytesIO, TextIOWrapper
HERE = os.path.dirname(__file__) HERE = os.path.dirname(__file__)
@ -433,6 +433,25 @@ def tiff_header_for_ccitt(width, height, img_size, ccitt_group=4):
) )
class CommandLineTests(unittest.TestCase):
def test_main_help(self):
# silence output
sys_stdout = sys.stdout
if PY3:
sys.stdout = TextIOWrapper(BytesIO())
else:
sys.stdout = BytesIO()
try:
img2pdf.main(['img2pdf', '--help'])
except SystemExit:
# argparse does sys.exit(0) on --help
res = sys.stdout.getvalue()
self.assertIn('img2pdf', res)
finally:
sys.stdout = sys_stdout
def test_suite(): def test_suite():
class TestImg2Pdf(unittest.TestCase): class TestImg2Pdf(unittest.TestCase):
pass pass
@ -656,4 +675,5 @@ def test_suite():
return unittest.TestSuite(( return unittest.TestSuite((
unittest.makeSuite(TestImg2Pdf), unittest.makeSuite(TestImg2Pdf),
unittest.makeSuite(CommandLineTests),
)) ))

Loading…
Cancel
Save