forked from josch/img2pdf
fixing a slew of python 2 issues
This commit is contained in:
parent
725462462b
commit
0e76a5bd97
2 changed files with 61 additions and 22 deletions
|
@ -1236,10 +1236,14 @@ 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
|
||||||
|
if PY3:
|
||||||
result = sys.stdin.buffer.read()
|
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:
|
||||||
|
if PY3:
|
||||||
try:
|
try:
|
||||||
if os.path.getsize(path) == 0:
|
if os.path.getsize(path) == 0:
|
||||||
raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
|
raise argparse.ArgumentTypeError("\"%s\" is empty" % path)
|
||||||
|
@ -1256,6 +1260,18 @@ def input_images(path):
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
raise argparse.ArgumentTypeError(
|
raise argparse.ArgumentTypeError(
|
||||||
"\"%s\" does not exist" % path)
|
"\"%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
|
||||||
|
|
||||||
|
@ -1338,7 +1354,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)
|
||||||
|
@ -1493,7 +1509,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,
|
||||||
|
@ -1697,7 +1713,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)
|
||||||
|
@ -1710,7 +1726,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:
|
||||||
|
if PY3:
|
||||||
args.images = [sys.stdin.buffer.read()]
|
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__)
|
||||||
|
|
||||||
|
@ -430,6 +430,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
|
||||||
|
@ -636,4 +655,5 @@ def test_suite():
|
||||||
|
|
||||||
return unittest.TestSuite((
|
return unittest.TestSuite((
|
||||||
unittest.makeSuite(TestImg2Pdf),
|
unittest.makeSuite(TestImg2Pdf),
|
||||||
|
unittest.makeSuite(CommandLineTests),
|
||||||
))
|
))
|
||||||
|
|
Loading…
Reference in a new issue