Merge branch 'agroszer-py2' into 'master'

fixing a slew of python 2 issues

See merge request !4
This commit is contained in:
josch 2018-12-11 17:16:50 +00:00
commit 4cebd9f15d
2 changed files with 61 additions and 22 deletions

View file

@ -1514,26 +1514,42 @@ def parse_borderarg(string):
def input_images(path):
if path == '-':
# 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:
raise argparse.ArgumentTypeError("\"%s\" is empty" % 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 IsADirectoryError:
raise argparse.ArgumentTypeError(
"\"%s\" is a directory" % path)
except PermissionError:
raise argparse.ArgumentTypeError(
"\"%s\" permission denied" % path)
except FileNotFoundError:
raise argparse.ArgumentTypeError(
"\"%s\" does not exist" % path)
if PY3:
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 IsADirectoryError:
raise argparse.ArgumentTypeError(
"\"%s\" is a directory" % path)
except PermissionError:
raise argparse.ArgumentTypeError(
"\"%s\" permission denied" % path)
except FileNotFoundError:
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
return result
@ -1616,7 +1632,7 @@ def valid_date(string):
raise argparse.ArgumentTypeError("cannot parse date: %s" % string)
def main():
def main(argv=sys.argv):
rendered_papersizes = ""
for k, v in sorted(papersizes.items()):
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(
'-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.')
outargs.add_argument(
'-C', '--colorspace', metavar='colorspace', type=parse_colorspacearg,
@ -1982,7 +1998,7 @@ values set via the --border option.
'--viewer-fullscreen', action="store_true",
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:
logging.basicConfig(level=logging.DEBUG)
@ -1998,7 +2014,10 @@ values set via the --border option.
if len(args.images) == 0:
logging.info("reading image from standard input")
try:
args.images = [sys.stdin.buffer.read()]
if PY3:
args.images = [sys.stdin.buffer.read()]
else:
args.images = [sys.stdin.read()]
except KeyboardInterrupt:
exit(0)

View file

@ -6,7 +6,7 @@ import struct
import sys
import zlib
from PIL import Image
from io import StringIO, BytesIO
from io import StringIO, BytesIO, TextIOWrapper
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():
class TestImg2Pdf(unittest.TestCase):
pass
@ -656,4 +675,5 @@ def test_suite():
return unittest.TestSuite((
unittest.makeSuite(TestImg2Pdf),
unittest.makeSuite(CommandLineTests),
))